diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java new file mode 100644 index 00000000..73bcb02b --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java @@ -0,0 +1,58 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.controller; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.CheckTodayDiaryResponse; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectDiaryOneResponse; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectMyVeggieListResponse; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieDiaryService; +import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieGardenService; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; + +@Slf4j +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/my-veggie/diary") +public class MyVeggieDiaryController { + + private final MyVeggieDiaryService myVeggieDiaryService; + + @PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE) + public BaseResponseDto settingMyVeggieDiary( + @RequestPart(value = "file", required = false) MultipartFile multipartFile, + @RequestParam("content") String content, + @RequestParam("isOpen") boolean isOpen, + @RequestParam("state") String state, + @RequestParam("myVeggieId") Long myVeggieId + ) throws IOException { + return myVeggieDiaryService.settingMyVeggieDiary( + multipartFile, + content, + isOpen, + state, + myVeggieId + ); + } + + @GetMapping(value = "/{myVeggieId}/check") + public BaseResponseDto checkTodayDiary( + @PathVariable("myVeggieId") Long myVeggieId + ) { + return myVeggieDiaryService.checkTodayDiary(MyVeggie.builder().id(myVeggieId).build()); + } + + @GetMapping(value = "/{myVeggieId}/one") + public BaseResponseDto selectDiaryOne( + @PathVariable("myVeggieId") Long myVeggieId + ) { + return myVeggieDiaryService.selectDiaryOne(MyVeggie.builder().id(myVeggieId).build()); + } + + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieGardenController.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieGardenController.java new file mode 100644 index 00000000..e5660dd5 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieGardenController.java @@ -0,0 +1,44 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.controller; + +import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRequest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieGardenService; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + + +@Slf4j +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/my-veggie") +public class MyVeggieGardenController { + + private final MyVeggieGardenService myVeggieGardenService; + + @PostMapping(value = "/") + public BaseResponseDto settingMyVeggie( + @AuthenticationPrincipal CustomUser user, + @Validated @RequestBody SettingMyVeggieRequest settingMyVeggi + ){ + return myVeggieGardenService.settingMyVeggie( + user.getUserId(), + settingMyVeggi + ); + } + + @GetMapping(value = "/simple-list") + public BaseResponseDto selectMyVeggieList( + @AuthenticationPrincipal CustomUser user + ){ + return myVeggieGardenService.selectMyVeggieList(user.getUserId()); + } + + + + +} 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 new file mode 100644 index 00000000..998b65bf --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieRoutineController.java @@ -0,0 +1,28 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.controller; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRoutineReqeuest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieRoutineService; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + + +@Slf4j +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/my-veggie/routine") +public class MyVeggieRoutineController { + + private final MyVeggieRoutineService myVeggieRoutineService; + + @PostMapping(value = "") + public BaseResponseDto settingMyVeggieRoutine( + @Validated @RequestBody SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest + ) { + + return myVeggieRoutineService.settingMyVeggieRoutine(settingMyVeggieRoutineReqeuest); + + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRequest.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRequest.java new file mode 100644 index 00000000..0d5b042d --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRequest.java @@ -0,0 +1,24 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request; + + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class SettingMyVeggieRequest { + + @NotNull(message = "null 값을 가지면 안됩니다.") + private String nickname; + + @NotNull(message = "null 값을 가지면 안됩니다.") + private Date birh; + + @NotNull(message = "null 값을 가지면 안됩니다.") + private Long veggiInfoId; +} 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/SettingMyVeggieRoutineReqeuest.java new file mode 100644 index 00000000..8eea5845 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SettingMyVeggieRoutineReqeuest.java @@ -0,0 +1,24 @@ +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 SettingMyVeggieRoutineReqeuest { + + @NotNull(message = "null 값을 가지면 안됩니다.") + private String content; + + private boolean notify; + + private int period; + + @NotNull(message = "null 값을 가지면 안됩니다.") + private Long myVeggieId; + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/CheckTodayDiaryResponse.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/CheckTodayDiaryResponse.java new file mode 100644 index 00000000..824a0f91 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/CheckTodayDiaryResponse.java @@ -0,0 +1,14 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor(staticName = "of") +@Getter +public class CheckTodayDiaryResponse { + + private boolean state; +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectDiaryOneResponse.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectDiaryOneResponse.java new file mode 100644 index 00000000..76e09a28 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectDiaryOneResponse.java @@ -0,0 +1,17 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@NoArgsConstructor +@AllArgsConstructor(staticName = "of") +@Getter +public class SelectDiaryOneResponse { + + private String image; + private String content; + private String date; +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectMyVeggieListDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectMyVeggieListDto.java new file mode 100644 index 00000000..0d7a40ac --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectMyVeggieListDto.java @@ -0,0 +1,15 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +public class SelectMyVeggieListDto { + + public Long myVeggieId; + public String nickname; +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectMyVeggieListResponse.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectMyVeggieListResponse.java new file mode 100644 index 00000000..22912cb0 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/SelectMyVeggieListResponse.java @@ -0,0 +1,18 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@NoArgsConstructor +@AllArgsConstructor(staticName = "of") +@Getter + +public class SelectMyVeggieListResponse { + + List simpleMyVeggieList; + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/MyVeggie.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/MyVeggie.java index fe3a33bc..309dba88 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/MyVeggie.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/entity/MyVeggie.java @@ -33,17 +33,17 @@ public class MyVeggie extends BaseEntity { private Date birth; @Column(name = "veggie_info_id") - private String veggieInfoId; + private Long veggieInfoId; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; - @OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @Builder.Default private List routines = new ArrayList<>(); - @OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @Builder.Default private List diaries = new ArrayList<>(); @@ -51,7 +51,7 @@ public class MyVeggie extends BaseEntity { @JoinColumn(name = "user_farm_club_id") private UserFarmClub userFarmClub; - public static MyVeggie createMyVegetable(String nickname, Date birth, String veggieInfoId, User user){ + public static MyVeggie createMyVegetable(String nickname, Date birth, Long veggieInfoId, User user){ MyVeggie newMyVeggie = MyVeggie.builder() .nickname(nickname) .birth(birth) 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 9fafe378..ad80c230 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 @@ -35,17 +35,22 @@ public class Routine extends BaseEntity { @Column(name = "period") private int period; + @Column(name = "notify") + private boolean notify; + + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "my_veggie_id") private MyVeggie myVeggie; - public static Routine createRoutine(Date date, String content, int period, MyVeggie myVeggie){ + public static Routine createRoutine(Date date, String content, int period, MyVeggie myVeggie, boolean notify){ Routine newRoutine = Routine.builder() .date(date) .content(content) .period(period) .myVeggie(myVeggie) + .notify(notify) .build(); myVeggie.addRoutine(newRoutine); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java new file mode 100644 index 00000000..15cc9ec0 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java @@ -0,0 +1,15 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.exception; + +import com.modernfarmer.farmusspring.global.response.ResponseCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum MyVeggieGardenErrorCode implements ResponseCode { + + ; + + private final int code; + private final String message; +} 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 new file mode 100644 index 00000000..4c9ea37f --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java @@ -0,0 +1,15 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.exception; + +import com.modernfarmer.farmusspring.domain.test.exception.TestException; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice(basePackages = "com.modernfarmer.farmusspring.domain.test") +public class MyVeggieGardenExceptionHandler { + + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenSuccessCode.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenSuccessCode.java new file mode 100644 index 00000000..8222d2ab --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenSuccessCode.java @@ -0,0 +1,15 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.exception; + +import com.modernfarmer.farmusspring.global.response.ResponseCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum MyVeggieGardenSuccessCode implements ResponseCode { + + + NOT_FOUND_DIARY(3000, "해당 일기가 존재하지 않습니다."); + private final int code; + private final String message; +} \ 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 new file mode 100644 index 00000000..be56c478 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java @@ -0,0 +1,34 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.repository; + + +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface MyVeggieRepository extends JpaRepository { + + Optional findById(Long id); + + @Query("SELECT d FROM diary AS d WHERE d.myVeggie = :myVeggie ORDER BY d.createdDate DESC") + List findDiariesByMyVeggie(MyVeggie myVeggie); + + @Query("SELECT d FROM diary AS d WHERE d.myVeggie = :myVeggie AND FUNCTION('DATE', d.createdDate) = CURRENT_DATE ") + Diary findDiariesByMyVeggieAndToday(MyVeggie myVeggie); + + @Query("SELECT mv FROM my_veggie AS mv JOIN FETCH mv.user WHERE mv.user.id = :userId") + List findMyVeggieUserId(@Param("userId") Long userId); + + + + + + + +} 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 new file mode 100644 index 00000000..6eddef61 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java @@ -0,0 +1,121 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.service; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.CheckTodayDiaryResponse; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectDiaryOneResponse; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenSuccessCode; +import com.modernfarmer.farmusspring.domain.myveggiegarden.repository.MyVeggieRepository; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenErrorCode; +import com.modernfarmer.farmusspring.global.response.SuccessCode; +import com.modernfarmer.farmusspring.infra.s3.S3Service; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; +import java.time.format.DateTimeFormatter; +import java.util.List; + + +@Slf4j +@AllArgsConstructor +@Service +public class MyVeggieDiaryService { + + private final S3Service s3Service; + private final MyVeggieGardenService myVeggieGardenService; + private final MyVeggieRepository myVeggieRepository; + + @Transactional + public BaseResponseDto settingMyVeggieDiary( + MultipartFile multipartFile, + String content, + boolean isOpen, + String state, + Long myVeggieId + ) throws IOException { + + String imageUrl = getImageUrl(multipartFile); + addMyyVeggieDiary( + content, + isOpen, + imageUrl, + state, + myVeggieId + ); + return BaseResponseDto.of(SuccessCode.SUCCESS,null); + } + + + @Transactional + public BaseResponseDto checkTodayDiary( + MyVeggie myVeggie + ) { + + Diary diary = selectTodayDiary(myVeggie); + boolean state = verifyDiaryState(diary); + return BaseResponseDto.of(SuccessCode.SUCCESS,CheckTodayDiaryResponse.of(state)); + } + + @Transactional + public BaseResponseDto selectDiaryOne( + MyVeggie myVeggie + ) { + List diaryList = selectDiaryByMyVeggie(myVeggie); + if(diaryList.isEmpty()) { + return BaseResponseDto.of(MyVeggieGardenSuccessCode.NOT_FOUND_DIARY, null); + } + return BaseResponseDto.of(SuccessCode.SUCCESS, + SelectDiaryOneResponse.of( + diaryList.get(0).getImage(), + diaryList.get(0).getContent(), + diaryList.get(0).getCreatedDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + )); + } + + + + public boolean verifyDiaryState(Diary diary){ + + if(diary == null){ + return true; + } + return false; + } + + + public Diary selectTodayDiary(MyVeggie myVeggie){ + return myVeggieRepository.findDiariesByMyVeggieAndToday(myVeggie); + } + + public List selectDiaryByMyVeggie(MyVeggie myVeggie){ + return myVeggieRepository.findDiariesByMyVeggie(myVeggie); + } + + private void addMyyVeggieDiary( + String content, + boolean isOpen, + String image, + String state, + Long myVeggieId + ){ + MyVeggie myVeggie = myVeggieGardenService.getMyVeggie(myVeggieId); + + + Diary newDiary = Diary.createDiary( + content, + isOpen, + image, + state, + myVeggie + ); + myVeggie.addDiary(newDiary); + } + + private String getImageUrl(MultipartFile multipartFile) throws IOException { + return s3Service.uploadImage(multipartFile, "dairyimage"); + } +} 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 new file mode 100644 index 00000000..3c4d7e01 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieGardenService.java @@ -0,0 +1,68 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.service; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRequest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectMyVeggieListDto; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectMyVeggieListResponse; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.user.entity.User; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import com.modernfarmer.farmusspring.global.response.SuccessCode; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import com.modernfarmer.farmusspring.domain.myveggiegarden.repository.MyVeggieRepository; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +@AllArgsConstructor +@Service +public class MyVeggieGardenService { + + private final MyVeggieRepository myVeggieRepository; + + @Transactional + public BaseResponseDto settingMyVeggie(Long userId, SettingMyVeggieRequest settingMyVeggieRequest) { + + addMyyVeggie(userId, settingMyVeggieRequest); + return BaseResponseDto.of(SuccessCode.SUCCESS,null); + } + + @Transactional + public BaseResponseDto selectMyVeggieList(Long userId) { + List myVeggieList = bringMyVeggieData(userId); + List selectMyVeggieLists = processingSimpleMyVeggieData(myVeggieList); + return BaseResponseDto.of(SuccessCode.SUCCESS,SelectMyVeggieListResponse.of(selectMyVeggieLists)); + } + + private List bringMyVeggieData(Long userId){ + return myVeggieRepository.findMyVeggieUserId(userId); + } + + private List processingSimpleMyVeggieData(List myVeggieList){ + return myVeggieList.stream() + .map(myVeggie -> new SelectMyVeggieListDto(myVeggie.getVeggieInfoId(),myVeggie.getNickname())) + .collect(Collectors.toList()); + } + + + + public MyVeggie getMyVeggie(Long userId){ + return myVeggieRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("채소가 존재하지 않습니다.")); + } + + private void addMyyVeggie(Long userId, SettingMyVeggieRequest settingMyVeggiRequest){ + + MyVeggie newMyVeggie = MyVeggie.createMyVegetable( + settingMyVeggiRequest.getNickname(), + settingMyVeggiRequest.getBirh(), + settingMyVeggiRequest.getVeggiInfoId(), + User.builder().id(userId).build() + ); + myVeggieRepository.save(newMyVeggie); + } + +} 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 new file mode 100644 index 00000000..48d124fc --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieRoutineService.java @@ -0,0 +1,46 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.service; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRoutineReqeuest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Routine; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import com.modernfarmer.farmusspring.global.response.SuccessCode; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; + +@Slf4j +@AllArgsConstructor +@Service +public class MyVeggieRoutineService { + + private final MyVeggieGardenService myVeggieGardenService; + + + @Transactional + public BaseResponseDto settingMyVeggieRoutine( + SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest + ) { + addMyyVeggieRoutine(settingMyVeggieRoutineReqeuest); + return BaseResponseDto.of(SuccessCode.SUCCESS,null); + } + + + + private void addMyyVeggieRoutine(SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest){ + MyVeggie myVeggie = myVeggieGardenService.getMyVeggie(settingMyVeggieRoutineReqeuest.getMyVeggieId()); + Routine newRoutine = Routine.createRoutine( + new Date(), + settingMyVeggieRoutineReqeuest.getContent(), + settingMyVeggieRoutineReqeuest.getPeriod(), + myVeggie, + settingMyVeggieRoutineReqeuest.isNotify() + ); + myVeggie.addRoutine(newRoutine); + } + + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/global/common/security/SecurityConfig.java b/src/main/java/com/modernfarmer/farmusspring/global/common/security/SecurityConfig.java index 0a8854f8..5e2fd934 100644 --- a/src/main/java/com/modernfarmer/farmusspring/global/common/security/SecurityConfig.java +++ b/src/main/java/com/modernfarmer/farmusspring/global/common/security/SecurityConfig.java @@ -56,7 +56,10 @@ public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospe "/api/user/profile-image", "/api/user/profile", "/api/veggie-info", - "/api/veggie-info/**" + "/api/veggie-info/**", + "api/my-veggie", + "api/my-veggie/diary", + "/api/my-veggie/simple-list" ).permitAll() ) .authorizeHttpRequests(request -> request.anyRequest().authenticated()); @@ -71,6 +74,9 @@ public WebSecurityCustomizer configure() { .requestMatchers("/api/auth/kakao-login") .requestMatchers("/api/auth/google-login") + .requestMatchers("/api/my-veggie/routine") + .requestMatchers("/api/my-veggie/diary/{myVeggieId}/one") + .requestMatchers("/api/my-veggie/diary/check") ); }