Skip to content

Commit 0eabe7e

Browse files
authored
Merge pull request #640 from JNU-econovation/feat/#639
[BE/FEAT] 식사 등록 단위에 따른 식사 등록 및 분석 로직 개선
2 parents 254ce70 + 338fdec commit 0eabe7e

File tree

12 files changed

+116
-239
lines changed

12 files changed

+116
-239
lines changed

resources/gaebaljip-develop-environment/mariadb-init/01_schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ CREATE TABLE `MEAL_FOOD_TB`
7373
`MEAL_FK` bigint(20) DEFAULT NULL,
7474
`MEAL_FOOD_MULTIPLE` double DEFAULT NULL,
7575
`MEAL_FOOD_G` int DEFAULT NULL,
76+
`MEAL_FOOD_UNIT_TYPE` varchar(255),
7677
PRIMARY KEY (`MEAL_FOOD_PK`),
7778
FOREIGN KEY (`FOOD_FK`) REFERENCES `FOOD_TB` (`FOOD_PK`),
7879
FOREIGN KEY (`MEAL_FK`)REFERENCES `MEAL_TB` (`MEAL_PK`)

resources/gaebaljip-local-environment/mariadb-init/01_schema.sql

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/main/java/com/gaebaljip/exceed/application/domain/meal/GStrategy.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/com/gaebaljip/exceed/application/domain/meal/MealFoodEntity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static List<MealFoodEntity> createMealFoods(
5454
i ->
5555
MealFoodEntity.builder()
5656
.unit(
57-
new Unit(
57+
Unit.createUnit(
5858
eatMealFoodDTOS.get(i).g(),
5959
eatMealFoodDTOS.get(i).multiple()))
6060
.foodEntity(foodEntities.get(i))
@@ -66,22 +66,22 @@ public static List<MealFoodEntity> createMealFoods(
6666

6767
public double getAdjustedCalorie() {
6868
return this.unit
69-
.getStrategy()
69+
.getUnitType()
7070
.measure(this.foodEntity.getCalorie(), unit, this.foodEntity.getServingSize());
7171
}
7272

7373
public double getAdjustedCarbohydrate() {
74-
return unit.getStrategy()
74+
return unit.getUnitType()
7575
.measure(this.foodEntity.getCarbohydrate(), unit, this.foodEntity.getServingSize());
7676
}
7777

7878
public double getAdjustedProtein() {
79-
return unit.getStrategy()
79+
return unit.getUnitType()
8080
.measure(this.foodEntity.getProtein(), unit, this.foodEntity.getServingSize());
8181
}
8282

8383
public double getAdjustedFat() {
84-
return unit.getStrategy()
84+
return unit.getUnitType()
8585
.measure(this.foodEntity.getFat(), unit, this.foodEntity.getServingSize());
8686
}
8787

src/main/java/com/gaebaljip/exceed/application/domain/meal/MeasureStrategy.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/com/gaebaljip/exceed/application/domain/meal/MultipleStrategy.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/com/gaebaljip/exceed/application/domain/meal/Unit.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
import javax.persistence.Column;
88
import javax.persistence.Embeddable;
9+
import javax.persistence.EnumType;
10+
import javax.persistence.Enumerated;
11+
12+
import com.gaebaljip.exceed.common.exception.meal.InvalidMultipleAndGException;
913

1014
import lombok.AllArgsConstructor;
1115
import lombok.Getter;
@@ -22,12 +26,18 @@ public class Unit {
2226
@Column(name = ENTITY_PREFIX + "_MULTIPLE")
2327
private Double multiple;
2428

25-
protected MeasureStrategy getStrategy() {
26-
if (this.getG() == null) {
27-
return new MultipleStrategy();
28-
} else {
29-
return new GStrategy();
29+
@Enumerated(EnumType.STRING)
30+
@Column(name = ENTITY_PREFIX + "_UNIT_TYPE")
31+
private UnitType unitType;
32+
33+
public static Unit createUnit(Integer g, Double multiple) {
34+
if (Objects.nonNull(g)) {
35+
return new Unit(g, multiple, UnitType.G);
36+
}
37+
if (Objects.nonNull(multiple)) {
38+
return new Unit(g, multiple, UnitType.MULTIPLE);
3039
}
40+
throw InvalidMultipleAndGException.EXCEPTION;
3141
}
3242

3343
@Override
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gaebaljip.exceed.application.domain.meal;
2+
3+
public enum UnitType {
4+
G {
5+
@Override
6+
public double measure(double nutrients, Unit unit, double servingSize) {
7+
return (unit.getG() / servingSize) * nutrients;
8+
}
9+
},
10+
MULTIPLE {
11+
@Override
12+
public double measure(double nutrients, Unit unit, double servingSize) {
13+
return nutrients * unit.getMultiple();
14+
}
15+
};
16+
17+
abstract double measure(double nutrients, Unit unit, double servingSize);
18+
}

src/main/resources/application-local.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ spring:
1313
naming:
1414
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
1515
dialect: org.hibernate.dialect.MariaDBDialect
16-
ddl-auto: validate
16+
ddl-auto: create
1717
properties:
1818
hibernate:
1919
show_sql : true

0 commit comments

Comments
 (0)