API 1.2 Diet tables, Open AI API

This commit is contained in:
Tibor Bossanyi 2023-02-10 19:10:02 +01:00
parent 676aa60e2b
commit b33c8cbf7f
75 changed files with 2812 additions and 69 deletions

View File

@ -11,7 +11,7 @@ plugins {
}
group = "com.aitrainer"
version = "1.1.0"
version = "1.2.0"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
@ -34,7 +34,7 @@ dependencies {
implementation("org.apache.logging.log4j:log4j-core:2.19.0")
implementation("org.apache.logging.log4j:log4j-api:2.19.0")
implementation("org.slf4j:slf4j-api:2.0.6")
//implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc") // JVM dependency
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.4.1")
implementation("io.jsonwebtoken:jjwt:0.9.1")
@ -48,6 +48,9 @@ dependencies {
implementation("jakarta.mail:jakarta.mail-api:2.1.1")
implementation("org.eclipse.angus:angus-mail:2.0.1")
implementation ("com.aallam.openai:openai-client:2.1.3")
implementation("io.ktor:ktor-client-java:2.2.3")
runtimeOnly("mysql:mysql-connector-java")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")

View File

@ -9,8 +9,8 @@ CREATE TABLE `membership` (
`duration_unit` ENUM('day','week','month','year') NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`training_plan_id` INT(11) NULL DEFAULT '0',
`training_plan_day_ids` CHAR(100) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`membership_id`) USING BTREE
INDEX `training_plan_id` (`training_plan_id`) USING BTREE,
PRIMARY KEY (`membership_id`) USING BTREE,
INDEX `training_plan_id` (`training_plan_id`) USING BTREE
)
ENGINE=InnoDB
;

162
data/db/update_1_2_0.sql Normal file
View File

@ -0,0 +1,162 @@
START TRANSACTION;
CREATE TABLE `customer_conversation` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`customer_id` INT(11) NOT NULL DEFAULT '0',
`conversation_date` DATETIME NULL DEFAULT NULL,
`question` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`answer` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `diet_user` (
`diet_user_id` INT(11) NOT NULL AUTO_INCREMENT,
`customer_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`diet_user_id`) USING BTREE
)
ENGINE=InnoDB
;
CREATE TABLE `diet_raw_material` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`diet_id` INT(11) NOT NULL DEFAULT '0',
`raw_material_id` INT(11) NULL DEFAULT '0',
`kcal_min` INT(11) NULL DEFAULT '0',
`kcal_max` INT(11) NULL DEFAULT '0',
`protein_min` INT(11) NULL DEFAULT '0',
`protein_max` INT(11) NULL DEFAULT '0',
`ch_min` INT(11) NULL DEFAULT '0',
`ch_max` INT(11) NULL DEFAULT '0',
`fat_min` INT(11) NULL DEFAULT '0',
`fat_max` INT(11) NULL DEFAULT '0',
`sugar` INT(11) NULL DEFAULT '0',
`name` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `diet_sensitivity` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(100) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `diet_user_consumption` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`diet_user_id` INT(11) NOT NULL,
`raw_material_id` INT(11) NOT NULL DEFAULT '0',
`date_consumption` DATETIME NOT NULL,
`name` CHAR(100) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`quantity` DOUBLE NOT NULL DEFAULT '0',
`quantity_unit` CHAR(10) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`cal` INT(11) NULL DEFAULT NULL,
`protein` DOUBLE NULL DEFAULT NULL,
`fat` DOUBLE NULL DEFAULT NULL,
`ch` DOUBLE NULL DEFAULT NULL,
`sugar` DOUBLE NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `diet_user_id` (`diet_user_id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `diet_user_preference` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`diet_user_id` INT(11) NOT NULL DEFAULT '0',
`raw_material_id` INT(11) NOT NULL DEFAULT '0',
`temperature` DECIMAL(1,0) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`) USING BTREE,
INDEX `diet_user_id` (`diet_user_id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `diet_user_sensitivity` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`sensitivity_id` INT(11) NOT NULL DEFAULT '0',
`diet_user_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`) USING BTREE,
INDEX `diet_user_id` (`diet_user_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB
;
CREATE TABLE `raw_material` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(100) NOT NULL COLLATE 'utf8mb4_general_ci',
`description` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`kcal_min` INT(11) NULL DEFAULT '0',
`kcal_max` INT(11) NULL DEFAULT '0',
`protein_min` INT(11) NULL DEFAULT '0',
`protein_max` INT(11) NULL DEFAULT '0',
`ch_min` INT(11) NULL DEFAULT '0',
`ch_max` INT(11) NULL DEFAULT '0',
`fat_min` INT(11) NULL DEFAULT '0',
`fat_max` INT(11) NULL DEFAULT '0',
`sugar` INT(11) NULL DEFAULT '0',
`store_id` INT(11) NULL DEFAULT '0',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DYNAMIC
;
CREATE TABLE `recipe` (
`recipe_id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(250) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`description` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`cal` INT(11) NULL DEFAULT NULL,
`protein` DOUBLE NULL DEFAULT NULL,
`fat` DOUBLE NULL DEFAULT NULL,
`ch` DOUBLE NULL DEFAULT NULL,
`diet_user_id` INT(11) NULL DEFAULT '0',
PRIMARY KEY (`recipe_id`) USING BTREE,
INDEX `name` (`name`) USING BTREE,
INDEX `diet_user_id` (`diet_user_id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `store` (
`store_id` INT(11) NOT NULL AUTO_INCREMENT,
`store_name` CHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`country` CHAR(4) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`store_id`) USING BTREE,
INDEX `store_name` (`store_name`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `recipe_raw_material` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`raw_material_id` INT(11) NOT NULL DEFAULT '0',
`recipe_id` INT(11) NOT NULL DEFAULT '0',
`quantity` INT(11) NULL DEFAULT '0',
`quantity_unit` CHAR(10) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
UPDATE configuration set config_value = "1.2.0", date_change=CURRENT_DATE WHERE config_key = "db_version";
COMMIT;

View File

@ -1,12 +1,14 @@
package com.aitrainer.api.controller
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping
@Component
class ApplicationProperties {
@Value("\${application.version}")
@ -21,7 +23,8 @@ class ApplicationProperties {
@Value("\${spring.datasource.password}")
private lateinit var datasourcePassword: String
@Value("\${openai.key}")
private lateinit var apiKey: String
@GetMapping("/version")
fun getVersion(): String {
@ -42,4 +45,11 @@ class ApplicationProperties {
fun getDatasourcePassword(): String {
return this.datasourcePassword
}
@GetMapping("/openAIKey")
fun getOpenAIKey(): String {
return this.apiKey
}
}

View File

@ -12,13 +12,9 @@ import org.springframework.http.HttpHeaders
import org.springframework.http.ResponseEntity
import org.springframework.security.access.annotation.Secured
import org.springframework.web.bind.annotation.*
import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets.UTF_8
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import javax.validation.Valid
@ -159,15 +155,6 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
@PostMapping("/club_registration")
fun clubRegistration(@Valid @RequestBody json: String): ResponseEntity<*> {
fun gzip(content: String): String {
val bos = ByteArrayOutputStream()
GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(content) }
return bos.toByteArray().toString()
}
fun unzip(content: ByteArray): String =
GZIPInputStream(content.inputStream()).bufferedReader(UTF_8).use { it.readText() }
val newUser: ClubUser = ClubUser().fromJson(json)
if ( newUser.email.isEmpty()) {
@ -252,7 +239,7 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
if ( emailTemplateService == null ) {
emailTemplateService = EmailTemplateService()
}
val html = emailTemplateService!!.getEmailBody(newUser.firstname, activationLink)
val html = emailTemplateService!!.getEmailBody(newUser.firstname, activationLink, "registration_email")
val subject = emailTemplateService!!.getSubject()
// send email

View File

@ -0,0 +1,25 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.CustomerConversation
import com.aitrainer.api.repository.CustomerConversationRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/api")
class CustomerConversationController (private val customerConversationRepository: CustomerConversationRepository
) {
@PostMapping("/customer_conversation")
fun insert(@RequestBody customerConversation: CustomerConversation): ResponseEntity<CustomerConversation> {
return ResponseEntity.ok().body(customerConversationRepository.save(customerConversation))
}
@GetMapping("/customer_conversation/{customerId}")
fun getByCustomerId(@PathVariable customerId: Long): ResponseEntity<List<CustomerConversation>> {
val list = customerConversationRepository.findByCustomerId(customerId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -2,7 +2,9 @@ package com.aitrainer.api.controller
import com.aitrainer.api.model.*
import com.aitrainer.api.model.CustomerMembership
import com.aitrainer.api.model.diet.*
import com.aitrainer.api.repository.*
import com.aitrainer.api.repository.diet.*
import com.google.gson.GsonBuilder
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
@ -21,8 +23,73 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
private val customerActivityRepository: CustomerActivityRepository,
private val customerTrainingPlanRepository: CustomerTrainingPlanRepository,
private val customerMembership: CustomerMembershipRepository,
private val dietRepository: DietRepository,
private val dietRawMaterialRepository: DietRawMaterialRepository,
private val dietUserConsumptionRepository: DietUserConsumptionRepository,
private val dietUserRepository: DietUserRepository,
private val dietUserPreferenceRepository: DietUserPreferenceRepository,
private val dietUserSensitivityRepository: DietUserSensitivityRepository
) {
@GetMapping("/diet_customer_package/{id}")
fun getDietCustomerPackageData(@PathVariable(value = "id") dietUserId: Long): ResponseEntity<String> {
if (dietUserId <= 0) {
return ResponseEntity.notFound().build()
}
val gson = GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setPrettyPrinting()
.create()
val dietUser: DietUser = dietUserRepository.findByDietUserId(dietUserId)
?: return ResponseEntity.notFound().build()
val customerId = dietUser.customerId
val customer: Customer = customerRepository.findByCustomerIdAndActive(customerId, "Y")
?: return ResponseEntity.notFound().build()
val customerJson: String = gson.toJson(customer)
val dietUserJson: String = gson.toJson(dietUser)
val listCustomerProperty = customerPropertyRepository.findLastPropertiesByCustomerId(customerId)
val listCustomerPropertyJson = gson.toJson(listCustomerProperty)
val listMembership = customerMembership.findAllByCustomer(customer)
val listMembershipJson = gson.toJson(listMembership)
val listDiet = dietRepository.findByDietUserId(dietUserId)
val listDietJson = gson.toJson(listDiet)
val listDietRawMaterial = dietRawMaterialRepository.findByDietId(dietUserId)
val listDietRawMaterialJson = gson.toJson(listDietRawMaterial)
val listDietUserConsumption = dietUserConsumptionRepository.findByDietUserId(dietUserId)
val listDietUserConsumptionJson = gson.toJson(listDietUserConsumption)
val listDietUserPreference = dietUserPreferenceRepository.findByDietUserId(dietUserId)
val listDietUserPreferenceJson = gson.toJson(listDietUserPreference)
val listDietUserSensitivity = dietUserSensitivityRepository.findByDietUserId(dietUserId)
val listDietUserSensitivityJson = gson.toJson(listDietUserSensitivity)
val packageJson: String =
getClassRecord(Customer::class.simpleName, customerJson) +
"|||" + getClassRecord(DietUser::class.simpleName, dietUserJson)
"|||" + getClassRecord(CustomerProperty::class.simpleName, listCustomerPropertyJson)
"|||" + getClassRecord(CustomerMembership::class.simpleName, listMembershipJson)
"|||" + getClassRecord(Diet::class.simpleName, listDietJson)
"|||" + getClassRecord(DietRawMaterial::class.simpleName, listDietRawMaterialJson)
"|||" + getClassRecord(DietUserConsumption::class.simpleName, listDietUserConsumptionJson)
"|||" + getClassRecord(DietUserPreference::class.simpleName, listDietUserPreferenceJson)
"|||" + getClassRecord(DietUserSensitivity::class.simpleName, listDietUserSensitivityJson)
return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(packageJson)
}
@GetMapping("/club_customer_package/{id}")
fun getCustomerClubPackageData(@PathVariable(value = "id") customerId: Long): ResponseEntity<String> {
if (customerId <= 0) {

View File

@ -1,10 +1,7 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.Evaluation
import com.aitrainer.api.model.ExerciseTree
import com.aitrainer.api.repository.EvaluationRepository
import com.aitrainer.api.repository.ExerciseTreeParentsRepository
import com.aitrainer.api.repository.ExerciseTreeRepository
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping

View File

@ -0,0 +1,30 @@
package com.aitrainer.api.controller
import com.aitrainer.api.openai.OpenAIService
import kotlinx.coroutines.*
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/api")
class OpenAIController() {
private val logger = LoggerFactory.getLogger(javaClass)
@OptIn(DelicateCoroutinesApi::class)
@GetMapping("/openai/completion")
fun getOpenAIResponse(question: String) : String {
var result = ""
val openAIService = OpenAIService()
val deferred = GlobalScope.async {
openAIService.completion(question)
}
runBlocking {
result = deferred.await()
//println("Result: $result" )
}
return result
}
}

View File

@ -1,7 +1,12 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.*
import com.aitrainer.api.model.diet.DietSensitivity
import com.aitrainer.api.model.diet.RawMaterial
import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.model.diet.Store
import com.aitrainer.api.repository.*
import com.aitrainer.api.repository.diet.*
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
@ -29,9 +34,50 @@ class PackageController(private val exerciseAbilityRepository: ExerciseAbilityRe
private val trainingPlanDayRepository: TrainingPlanDayRepository,
private val appTextRepository: AppTextRepository,
private val trainingProgramRepository: TrainingProgramRepository,
private val membershipRepository: MembershipRepository
private val membershipRepository: MembershipRepository,
private val storeRepository: StoreRepository,
private val recipeRepository: RecipeRepository,
private val rawMaterialRepository: RawMaterialRepository,
private val dietSensitivityRepository: DietSensitivityRepository
) {
@GetMapping("/diet_package")
fun getDietPackageData(): ResponseEntity<String> {
val gson = GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setPrettyPrinting()
.create()
val listProperty:List<Property> = propertyRepository.getProperties()
val listPropertyJson: String = gson.toJson(listProperty)
val listMembership = membershipRepository.findAll()
val listMembershipJson = gson.toJson(listMembership)
val listStore = storeRepository.findAll()
val listStoreJson = gson.toJson(listStore)
val listRecipe = recipeRepository.findAll()
val listRecipeJson = gson.toJson(listRecipe)
val listRawMaterial = rawMaterialRepository.findAll()
val listRawMaterialJson = gson.toJson(listRawMaterial)
val listDietSensitivity = dietSensitivityRepository.findAll()
val listDietSensitivityJson = gson.toJson(listDietSensitivity)
val packageJson: String =
getClassRecord(Property::class.simpleName, listPropertyJson) +
"|||" + getClassRecord(Membership::class.simpleName, listMembershipJson) +
"|||" + getClassRecord(Store::class.simpleName, listStoreJson) +
"|||" + getClassRecord(Recipe::class.simpleName, listRecipeJson) +
"|||" + getClassRecord(RawMaterial::class.simpleName, listRawMaterialJson) +
"|||" + getClassRecord(DietSensitivity::class.simpleName, listDietSensitivityJson)
return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(packageJson)
}
@GetMapping("/club_package")
fun getClubPackageData(): ResponseEntity<String> {

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.Diet
import com.aitrainer.api.repository.diet.DietRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*
@RestController
@RequestMapping("/api")
class DietController(private val dietRepository: DietRepository) {
@PostMapping("/diet")
fun insert(@RequestBody diet: Diet): ResponseEntity<*> {
return ResponseEntity.ok().body(dietRepository.save(diet))
}
@GetMapping("/diet/{dietUserId}")
fun getByDietUserId(@PathVariable dietUserId: Long): ResponseEntity<List<Diet>> {
val list = dietRepository.findByDietUserId(dietUserId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,142 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.Customer
import com.aitrainer.api.model.CustomerMembership
import com.aitrainer.api.model.CustomerPropertyProperty
import com.aitrainer.api.model.diet.DietCustomer
import com.aitrainer.api.model.diet.DietUser
import com.aitrainer.api.repository.CustomerRepository
import com.aitrainer.api.repository.diet.DietUserRepository
import com.aitrainer.api.service.Email
import com.aitrainer.api.service.EmailTemplateService
import com.aitrainer.api.service.Firebase
import com.aitrainer.api.service.ServiceBeans
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
@RestController
@RequestMapping("/api")
class DietCustomerController(private val dietUserRepository: DietUserRepository, private val customerRepository: CustomerRepository) {
@Autowired
var serviceBeans: ServiceBeans? = null
@Autowired
private var emailTemplateService: EmailTemplateService? = null
@PostMapping("/diet_registration")
fun insert(@RequestBody dietCustomerJson: String): ResponseEntity<*> {
val newDietCustomer: DietCustomer = DietCustomer().fromJson(dietCustomerJson)
if ( newDietCustomer.email.isEmpty()) {
return ResponseEntity.badRequest().body("No Email")
}
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
val nowFormatted = current.format(formatter)
val stringCharacters = ('0'..'z').toList().toTypedArray()
val genPassword = (1..10).map { stringCharacters.random() }.joinToString("")
val idToken: String?
var existingCustomer: Customer? = customerRepository.findByEmailAndActive(newDietCustomer.email, "Y")
if (existingCustomer == null ) {
val firebase = Firebase()
val signupResponse = firebase.signUp(newDietCustomer.email, genPassword)
?: return ResponseEntity.badRequest().body("Firebase exception ${firebase.error}")
idToken = signupResponse.idToken
val savingCustomer = Customer()
if ( serviceBeans == null ) {
serviceBeans = ServiceBeans()
}
with (savingCustomer) {
email = newDietCustomer.email
password = serviceBeans!!.passwordEncoder().encode(genPassword)
firebaseRegToken = signupResponse.idToken
firebaseUid = signupResponse.localId
dateAdd = nowFormatted
firstname = newDietCustomer.firstname
name = ""
goal = newDietCustomer.goal
fitnessLevel = newDietCustomer.fitnessLevel
birthYear = newDietCustomer.birthYear
sex = newDietCustomer.sex
}
val newCustomer = customerRepository.save(savingCustomer)
if ( newDietCustomer.weight != 0.0 ) {
val property = CustomerPropertyProperty()
with (property) {
propertyId = 1
propertyValue = newDietCustomer.weight
dateAdd= nowFormatted
goal = false
customer = newCustomer
}
newCustomer.properties.add(property)
}
if ( newDietCustomer.height != 0.0 ) {
val property = CustomerPropertyProperty()
with (property) {
propertyId = 2
propertyValue = newDietCustomer.height
dateAdd = nowFormatted
goal = false
customer = newCustomer
}
newCustomer.properties.add(property)
}
val newMembershipId = newDietCustomer.membershipId
if ( newMembershipId != 0L) {
val membership = CustomerMembership()
with(membership) {
customer = newCustomer
membershipId = newMembershipId
startDate = nowFormatted
}
newCustomer.memberships.add(membership)
}
customerRepository.save(newCustomer)
existingCustomer = newCustomer
} else {
val existingDietUser = dietUserRepository.findByCustomerId(existingCustomer.customerId)
if ( existingDietUser != null ) {
return ResponseEntity.badRequest().body("DietCustomer exists")
} else {
val newDietUser = DietUser(
customerId = existingCustomer.customerId
)
dietUserRepository.save(newDietUser)
}
idToken = existingCustomer.firebaseRegToken!!
}
// create email link
val activationLink = "https://diet4you.andio.hu/welcome/id=$idToken"
if ( emailTemplateService == null ) {
emailTemplateService = EmailTemplateService()
}
val html = emailTemplateService!!.getEmailBody(newDietCustomer.firstname, activationLink, "diet_registration_email")
val subject = emailTemplateService!!.getSubjectDiet()
// send email
val email = Email()
email.send(newDietCustomer.email, html, subject)
return ResponseEntity.ok().body(existingCustomer)
}
}

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.DietRawMaterial
import com.aitrainer.api.repository.diet.DietRawMaterialRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*
@RestController
@RequestMapping("/api")
class DietRawMaterialController(private val dietRawMaterialRepository: DietRawMaterialRepository) {
@PostMapping("/diet_raw_material")
fun insert(@RequestBody dietRawMaterial: DietRawMaterial): ResponseEntity<*> {
return ResponseEntity.ok().body(dietRawMaterialRepository.save(dietRawMaterial))
}
@GetMapping("/diet_raw_material/{dietId}")
fun getByDietUserId(@PathVariable dietId: Long): ResponseEntity<List<DietRawMaterial>> {
val list = dietRawMaterialRepository.findByDietId(dietId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.DietSensitivity
import com.aitrainer.api.repository.diet.DietSensitivityRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class DietSensitivityController(private val dietSensitivityRepository: DietSensitivityRepository) {
@PostMapping ("/diet_sensitivity")
fun insert(@RequestBody dietSensitivity: DietSensitivity): ResponseEntity<DietSensitivity> {
val newDietSensitivity = dietSensitivityRepository.save(dietSensitivity)
return ResponseEntity.ok().body(newDietSensitivity)
}
@GetMapping("/diet_sensitivity")
fun getAll(): ResponseEntity<List<DietSensitivity>> {
val list = dietSensitivityRepository.findAll()
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,43 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.DietUserConsumption
import com.aitrainer.api.repository.diet.DietUserConsumptionRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*
@RestController
@RequestMapping("/api")
class DietUserConsumptionController(private val dietUserConsumptionRepository: DietUserConsumptionRepository) {
@PostMapping("/diet_user_consumption")
fun insert(@RequestBody dietUserConsumption: DietUserConsumption): ResponseEntity<*> {
return ResponseEntity.ok().body(dietUserConsumptionRepository.save(dietUserConsumption))
}
@PostMapping("/diet_user_consumption/{id}")
fun update(@PathVariable(value = "id") id: Long, @RequestBody dietUserConsumption: DietUserConsumption): ResponseEntity<*> {
return dietUserConsumptionRepository.findById(id).map { existingConsumption ->
val updatedConsumption: DietUserConsumption = existingConsumption.copy(
rawMaterialId = dietUserConsumption.rawMaterialId,
dateConsumption = dietUserConsumption.dateConsumption,
name = dietUserConsumption.name,
quantity = dietUserConsumption.quantity,
quantityUnit = dietUserConsumption.quantityUnit,
cal = dietUserConsumption.cal,
protein = dietUserConsumption.protein,
fat = dietUserConsumption.fat,
ch = dietUserConsumption.ch,
sugar = dietUserConsumption.sugar
)
ResponseEntity.ok().body(dietUserConsumptionRepository.save(updatedConsumption))
}.orElse(ResponseEntity.notFound().build())
}
@GetMapping("/diet_user_consumption/{dietUserId}")
fun getByDietUserId(@PathVariable dietUserId: Long): ResponseEntity<List<DietUserConsumption>> {
val list = dietUserConsumptionRepository.findByDietUserId(dietUserId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.DietUser
import com.aitrainer.api.repository.diet.DietUserRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class DietUserController(private val dietUserRepository: DietUserRepository) {
@PostMapping ("/diet_user")
fun insert(@RequestBody dietUser: DietUser): ResponseEntity<DietUser> {
val newDietUser = dietUserRepository.save(dietUser)
return ResponseEntity.ok().body(newDietUser)
}
@GetMapping("/diet_user/{customerId}")
fun getByCustomerId(@PathVariable customerId: Long): ResponseEntity<DietUser> {
val dietUser = dietUserRepository.findByCustomerId(customerId)
return if (dietUser == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(dietUser)
}
}

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.DietUserPreference
import com.aitrainer.api.repository.diet.DietUserPreferenceRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*
@RestController
@RequestMapping("/api")
class DietUserPreferenceController(private val dietUserPreferenceRepository: DietUserPreferenceRepository) {
@PostMapping("/diet_user_preference")
fun insert(@RequestBody dietUserPreference: DietUserPreference): ResponseEntity<*> {
return ResponseEntity.ok().body(dietUserPreferenceRepository.save(dietUserPreference))
}
@GetMapping("/diet_user_preference/{dietUserId}")
fun getByDietUserId(@PathVariable dietUserId: Long): ResponseEntity<List<DietUserPreference>> {
val list = dietUserPreferenceRepository.findByDietUserId(dietUserId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.DietUserSensitivity
import com.aitrainer.api.repository.diet.DietUserSensitivityRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*
@RestController
@RequestMapping("/api")
class DietUserSensitivityController(private val dietUserSensitivityRepository: DietUserSensitivityRepository) {
@PostMapping("/diet_user_sensitivity")
fun insert(@RequestBody dietUserSensitivity: DietUserSensitivity): ResponseEntity<*> {
return ResponseEntity.ok().body(dietUserSensitivityRepository.save(dietUserSensitivity))
}
@GetMapping("/diet_user_sensitivity/{dietUserId}")
fun getByDietUserId(@PathVariable dietUserId: Long): ResponseEntity<List<DietUserSensitivity>> {
val list = dietUserSensitivityRepository.findByDietUserId(dietUserId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,31 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.RawMaterial
import com.aitrainer.api.repository.diet.RawMaterialRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class RawMaterialController(private val rawMaterialRepository: RawMaterialRepository) {
@PostMapping ("/raw_material")
fun insert(@RequestBody rawMaterial: RawMaterial): ResponseEntity<RawMaterial> {
val newRawMaterial = rawMaterialRepository.save(rawMaterial)
return ResponseEntity.ok().body(newRawMaterial)
}
@GetMapping("/raw_material")
fun getAll(): ResponseEntity<List<RawMaterial>> {
val list = rawMaterialRepository.findAll()
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
@GetMapping("/raw_material/{id}")
fun getByRawMaterialId(@PathVariable id: Long): ResponseEntity<RawMaterial> {
val rawMaterial = rawMaterialRepository.findByRawMaterialId(id)
return if (rawMaterial == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(rawMaterial)
}
}

View File

@ -0,0 +1,58 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.repository.diet.RecipeRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class RecipeController(private val recipeRepository: RecipeRepository) {
@PostMapping ("/recipe")
fun insert(@RequestBody recipe: Recipe): ResponseEntity<Recipe> {
val newRecipe = recipeRepository.save(recipe)
return ResponseEntity.ok().body(newRecipe)
}
@PostMapping("/recipe/{id}")
fun update(@PathVariable(value = "id") id: Long, @RequestBody recipe: Recipe): ResponseEntity<Recipe> {
val existingRecipe = recipeRepository.findByRecipeId(id) ?: return ResponseEntity.notFound().build()
val updatedRecipe: Recipe = existingRecipe.copy(
name = recipe.name,
description = recipe.description,
cal = recipe.cal,
ch = recipe.ch,
fat = recipe.fat,
protein = recipe.protein,
dietUserId = recipe.dietUserId
)
recipe.rawMaterials.forEach {
it.recipe = recipe
updatedRecipe.rawMaterials.add(it)
}
return ResponseEntity.ok().body(recipeRepository.save(updatedRecipe))
}
@GetMapping("/recipe")
fun getAll(): ResponseEntity<List<Recipe>> {
val list = recipeRepository.findAll()
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
@GetMapping("/recipe/{dietUserId}")
fun getByDietUserId(@PathVariable dietUserId: Long): ResponseEntity<List<Recipe>> {
val list = recipeRepository.findByDietUserId(dietUserId)
return if (list == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
@GetMapping("/recipe/name/{name}")
fun getByName(@PathVariable name: String): ResponseEntity<List<Recipe>> {
val list = recipeRepository.findByName(name)
return if (list == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -0,0 +1,17 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.RecipeRawMaterial
import com.aitrainer.api.repository.diet.RecipeRawMaterialRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class RecipeRawMaterialController(private val recipeRawMaterialRepository: RecipeRawMaterialRepository) {
@PostMapping ("/recipe_raw_material")
fun insert(@RequestBody recipeRawMaterial: RecipeRawMaterial): ResponseEntity<RecipeRawMaterial> {
val newRecipe = recipeRawMaterialRepository.save(recipeRawMaterial)
return ResponseEntity.ok().body(recipeRawMaterial)
}
}

View File

@ -0,0 +1,32 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.Store
import com.aitrainer.api.repository.diet.StoreRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class StoreController(private val storeRepository: StoreRepository) {
@PostMapping ("/store")
fun insert(@RequestBody store: Store): ResponseEntity<Store> {
val newStore = storeRepository.save(store)
return ResponseEntity.ok().body(newStore)
}
@GetMapping("/store")
fun getAll(): ResponseEntity<List<Store>> {
val list = storeRepository.findAll()
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
@GetMapping("/store/{name}/{country}")
fun getByNameAndCountry(@PathVariable name: String, @PathVariable country: String ): ResponseEntity<Store> {
val store = storeRepository.findByNameAndCountry(name, country)
return if (store == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(store)
}
}

View File

@ -0,0 +1,17 @@
package com.aitrainer.api.model
import com.google.gson.annotations.Expose
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import javax.validation.constraints.NotNull
@Entity
data class CustomerConversation(
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0,
@Expose @get: NotNull val customerId: Long,
@Expose @get: NotNull val conversationDate: String = "",
@Expose @get: NotNull val question: String = "",
@Expose @get: NotNull val answer: String = ""
)

View File

@ -0,0 +1,27 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
@Entity
data class Diet (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val dietId: Long = 0,
@Expose @get: NotNull val dietUserId: Long = 0,
@Expose @get: NotNull val dietText: String = "",
@Expose @get: NotNull val monday: String = "",
@Expose @get: NotNull val tuesday: String = "",
@Expose @get: NotNull val wednesday: String = "",
@Expose @get: NotNull val thursday: String = "",
@Expose @get: NotNull val friday: String = "",
@Expose @get: NotNull val saturday: String = "",
@Expose @get: NotNull val sunday: String = ""
)

View File

@ -0,0 +1,23 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@Serializable
data class DietCustomer(
@Expose var firstname: String = "",
@Expose var email: String = "",
@Expose var sex: String = "m",
@Expose var goal: String = "",
@Expose var fitnessLevel: String = "beginner",
@Expose var birthYear: Int = 0,
@Expose var weight: Double = 0.0,
@Expose var height: Double = 0.0,
@Expose var membershipId: Long = 0,
){
fun fromJson(json: String): DietCustomer {
return Json.decodeFromString(serializer(), json)
}
}

View File

@ -0,0 +1,22 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
@Entity
data class DietRawMaterial (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
@Expose @get: NotNull val dietId: Long = 0,
@Expose @get: NotNull val rawMaterialId: Long = 0,
@Expose @get: NotNull val name: String = "",
@Expose @get: NotNull val kcalMin: Int = 0,
@Expose @get: NotNull val kcalMax: Int = 0,
@Expose @get: NotNull val proteinMin: Int = 0,
@Expose @get: NotNull val proteinMax: Int = 0,
@Expose @get: NotNull val fatMin: Int = 0,
@Expose @get: NotNull val fatMax: Int = 0,
@Expose @get: NotNull val chMin: Int = 0,
@Expose @get: NotNull val chMax: Int = 0,
@Expose @get: NotNull val sugar: Int = 0,
)

View File

@ -0,0 +1,14 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import javax.validation.constraints.NotNull
@Entity
data class DietSensitivity(
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0,
@Expose @get: NotNull val name: String = "",
)

View File

@ -0,0 +1,14 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import javax.validation.constraints.NotNull
@Entity
data class DietUser(
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var dietUserId: Long = 0,
@Expose @get: NotNull val customerId: Long
)

View File

@ -0,0 +1,24 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
import org.springframework.lang.NonNull
@Entity
data class DietUserConsumption(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
@Expose @get: NotNull val dietUserId: Long = 0,
@Expose @get: NotNull val rawMaterialId: Long = 0,
@Expose @get: NotNull val name: String = "",
@Expose @get: NonNull var dateConsumption: String = "",
@Expose @get: NotNull val quantity: Double = 0.0,
@Expose @get: NotNull val quantityUnit: String = "",
@Expose @get: NotNull val cal: Int = 0,
@Expose @get: NotNull val protein: Double = 0.0,
@Expose @get: NotNull val fat: Double = 0.0,
@Expose @get: NotNull val ch: Double = 0.0,
@Expose @get: NotNull val sugar: Double = 0.0,
)

View File

@ -0,0 +1,16 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import javax.validation.constraints.NotNull
@Entity
data class DietUserPreference(
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0,
@Expose @get: NotNull val dietUserId: Long = 0,
@Expose @get: NotNull val rawMaterialId: Long = 0,
@Expose @get: NotNull val temperature: Byte = 0,
)

View File

@ -0,0 +1,15 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import javax.validation.constraints.NotNull
@Entity
data class DietUserSensitivity(
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0,
@Expose @get: NotNull val dietUserId: Long = 0,
@Expose @get: NotNull val sensitivityId: Long = 0,
)

View File

@ -0,0 +1,22 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
@Entity
data class RawMaterial (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
@Expose @get: NotNull val name: String = "",
@Expose @get: NotNull val description: String = "",
@Expose @get: NotNull val kcalMin: Int = 0,
@Expose @get: NotNull val kcalMax: Int = 0,
@Expose @get: NotNull val proteinMin: Int = 0,
@Expose @get: NotNull val proteinMax: Int = 0,
@Expose @get: NotNull val fatMin: Int = 0,
@Expose @get: NotNull val fatMax: Int = 0,
@Expose @get: NotNull val chMin: Int = 0,
@Expose @get: NotNull val chMax: Int = 0,
@Expose @get: NotNull val sugar: Int = 0,
@Expose @get: NotNull val storeId: Long = 0,
)

View File

@ -0,0 +1,26 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
import org.jetbrains.annotations.NotNull
@Entity
data class Recipe (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val recipeId: Long = 0,
@Expose @get: NotNull val name: String = "",
@Expose @get: NotNull val description: String = "",
@Expose @get: NotNull val cal: Int = 0,
@Expose @get: NotNull val protein: Double = 0.0,
@Expose @get: NotNull val fat: Double = 0.0,
@Expose @get: NotNull val ch: Double = 0.0,
@Expose @get: NotNull val dietUserId: Long = 0,
) {
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "recipe")
@Fetch(value = FetchMode.SUBSELECT)
@Expose val rawMaterials: MutableList<RecipeRawMaterial> = mutableListOf()
}

View File

@ -0,0 +1,20 @@
package com.aitrainer.api.model.diet
import com.fasterxml.jackson.annotation.JsonIgnore
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
@Entity
data class RecipeRawMaterial (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
@Expose @get: NotNull var rawMaterialId: Int = 0,
@Expose @get: NotNull var quantity: Int = 0,
@Expose @get: NotNull var quantityUnit: String = "",
) {
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "recipeId", nullable = false)
@JsonIgnore
var recipe: Recipe? = null
}

View File

@ -0,0 +1,13 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
@Entity
data class Store (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val storeId: Long = 0,
@Expose @get: NotNull val storeName: String = "",
@Expose @get: NotNull val country: String = "",
)

View File

@ -0,0 +1,53 @@
package com.aitrainer.api.openai
import com.aallam.openai.client.OpenAI
import com.aallam.openai.api.completion.CompletionRequest
import com.aallam.openai.api.completion.TextCompletion
import com.aallam.openai.api.model.Model
import com.aallam.openai.api.model.ModelId
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.Properties
class OpenAIService {
private var openAI: OpenAI? = null
var model: Model? = null
private val properties = Properties()
init {
val inputStream = ClassLoader.getSystemResourceAsStream("application.properties")
properties.load(inputStream)
inputStream?.close()
}
private var modelId: ModelId? = null
private suspend fun connect(modelName: String) {
openAI = OpenAI(properties.getProperty("openai.key"))
modelId = ModelId(modelName)
model = openAI!!.model(modelId!!)
}
suspend fun completion(question: String): String {
return withContext(Dispatchers.IO) {
if (openAI == null) {
connect("text-davinci-003")
}
val completionRequest = CompletionRequest(
model = modelId!!,
prompt = question,
//echo = true,
maxTokens = 128,
temperature=0.1,
)
val completion: TextCompletion = openAI!!.completion(completionRequest)
val result = completion.choices[0].text
//println("Completion: $result")
result
}
}
}

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository
import com.aitrainer.api.model.CustomerConversation
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface CustomerConversationRepository : JpaRepository<CustomerConversation, Long> {
fun findByCustomerId(customerId: Long): List<CustomerConversation>
}

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.DietRawMaterial
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface DietRawMaterialRepository : JpaRepository<DietRawMaterial, Long> {
fun findByDietId(dietId: Long): List<DietRawMaterial>
}

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.Diet
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface DietRepository : JpaRepository<Diet, Long> {
fun findByDietUserId(dietUserId: Long): List<Diet>
}

View File

@ -0,0 +1,6 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.DietSensitivity
import org.springframework.data.jpa.repository.JpaRepository
interface DietSensitivityRepository : JpaRepository<DietSensitivity, Int>

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.DietUserConsumption
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface DietUserConsumptionRepository : JpaRepository<DietUserConsumption, Long> {
fun findByDietUserId(dietUserId: Long): List<DietUserConsumption>
}

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.DietUserPreference
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface DietUserPreferenceRepository : JpaRepository<DietUserPreference, Long> {
fun findByDietUserId(dietUserId: Long): List<DietUserPreference>
}

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.DietUser
import org.springframework.data.jpa.repository.JpaRepository
interface DietUserRepository : JpaRepository<DietUser, Int> {
fun findByCustomerId(customerId: Long): DietUser?
fun findByDietUserId(dietUserId: Long): DietUser?
}

View File

@ -0,0 +1,10 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.DietUserSensitivity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface DietUserSensitivityRepository : JpaRepository<DietUserSensitivity, Long> {
fun findByDietUserId(dietUserId: Long): List<DietUserSensitivity>
}

View File

@ -0,0 +1,12 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.RawMaterial
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
interface RawMaterialRepository : JpaRepository<RawMaterial, Int> {
@Query(" FROM RawMaterial " +
" WHERE id = :id"
)
fun findByRawMaterialId(id: Long): RawMaterial?
}

View File

@ -0,0 +1,9 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.RecipeRawMaterial
import org.springframework.data.jpa.repository.JpaRepository
interface RecipeRawMaterialRepository : JpaRepository<RecipeRawMaterial, Int> {
fun findByRawMaterialId(rawMaterialId: Long): List<RecipeRawMaterial>?
}

View File

@ -0,0 +1,14 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.Recipe
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
interface RecipeRepository : JpaRepository<Recipe, Int> {
@Query(" FROM Recipe WHERE name like %:name%")
fun findByName(name: String): List<Recipe>?
fun findByDietUserId(dietUserId: Long): List<Recipe>?
@Query(" FROM Recipe WHERE recipeId = :recipeId")
fun findByRecipeId(recipeId: Long): Recipe?
}

View File

@ -0,0 +1,11 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.Store
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
interface StoreRepository : JpaRepository<Store, Int> {
@Query(" FROM Store WHERE storeName = :name and country = :country")
fun findByNameAndCountry(name: String, country: String ): Store?
}

View File

@ -3,9 +3,6 @@ package com.aitrainer.api.service
import java.util.Properties
import jakarta.mail.*
import jakarta.mail.internet.*
import org.jasypt.encryption.StringEncryptor
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig
class Email {
@ -19,23 +16,9 @@ class Email {
put("mail.smtp.port", "587")
}
fun getEncryptor(): StringEncryptor {
val encryptor = PooledPBEStringEncryptor()
val config = SimpleStringPBEConfig()
config.password = "workouttest"
config.algorithm = "PBEWithMD5AndDES"
config.setKeyObtentionIterations("1000")
config.setPoolSize("1")
config.providerName = "SunJCE"
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator")
config.stringOutputType = "base64"
encryptor.setConfig(config)
return encryptor
}
val session: Session = Session.getInstance(properties, object : Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication("service@workouttest.com", getEncryptor().decrypt(encodedPassword))
return PasswordAuthentication("service@workouttest.com", Encryptor.getEncryptor().decrypt(encodedPassword))
}
})
@ -47,7 +30,7 @@ class Email {
}
val bodyPart = MimeBodyPart().apply {
setContent(emailBody, "text/html")
setContent(emailBody, "text/html; charset=UTF-8")
}
val multipart = MimeMultipart().apply {

View File

@ -10,14 +10,15 @@ class EmailTemplateService {
@Autowired
private var templateEngine: TemplateEngine? = null
fun getEmailBody(firstname: String, activationLink: String): String {
fun getEmailBody(firstname: String, activationLink: String, template: String): String {
val context = Context()
context.setVariable("firstname", firstname)
context.setVariable("activationLink", activationLink)
if ( templateEngine == null) {
templateEngine = TemplateEngine()
}
return templateEngine!!.process("registration_email", context)
return templateEngine!!.process(template, context)
}
fun getSubject(): String {
@ -27,5 +28,12 @@ class EmailTemplateService {
}
return templateEngine!!.process("registration_subject", context)
}
fun getSubjectDiet(): String {
val context = Context()
if ( templateEngine == null) {
templateEngine = TemplateEngine()
}
return templateEngine!!.process("diet_registration_subject", context)
}
}

View File

@ -0,0 +1,21 @@
package com.aitrainer.api.service
import org.jasypt.encryption.StringEncryptor
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig
object Encryptor {
fun getEncryptor(): StringEncryptor {
val encryptor = PooledPBEStringEncryptor()
val config = SimpleStringPBEConfig()
config.password = "workouttest"
config.algorithm = "PBEWithMD5AndDES"
config.setKeyObtentionIterations("1000")
config.setPoolSize("1")
config.providerName = "SunJCE"
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator")
config.stringOutputType = "base64"
encryptor.setConfig(config)
return encryptor
}
}

View File

@ -14,6 +14,6 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structue has been changed, increment this version number
application.version=1.1.0
application.version=1.2.0
jwt.secret=aitrainer

View File

@ -17,6 +17,9 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.1.0
application.version=1.2.0
jwt.secret=aitrainer
openai.key=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
spring.mail.properties.mail.mime.charset=UTF-8

View File

@ -28,7 +28,7 @@
</appender>
<!-- <logger name="org.springframework" level="DEBUG" />
<!--<logger name="org.springframework" level="DEBUG" />
<logger name="org.apache.tomcat" level="DEBUG"/>
<logger name="org.apache.coyote" level="DEBUG"/>
<logger name="com.github.ulisesbocchio" level="DEBUG" />

View File

@ -0,0 +1,25 @@
<html lang="hu" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Registration</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Szia ' + ${firstname} + '!'">Szia [firstname]!</p>
<p>
Üdvözlünk a Diet 4 You (Diéta Neked) tagjai között! Örülünk, hogy velünk vagy. Az étrended a Diet4You zárt felületén tudod elérni. A belépéshez használd ezt a linket:<br/><br/>
<a th:href="${activationLink}" th:text="${activationLink}">${activationLink}</a>
</p>
<p>
Kérlek, kattints a linkre a fiókod aktiválásához. Ha bármilyen problémád van, ne habozz velünk kapcsolatba lépni.
</p>
<p>
Köszönjük, hogy velünk dolgozol.
</p>
<p>
Üdvözlettel,<br>
Diéta Neked Csapata<br/>
Diet 4 You Team<br/>
mailto: diet4you@andio.hu<br/>
</p>
</body>
</html>

View File

@ -0,0 +1 @@
[Diet4You] Üdv a céltudatosok között!

View File

@ -1,6 +1,10 @@
<html lang="hu" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Registration</title>
<meta charset="utf-8" />
</head>
<body>
<p th:text="'Szia ' + ${firstname}">Szia [firstname]!</p>
<p th:text="'Szia ' + ${firstname} + '!'">Szia [firstname]!</p>
<p>
Üdvözlünk a Workout Test Club tagjai között! Örülünk, hogy velünk vagy. A Workout Test Club-ba való belépéshez használad ezt a linket:<br/><br/>
<a th:href="${activationLink}" th:text="${activationLink}">${activationLink}</a>

View File

@ -2,7 +2,9 @@ package com.aitrainer.api.test
import com.aitrainer.api.controller.CustomerPackageController
import com.aitrainer.api.model.*
import com.aitrainer.api.model.diet.*
import com.aitrainer.api.repository.*
import com.aitrainer.api.repository.diet.*
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.junit.jupiter.api.Test
@ -45,6 +47,109 @@ class AppCustomerPackageTest {
@Autowired
private lateinit var customerMembershipRepository: CustomerMembershipRepository
@Autowired
private lateinit var dietRepository: DietRepository
@Autowired
private lateinit var dietRawMaterialRepository: DietRawMaterialRepository
@Autowired
private lateinit var dietUserConsumptionRepository: DietUserConsumptionRepository
@Autowired
private lateinit var dietUserRepository: DietUserRepository
@Autowired
private lateinit var dietUserPreferenceRepository: DietUserPreferenceRepository
@Autowired
private lateinit var dietUserSensitivityRepository: DietUserSensitivityRepository
@Test
fun dietCustomerPackageTest() {
val gson = Gson()
val controller = CustomerPackageController(
customerRepository,
customerExerciseDeviceRepository,
exercisesRepository,
purchaseRepository,
customerPropertyRepository,
exerciseResultRepository,
customerActivityRepository,
customerTrainingPlanRepository,
customerMembershipRepository,
dietRepository,
dietRawMaterialRepository,
dietUserConsumptionRepository,
dietUserRepository,
dietUserPreferenceRepository,
dietUserSensitivityRepository
)
val response = controller.getDietCustomerPackageData(2)
assertEquals(response.statusCode, HttpStatus.OK)
val dietCustomerPackageJson: String = response.body as String
assertTrue(dietCustomerPackageJson.isNotEmpty())
val packages = dietCustomerPackageJson.split("|||").toTypedArray()
packages.forEach {
val record = it.split("***")
print(record[0] + "\n")
when {
record[0] == Diet::class.simpleName -> {
val dietJson: String = record[1]
val type = object : TypeToken<List<Diet?>?>() {}.type
val list: List<Diet> = gson.fromJson(dietJson, type)
assertEquals(list[0].monday, "Monday meal")
assertEquals(list[0].dietText, "Test diet text")
}
record[0] == DietRawMaterial::class.simpleName -> {
val dietRawMaterialJson: String = record[1]
val type = object : TypeToken<List<DietRawMaterial?>?>() {}.type
val list: List<DietRawMaterial> = gson.fromJson(dietRawMaterialJson, type)
assertEquals(list[0].name, "Tükörtojás")
assertEquals(list[0].kcalMin, 70)
}
record[0] == DietUserConsumption::class.simpleName -> {
val dietUserConsumptionJson: String = record[1]
val type = object : TypeToken<List<DietUserConsumption?>?>() {}.type
val list: List<DietUserConsumption> = gson.fromJson(dietUserConsumptionJson, type)
assertEquals(list[0].name, "Tükörtojás")
assertEquals(list[0].quantity, 120.0)
assertEquals(list[0].ch, 1.0)
}
record[0] == DietUser::class.simpleName -> {
val dietUserJson: String = record[1]
val type = object : TypeToken<DietUser?>() {}.type
val list: DietUser = gson.fromJson(dietUserJson, type)
assertEquals(list.customerId, 764)
}
record[0] == DietUserPreference::class.simpleName -> {
val dietUserPreferenceJson: String = record[1]
val type = object : TypeToken<List<DietUserPreference?>?>() {}.type
val list: List<DietUserPreference> = gson.fromJson(dietUserPreferenceJson, type)
assertEquals(list[0].dietUserId, 5)
assertEquals(list[0].temperature, -1)
}
record[0] == DietUserSensitivity::class.simpleName -> {
val dietUserSensitivityJson: String = record[1]
val type = object : TypeToken<List<DietUserSensitivity?>?>() {}.type
val list: List<DietUserSensitivity> = gson.fromJson(dietUserSensitivityJson, type)
assertEquals(list[0].dietUserId, 5)
assertEquals(list[0].sensitivityId, 1)
}
}
}
}
@Test
fun customerClubPackageTest() {
val gson = Gson()
@ -58,7 +163,13 @@ class AppCustomerPackageTest {
exerciseResultRepository,
customerActivityRepository,
customerTrainingPlanRepository,
customerMembershipRepository
customerMembershipRepository,
dietRepository,
dietRawMaterialRepository,
dietUserConsumptionRepository,
dietUserRepository,
dietUserPreferenceRepository,
dietUserSensitivityRepository
)
val response = controller.getCustomerClubPackageData(90)
@ -106,7 +217,15 @@ class AppCustomerPackageTest {
val controller = CustomerPackageController(customerRepository, customerExerciseDeviceRepository,
exercisesRepository, purchaseRepository, customerPropertyRepository,
exerciseResultRepository, customerActivityRepository, customerTrainingPlanRepository, customerMembershipRepository )
exerciseResultRepository, customerActivityRepository, customerTrainingPlanRepository,
customerMembershipRepository,
dietRepository,
dietRawMaterialRepository,
dietUserConsumptionRepository,
dietUserRepository,
dietUserPreferenceRepository,
dietUserSensitivityRepository )
var response: ResponseEntity<*> = controller.getCustomerPackageData(91)
assertEquals(response.statusCode, HttpStatus.NOT_FOUND)

View File

@ -2,7 +2,12 @@ package com.aitrainer.api.test
import com.aitrainer.api.controller.PackageController
import com.aitrainer.api.model.*
import com.aitrainer.api.model.diet.DietSensitivity
import com.aitrainer.api.model.diet.RawMaterial
import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.model.diet.Store
import com.aitrainer.api.repository.*
import com.aitrainer.api.repository.diet.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
@ -56,6 +61,105 @@ class AppPackageTest {
@Autowired
private lateinit var membershipRepository: MembershipRepository
@Autowired
private lateinit var dietSensitivityRepository: DietSensitivityRepository
@Autowired
private lateinit var rawMaterialRepository: RawMaterialRepository
@Autowired
private lateinit var recipeRepository: RecipeRepository
@Autowired
private lateinit var storeRepository: StoreRepository
@Test
fun testDietPackage() {
val gson = Gson()
val controller = PackageController(
exerciseAbilityRepository,
exerciseTypeRepository,
exerciseTreeRepository,
propertyRepository,
productRepository,
exerciseDeviceRepository,
exerciseTreeParentsRepository,
exercisePlanTemplateRepository,
evaluationRepository,
sportRepository,
tutorialRepository,
descriptionRepository,
faqRepository,
trainingPlanRepository,
splitTestsRepository,
trainingPlanDayRepository,
appTextRepository,
trainingProgramRepository,
membershipRepository,
storeRepository,
recipeRepository,
rawMaterialRepository,
dietSensitivityRepository,
)
val response: ResponseEntity<*> = controller.getDietPackageData()
val dietPackageJson: String = response.body as String
assertTrue(dietPackageJson.isNotEmpty())
val packages = dietPackageJson.split("|||").toTypedArray()
packages.forEach {
val record = it.split("***")
if (record[0] == Membership::class.simpleName) {
val membershipJson: String = record[1]
val type = object : TypeToken<List<Membership?>?>() {}.type
val listMembership: List<Membership> = gson.fromJson(membershipJson, type)
assertTrue(listMembership.isNotEmpty())
} else if ( record[0] == Store::class.simpleName) {
val storeJson: String = record[1]
val type = object : TypeToken<List<Store?>?>() {}.type
val listStore: List<Store> = gson.fromJson(storeJson, type)
assertTrue(listStore.isNotEmpty())
assertEquals(listStore[0].storeName, "Tesco")
assertEquals(listStore[0].country, "HU")
assertEquals(listStore[3].country, "DE")
assertEquals(listStore[3].storeName, "Aldi")
} else if ( record[0] == Recipe::class.simpleName) {
val recipeJson: String = record[1]
val type = object : TypeToken<List<Recipe?>?>() {}.type
val list: List<Recipe> = gson.fromJson(recipeJson, type)
assertTrue(list.isNotEmpty())
assertEquals(list[0].name, "Bableves")
assertEquals(list[0].cal, 180)
assertEquals(list[0].dietUserId, 4)
assertEquals(list[2].name, "Borsóleves")
assertEquals(list[2].protein, 45.0)
assertEquals(list[2].dietUserId, 1)
} else if ( record[0] == RawMaterial::class.simpleName) {
val rawMaterialJson: String = record[1]
val type = object : TypeToken<List<RawMaterial?>?>() {}.type
val list: List<RawMaterial> = gson.fromJson(rawMaterialJson, type)
assertTrue(list.isNotEmpty())
assertEquals(list[0].name, "Tükörtojás")
assertEquals(list[0].kcalMin, 70)
assertEquals(list[0].proteinMax, 25)
assertEquals(list[0].storeId, 4)
assertEquals(list[1].name, "Tigris buci")
} else if ( record[0] == DietSensitivity::class.simpleName) {
val dietSensitivityJson: String = record[1]
val type = object : TypeToken<List<DietSensitivity?>?>() {}.type
val list: List<DietSensitivity> = gson.fromJson(dietSensitivityJson, type)
assertTrue(list.isNotEmpty())
assertEquals(list[0].name, "Nut Allergy")
assertEquals(list[1].name, "Penicillin sensitivity")
}
}
}
@Test
fun testClubPackage() {
val gson = Gson()
@ -79,7 +183,11 @@ class AppPackageTest {
trainingPlanDayRepository,
appTextRepository,
trainingProgramRepository,
membershipRepository
membershipRepository,
storeRepository,
recipeRepository,
rawMaterialRepository,
dietSensitivityRepository,
)
val response: ResponseEntity<*> = controller.getClubPackageData()
@ -135,7 +243,11 @@ class AppPackageTest {
trainingPlanDayRepository,
appTextRepository,
trainingProgramRepository,
membershipRepository
membershipRepository,
storeRepository,
recipeRepository,
rawMaterialRepository,
dietSensitivityRepository,
)
val response: ResponseEntity<*> = controller.getPackageData()

View File

@ -0,0 +1,20 @@
package com.aitrainer.api.test
import com.aitrainer.api.controller.OpenAIController
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CompletionTest {
@Test
fun testCompletion() {
val openAIController = OpenAIController()
val response = openAIController.getOpenAIResponse("Mennyi 3 darab közepes tükörtojás kalóriaértéke? Csak egy szám intervallumértéket adj vissza eredményként")
println(response)
// response = openAIController.getOpenAIResponse("Az utolsó 5 kérdésemre: Mennyi 3 darab tükörtojás kalóriaértéke? nagy szórású intervallumokat adtál vissza. Hogyan tudom pontosítani a kérdést?")
// println(response)
}
}

View File

@ -0,0 +1,76 @@
package com.aitrainer.api.test
import com.aitrainer.api.model.CustomerConversation
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CustomerConversationTest {
@Autowired
private lateinit var mockMvc: MockMvc
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert customer conversation`() {
val customerConversation = CustomerConversation(1, 123, "2022-01-01 10:10:10", "What's up?", "Not much.")
val gson = Gson()
mockMvc.perform(
post("/api/customer_conversation")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(gson.toJson(customerConversation))
)
.andExpect(status().isOk)
.andExpect(content().json("""
{
"id": 1,
"customerId": 123,
"conversationDate": "2022-01-01 10:10:10",
"question": "What's up?",
"answer": "Not much."
}
""".trimIndent()))
}
@Test
fun `get customer conversation by customer id`() {
mockMvc.perform(get("/api/customer_conversation/123")
.header("Authorization", "Bearer $authToken"))
.andExpect(status().isOk)
.andExpect(content().json("""
[
{
"id": 1,
"customerId": 123,
"conversationDate": "2022-01-01 10:10:10",
"question": "What's up?",
"answer": "Not much."
}
]
""".trimIndent()))
}
}

View File

@ -1,26 +1,31 @@
package com.aitrainer.api.test
import com.aitrainer.api.controller.CustomerMembershipController
import com.aitrainer.api.model.Customer
import com.aitrainer.api.model.CustomerMembership
import com.aitrainer.api.repository.CustomerMembershipRepository
import com.aitrainer.api.repository.CustomerRepository
import com.google.gson.Gson
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.test.web.servlet.MockMvc
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CustomerMembershipTest {
@Autowired
private lateinit var customerMembershipRepository: CustomerMembershipRepository
@Autowired lateinit var customerRepository: CustomerRepository
@Autowired
lateinit var customerRepository: CustomerRepository
@Autowired
private lateinit var mockMvc: MockMvc
@Test
@ -37,20 +42,44 @@ class CustomerMembershipTest {
@Test
fun testInsert() {
/* val foundCustomer: Customer? = customerRepository.findByCustomerIdAndActive(90, "Y")
val customerMembership = CustomerMembership(
membershipId = 3,
startDate = "2023-01-30",
)
customerMembership.customer = foundCustomer
/* val authToken = Tokenizer.getToken()
val foundCustomer: Customer? = customerRepository.findByCustomerIdAndActive(90, "Y")
val controller = CustomerMembershipController(customerMembershipRepository, customerRepository)
val response = controller.createNewCustomerMembership(customerMembership)
val customerMembership = CustomerMembership(
membershipId = 3,
startDate = "2023-01-30",
)
customerMembership.customer = foundCustomer
val newCustomerMembership = response.body
mockMvc.perform(
MockMvcRequestBuilders.post("/api/customer_membership")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(customerMembership))
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.jsonPath("$.membership").value(3))
.andReturn()
assertTrue(newCustomerMembership is CustomerMembership)
customerMembershipRepository.delete(newCustomerMembership) // does not delete the customer_membership
*/
val foundCustomer: Customer? = customerRepository.findByCustomerIdAndActive(90, "Y")
val customerMembership = CustomerMembership(
membershipId = 3,
startDate = "2023-01-30",
)
customerMembership.customer = foundCustomer
val controller = CustomerMembershipController(customerMembershipRepository, customerRepository)
val response = controller.createNewCustomerMembership(customerMembership)
val newCustomerMembership = response.body
assertTrue(newCustomerMembership is CustomerMembership)
customerMembershipRepository.delete(newCustomerMembership) // does not delete the customer_membership
*/
}
private fun toJson(obj: Any): String {
val str = Gson().toJson(obj)
return str
}
}

View File

@ -5,14 +5,22 @@ import com.aitrainer.api.model.Customer
import com.aitrainer.api.model.User
import com.aitrainer.api.repository.CustomerRepository
import com.aitrainer.api.service.ServiceBeans
import com.google.gson.Gson
import org.json.JSONObject
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.MvcResult
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.assertEquals
@ -21,11 +29,15 @@ import kotlin.test.assertTrue
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@AutoConfigureMockMvc
class CustomerTests {
@Autowired
private lateinit var mockMvc: MockMvc
@Autowired
private var serviceBean: ServiceBeans? = null
private var authToken: String? = ""
@BeforeAll
fun init() {
if ( serviceBean == null ) { serviceBean = ServiceBeans() }
@ -315,4 +327,35 @@ class CustomerTests {
}
@Test
fun `insert recipes successfully`() {
authToken = Tokenizer.getToken()
val customer = Customer(
name = "Kadarka",
firstname = "József",
age = 32,
dateAdd = "2023-02-09 08:00",
birthYear = 1987
)
val mvcResult: MvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(customer))
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("Kadarka"))
.andExpect(MockMvcResultMatchers.jsonPath("$.age").value(32))
.andExpect(MockMvcResultMatchers.jsonPath("$.birthYear").value(1987))
.andReturn()
val customerId = JSONObject(mvcResult.response.contentAsString).getInt("customerId")
println("MockCustomer Id $customerId")
assertTrue(customerId > 0)
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -26,7 +26,7 @@ class EmailTest {
@Test
fun testTemplate() {
val html = emailTemplateService.getEmailBody("Tibor", "https://very.link")
val html = emailTemplateService.getEmailBody("Tibor", "https://very.link", "registration_email")
print(html)
assertTrue (html.isNotEmpty())
assertTrue(html.contains("Tibor"))

View File

@ -0,0 +1,38 @@
package com.aitrainer.api.test
import com.aitrainer.api.service.Encryptor
import org.json.JSONObject
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.web.client.RestTemplate
object Tokenizer {
fun getToken(): String {
val headers = HttpHeaders().apply {
contentType = MediaType.APPLICATION_JSON
put("Authorization", arrayOf("1").toMutableList())
}
val restTemplate = RestTemplate()
val username = "bosi"
val password = Encryptor.getEncryptor().decrypt("WZplPYr8WmrLHshesY4T6oXplK3MlUVJ")
val body = JSONObject().apply {
put("username", username)
put("password", password)
}.toString()
val request = HttpEntity(body, headers)
val response = restTemplate.postForEntity("https://apitest.workouttest.org/api/authenticate", request, String::class.java)
if ( response.body == null) {
throw Exception("No authentication")
}
val jsonObject = JSONObject(response.body!!)
return jsonObject.getString("token")
}
}

View File

@ -0,0 +1,132 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.DietUserConsumption
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietConsumptionTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val dietUserConsumption = DietUserConsumption(
id = 1,
dietUserId = 5,
rawMaterialId = 10,
name = "Tükörtojás",
dateConsumption = "2023-02-08 17:00",
quantity = 120.0,
quantityUnit = "g",
cal = 70,
protein = 32.0,
fat = 11.0,
ch = 1.0,
sugar = 0.0,
)
private var dietUserConsumption2 = DietUserConsumption(
id = 2,
dietUserId = 5,
rawMaterialId = 11,
name = "Rozs zsömle",
dateConsumption = "2023-02-08 16:00",
quantity = 80.0,
quantityUnit = "g",
cal = 60,
protein = 11.0,
fat = 4.0,
ch = 34.0,
sugar = 8.0,
)
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert dietUserConsumption successfully`() {
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_consumption")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietUserConsumption))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(5))
.andExpect(jsonPath("$.rawMaterialId").value(10))
.andExpect(jsonPath("$.protein").value(32.0))
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_consumption")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietUserConsumption2))
).andExpect(status().isOk)
val dietUserConsumption3 = DietUserConsumption(
id = 2,
dietUserId = 5,
rawMaterialId = 11,
name = "Rozs zsömle",
dateConsumption = "2023-02-08 16:30",
quantity = 80.0,
quantityUnit = "g",
cal = 65, //**
protein = 11.0,
fat = 4.0,
ch = 44.0, //**
sugar = 8.0,
)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_consumption/2")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietUserConsumption3))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(5))
.andExpect(jsonPath("$.rawMaterialId").value(11))
.andExpect(jsonPath("$.sugar").value(8.0))
.andExpect(jsonPath("$.cal").value(65.0))
.andExpect(jsonPath("$.ch").value(44.0))
// Act & Assert
mockMvc.perform(get("/api/diet_user_consumption/5")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Tükörtojás"))
.andExpect(jsonPath("$.[1].name").value("Rozs zsömle"))
.andExpect(jsonPath("$.[1].ch").value(44))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,84 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.DietRawMaterial
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietRawMaterialTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val dietRawMaterial = DietRawMaterial(
dietId = 1,
rawMaterialId = 10,
name = "Tükörtojás",
kcalMin = 70,
kcalMax = 110,
proteinMin = 19,
proteinMax = 25,
fatMin = 12,
fatMax = 15,
chMin = 1,
chMax = 3,
sugar = 0,
)
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert dietRawMaterial successfully`() {
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_raw_material")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietRawMaterial))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.dietId").value(1))
.andExpect(jsonPath("$.rawMaterialId").value(10))
.andExpect(jsonPath("$.proteinMax").value(25))
}
@Test
fun `get dietRawMaterial list by dietId id successfully`() {
// Act & Assert
mockMvc.perform(get("/api/diet_raw_material/{dietId}", 1)
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Tükörtojás"))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,70 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.DietSensitivity
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietSensitivityTest {
@Autowired
lateinit var mockMvc: MockMvc
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
private val dietSensitivity = DietSensitivity(1, name = "Nut Allergy")
private val dietSensitivity2 = DietSensitivity(2, name = "Penicillin sensitivity")
@Test
fun `insert diet sensitivity successfully`() {
val gson = Gson()
mockMvc.perform(
post("/api/diet_sensitivity")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(gson.toJson(dietSensitivity))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.name").value("Nut Allergy"))
mockMvc.perform(
post("/api/diet_sensitivity")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(gson.toJson(dietSensitivity2))
)
.andExpect(status().isOk)
}
@Test
fun `get all diet sensitivity successfully`() {
mockMvc.perform(get("/api/diet_sensitivity")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Nut Allergy"))
.andExpect(jsonPath("$.[1].name").value("Penicillin sensitivity"))
}
}

View File

@ -0,0 +1,79 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.Diet
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val diet = Diet(
dietUserId = 1,
dietText = "Test diet text",
monday = "Monday meal",
tuesday = "Tuesday meal",
wednesday = "Wednesday meal",
thursday = "Thursday meal",
friday = "Friday meal",
saturday = "Saturday meal",
sunday = "Sunday meal"
)
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert diet successfully`() {
// Arrange
//given(dietRepository.save(diet)).willReturn(diet)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(diet))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(1))
}
@Test
fun `get diet list by dietUserId id successfully`() {
mockMvc.perform(get("/api/diet/{dietUserId}", 1)
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].monday").value("Monday meal"))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,93 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.DietUserPreference
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietUserPreferenceTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val dietUserPreference = DietUserPreference(
id = 1,
rawMaterialId = 10,
dietUserId = 5,
temperature = -1
)
private val dietUserPreference2 = DietUserPreference(
id = 2,
rawMaterialId = 11,
dietUserId = 5,
temperature = 1
)
private var authToken: String = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert dietUserPreference successfully`() {
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_preference")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietUserPreference))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(5))
.andExpect(jsonPath("$.rawMaterialId").value(10))
.andExpect(jsonPath("$.temperature").value(-1))
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_preference")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietUserPreference2))
)
.andExpect(status().isOk)
}
@Test
fun `get dietUserPreference list by dietUserId id successfully`() {
// Act & Assert
mockMvc.perform(get("/api/diet_user_preference/{dietUserId}", 5)
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].temperature").value(-1))
.andExpect(jsonPath("$.[0].rawMaterialId").value(10))
.andExpect(jsonPath("$.[1].temperature").value(1))
.andExpect(jsonPath("$.[1].rawMaterialId").value(11))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,69 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.DietUserSensitivity
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietUserSensitivityTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val dietUserSensitivity = DietUserSensitivity(
sensitivityId = 1,
dietUserId = 5,
)
private var authToken: String = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert dietUserSensitivity successfully`() {
mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_sensitivity")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(dietUserSensitivity))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(5))
.andExpect(jsonPath("$.sensitivityId").value(1))
// Act & Assert
mockMvc.perform(get("/api/diet_user_sensitivity/{dietUserId}", 5)
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].dietUserId").value(5))
.andExpect(jsonPath("$.[0].sensitivityId").value(1))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,55 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.DietUser
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DietUserTest {
@Autowired
lateinit var mockMvc: MockMvc
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert diet user successfully`() {
val dietUser = DietUser(1, customerId = 1)
val gson = Gson()
mockMvc.perform(
post("/api/diet_user")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(gson.toJson(dietUser))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.customerId").value(1))
mockMvc.perform(get("/api/diet_user/${dietUser.dietUserId}")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.customerId").value(1))
}
}

View File

@ -0,0 +1,139 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.RawMaterial
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RawMaterialTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val rawMaterial = RawMaterial(
name = "Tükörtojás",
kcalMin = 70,
kcalMax = 110,
proteinMin = 19,
proteinMax = 25,
fatMin = 12,
fatMax = 15,
chMin = 1,
chMax = 3,
sugar = 0,
storeId = 4,
)
private val rawMaterial2 = RawMaterial(
name = "Tigris buci",
kcalMin = 123,
kcalMax = 140,
proteinMin = 19,
proteinMax = 25,
fatMin = 30,
fatMax = 34,
chMin = 50,
chMax = 60,
sugar = 5,
storeId = 2,
)
private val rawMaterial3 = RawMaterial(
name = "Tükörtojás",
kcalMin = 75,
kcalMax = 112,
proteinMin = 12,
proteinMax = 20,
fatMin = 12,
fatMax = 15,
chMin = 1,
chMax = 3,
sugar = 0,
storeId = 2,
)
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert rawMaterials successfully`() {
mockMvc.perform(
MockMvcRequestBuilders.post("/api/raw_material")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(rawMaterial))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.sugar").value(0))
.andExpect(jsonPath("$.storeId").value(4))
.andExpect(jsonPath("$.proteinMax").value(25))
val mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/api/raw_material")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(rawMaterial2))
)
.andExpect(status().isOk)
.andReturn()
val gson= Gson()
val newRawMaterialJson = mvcResult.response.contentAsString
val newRawMaterial = gson.fromJson(newRawMaterialJson, RawMaterial::class.java)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/raw_material")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(rawMaterial3))
)
.andExpect(status().isOk)
// Act & Assert
mockMvc.perform(get("/api/raw_material" )
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Tükörtojás"))
.andExpect(jsonPath("$.[1].name").value("Tigris buci"))
.andExpect(jsonPath("$.[2].storeId").value(2))
.andExpect(jsonPath("$.[2].name").value("Tükörtojás"))
// Act & Assert
mockMvc.perform(get("/api/raw_material/${newRawMaterial.id}")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.name").value("Tigris buci"))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,180 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.model.diet.RecipeRawMaterial
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.MvcResult
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RecipeTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val recipe1 = Recipe(
name = "Bableves",
description = "Hozzávalók: ---- Elkészítés: ---",
cal = 180,
protein = 19.0,
fat = 56.0,
ch = 23.0,
dietUserId = 4,
)
private val recipe2 = Recipe(
name = "Mákos tészta",
description = "Hozzávalók: ---- Elkészítés: ---",
cal = 220,
protein = 19.0,
fat = 30.0,
ch = 78.0,
dietUserId = 4,
)
private val recipe3 = Recipe(
name = "Borsóleves",
description = "Hozzávalók: ---- Elkészítés: ---",
cal = 160,
protein = 45.0,
fat = 30.0,
ch = 34.0,
dietUserId = 1,
)
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@AfterAll
fun tearDown() {
}
@Test
fun `insert recipes successfully`() {
val mvcResult: MvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/api/recipe")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(recipe1))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.name").value("Bableves"))
.andExpect(jsonPath("$.cal").value(180))
.andExpect(jsonPath("$.dietUserId").value(4))
.andReturn()
val gson= Gson()
val newRecipeJson = mvcResult.response.contentAsString
val newRecipe = gson.fromJson(newRecipeJson, Recipe::class.java)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/recipe")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(recipe2))
)
.andExpect(status().isOk)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/recipe")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(recipe3))
)
.andExpect(status().isOk)
val recipeRawMaterial1 = RecipeRawMaterial()
with(recipeRawMaterial1) {
quantity = 30
quantityUnit = "g"
rawMaterialId = 2
recipe = recipe1
}
newRecipe.rawMaterials.add(recipeRawMaterial1)
val recipeRawMaterial2 = RecipeRawMaterial()
with (recipeRawMaterial2) {
quantity = 50
quantityUnit = "g"
rawMaterialId = 3
recipe = recipe1
}
newRecipe.rawMaterials.add(recipeRawMaterial2)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/recipe/"+newRecipe.recipeId)
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(newRecipe))
).andExpect(status().isOk)
.andExpect(jsonPath("$.cal").value(180))
.andExpect(jsonPath("$.rawMaterials.[0].quantity").value(30))
.andExpect(jsonPath("$.rawMaterials.[0].rawMaterialId").value(2))
.andExpect(jsonPath("$.rawMaterials.[1].quantity").value(50))
.andExpect(jsonPath("$.rawMaterials.[1].rawMaterialId").value(3))
// Act & Assert
mockMvc.perform(get("/api/recipe" )
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Bableves"))
.andExpect(jsonPath("$.[1].name").value("Mákos tészta"))
.andExpect(jsonPath("$.[2].dietUserId").value(1))
.andExpect(jsonPath("$.[2].name").value("Borsóleves"))
.andExpect(jsonPath("$.[0].rawMaterials.[0].quantity").value(30))
.andExpect(jsonPath("$.[0].rawMaterials.[1].quantity").value(50))
.andExpect(jsonPath("$.[0].rawMaterials.[1].rawMaterialId").value(3))
// Act & Assert
mockMvc.perform(get("/api/recipe/4")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Bableves"))
.andExpect(jsonPath("$.[1].name").value("Mákos tészta"))
.andExpect(jsonPath("$.[0].rawMaterials.[1].rawMaterialId").value(3))
// Act & Assert
mockMvc.perform(get("/api/recipe/name/leves")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Bableves"))
.andExpect(jsonPath("$.[1].name").value("Borsóleves"))
.andExpect(jsonPath("$.[0].rawMaterials.[1].rawMaterialId").value(3))
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}

View File

@ -0,0 +1,138 @@
package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.Store
import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class StoreTest {
@Autowired
private lateinit var mockMvc: MockMvc
private val store = Store(
storeId = 1,
storeName = "Tesco",
country = "HU"
)
private val store2 = Store(
storeId = 2,
storeName = "Auchan",
country = "HU"
)
private val store3 = Store(
storeId = 3,
storeName = "Aldi",
country = "HU"
)
private val store4 = Store(
storeId = 4,
storeName = "Aldi",
country = "DE"
)
private var authToken: String? = ""
@BeforeAll
fun initTest() {
authToken = Tokenizer.getToken()
}
@Test
fun `insert recipes successfully`() {
mockMvc.perform(
MockMvcRequestBuilders.post("/api/store")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(store))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.storeName").value("Tesco"))
.andExpect(jsonPath("$.country").value("HU"))
mockMvc.perform(
MockMvcRequestBuilders.post("/api/store")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(store2))
)
.andExpect(status().isOk)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/store")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(store3))
)
.andExpect(status().isOk)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/store")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken")
.content(toJson(store4))
)
.andExpect(status().isOk)
}
@Test
fun `get all stores successfully`() {
// Act & Assert
mockMvc.perform(get("/api/store" )
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].storeName").value("Tesco"))
.andExpect(jsonPath("$.[1].storeName").value("Auchan"))
.andExpect(jsonPath("$.[2].storeName").value("Aldi"))
.andExpect(jsonPath("$.[3].storeName").value("Aldi"))
.andExpect(jsonPath("$.[3].country").value("DE"))
}
@Test
fun `get store by name successfully`() {
// Act & Assert
mockMvc.perform(get("/api/store/Aldi/HU")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.storeName").value("Aldi"))
.andExpect(jsonPath("$.country").value("HU"))
}
@Test
fun `get recipe by name unsuccessfully`() {
// Act & Assert
mockMvc.perform(get("/api/store/Aldi/AT")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound)
}
private fun toJson(obj: Any): String {
return Gson().toJson(obj)
}
}