API 1.2+2 open ai fixes

This commit is contained in:
Tibor Bossanyi 2023-03-09 23:23:14 +01:00
parent e3107147a1
commit cae214f87e
31 changed files with 269 additions and 421 deletions

View File

@ -44,26 +44,6 @@ COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB ENGINE=InnoDB
; ;
CREATE TABLE `diet_raw_material` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`diet_meal_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` ( CREATE TABLE `diet_sensitivity` (
`id` INT(11) NOT NULL AUTO_INCREMENT, `id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(100) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_general_ci', `name` CHAR(100) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_general_ci',
@ -116,22 +96,19 @@ COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB ENGINE=InnoDB
; ;
CREATE TABLE `meal` (
CREATE TABLE `raw_material` (
`id` INT(11) NOT NULL AUTO_INCREMENT, `id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(100) NOT NULL COLLATE 'utf8mb4_general_ci', `name` CHAR(100) NOT NULL COLLATE 'utf8mb4_general_ci',
`description` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci', `description` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
`kcal_min` INT(11) NULL DEFAULT '0', `cal_min` DOUBLE NULL DEFAULT NULL,
`kcal_max` INT(11) NULL DEFAULT '0', `cal_max` DOUBLE NULL DEFAULT NULL,
`protein_min` INT(11) NULL DEFAULT '0', `protein_min` DOUBLE NULL DEFAULT NULL,
`protein_max` INT(11) NULL DEFAULT '0', `protein_max` DOUBLE NULL DEFAULT NULL,
`ch_min` INT(11) NULL DEFAULT '0', `ch_min` DOUBLE NULL DEFAULT NULL,
`ch_max` INT(11) NULL DEFAULT '0', `ch_max` DOUBLE NULL DEFAULT NULL,
`fat_min` INT(11) NULL DEFAULT '0', `fat_min` DOUBLE NULL DEFAULT NULL,
`fat_max` INT(11) NULL DEFAULT '0', `fat_max` DOUBLE NULL DEFAULT NULL,
`sugar` INT(11) NULL DEFAULT '0', `sugar` DOUBLE NULL DEFAULT NULL,
`store_id` INT(11) NULL DEFAULT '0',
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) )
COLLATE='utf8mb4_general_ci' COLLATE='utf8mb4_general_ci'

View File

@ -24,7 +24,6 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
private val customerTrainingPlanRepository: CustomerTrainingPlanRepository, private val customerTrainingPlanRepository: CustomerTrainingPlanRepository,
private val customerMembership: CustomerMembershipRepository, private val customerMembership: CustomerMembershipRepository,
private val dietRepository: DietRepository, private val dietRepository: DietRepository,
private val dietRawMaterialRepository: DietRawMaterialRepository,
private val dietUserConsumptionRepository: DietUserConsumptionRepository, private val dietUserConsumptionRepository: DietUserConsumptionRepository,
private val dietUserRepository: DietUserRepository, private val dietUserRepository: DietUserRepository,
private val dietUserPreferenceRepository: DietUserPreferenceRepository, private val dietUserPreferenceRepository: DietUserPreferenceRepository,
@ -57,9 +56,6 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
val listDiet = dietRepository.findByDietUserId(dietUserId) val listDiet = dietRepository.findByDietUserId(dietUserId)
val listDietJson = gson.toJson(listDiet) val listDietJson = gson.toJson(listDiet)
val listDietRawMaterial = dietRawMaterialRepository.findByDietMealId(dietUserId)
val listDietRawMaterialJson = gson.toJson(listDietRawMaterial)
val listDietUserConsumption = dietUserConsumptionRepository.findByDietUserId(dietUserId) val listDietUserConsumption = dietUserConsumptionRepository.findByDietUserId(dietUserId)
val listDietUserConsumptionJson = gson.toJson(listDietUserConsumption) val listDietUserConsumptionJson = gson.toJson(listDietUserConsumption)
@ -76,7 +72,6 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
getClassRecord(Customer::class.simpleName, customerJson) + getClassRecord(Customer::class.simpleName, customerJson) +
"|||" + getClassRecord(DietUser::class.simpleName, dietUserJson) + "|||" + getClassRecord(DietUser::class.simpleName, dietUserJson) +
"|||" + getClassRecord(Diet::class.simpleName, listDietJson) + "|||" + getClassRecord(Diet::class.simpleName, listDietJson) +
"|||" + getClassRecord(DietRawMaterial::class.simpleName, listDietRawMaterialJson) +
"|||" + getClassRecord(DietUserConsumption::class.simpleName, listDietUserConsumptionJson) + "|||" + getClassRecord(DietUserConsumption::class.simpleName, listDietUserConsumptionJson) +
"|||" + getClassRecord(DietUserPreference::class.simpleName, listDietUserPreferenceJson) + "|||" + getClassRecord(DietUserPreference::class.simpleName, listDietUserPreferenceJson) +
"|||" + getClassRecord(DietUserSensitivity::class.simpleName, listDietUserSensitivityJson) + "|||" + getClassRecord(DietUserSensitivity::class.simpleName, listDietUserSensitivityJson) +

View File

