Skip to content

Commit

Permalink
Merge pull request #20 from 2024-tantanmen/feature/#16-carbofootprint
Browse files Browse the repository at this point in the history
โœจ FEAT. ํƒ„์ˆ˜ ๋ฐœ์ž๊ตญ ์ผ์ • ๊ธฐ๋Šฅ ๊ตฌํ˜„
  • Loading branch information
junhaa authored Jul 31, 2024
2 parents 50408fc + 0484899 commit 8ee62c4
Show file tree
Hide file tree
Showing 22 changed files with 1,017 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.tantanmen.carbofootprint.domain.community.entity.mapping.ChatMessage;
import com.tantanmen.carbofootprint.domain.community.entity.mapping.MemberChatRoom;
import com.tantanmen.carbofootprint.domain.schedule.entity.Schedule;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -51,6 +52,9 @@ public class Member {
@OneToMany(mappedBy = "sender", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ChatMessage> chatMessageList;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Schedule> scheduleList;

// ์—ฐ๊ด€ ๊ด€๊ณ„ ํŽธ์˜ ๋ฉ”์„œ๋“œ
public void addAuthority(Authority authority) {
authority.changeMember(this);
Expand All @@ -66,4 +70,9 @@ public void addChatMessage(ChatMessage chatMessage) {
chatMessage.changeSender(this);
chatMessageList.add(chatMessage);
}

public void addSchedule(Schedule schedule){
schedule.changeMember(this);
scheduleList.add(schedule);
}
}
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();
}
}
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);
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit 8ee62c4

Please sign in to comment.