-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
โจ FEAT. ํ์ ๋ฐ์๊ตญ ์ผ์ ๊ธฐ๋ฅ ๊ตฌํ
- Loading branch information
Showing
22 changed files
with
1,017 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/main/java/com/tantanmen/carbofootprint/domain/schedule/convertor/ScheduleConvertor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.tantanmen.carbofootprint.domain.schedule.convertor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.tantanmen.carbofootprint.domain.schedule.entity.Schedule; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.FirstMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.OtherMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.SecondMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.ThirdMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.enums.FoodType; | ||
import com.tantanmen.carbofootprint.domain.schedule.web.dto.ScheduleRequestDto; | ||
import com.tantanmen.carbofootprint.domain.schedule.web.dto.ScheduleResponseDto; | ||
|
||
public class ScheduleConvertor { | ||
/** | ||
* ์์ฒญ DTO -> Schedule ๊ฐ์ฒด | ||
* ์๋จ ์ ๋ณด๋ ๋ฐ๋ก ์ฐ๊ด๊ด๊ณ ๋งคํ | ||
*/ | ||
public static Schedule toSchedule(ScheduleRequestDto.AddScheduleRequestDto request){ | ||
Schedule schedule = Schedule.builder() | ||
.totalKcal(request.getCalorie()) | ||
.exerciseDuration(request.getWorkoutTime()) | ||
.stepCount(request.getStepCount()) | ||
.firstMealList(new ArrayList<>()) | ||
.secondMealList(new ArrayList<>()) | ||
.thirdMealList(new ArrayList<>()) | ||
.otherMealList(new ArrayList<>()) | ||
.month(request.getMonth()) | ||
.day(request.getDay()) | ||
.build(); | ||
|
||
addMealList(request, schedule); | ||
|
||
return schedule; | ||
} | ||
|
||
|
||
|
||
|
||
/** | ||
* FoodType ์์ฒญ List -> ์๋จ ๋ฐ์ดํฐ๋ก ๋ณ๊ฒฝ | ||
*/ | ||
|
||
public static void addMealList(ScheduleRequestDto.AddScheduleRequestDto request, Schedule schedule){ | ||
toFirstMealList(request.getFirstMeal(), schedule); | ||
toSecondMealList(request.getSecondMeal(), schedule); | ||
toThirdMealList(request.getThirdMeal(), schedule); | ||
toOtherMealList(request.getExtraMeal(), schedule); | ||
} | ||
|
||
private static void toFirstMealList(List<FoodType> request, Schedule schedule){ | ||
request.stream().forEach(foodType -> { | ||
FirstMeal firstMeal = FirstMeal.builder() | ||
.foodType(foodType) | ||
.build(); | ||
|
||
schedule.addFirstMeal(firstMeal); | ||
}); | ||
} | ||
|
||
private static void toSecondMealList(List<FoodType> request, Schedule schedule){ | ||
request.stream().forEach(foodType -> { | ||
SecondMeal secondMeal = SecondMeal.builder() | ||
.foodType(foodType) | ||
.build(); | ||
|
||
schedule.addSecondMeal(secondMeal); | ||
}); | ||
} | ||
|
||
private static void toThirdMealList(List<FoodType> request, Schedule schedule){ | ||
request.stream().forEach(foodType -> { | ||
ThirdMeal thirdMeal = ThirdMeal.builder() | ||
.foodType(foodType) | ||
.build(); | ||
|
||
schedule.addThirdMeal(thirdMeal); | ||
}); | ||
} | ||
|
||
private static void toOtherMealList(List<FoodType> request, Schedule schedule){ | ||
request.stream().forEach(foodType -> { | ||
OtherMeal otherMeal = OtherMeal.builder() | ||
.foodType(foodType) | ||
.build(); | ||
|
||
schedule.addOtherMeal(otherMeal); | ||
}); | ||
} | ||
|
||
/** | ||
* List<Schedule> => List<DTO>๋ก ๋ณํ | ||
*/ | ||
public static ScheduleResponseDto.FindAllScheduleResponseDto toFindAllScheduleResponseDto(List<Schedule> scheduleList){ | ||
List<ScheduleResponseDto.SchedulePreviewResponseDto> august_schedule_list = new ArrayList<>(); | ||
List<ScheduleResponseDto.SchedulePreviewResponseDto> september_schedule_list = new ArrayList<>(); | ||
|
||
for (Schedule schedule : scheduleList) { | ||
ScheduleResponseDto.SchedulePreviewResponseDto result = toSchedulePreviewResponseDto( | ||
schedule); | ||
if (schedule.getMonth() == 8){ | ||
august_schedule_list.add(result); | ||
} | ||
else september_schedule_list.add(result); | ||
} | ||
|
||
return ScheduleResponseDto.FindAllScheduleResponseDto.builder() | ||
.august_schedule_list(august_schedule_list) | ||
.september_schedule_list(september_schedule_list) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Schedule => DTO๋ก ๋ณํ | ||
*/ | ||
private static ScheduleResponseDto.SchedulePreviewResponseDto toSchedulePreviewResponseDto(Schedule schedule){ | ||
return ScheduleResponseDto.SchedulePreviewResponseDto.builder() | ||
.title(schedule.getTitle()) | ||
.calorie(schedule.getTotalKcal()) | ||
.workoutTime(schedule.getExerciseDuration()) | ||
.stepCount(schedule.getStepCount()) | ||
.firstMeal(schedule.getFirstMealList().stream().map(FirstMeal::getFoodType).collect(Collectors.toList())) | ||
.secondMeal(schedule.getSecondMealList().stream().map(SecondMeal::getFoodType).collect(Collectors.toList())) | ||
.thirdMeal(schedule.getThirdMealList().stream().map(ThirdMeal::getFoodType).collect(Collectors.toList())) | ||
.extraMeal(schedule.getOtherMealList().stream().map(OtherMeal::getFoodType).collect(Collectors.toList())) | ||
.day(schedule.getDay()) | ||
.build(); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/main/java/com/tantanmen/carbofootprint/domain/schedule/entity/Schedule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.tantanmen.carbofootprint.domain.schedule.entity; | ||
|
||
import java.util.List; | ||
|
||
import com.tantanmen.carbofootprint.domain.member.entity.Member; | ||
import com.tantanmen.carbofootprint.domain.schedule.convertor.ScheduleConvertor; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.FirstMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.OtherMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.SecondMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.entity.meal.ThirdMeal; | ||
import com.tantanmen.carbofootprint.domain.schedule.web.dto.ScheduleRequestDto; | ||
import com.tantanmen.carbofootprint.global.entity.BaseEntity; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* ํ์ ๋ฐ์๊ตญ ์ผ์ Entity | ||
*/ | ||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "schedule") | ||
public class Schedule extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "title") | ||
private String title; | ||
|
||
@Column(name = "total_kcal") | ||
private Long totalKcal; | ||
|
||
@Column(name = "exercise_duration") | ||
private Long exerciseDuration; | ||
|
||
@Column(name = "step_count") | ||
private Long stepCount; | ||
|
||
@Column(name = "month") | ||
private Long month; | ||
|
||
@Column(name = "day") | ||
private Long day; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@OneToMany(mappedBy = "schedule", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<FirstMeal> firstMealList; | ||
|
||
@OneToMany(mappedBy = "schedule", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<SecondMeal> secondMealList; | ||
|
||
@OneToMany(mappedBy = "schedule", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<ThirdMeal> thirdMealList; | ||
|
||
@OneToMany(mappedBy = "schedule", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<OtherMeal> otherMealList; | ||
|
||
|
||
// ์ฐ๊ด ๊ด๊ณ ํธ์ ๋ฉ์๋ | ||
public void addFirstMeal(FirstMeal firstMeal){ | ||
firstMeal.changeSchedule(this); | ||
firstMealList.add(firstMeal); | ||
} | ||
|
||
public void addSecondMeal(SecondMeal secondMeal){ | ||
secondMeal.changeSchedule(this); | ||
secondMealList.add(secondMeal); | ||
} | ||
|
||
public void addThirdMeal(ThirdMeal thirdMeal){ | ||
thirdMeal.changeSchedule(this); | ||
thirdMealList.add(thirdMeal); | ||
} | ||
|
||
public void addOtherMeal(OtherMeal otherMeal){ | ||
otherMeal.changeSchedule(this); | ||
otherMealList.add(otherMeal); | ||
} | ||
|
||
public void changeMember(Member member){ | ||
this.member = member; | ||
} | ||
|
||
/** | ||
* Schedule ๋ด๋ถ ๋ฐ์ดํฐ ๋ณ๊ฒฝ | ||
*/ | ||
public void updateData(ScheduleRequestDto.AddScheduleRequestDto request){ | ||
this.totalKcal = request.getCalorie(); | ||
this.title = request.getTitle(); | ||
this.exerciseDuration = request.getWorkoutTime(); | ||
this.stepCount = request.getStepCount(); | ||
this.month = request.getMonth(); | ||
this.day = request.getDay(); | ||
|
||
this.firstMealList.clear(); | ||
this.secondMealList.clear(); | ||
this.thirdMealList.clear(); | ||
this.otherMealList.clear(); | ||
|
||
ScheduleConvertor.addMealList(request, this); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/tantanmen/carbofootprint/domain/schedule/entity/meal/FirstMeal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.tantanmen.carbofootprint.domain.schedule.entity.meal; | ||
|
||
import com.tantanmen.carbofootprint.domain.schedule.entity.Schedule; | ||
import com.tantanmen.carbofootprint.domain.schedule.enums.FoodType; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* ์ฒซ ๋ผ Entity | ||
*/ | ||
@Entity | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "first_meal") | ||
public class FirstMeal { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private FoodType foodType; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "schedule_id") | ||
private Schedule schedule; | ||
|
||
// ์ฐ๊ด ๊ด๊ณ ํธ์ ๋ฉ์๋ | ||
public void changeSchedule(Schedule schedule){ | ||
this.schedule = schedule; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/tantanmen/carbofootprint/domain/schedule/entity/meal/OtherMeal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.tantanmen.carbofootprint.domain.schedule.entity.meal; | ||
|
||
import com.tantanmen.carbofootprint.domain.schedule.entity.Schedule; | ||
import com.tantanmen.carbofootprint.domain.schedule.enums.FoodType; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* ๋๋จน ๋ผ Entity | ||
*/ | ||
@Entity | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "other_meal") | ||
public class OtherMeal { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private FoodType foodType; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "schedule_id") | ||
private Schedule schedule; | ||
|
||
// ์ฐ๊ด ๊ด๊ณ ํธ์ ๋ฉ์๋ | ||
public void changeSchedule(Schedule schedule){ | ||
this.schedule = schedule; | ||
} | ||
} |
Oops, something went wrong.