@ -2,7 +2,7 @@ package com.aitrainer.api.controller
import com.aitrainer.api.model.* import com.aitrainer.api.model.*
import com.aitrainer.api.model.diet.DietSensitivity import com.aitrainer.api.model.diet.DietSensitivity
import com.aitrainer.api.model.diet.RawMaterial import com.aitrainer.api.model.diet.Meal
import com.aitrainer.api.model.diet.Recipe import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.model.diet.Store import com.aitrainer.api.model.diet.Store
import com.aitrainer.api.repository.* import com.aitrainer.api.repository.*
@ -37,7 +37,7 @@ class PackageController(private val exerciseAbilityRepository: ExerciseAbilityRe
private val membershipRepository: MembershipRepository, private val membershipRepository: MembershipRepository,
private val storeRepository: StoreRepository, private val storeRepository: StoreRepository,
private val recipeRepository: RecipeRepository, private val recipeRepository: RecipeRepository,
private val rawMaterialRepository: RawMaterialRepository, private val mealRepository: MealRepository,
private val dietSensitivityRepository: DietSensitivityRepository private val dietSensitivityRepository: DietSensitivityRepository
) { ) {
@ -60,7 +60,7 @@ class PackageController(private val exerciseAbilityRepository: ExerciseAbilityRe
val listRecipe = recipeRepository.findAll() val listRecipe = recipeRepository.findAll()
val listRecipeJson = gson.toJson(listRecipe) val listRecipeJson = gson.toJson(listRecipe)
val listRawMaterial = rawMaterialRepository.findAll() val listRawMaterial = mealRepository.findAll()
val listRawMaterialJson = gson.toJson(listRawMaterial) val listRawMaterialJson = gson.toJson(listRawMaterial)
val listDietSensitivity = dietSensitivityRepository.findAll() val listDietSensitivity = dietSensitivityRepository.findAll()
@ -71,7 +71,7 @@ class PackageController(private val exerciseAbilityRepository: ExerciseAbilityRe
"|||" + getClassRecord(Membership::class.simpleName, listMembershipJson) + "|||" + getClassRecord(Membership::class.simpleName, listMembershipJson) +
"|||" + getClassRecord(Store::class.simpleName, listStoreJson) + "|||" + getClassRecord(Store::class.simpleName, listStoreJson) +
"|||" + getClassRecord(Recipe::class.simpleName, listRecipeJson) + "|||" + getClassRecord(Recipe::class.simpleName, listRecipeJson) +
"|||" + getClassRecord(RawMaterial::class.simpleName, listRawMaterialJson) + "|||" + getClassRecord(Meal::class.simpleName, listRawMaterialJson) +
"|||" + getClassRecord(DietSensitivity::class.simpleName, listDietSensitivityJson) "|||" + getClassRecord(DietSensitivity::class.simpleName, listDietSensitivityJson)
return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else

View File

@ -1,24 +0,0 @@
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/{dietMealId}")
fun getByDietUserId(@PathVariable dietMealId: Long): ResponseEntity<List<DietRawMaterial>> {
val list = dietRawMaterialRepository.findByDietMealId(dietMealId)
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
}

View File

@ -19,7 +19,7 @@ class DietUserConsumptionController(private val dietUserConsumptionRepository: D
fun update(@PathVariable(value = "id") id: Long, @RequestBody dietUserConsumption: DietUserConsumption): ResponseEntity<*> { fun update(@PathVariable(value = "id") id: Long, @RequestBody dietUserConsumption: DietUserConsumption): ResponseEntity<*> {
return dietUserConsumptionRepository.findById(id).map { existingConsumption -> return dietUserConsumptionRepository.findById(id).map { existingConsumption ->
val updatedConsumption: DietUserConsumption = existingConsumption.copy( val updatedConsumption: DietUserConsumption = existingConsumption.copy(
rawMaterialId = dietUserConsumption.rawMaterialId, mealId = dietUserConsumption.mealId,
dateConsumption = dietUserConsumption.dateConsumption, dateConsumption = dietUserConsumption.dateConsumption,
name = dietUserConsumption.name, name = dietUserConsumption.name,
quantity = dietUserConsumption.quantity, quantity = dietUserConsumption.quantity,

View File

@ -0,0 +1,39 @@
package com.aitrainer.api.controller.diet
import com.aitrainer.api.model.diet.Meal
import com.aitrainer.api.repository.diet.MealRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("api")
class MealController(private val mealRepository: MealRepository) {
@PostMapping ("/meal")
fun insert(@RequestBody meal: Meal): ResponseEntity<Meal> {
val newMeal = mealRepository.save(meal)
return ResponseEntity.ok().body(newMeal)
}
@GetMapping("/meal")
fun getAll(): ResponseEntity<List<Meal>> {
val list = mealRepository.findAll()
return if (list.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
@GetMapping("/meal/{id}")
fun getById(@PathVariable id: Long): ResponseEntity<Meal> {
val rawMaterial = mealRepository.findById(id)
return if (rawMaterial == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(rawMaterial)
}
@GetMapping("/meal/by_name/{name}")
fun getByName(@PathVariable name: String): ResponseEntity<Meal> {
val meal = mealRepository.findByName(name)
return if (meal == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(meal)
}
}

View File

@ -1,31 +0,0 @@
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

@ -27,11 +27,10 @@ class RecipeController(private val recipeRepository: RecipeRepository) {
fat = recipe.fat, fat = recipe.fat,
protein = recipe.protein, protein = recipe.protein,
dietUserId = recipe.dietUserId, dietUserId = recipe.dietUserId,
mealId = recipe.mealId
) )
recipe.rawMaterials.forEach { recipe.meals.forEach {
it.recipe = recipe it.recipe = recipe
updatedRecipe.rawMaterials.add(it) updatedRecipe.meals.add(it)
} }
return ResponseEntity.ok().body(recipeRepository.save(updatedRecipe)) return ResponseEntity.ok().body(recipeRepository.save(updatedRecipe))
} }
@ -50,13 +49,6 @@ class RecipeController(private val recipeRepository: RecipeRepository) {
ResponseEntity.ok().body(list) ResponseEntity.ok().body(list)
} }
@GetMapping("/recipe/meal/{mealId}")
fun getByMealId(@PathVariable mealId: Long): ResponseEntity<List<Recipe>> {
val list = recipeRepository.findByMealId(mealId)
return if (list == null) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(list)
}
@GetMapping("/recipe/name/{name}") @GetMapping("/recipe/name/{name}")
fun getByName(@PathVariable name: String): ResponseEntity<List<Recipe>> { fun getByName(@PathVariable name: String): ResponseEntity<List<Recipe>> {
val list = recipeRepository.findByName(name) val list = recipeRepository.findByName(name)

View File

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

View File

@ -1,17 +0,0 @@
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

@ -8,9 +8,11 @@ import org.jetbrains.annotations.NotNull
@Entity @Entity
data class DietMeal ( data class DietMeal (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0, @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
//@Expose @get: NotNull val dietId: Long = 0, @Expose @get: NotNull val mealId: Long = 0,
@Expose @get: NotNull val mealName: String = "", @Expose @get: NotNull val mealName: String = "",
@Expose @get: NotNull val meal: String = "", @Expose @get: NotNull val meal: String = "",
@Expose @get: NotNull val quantity: Double = 0.0,
@Expose @get: NotNull val quantityUnit: String = "",
) { ) {
@ManyToOne(fetch = FetchType.EAGER, optional = false) @ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "dietId", nullable = false) @JoinColumn(name = "dietId", nullable = false)

View File

@ -1,22 +0,0 @@
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 dietMealId: 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

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

View File

@ -0,0 +1,23 @@
package com.aitrainer.api.model.diet
import com.google.gson.annotations.Expose
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
@Entity
data class Meal (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
@Expose @get: NotNull val name: String = "",
@Expose @get: NotNull val quantity: Double = 0.0,
@Expose @get: NotNull val quantityUnit: String = "",
@Expose @get: NotNull val description: String = "",
@Expose @get: NotNull val calMin: Double = 0.0,
@Expose @get: NotNull val calMax: Double = 0.0,
@Expose @get: NotNull val proteinMin: Double = 0.0,
@Expose @get: NotNull val proteinMax: Double = 0.0,
@Expose @get: NotNull val fatMin: Double = 0.0,
@Expose @get: NotNull val fatMax: Double = 0.0,
@Expose @get: NotNull val chMin: Double = 0.0,
@Expose @get: NotNull val chMax: Double = 0.0,
@Expose @get: NotNull val sugar: Double = 0.0,
)

View File

@ -1,22 +0,0 @@
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

@ -19,9 +19,8 @@ data class Recipe (
@Expose @get: NotNull val fat: Double = 0.0, @Expose @get: NotNull val fat: Double = 0.0,
@Expose @get: NotNull val ch: Double = 0.0, @Expose @get: NotNull val ch: Double = 0.0,
@Expose @get: NotNull val dietUserId: Long = 0, @Expose @get: NotNull val dietUserId: Long = 0,
@Expose @get: NotNull val mealId: Long = 0,
) { ) {
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "recipe") @OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "recipe")
@Fetch(value = FetchMode.SUBSELECT) @Fetch(value = FetchMode.SUBSELECT)
@Expose val rawMaterials: MutableList<RecipeRawMaterial> = mutableListOf() @Expose val meals: MutableList<RecipeMeal> = mutableListOf()
} }

View File

@ -6,12 +6,11 @@ import jakarta.persistence.*
import org.jetbrains.annotations.NotNull import org.jetbrains.annotations.NotNull
@Entity @Entity
data class RecipeRawMaterial ( data class RecipeMeal (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0, @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose val id: Long = 0,
@Expose @get: NotNull var rawMaterialId: Int = 0, @Expose @get: NotNull var mealId: Int = 0,
@Expose @get: NotNull var quantity: Int = 0, @Expose @get: NotNull var quantity: Int = 0,
@Expose @get: NotNull var quantityUnit: String = "",
) { ) {
@ManyToOne(fetch = FetchType.EAGER, optional = false) @ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "recipeId", nullable = false) @JoinColumn(name = "recipeId", nullable = false)

View File

@ -1,10 +0,0 @@
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 findByDietMealId(dietMealId: Long): List<DietRawMaterial>
}

View File

@ -0,0 +1,16 @@
package com.aitrainer.api.repository.diet
import com.aitrainer.api.model.diet.Meal
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
interface MealRepository : JpaRepository<Meal, Int> {
@Query(" FROM Meal WHERE id = :id")
fun findById(id: Long): Meal?
@Query(" FROM Meal WHERE name = :name")
fun findByName(name: String): Meal?
@Query(" FROM Meal WHERE name = :name AND quantity = :quantity")
fun findByNameAndQuantity(name: String, quantity: Double): Meal?
}

View File

@ -1,12 +0,0 @@
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.RecipeMeal
import org.springframework.data.jpa.repository.JpaRepository
interface RecipeMealRepository : JpaRepository<RecipeMeal, Int> {
fun findByMealId(mealId: Long): List<RecipeMeal>?
}

View File

@ -1,9 +0,0 @@
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

@ -13,6 +13,4 @@ interface RecipeRepository : JpaRepository<Recipe, Int> {
@Query(" FROM Recipe WHERE recipeId = :recipeId") @Query(" FROM Recipe WHERE recipeId = :recipeId")
fun findByRecipeId(recipeId: Long): Recipe? fun findByRecipeId(recipeId: Long): Recipe?
@Query(" FROM Recipe WHERE mealId = :mealId")
fun findByMealId(mealId: Long): List<Recipe>?
} }

View File

@ -50,9 +50,6 @@ class AppCustomerPackageTest {
@Autowired @Autowired
private lateinit var dietRepository: DietRepository private lateinit var dietRepository: DietRepository
@Autowired
private lateinit var dietRawMaterialRepository: DietRawMaterialRepository
@Autowired @Autowired
private lateinit var dietUserConsumptionRepository: DietUserConsumptionRepository private lateinit var dietUserConsumptionRepository: DietUserConsumptionRepository
@ -83,7 +80,6 @@ class AppCustomerPackageTest {
customerTrainingPlanRepository, customerTrainingPlanRepository,
customerMembershipRepository, customerMembershipRepository,
dietRepository, dietRepository,
dietRawMaterialRepository,
dietUserConsumptionRepository, dietUserConsumptionRepository,
dietUserRepository, dietUserRepository,
dietUserPreferenceRepository, dietUserPreferenceRepository,
@ -109,14 +105,8 @@ class AppCustomerPackageTest {
val list: List<Diet> = gson.fromJson(dietJson, type) val list: List<Diet> = gson.fromJson(dietJson, type)
assertEquals(list[0].dietText, "Test diet text") assertEquals(list[0].dietText, "Test diet text")
assertEquals(list[0].meals[0].mealName, "monday|breakfast") assertEquals(list[0].meals[0].mealName, "monday|breakfast")
} assertEquals(list[0].meals[0].quantity, 2.0)
assertEquals(list[0].meals[2].quantityUnit, "tányér")
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 -> { record[0] == DietUserConsumption::class.simpleName -> {
@ -139,7 +129,7 @@ class AppCustomerPackageTest {
val dietUserPreferenceJson: String = record[1] val dietUserPreferenceJson: String = record[1]
val type = object : TypeToken<List<DietUserPreference?>?>() {}.type val type = object : TypeToken<List<DietUserPreference?>?>() {}.type
val list: List<DietUserPreference> = gson.fromJson(dietUserPreferenceJson, type) val list: List<DietUserPreference> = gson.fromJson(dietUserPreferenceJson, type)
assertEquals(list[0].dietUserId, 5) assertEquals(list[0].dietUserId, 2)
assertEquals(list[0].temperature, -1) assertEquals(list[0].temperature, -1)
} }
@ -147,7 +137,7 @@ class AppCustomerPackageTest {
val dietUserSensitivityJson: String = record[1] val dietUserSensitivityJson: String = record[1]
val type = object : TypeToken<List<DietUserSensitivity?>?>() {}.type val type = object : TypeToken<List<DietUserSensitivity?>?>() {}.type
val list: List<DietUserSensitivity> = gson.fromJson(dietUserSensitivityJson, type) val list: List<DietUserSensitivity> = gson.fromJson(dietUserSensitivityJson, type)
assertEquals(list[0].dietUserId, 5) assertEquals(list[0].dietUserId, 2)
assertEquals(list[0].sensitivityId, 1) assertEquals(list[0].sensitivityId, 1)
} }
} }
@ -169,7 +159,6 @@ class AppCustomerPackageTest {
customerTrainingPlanRepository, customerTrainingPlanRepository,
customerMembershipRepository, customerMembershipRepository,
dietRepository, dietRepository,
dietRawMaterialRepository,
dietUserConsumptionRepository, dietUserConsumptionRepository,
dietUserRepository, dietUserRepository,
dietUserPreferenceRepository, dietUserPreferenceRepository,
@ -225,7 +214,6 @@ class AppCustomerPackageTest {
exerciseResultRepository, customerActivityRepository, customerTrainingPlanRepository, exerciseResultRepository, customerActivityRepository, customerTrainingPlanRepository,
customerMembershipRepository, customerMembershipRepository,
dietRepository, dietRepository,
dietRawMaterialRepository,
dietUserConsumptionRepository, dietUserConsumptionRepository,
dietUserRepository, dietUserRepository,
dietUserPreferenceRepository, dietUserPreferenceRepository,

View File

@ -3,7 +3,7 @@ package com.aitrainer.api.test
import com.aitrainer.api.controller.PackageController import com.aitrainer.api.controller.PackageController
import com.aitrainer.api.model.* import com.aitrainer.api.model.*
import com.aitrainer.api.model.diet.DietSensitivity import com.aitrainer.api.model.diet.DietSensitivity
import com.aitrainer.api.model.diet.RawMaterial import com.aitrainer.api.model.diet.Meal
import com.aitrainer.api.model.diet.Recipe import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.model.diet.Store import com.aitrainer.api.model.diet.Store
import com.aitrainer.api.repository.* import com.aitrainer.api.repository.*
@ -65,7 +65,7 @@ class AppPackageTest {
private lateinit var dietSensitivityRepository: DietSensitivityRepository private lateinit var dietSensitivityRepository: DietSensitivityRepository
@Autowired @Autowired
private lateinit var rawMaterialRepository: RawMaterialRepository private lateinit var mealRepository: MealRepository
@Autowired @Autowired
private lateinit var recipeRepository: RecipeRepository private lateinit var recipeRepository: RecipeRepository
@ -100,7 +100,7 @@ class AppPackageTest {
membershipRepository, membershipRepository,
storeRepository, storeRepository,
recipeRepository, recipeRepository,
rawMaterialRepository, mealRepository,
dietSensitivityRepository, dietSensitivityRepository,
) )
val response: ResponseEntity<*> = controller.getDietPackageData() val response: ResponseEntity<*> = controller.getDietPackageData()
@ -139,16 +139,15 @@ class AppPackageTest {
assertEquals(list[2].name, "Borsóleves") assertEquals(list[2].name, "Borsóleves")
assertEquals(list[2].protein, 45.0) assertEquals(list[2].protein, 45.0)
assertEquals(list[2].dietUserId, 1) assertEquals(list[2].dietUserId, 1)
} else if ( record[0] == RawMaterial::class.simpleName) { } else if ( record[0] == Meal::class.simpleName) {
val rawMaterialJson: String = record[1] val mealJson: String = record[1]
val type = object : TypeToken<List<RawMaterial?>?>() {}.type val type = object : TypeToken<List<Meal?>?>() {}.type
val list: List<RawMaterial> = gson.fromJson(rawMaterialJson, type) val list: List<Meal> = gson.fromJson(mealJson, type)
assertTrue(list.isNotEmpty()) assertTrue(list.isNotEmpty())
assertEquals(list[0].name, "Tükörtojás") assertEquals(list[0].name, "Tükörtojás1")
assertEquals(list[0].kcalMin, 70) assertEquals(list[0].calMin, 70.0)
assertEquals(list[0].proteinMax, 25) assertEquals(list[0].proteinMax, 25.0)
assertEquals(list[0].storeId, 4) assertEquals(list[1].name, "Tigris buci1")
assertEquals(list[1].name, "Tigris buci")
} else if ( record[0] == DietSensitivity::class.simpleName) { } else if ( record[0] == DietSensitivity::class.simpleName) {
val dietSensitivityJson: String = record[1] val dietSensitivityJson: String = record[1]
val type = object : TypeToken<List<DietSensitivity?>?>() {}.type val type = object : TypeToken<List<DietSensitivity?>?>() {}.type
@ -186,7 +185,7 @@ class AppPackageTest {
membershipRepository, membershipRepository,
storeRepository, storeRepository,
recipeRepository, recipeRepository,
rawMaterialRepository, mealRepository,
dietSensitivityRepository, dietSensitivityRepository,
) )
val response: ResponseEntity<*> = controller.getClubPackageData() val response: ResponseEntity<*> = controller.getClubPackageData()
@ -246,7 +245,7 @@ class AppPackageTest {
membershipRepository, membershipRepository,
storeRepository, storeRepository,
recipeRepository, recipeRepository,
rawMaterialRepository, mealRepository,
dietSensitivityRepository, dietSensitivityRepository,
) )
val response: ResponseEntity<*> = controller.getPackageData() val response: ResponseEntity<*> = controller.getPackageData()

View File

@ -28,14 +28,14 @@ class DietConsumptionTest {
private lateinit var mockMvc: MockMvc private lateinit var mockMvc: MockMvc
private val dietUserConsumption = DietUserConsumption( private val dietUserConsumption = DietUserConsumption(
id = 1, id = 3,
dietUserId = 5, dietUserId = 5,
rawMaterialId = 10, mealId = 10,
name = "Tükörtojás", name = "Tükörtojás",
dateConsumption = "2023-02-08 17:00", dateConsumption = "2023-02-08 17:00",
quantity = 120.0, quantity = 120.0,
quantityUnit = "g", quantityUnit = "g",
cal = 70, cal = 70.0,
protein = 32.0, protein = 32.0,
fat = 11.0, fat = 11.0,
ch = 1.0, ch = 1.0,
@ -43,14 +43,14 @@ class DietConsumptionTest {
) )
private var dietUserConsumption2 = DietUserConsumption( private var dietUserConsumption2 = DietUserConsumption(
id = 2, id = 4,
dietUserId = 5, dietUserId = 5,
rawMaterialId = 11, mealId = 11,
name = "Rozs zsömle", name = "Rozs zsömle",
dateConsumption = "2023-02-08 16:00", dateConsumption = "2023-02-08 16:00",
quantity = 80.0, quantity = 80.0,
quantityUnit = "g", quantityUnit = "g",
cal = 60, cal = 60.0,
protein = 11.0, protein = 11.0,
fat = 4.0, fat = 4.0,
ch = 34.0, ch = 34.0,
@ -76,7 +76,7 @@ class DietConsumptionTest {
) )
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(5)) .andExpect(jsonPath("$.dietUserId").value(5))
.andExpect(jsonPath("$.rawMaterialId").value(10)) .andExpect(jsonPath("$.mealId").value(10))
.andExpect(jsonPath("$.protein").value(32.0)) .andExpect(jsonPath("$.protein").value(32.0))
mockMvc.perform( mockMvc.perform(
@ -88,14 +88,14 @@ class DietConsumptionTest {
val dietUserConsumption3 = DietUserConsumption( val dietUserConsumption3 = DietUserConsumption(
id = 2, id = 4,
dietUserId = 5, dietUserId = 5,
rawMaterialId = 11, mealId = 11,
name = "Rozs zsömle", name = "Rozs zsömle",
dateConsumption = "2023-02-08 16:30", dateConsumption = "2023-02-08 16:30",
quantity = 80.0, quantity = 80.0,
quantityUnit = "g", quantityUnit = "g",
cal = 65, //** cal = 65.0, //**
protein = 11.0, protein = 11.0,
fat = 4.0, fat = 4.0,
ch = 44.0, //** ch = 44.0, //**
@ -103,14 +103,14 @@ class DietConsumptionTest {
) )
mockMvc.perform( mockMvc.perform(
MockMvcRequestBuilders.post("/api/diet_user_consumption/2") MockMvcRequestBuilders.post("/api/diet_user_consumption/4")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.content(toJson(dietUserConsumption3)) .content(toJson(dietUserConsumption3))
) )
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(5)) .andExpect(jsonPath("$.dietUserId").value(5))
.andExpect(jsonPath("$.rawMaterialId").value(11)) .andExpect(jsonPath("$.mealId").value(11))
.andExpect(jsonPath("$.sugar").value(8.0)) .andExpect(jsonPath("$.sugar").value(8.0))
.andExpect(jsonPath("$.cal").value(65.0)) .andExpect(jsonPath("$.cal").value(65.0))
.andExpect(jsonPath("$.ch").value(44.0)) .andExpect(jsonPath("$.ch").value(44.0))

View File

@ -1,80 +0,0 @@
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(
dietMealId = 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("$.dietMealId").value(1))
.andExpect(jsonPath("$.rawMaterialId").value(10))
.andExpect(jsonPath("$.proteinMax").value(25))
// Act & Assert
mockMvc.perform(get("/api/diet_raw_material/{dietMealId}", 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

@ -33,6 +33,7 @@ class DietTest {
private val diet = Diet( private val diet = Diet(
dietUserId = 1, dietUserId = 1,
dietText = "Test diet text", dietText = "Test diet text",
startDate = "2023-02-02"
) )
private var authToken: String? = "" private var authToken: String? = ""
@ -65,18 +66,26 @@ class DietTest {
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.[0].dietText").value("Test diet text")) .andExpect(jsonPath("$.[0].dietText").value("Test diet text"))
.andExpect(jsonPath("$.[0].startDate").value("2023-02-02"))
val meal = DietMeal( val meal = DietMeal(
mealName = "monday|breakfast", mealName = "monday|breakfast",
meal = "Főtt tojás" meal = "Főtt tojás",
quantity = 2.0,
mealId = 0
) )
val meal2 = DietMeal( val meal2 = DietMeal(
mealName = "monday|lunch", mealName = "monday|lunch",
meal = "Disznópörkölt" meal = "Disznópörkölt",
quantity = 120.0,
mealId = 0
) )
val meal3 = DietMeal( val meal3 = DietMeal(
mealName = "monday|lunch", mealName = "monday|lunch",
meal = "Savanyúkáposzta" meal = "Savanyúkáposzta",
quantity = 2.0,
quantityUnit = "tányér",
mealId = 3
) )
newDiet.meals.add(meal) newDiet.meals.add(meal)
@ -93,7 +102,12 @@ class DietTest {
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.dietUserId").value(1)) .andExpect(jsonPath("$.dietUserId").value(1))
.andExpect(jsonPath("$.meals[0].meal").value("Főtt tojás")) .andExpect(jsonPath("$.meals[0].meal").value("Főtt tojás"))
.andExpect(jsonPath("$.meals[0].quantity").value(2.0))
.andExpect(jsonPath("$.meals[1].mealName").value("monday|lunch")) .andExpect(jsonPath("$.meals[1].mealName").value("monday|lunch"))
.andExpect(jsonPath("$.meals[2].meal").value("Savanyúkáposzta"))
.andExpect(jsonPath("$.meals[2].quantity").value(2.0))
.andExpect(jsonPath("$.meals[2].mealId").value(3))
.andExpect(jsonPath("$.meals[2].quantityUnit").value("tányér"))

View File

@ -1,6 +1,7 @@
package com.aitrainer.api.test.diet package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.RawMaterial import com.aitrainer.api.model.diet.Meal
import com.aitrainer.api.repository.diet.MealRepository
import com.aitrainer.api.test.Tokenizer import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson import com.google.gson.Gson
import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeAll
@ -22,52 +23,59 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RawMaterialTest { class MealTest {
@Autowired @Autowired
private lateinit var mockMvc: MockMvc private lateinit var mockMvc: MockMvc
private val rawMaterial = RawMaterial( @Autowired
private lateinit var mealRepository: MealRepository
private val meal = Meal(
name = "Tükörtojás", name = "Tükörtojás",
kcalMin = 70, quantity = 90.0,
kcalMax = 110, quantityUnit = "g",
proteinMin = 19, calMin = 70.0,
proteinMax = 25, calMax = 110.0,
fatMin = 12, proteinMin = 19.0,
fatMax = 15, proteinMax = 25.0,
chMin = 1, fatMin = 12.0,
chMax = 3, fatMax = 15.0,
sugar = 0, chMin = 1.0,
storeId = 4, chMax = 3.0,
sugar = 0.0,
) )
private val rawMaterial2 = RawMaterial( private val meal2 = Meal(
name = "Tigris buci", name = "Tigris buci",
kcalMin = 123, quantity = 3.0,
kcalMax = 140, quantityUnit = "db",
proteinMin = 19, calMin = 123.0,
proteinMax = 25, calMax = 140.0,
fatMin = 30, proteinMin = 19.0,
fatMax = 34, proteinMax = 25.0,
chMin = 50, fatMin = 30.0,
chMax = 60, fatMax = 34.0,
sugar = 5, chMin = 50.0,
storeId = 2, chMax = 60.0,
sugar = 5.0,
) )
private val rawMaterial3 = RawMaterial( private val meal3 = Meal(
name = "Töltötttojás",
quantity = 330.0,
calMin = 75.0,
calMax = 112.0,
proteinMin = 12.0,
proteinMax = 20.0,
fatMin = 12.0,
fatMax = 15.0,
chMin = 1.0,
chMax = 3.0,
sugar = 0.0,
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? = "" private var authToken: String? = ""
@ -79,58 +87,68 @@ class RawMaterialTest {
@Test @Test
fun `insert rawMaterials successfully`() { fun `insert meals successfully`() {
mockMvc.perform( mockMvc.perform(
MockMvcRequestBuilders.post("/api/raw_material") MockMvcRequestBuilders.post("/api/meal")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.content(toJson(rawMaterial)) .content(toJson(meal))
) )
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.sugar").value(0)) .andExpect(jsonPath("$.sugar").value(0.0))
.andExpect(jsonPath("$.storeId").value(4)) .andExpect(jsonPath("$.quantity").value(90.0))
.andExpect(jsonPath("$.proteinMax").value(25)) .andExpect(jsonPath("$.proteinMax").value(25.0))
val mvcResult = mockMvc.perform( val mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/api/raw_material") MockMvcRequestBuilders.post("/api/meal")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.content(toJson(rawMaterial2)) .content(toJson(meal2))
) )
.andExpect(status().isOk) .andExpect(status().isOk)
.andReturn() .andReturn()
val gson= Gson() val gson= Gson()
val newRawMaterialJson = mvcResult.response.contentAsString val newRawMaterialJson = mvcResult.response.contentAsString
val newRawMaterial = gson.fromJson(newRawMaterialJson, RawMaterial::class.java) val newMeal = gson.fromJson(newRawMaterialJson, Meal::class.java)
mockMvc.perform( mockMvc.perform(
MockMvcRequestBuilders.post("/api/raw_material") MockMvcRequestBuilders.post("/api/meal")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.content(toJson(rawMaterial3)) .content(toJson(meal3))
) )
.andExpect(status().isOk) .andExpect(status().isOk)
// Act & Assert // Act & Assert
mockMvc.perform(get("/api/raw_material" ) mockMvc.perform(get("/api/meal" )
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Tükörtojás")) .andExpect(jsonPath("$.[3].name").value("Tükörtojás"))
.andExpect(jsonPath("$.[1].name").value("Tigris buci")) .andExpect(jsonPath("$.[4].name").value("Tigris buci"))
.andExpect(jsonPath("$.[2].storeId").value(2)) .andExpect(jsonPath("$.[5].quantity").value(330.0))
.andExpect(jsonPath("$.[2].name").value("Tükörtojás")) .andExpect(jsonPath("$.[5].name").value("Töltötttojás"))
// Act & Assert // Act & Assert
mockMvc.perform(get("/api/raw_material/${newRawMaterial.id}") mockMvc.perform(get("/api/meal/${newMeal.id}")
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.name").value("Tigris buci")) .andExpect(jsonPath("$.name").value("Tigris buci"))
mockMvc.perform(get("/api/meal/by_name/Töltötttojás")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.name").value("Töltötttojás"))
.andExpect(jsonPath("$.quantity").value(330.0))
mealRepository.delete(meal)
mealRepository.delete(meal2)
mealRepository.delete(meal3)
} }
private fun toJson(obj: Any): String { private fun toJson(obj: Any): String {

View File

@ -1,7 +1,7 @@
package com.aitrainer.api.test.diet package com.aitrainer.api.test.diet
import com.aitrainer.api.model.diet.Recipe import com.aitrainer.api.model.diet.Recipe
import com.aitrainer.api.model.diet.RecipeRawMaterial import com.aitrainer.api.model.diet.RecipeMeal
import com.aitrainer.api.test.Tokenizer import com.aitrainer.api.test.Tokenizer
import com.google.gson.Gson import com.google.gson.Gson
import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.AfterAll
@ -38,7 +38,6 @@ class RecipeTest {
fat = 56.0, fat = 56.0,
ch = 23.0, ch = 23.0,
dietUserId = 4, dietUserId = 4,
mealId = 0
) )
private val recipe2 = Recipe( private val recipe2 = Recipe(
name = "Mákos tészta", name = "Mákos tészta",
@ -58,7 +57,6 @@ class RecipeTest {
fat = 30.0, fat = 30.0,
ch = 34.0, ch = 34.0,
dietUserId = 1, dietUserId = 1,
mealId = 1
) )
private var authToken: String? = "" private var authToken: String? = ""
@ -111,23 +109,21 @@ class RecipeTest {
.andExpect(status().isOk) .andExpect(status().isOk)
val recipeRawMaterial1 = RecipeRawMaterial() val recipeMeal1 = RecipeMeal()
with(recipeRawMaterial1) { with(recipeMeal1) {
quantity = 30 quantity = 30
quantityUnit = "g" mealId = 2
rawMaterialId = 2
recipe = recipe1 recipe = recipe1
} }
newRecipe.rawMaterials.add(recipeRawMaterial1) newRecipe.meals.add(recipeMeal1)
val recipeRawMaterial2 = RecipeRawMaterial() val recipeMeal2 = RecipeMeal()
with (recipeRawMaterial2) { with (recipeMeal2) {
quantity = 50 quantity = 50
quantityUnit = "g" mealId = 3
rawMaterialId = 3
recipe = recipe1 recipe = recipe1
} }
newRecipe.rawMaterials.add(recipeRawMaterial2) newRecipe.meals.add(recipeMeal2)
mockMvc.perform( mockMvc.perform(
MockMvcRequestBuilders.post("/api/recipe/"+newRecipe.recipeId) MockMvcRequestBuilders.post("/api/recipe/"+newRecipe.recipeId)
@ -136,12 +132,12 @@ class RecipeTest {
.content(toJson(newRecipe)) .content(toJson(newRecipe))
).andExpect(status().isOk) ).andExpect(status().isOk)
.andExpect(jsonPath("$.cal").value(180)) .andExpect(jsonPath("$.cal").value(180))
.andExpect(jsonPath("$.rawMaterials.[0].quantity").value(30)) .andExpect(jsonPath("$.meals.[0].quantity").value(30))
.andExpect(jsonPath("$.rawMaterials.[0].rawMaterialId").value(2)) .andExpect(jsonPath("$.meals.[0].mealId").value(2))
.andExpect(jsonPath("$.rawMaterials.[1].quantity").value(50)) .andExpect(jsonPath("$.meals.[1].quantity").value(50))
.andExpect(jsonPath("$.rawMaterials.[1].rawMaterialId").value(3)) .andExpect(jsonPath("$.meals.[1].mealId").value(3))
// Act & Assert
mockMvc.perform(get("/api/recipe" ) mockMvc.perform(get("/api/recipe" )
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
@ -150,19 +146,19 @@ class RecipeTest {
.andExpect(jsonPath("$.[1].name").value("Mákos tészta")) .andExpect(jsonPath("$.[1].name").value("Mákos tészta"))
.andExpect(jsonPath("$.[2].dietUserId").value(1)) .andExpect(jsonPath("$.[2].dietUserId").value(1))
.andExpect(jsonPath("$.[2].name").value("Borsóleves")) .andExpect(jsonPath("$.[2].name").value("Borsóleves"))
.andExpect(jsonPath("$.[0].rawMaterials.[0].quantity").value(30)) .andExpect(jsonPath("$.[0].meals.[0].quantity").value(30))
.andExpect(jsonPath("$.[0].rawMaterials.[1].quantity").value(50)) .andExpect(jsonPath("$.[0].meals.[1].quantity").value(50))
.andExpect(jsonPath("$.[0].rawMaterials.[1].rawMaterialId").value(3)) .andExpect(jsonPath("$.[0].meals.[1].mealId").value(3))
// Act & Assert // GET by User ID
mockMvc.perform(get("/api/recipe/user/4") mockMvc.perform(get("/api/recipe/user/4")
.header("Authorization", "Bearer $authToken") .header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Bableves")) .andExpect(jsonPath("$.[0].name").value("Bableves"))
.andExpect(jsonPath("$.[1].name").value("Mákos tészta")) .andExpect(jsonPath("$.[1].name").value("Mákos tészta"))
.andExpect(jsonPath("$.[0].rawMaterials.[1].rawMaterialId").value(3)) .andExpect(jsonPath("$.[0].meals.[1].mealId").value(3))
mockMvc.perform(get("/api/recipe/name/leves") mockMvc.perform(get("/api/recipe/name/leves")
@ -171,14 +167,7 @@ class RecipeTest {
.andExpect(status().isOk) .andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Bableves")) .andExpect(jsonPath("$.[0].name").value("Bableves"))
.andExpect(jsonPath("$.[1].name").value("Borsóleves")) .andExpect(jsonPath("$.[1].name").value("Borsóleves"))
.andExpect(jsonPath("$.[0].rawMaterials.[1].rawMaterialId").value(3)) .andExpect(jsonPath("$.[0].meals.[1].mealId").value(3))
// GET by User ID
mockMvc.perform(get("/api/recipe/meal/1")
.header("Authorization", "Bearer $authToken")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk)
.andExpect(jsonPath("$.[0].name").value("Borsóleves"))
} }

View File

@ -34,7 +34,7 @@ class OpenAITest {
} }
@Test /* @Test
fun `get a question successfully`() { fun `get a question successfully`() {
val question = "Who the f. is Alice?" val question = "Who the f. is Alice?"
@ -47,7 +47,7 @@ class OpenAITest {
.andExpect(MockMvcResultMatchers.status().isOk) .andExpect(MockMvcResultMatchers.status().isOk)
//.andExpect(MockMvcResultMatchers.content().string("Sorry I dont know")) //.andExpect(MockMvcResultMatchers.content().string("Sorry I dont know"))
} }*/
@Test @Test
fun `get a question successfully with model name`() { fun `get a question successfully with model name`() {