diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieRoutineController.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieRoutineController.java index b44c8bc2..d019f8ae 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieRoutineController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieRoutineController.java @@ -1,13 +1,17 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.controller; import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; -import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.MyRoutineCheck; -import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRoutineReqeuest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineCheck; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineDelete; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineUpdate; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineSetting; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.MyRoutineList; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.MyVeggieRoutine; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.RoutineMonthChecking; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; -import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Routine; import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieRoutineService; +import com.modernfarmer.farmusspring.domain.myveggiegarden.util.DateManager; +import com.modernfarmer.farmusspring.domain.user.entity.User; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; import com.modernfarmer.farmusspring.global.response.SuccessCode; import lombok.RequiredArgsConstructor; @@ -16,6 +20,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.text.ParseException; +import java.util.Date; import java.util.List; @@ -27,18 +33,31 @@ public class MyVeggieRoutineController { private final MyVeggieRoutineService myVeggieRoutineService; - @PostMapping(value = "") - public BaseResponseDto settingMyVeggieRoutine( - @Validated @RequestBody SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest - ) { - return myVeggieRoutineService.settingMyVeggieRoutine(settingMyVeggieRoutineReqeuest); + @PostMapping() + public BaseResponseDto settingVeggieRoutine(@Validated @RequestBody RoutineSetting settingRoutine) { + myVeggieRoutineService.settingVeggieRoutine(settingRoutine.getMyVeggieId(), settingRoutine.getContent(), settingRoutine.getPeriod()); + log.info("루틴 추가 완료"); + return BaseResponseDto.of(SuccessCode.SUCCESS,null); + } + + @PatchMapping() + public BaseResponseDto modifyRoutine(@Validated @RequestBody RoutineUpdate routineUpdate) { + myVeggieRoutineService.modifyRoutine(routineUpdate); + log.info("루틴 수정 완료"); + return BaseResponseDto.of(SuccessCode.SUCCESS,null); + } + + @DeleteMapping + public BaseResponseDto eraseRoutine(@Validated @RequestBody RoutineDelete routineDelete) { + myVeggieRoutineService.eraseRoutine(routineDelete); + log.info("루틴 삭제 완료"); + return BaseResponseDto.of(SuccessCode.SUCCESS,null); } @PostMapping(value = "/check") - public BaseResponseDto checkMyVeggieRoutine( - @Validated @RequestBody MyRoutineCheck myRoutineCheck - ) { - myVeggieRoutineService.checkMyVeggieRoutine(myRoutineCheck.getRoutineId()); + public BaseResponseDto checkVeggieRoutine(@Validated @RequestBody RoutineCheck routineCheck) { + myVeggieRoutineService.checkVeggieRoutine(routineCheck.getRoutineId()); + log.info("루틴 체킹 완료"); return BaseResponseDto.of(SuccessCode.SUCCESS, null); } @@ -48,17 +67,39 @@ public BaseResponseDto selectMyVeggieRoutineById( ) { MyVeggie myVeggie = MyVeggie.builder().id(myVeggieId).build(); List result = myVeggieRoutineService.selectMyVeggieRoutineById(myVeggie); - return BaseResponseDto.of(SuccessCode.SUCCESS, result); } - @GetMapping(value = "/list") - public BaseResponseDto selectMyVeggieRoutine( - @AuthenticationPrincipal CustomUser user - ) { +// @GetMapping(value = "/list") +// public BaseResponseDto selectMyVeggieRoutine( +// @AuthenticationPrincipal CustomUser user +// ) { +// List result = myVeggieRoutineService.selectMyVeggieRoutine(user.getUserId()); +// log.info("채소별 리스트 조회 종료"); +// return BaseResponseDto.of(SuccessCode.SUCCESS, result); +// } - List result = myVeggieRoutineService.selectMyVeggieRoutine(user.getUserId()); - log.info("채소별 리스트 조회 종료"); - return BaseResponseDto.of(SuccessCode.SUCCESS, result); + @GetMapping("/month/{month}") + public BaseResponseDto selectRoutineCheckingAccordingToMonth( + @AuthenticationPrincipal CustomUser user, + @PathVariable("month") String month + ) throws ParseException { + User userObject = User.builder().id(user.getUserId()).build(); + Date parsingDate = DateManager.formatMonthStringToDate(month); + RoutineMonthChecking routineMonthChecking = myVeggieRoutineService.selectRoutineCheckingAccordingToMonth(userObject,parsingDate); + log.info("월 기준 날짜 체킹 상태 조회 완료"); + return BaseResponseDto.of(SuccessCode.SUCCESS, routineMonthChecking); + } + + + @GetMapping("/date/{date}") + public BaseResponseDto selectRoutineAccordingToDate( + @AuthenticationPrincipal CustomUser user, + @PathVariable("date") String date) throws ParseException { + User userObject = User.builder().id(user.getUserId()).build(); + Date parsingDate = DateManager.formatDayStringToDate(date); + List myRoutineLists = myVeggieRoutineService.selectRoutineAccordingToDate(userObject, parsingDate); + log.info("날짜별 루틴 조회 완료"); + return BaseResponseDto.of(SuccessCode.SUCCESS, myRoutineLists); } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/TestController.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/TestController.java deleted file mode 100644 index 8d7db573..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/TestController.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.myveggiegarden.controller; - -public class TestController { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/MyRoutineCheck.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineCheck.java similarity index 92% rename from src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/MyRoutineCheck.java rename to src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineCheck.java index 4e4cd0f9..f7ae4f5d 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/MyRoutineCheck.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineCheck.java @@ -10,7 +10,7 @@ @AllArgsConstructor @NoArgsConstructor -public class MyRoutineCheck { +public class RoutineCheck { @NotNull(message = "null 값을 가지면 안됩니다.") private Long routineId; diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineDelete.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineDelete.java new file mode 100644 index 00000000..bbab0e08 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineDelete.java @@ -0,0 +1,16 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request; + + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class RoutineDelete { + + @NotNull(message = "null 값을 가지면 안됩니다.") + private Long routineId; +} \ No newline at end of file diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRoutineReqeuest.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineSetting.java similarity index 91% rename from src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRoutineReqeuest.java rename to src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineSetting.java index 8eea5845..062687bb 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRoutineReqeuest.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineSetting.java @@ -9,7 +9,7 @@ @Getter @AllArgsConstructor @NoArgsConstructor -public class SettingMyVeggieRoutineReqeuest { +public class RoutineSetting { @NotNull(message = "null 값을 가지면 안됩니다.") private String content; diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineUpdate.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineUpdate.java new file mode 100644 index 00000000..ff9f6d3d --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/RoutineUpdate.java @@ -0,0 +1,21 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request; + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class RoutineUpdate { + + @NotNull(message = "null 값을 가지면 안됩니다.") + private String content; + + + private int period; + + @NotNull(message = "null 값을 가지면 안됩니다.") + private Long routineId; +} \ No newline at end of file diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyRoutineList.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyRoutineList.java index 01d622c4..d3ef231e 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyRoutineList.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyRoutineList.java @@ -1,5 +1,4 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response; - import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Routine; import lombok.AllArgsConstructor; @@ -23,18 +22,17 @@ public class MyRoutineList { private String nickname; private String veggieName; + private Long myVeggieId; private List myVeggieRoutineList; - public static MyRoutineList of(MyVeggie myVeggie, List myVeggieRoutineList){ return new MyRoutineList( myVeggie.getNickname(), myVeggie.getVeggieName(), - myVeggieRoutineList - ); + myVeggie.getId(), + myVeggieRoutineList); } - public static List processData(List myVeggieList){ return myVeggieList.stream() .map(myVeggie -> { @@ -44,33 +42,18 @@ public static List processData(List myVeggieList){ } private static List checkRoutine(MyVeggie myVeggie){ - if(myVeggie.getRoutines().isEmpty()){ return new ArrayList<>(); } return myVeggie.getRoutines().stream() .map(routine -> { boolean check; - check = signRoutineCheck(routine.getDate()); + // check = signRoutineCheck(routine.getDate()); + check = true; return new MyVeggieRoutine(check, routine.getContent(), routine.getPeriod(), routine.getId() ); - }) + }) .toList(); } - private static Boolean signRoutineCheck(Date date){ - boolean check = true; - String date1 = String.valueOf(date); - LocalDate currentDate = LocalDate.now(); - LocalTime fixedTime = LocalTime.of(0, 0, 0, 0); - LocalDateTime dateTime = LocalDateTime.of(currentDate, fixedTime); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); - String formattedDateTime = dateTime.format(formatter); - if (date1.equals(formattedDateTime)) { - check = false; - } - log.info(String.valueOf(date1)); - log.info(String.valueOf(formattedDateTime)); - return check; - } -} +} \ No newline at end of file diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyVeggieRoutine.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyVeggieRoutine.java index 7784ed5a..fb586635 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyVeggieRoutine.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/MyVeggieRoutine.java @@ -23,50 +23,25 @@ @Slf4j public class MyVeggieRoutine { - private Boolean check; + private Boolean complete; private String content; private int period; private Long routineId; - public static MyVeggieRoutine of(Routine routine, Boolean check){ + public static MyVeggieRoutine of(Routine routine){ return new MyVeggieRoutine( - check, + routine.isComplete(), routine.getContent(), routine.getPeriod(), - routine.getId() - ); + routine.getId()); } - - public static List processData(List routineList){ return routineList.stream() .map(routine -> { - boolean check; - log.info(String.valueOf(routine.getDate())); - log.info("true/false 체크"); - check = signRoutineCheck(routine.getDate()); - return MyVeggieRoutine.of(routine, check); + return MyVeggieRoutine.of(routine); }) .toList(); } - - - private static Boolean signRoutineCheck(Date date){ - boolean check = true; - String date1 = String.valueOf(date); - LocalDate currentDate = LocalDate.now(); - LocalTime fixedTime = LocalTime.of(0, 0, 0, 0); - LocalDateTime dateTime = LocalDateTime.of(currentDate, fixedTime); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); - String formattedDateTime = dateTime.format(formatter); - if (date1.equals(formattedDateTime)) { - check = false; - } - log.info(String.valueOf(date1)); - log.info(String.valueOf(formattedDateTime)); - return check; - } - } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/RoutineMonthChecking.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/RoutineMonthChecking.java new file mode 100644 index 00000000..7fb135dc --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/RoutineMonthChecking.java @@ -0,0 +1,27 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.util.Date; +import java.util.List; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Slf4j +public class RoutineMonthChecking { + + private List date; + + public static RoutineMonthChecking of(List date){ + return new RoutineMonthChecking(date); + } + + + + +} \ No newline at end of file diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/Routine.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/Routine.java index ad80c230..9dc03173 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/Routine.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/Routine.java @@ -26,7 +26,8 @@ public class Routine extends BaseEntity { @Column(name = "routine_id") private Long id; - @Column(name = "date") + @Temporal(TemporalType.DATE) + @Column(name = "date", columnDefinition = "DATE") private Date date; @Column(name = "content") @@ -35,8 +36,8 @@ public class Routine extends BaseEntity { @Column(name = "period") private int period; - @Column(name = "notify") - private boolean notify; + @Column(name = "complete") + private boolean complete; @ManyToOne(fetch = FetchType.LAZY) @@ -44,13 +45,13 @@ public class Routine extends BaseEntity { private MyVeggie myVeggie; - public static Routine createRoutine(Date date, String content, int period, MyVeggie myVeggie, boolean notify){ + public static Routine createRoutine(Date date, String content, int period, MyVeggie myVeggie, boolean complete){ Routine newRoutine = Routine.builder() .date(date) .content(content) .period(period) .myVeggie(myVeggie) - .notify(notify) + .complete(complete) .build(); myVeggie.addRoutine(newRoutine); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java index f5fffef0..8170a089 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java @@ -2,6 +2,7 @@ import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.MyVeggieGardenBaseException; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.RoutineNotFoundException; import com.modernfarmer.farmusspring.domain.test.exception.TestException; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; import jakarta.servlet.http.HttpServletRequest; @@ -13,8 +14,8 @@ @RestControllerAdvice(basePackages = "com.modernfarmer.farmusspring.domain.myveggiegarden.controller") public class MyVeggieGardenExceptionHandler { - @ExceptionHandler(RoutineNotFountException.class) - public BaseResponseDto handleRoutineNotFoundException(RoutineNotFountException e, HttpServletRequest request) { + @ExceptionHandler(RoutineNotFoundException.class) + public BaseResponseDto handleRoutineNotFoundException(RoutineNotFoundException e, HttpServletRequest request) { log.error("RoutineException : {} {} errMessage={}\n", request.getMethod(), request.getRequestURI(), diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/RoutineNotFountException.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/RoutineNotFountException.java deleted file mode 100644 index 977af058..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/RoutineNotFountException.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.modernfarmer.farmusspring.domain.myveggiegarden.exception; - -public class RoutineNotFountException extends RuntimeException { - - - public RoutineNotFountException(String message) { - super(message); - } - -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/RoutineNotFoundException.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/RoutineNotFoundException.java new file mode 100644 index 00000000..7795cf59 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/RoutineNotFoundException.java @@ -0,0 +1,12 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenErrorCode; + +public class RoutineNotFoundException extends MyVeggieGardenBaseException { + + + public RoutineNotFoundException(String message, MyVeggieGardenErrorCode errorCode) { + super(message, errorCode); + } + +} \ No newline at end of file diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java index 14dbc1e9..2384b01b 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java @@ -73,4 +73,10 @@ void updateMyVeggie(@Param("myVeggieId") Long myVeggieId, @Modifying @Query("UPDATE routine AS r SET r.date = :date WHERE r.id = :routineId") void updateRoutinePeriod(@Param("routineId") Long routineId, @Param("date") Date date); + + @Query("SELECT mv FROM my_veggie AS mv " + + "LEFT JOIN mv.routines AS r " + + "WHERE mv.user= :user " + ) + List findMyVeggieAndRoutineByUserWithDate(@Param("user") User user); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/RoutineRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/RoutineRepository.java new file mode 100644 index 00000000..7a875755 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/RoutineRepository.java @@ -0,0 +1,42 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.repository; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Routine; +import com.modernfarmer.farmusspring.domain.user.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.Date; +import java.util.List; +import java.util.Optional; + +@Repository +public interface RoutineRepository extends JpaRepository { + + @Query("SELECT r FROM routine AS r WHERE r.id = :routineId ") + Optional findRoutineById(@Param("routineId") Long routineId); + + + @Modifying + @Query("UPDATE routine AS r SET r.content = :content, r.period = :period WHERE r.id = :routineId") + void updateRoutine(@Param("routineId") Long routineId, @Param("content") String content, @Param("period") int period); + + @Modifying + @Query("DELETE FROM routine AS r WHERE r.id = :routineId") + void deleteRoutine(@Param("routineId") Long routineId); + + + @Modifying + @Query("UPDATE routine AS r SET r.complete = true WHERE r.id = :routineId") + void updateRoutineComplete(@Param("routine") Routine routine, @Param("routineId") Long routineId); + + @Query("SELECT r FROM routine AS r " + + "JOIN FETCH r.myVeggie AS mv " + + "WHERE mv.user = :user AND " + + "FUNCTION('YEAR', r.date) = FUNCTION('YEAR', :month) AND FUNCTION('MONTH', r.date) = FUNCTION('MONTH', :month)") + List findRoutineAndRoutineAndMyVeggieByMonthWithUser(@Param("month") Date month, @Param("user") User user ); + +} \ No newline at end of file diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/TestRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/TestRepository.java deleted file mode 100644 index 9e46b684..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/TestRepository.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.myveggiegarden.repository; - -public class TestRepository { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java index a1bc13a4..e34426c9 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java @@ -36,7 +36,6 @@ public class MyVeggieDiaryService { private final MyVeggieRepository myVeggieRepository; private final UserService userService; private final DiaryRepository diaryRepository; - private final DateManager dateManager; @Transactional public BaseResponseDto settingMyVeggieDiary( @@ -84,7 +83,7 @@ private List proccessFarmClubData(List diaryList){ return FarmClubDiary.of( diary, user, - dateManager.dotDateTime(diary.getCreatedDate()), + DateManager.dotDateTime(diary.getCreatedDate()), diary.getDiaryComments().size(), diary.getDiaryLikes().size() );}).toList(); @@ -153,7 +152,7 @@ public BaseResponseDto selectDiaryOne( SelectDiaryOneResponse.of( diaryList.get(0).getImage(), diaryList.get(0).getContent(), - dateManager.formatDate(diaryList.get(0).getCreatedDate()) + DateManager.formatDate(diaryList.get(0).getCreatedDate()) )); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieGardenService.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieGardenService.java index ebb5fdca..1fee0f02 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieGardenService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieGardenService.java @@ -27,9 +27,7 @@ public class MyVeggieGardenService { private final MyVeggieRepository myVeggieRepository; - private final DateManager dateManager; private final MyDetailMyVeggieDto myDetailMyVeggieDto; - private final VeggieInfoHelper veggieInfoHelper; @@ -63,8 +61,8 @@ public BaseResponseDto selectMyVeggieProfile(Long myVeggie.getNickname(), myVeggie.getVeggieName(), myVeggie.getVeggieImage(), - dateManager.parsingDotDate(myVeggie.getBirth()), - dateManager.calculateDay(myVeggie.getBirth(), new Date()), + DateManager.parsingDotDate(myVeggie.getBirth()), + DateManager.calculateDay(myVeggie.getBirth(), new Date()), checkFarmClubAffiliation(myVeggie) )); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieRoutineService.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieRoutineService.java index 7d18fe35..16eb71d0 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieRoutineService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieRoutineService.java @@ -1,26 +1,25 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.service; -import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRoutineReqeuest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineDelete; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineUpdate; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.RoutineSetting; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.MyRoutineList; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.MyVeggieRoutine; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.RoutineMonthChecking; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Routine; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenErrorCode; -import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.RoutineNotFountException; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.RoutineNotFoundException; import com.modernfarmer.farmusspring.domain.myveggiegarden.repository.MyVeggieRepository; +import com.modernfarmer.farmusspring.domain.myveggiegarden.repository.RoutineRepository; import com.modernfarmer.farmusspring.domain.myveggiegarden.util.DateManager; -import com.modernfarmer.farmusspring.global.response.BaseResponseDto; -import com.modernfarmer.farmusspring.global.response.SuccessCode; +import com.modernfarmer.farmusspring.domain.user.entity.User; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Optional; +import java.util.*; @Slf4j @AllArgsConstructor @@ -30,21 +29,28 @@ public class MyVeggieRoutineService { private final MyVeggieGardenService myVeggieGardenService; private final MyVeggieRepository myVeggieRepository; private final DateManager dateManager; + private final RoutineRepository routineRepository; + @Transactional + public void settingVeggieRoutine(Long veggieId, String content, int period) { + addVeggieRoutine(veggieId, content, period, new Date()); + } @Transactional - public BaseResponseDto settingMyVeggieRoutine( - SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest - ) { - addMyyVeggieRoutine(settingMyVeggieRoutineReqeuest); - return BaseResponseDto.of(SuccessCode.SUCCESS,null); + public void modifyRoutine(RoutineUpdate routineUpdate) { + verifyRoutine(routineRepository.findRoutineById(routineUpdate.getRoutineId())); + routineRepository.updateRoutine(routineUpdate.getRoutineId(), routineUpdate.getContent(), routineUpdate.getPeriod()); } + @Transactional + public void eraseRoutine(RoutineDelete routineDelete) { + verifyRoutine(routineRepository.findRoutineById(routineDelete.getRoutineId())); + routineRepository.deleteRoutine(routineDelete.getRoutineId()); + } @Transactional public List selectMyVeggieRoutineById(MyVeggie myVeggie) { - List routineList = myVeggieRepository.findMyVeggieRoutineById(myVeggie); return MyVeggieRoutine.processData(routineList); } @@ -57,35 +63,57 @@ public List selectMyVeggieRoutine(Long userId) { } @Transactional - public void checkMyVeggieRoutine(Long routineId) { - Optional routine = selectRoutineByRoutineId(routineId); - Date addedDate = dateManager.addDate(routine.get().getDate(), routine.get().getPeriod()); - myVeggieRepository.updateRoutinePeriod(routineId, addedDate); + public RoutineMonthChecking selectRoutineCheckingAccordingToMonth(User user, Date month) { + List routineTimeList = routineRepository.findRoutineAndRoutineAndMyVeggieByMonthWithUser(month, user); + return RoutineMonthChecking.of(extractDate(routineTimeList)); } + @Transactional + public List selectRoutineAccordingToDate(User user, Date day) { + List myVeggieList = myVeggieRepository.findMyVeggieAndRoutineByUserWithDate(user); + return mappingMyVeggieListData(myVeggieList, day); + } + public List mappingMyVeggieListData(List myVeggieList, Date day){ + return myVeggieList.stream() + .map(myVeggie -> MyRoutineList.of(myVeggie, mappingRoutineListData(myVeggie, day))) + .toList(); + } + private List mappingRoutineListData(MyVeggie myVeggie, Date day) { + return myVeggie.getRoutines().stream() + .filter(routine -> Objects.equals(DateManager.formatDayDateToString(day), routine.getDate().toString())) + .map(MyVeggieRoutine::of) + .toList(); + } + @Transactional + public void checkVeggieRoutine(Long routineId) { + Optional routine = routineRepository.findRoutineById(routineId); + verifyRoutine(routine); + Date addedDate = DateManager.addDate(routine.get().getDate(), routine.get().getPeriod()); + routineRepository.updateRoutineComplete(routine.get(), routineId); + addVeggieRoutine(routine.get().getMyVeggie().getId(), routine.get().getContent(), routine.get().getPeriod(), addedDate); + } - - - public Optional selectRoutineByRoutineId(Long routineId){ - return Optional.ofNullable(myVeggieRepository.findRoutineById(routineId) - .orElseThrow(() -> new RoutineNotFountException("해당 루틴이 존재하지 않습니다."))); + public void verifyRoutine(Optional routine){ + if(routine.isEmpty()) + throw new RoutineNotFoundException("존재하지 않는 루틴입니다.", MyVeggieGardenErrorCode.NOT_FOUND_ROUTINE); } - private void addMyyVeggieRoutine(SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest){ - MyVeggie myVeggie = myVeggieGardenService.getMyVeggie(settingMyVeggieRoutineReqeuest.getMyVeggieId()); - Routine newRoutine = Routine.createRoutine( - new Date(), - settingMyVeggieRoutineReqeuest.getContent(), - settingMyVeggieRoutineReqeuest.getPeriod(), - myVeggie, - settingMyVeggieRoutineReqeuest.isNotify() - ); + private void addVeggieRoutine(Long veggieId, String content, int period, Date date){ + MyVeggie myVeggie = myVeggieGardenService.getMyVeggie(veggieId); + Routine newRoutine = Routine.createRoutine(date,content,period, myVeggie, false); myVeggie.addRoutine(newRoutine); } + public List extractDate(List routineList){ + return routineList.stream() + .map(Routine::getDate) + .distinct() + .toList(); + } + } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/TestService.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/TestService.java deleted file mode 100644 index 7cd1761a..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/TestService.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.myveggiegarden.service; - -public class TestService { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/util/DateManager.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/util/DateManager.java index 38f78cbe..3e16d5cb 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/util/DateManager.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/util/DateManager.java @@ -2,6 +2,7 @@ import org.springframework.stereotype.Component; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -12,7 +13,7 @@ @Component public class DateManager { - public String parsingDotDate(Date date){ + public static String parsingDotDate(Date date){ SimpleDateFormat format = new SimpleDateFormat("yy.MM.dd"); return format.format(date); } @@ -23,11 +24,11 @@ public static String parsingDotDateTime(LocalDateTime date){ return date.format(formatter); } - public String dotDateTime(LocalDateTime date){ + public static String dotDateTime(LocalDateTime date){ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy.MM.dd HH:mm"); return date.format(formatter); } - public int calculateDay(Date startDate, Date endDate) { + public static int calculateDay(Date startDate, Date endDate) { long differenceMillis = endDate.getTime() - startDate.getTime(); long differenceDays = differenceMillis / (1000 * 60 * 60 * 24); return (int) differenceDays; @@ -41,11 +42,26 @@ public static String formatDate(LocalDateTime date) { } - public Date addDate(Date date, int addDate){ + public static Date addDate(Date date, int addDate){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, addDate); // 3일을 더함 return cal.getTime(); } + public static Date formatMonthStringToDate(String month) throws ParseException { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM"); + return dateFormat.parse(month); + } + + public static Date formatDayStringToDate(String day) throws ParseException { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + return dateFormat.parse(day); + } + + public static String formatDayDateToString(Date day) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + return dateFormat.format(day); + } + } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/util/DateManager.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/util/DateManager.java index 20f6b8a6..f2f047f4 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/util/DateManager.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/util/DateManager.java @@ -25,7 +25,7 @@ public static String parsingDotDateTime(LocalDateTime date){ return date.format(formatter); } - public String dotDateTime(LocalDateTime date){ + public static String dotDateTime(LocalDateTime date){ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy.MM.dd HH:mm"); return date.format(formatter); }