diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/history/controller/HistoryController.java b/src/main/java/com/modernfarmer/farmusspring/domain/history/controller/HistoryController.java index e645ad5b..987c886e 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/history/controller/HistoryController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/history/controller/HistoryController.java @@ -1,6 +1,7 @@ package com.modernfarmer.farmusspring.domain.history.controller; import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; +import com.modernfarmer.farmusspring.domain.history.dto.req.VeggieHistoryResultPostRequestDto; import com.modernfarmer.farmusspring.domain.history.dto.res.*; import com.modernfarmer.farmusspring.domain.history.service.HistoryService; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; @@ -12,6 +13,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; @Slf4j @RestController @@ -77,4 +79,13 @@ public BaseResponseDto getVeggieHistoryDetail( ) { return BaseResponseDto.of(SuccessCode.SUCCESS, historyService.getVeggieHistoryDetail(detailId)); } + + @PostMapping("/veggie/result") + public BaseResponseDto postFarmResult( + @RequestPart("image") final MultipartFile image, + @RequestPart("requestDto") final VeggieHistoryResultPostRequestDto requestDto + ) { + historyService.createVeggieHistoryResult(requestDto, image); + return BaseResponseDto.of(SuccessCode.CREATED, null); + } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/history/document/History.java b/src/main/java/com/modernfarmer/farmusspring/domain/history/document/History.java index 563231b8..fd081ce4 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/history/document/History.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/history/document/History.java @@ -90,4 +90,12 @@ public void addVeggieHistoryDetail(Detail detail) { public void addFarmClubHistoryDetail(Detail detail) { farmClubHistoryDetails.add(detail); } + + public void addVeggieHistoryIcon(Icon icon) { + veggieHistoryIcons.add(icon); + } + + public void addFarmClubHistoryIcon(Icon icon) { + farmClubHistoryIcons.add(icon); + } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/history/document/HistoryVeggieDetail.java b/src/main/java/com/modernfarmer/farmusspring/domain/history/document/HistoryVeggieDetail.java index de76a64c..b1a23120 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/history/document/HistoryVeggieDetail.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/history/document/HistoryVeggieDetail.java @@ -43,6 +43,14 @@ public static HistoryVeggieDetail createHistoryDetail(List diaryPos .build(); } + public static HistoryPost createHistoryPost(String postImage, String content, String dateTime) { + return HistoryPost.builder() + .postImage(postImage) + .content(content) + .dateTime(dateTime) + .build(); + } + public void updateHistoryDetailResult(HistoryPost farmResult) { this.farmResult = farmResult; } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/history/dto/req/VeggieHistoryResultPostRequestDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/history/dto/req/VeggieHistoryResultPostRequestDto.java new file mode 100644 index 00000000..017103b7 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/history/dto/req/VeggieHistoryResultPostRequestDto.java @@ -0,0 +1,7 @@ +package com.modernfarmer.farmusspring.domain.history.dto.req; + +public record VeggieHistoryResultPostRequestDto( + String historyDetailId, + String content +) { +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/history/helper/HistoryHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/history/helper/HistoryHelper.java index 835cce40..bb4441e5 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/history/helper/HistoryHelper.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/history/helper/HistoryHelper.java @@ -12,9 +12,13 @@ import com.modernfarmer.farmusspring.domain.history.repository.HistoryVeggieDetailRepository; import com.modernfarmer.farmusspring.domain.history.vo.HistoryDetailVo; import com.modernfarmer.farmusspring.domain.history.vo.MissionPostHistoryVo; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.helper.MyVeggieHelper; +import com.modernfarmer.farmusspring.domain.myveggiegarden.util.DateManager; import com.modernfarmer.farmusspring.domain.veggieinfo.helper.VeggieInfoHelper; import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; +import jakarta.transaction.Transactional; import com.modernfarmer.farmusspring.domain.veggieinfo.vo.VeggieInfoVo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -22,6 +26,8 @@ import org.springframework.stereotype.Component; import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Date; import java.util.List; import static com.modernfarmer.farmusspring.domain.history.utils.StringUtil.getHistoryPeriod; @@ -68,24 +74,36 @@ public void createFarmClubHistoryDetail(Long userId, Long userFarmClubId, String historyRepository.save(history); } - public void createVeggieHistoryDetail(Long myVeggieId) { - + @Transactional + public void createVeggieHistoryDetail(Long userId, Long myVeggieId, HistoryVeggieDetail.HistoryPost farmResult) { + MyVeggie myVeggie = myVeggieHelper.getMyVeggieEntity(myVeggieId); + VeggieInfoVo veggieInfo = veggieInfoHelper.getVeggieInfo(myVeggie.getVeggieInfoId()); // 해당 채소의 모든 성장일기를 가져옴 // 이미지, 내용, 날짜 + List diaries = myVeggieHelper.getDiariesByMyVeggie(myVeggie); + List diaryHistories = diaries.stream() + .map(diary -> HistoryVeggieDetail.HistoryPost.builder() + .postImage(diary.getImage()) + .content(diary.getContent()) + .dateTime(DateManager.parsingDotDateTime(diary.getCreatedDate())) + .build()) + .toList(); + HistoryVeggieDetail historyVeggieDetail = HistoryVeggieDetail.createHistoryDetail(diaryHistories, farmResult); + String veggieDetailId = historyVeggieDetailRepository.save(historyVeggieDetail).getId().toHexString(); - // 히스토리 베지 디테일 생성 및 아이디 반환 - - - // 반환한 아이디로 히스토리 디테일 생성 - - - // 내채소 아이디로 채소 이미지, 채소 닉네임, 채소명, 팜클럽 기간 조회 - - - // 유저 히스토리 조회 및 디테일 추가 - - + History.Detail historyDetail = History.Detail.createDetail( + veggieDetailId, + myVeggie.getVeggieImage(), + veggieInfo.backgroundColor(), + myVeggie.getNickname(), + myVeggie.getVeggieName(), + getHistoryPeriod(myVeggie.getBirth().toString(), LocalDate.now().toString())); + History.Icon icon = History.Icon.createIcon(veggieInfo.veggieImage(), veggieInfo.backgroundColor()); + History history = getUserHistory(userId); + history.getVeggieHistoryDetails().add(historyDetail); + history.getVeggieHistoryIcons().add(icon); + historyRepository.save(history); } private static List getHistoryClubPostList(List missionPostHistoryList, List stepList) { @@ -107,6 +125,13 @@ private static List getHistoryClubPostLis .toList(); } + @Transactional + public void createVeggieHistoryResult(HistoryVeggieDetail.HistoryPost farmResult, String veggieDetailId) { + HistoryVeggieDetail historyVeggieDetail = getVeggieHistoryDetail(veggieDetailId); + historyVeggieDetail.updateHistoryDetailResult(farmResult); + historyVeggieDetailRepository.save(historyVeggieDetail); + } + public History getUserHistory(Long userId) { return historyRepository.findByUserId(userId) .orElseThrow(() -> new HistoryEntityNotFoundException("해당 유저의 히스토리가 존재하지 않습니다.", HistoryErrorCode.ENTITY_NOT_FOUND)); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/history/service/HistoryService.java b/src/main/java/com/modernfarmer/farmusspring/domain/history/service/HistoryService.java index 44e4b6fd..d9da602a 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/history/service/HistoryService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/history/service/HistoryService.java @@ -3,14 +3,19 @@ import com.modernfarmer.farmusspring.domain.history.document.History; import com.modernfarmer.farmusspring.domain.history.document.HistoryFarmClubDetail; import com.modernfarmer.farmusspring.domain.history.document.HistoryVeggieDetail; +import com.modernfarmer.farmusspring.domain.history.dto.req.VeggieHistoryResultPostRequestDto; import com.modernfarmer.farmusspring.domain.history.dto.res.*; import com.modernfarmer.farmusspring.domain.history.helper.HistoryHelper; import com.modernfarmer.farmusspring.domain.history.repository.HistoryRepository; +import com.modernfarmer.farmusspring.domain.myveggiegarden.util.DateManager; +import com.modernfarmer.farmusspring.infra.s3.S3Service; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.bson.types.ObjectId; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import java.time.LocalDateTime; import java.util.ArrayList; @Service @@ -19,6 +24,7 @@ public class HistoryService { private final HistoryHelper historyHelper; + private final S3Service s3Service; public void createHistory(Long userId) { historyHelper.createUserHistory(userId); @@ -53,4 +59,14 @@ public FarmClubHistoryListResponseDto getFarmClubHistories(Long userId) { History history = historyHelper.getUserHistory(userId); return FarmClubHistoryListResponseDto.of(history.getFarmClubHistoryDetails()); } + + public void createVeggieHistoryResult(VeggieHistoryResultPostRequestDto requestDto, MultipartFile image) { + String imageUrl = s3Service.uploadImage(image, "farm-result"); + HistoryVeggieDetail.HistoryPost farmResult = HistoryVeggieDetail.createHistoryPost( + imageUrl, + requestDto.content(), + DateManager.parsingDotDateTime(LocalDateTime.now()) + ); + historyHelper.createVeggieHistoryResult(farmResult, requestDto.historyDetailId()); + } } 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 index ccf09c58..dbeb7e6b 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieGardenController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieGardenController.java @@ -5,6 +5,7 @@ import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.DeleteMyVeggieRequest; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.MyVeggieUpdate; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRequest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SuccessFarmingRequestDto; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.MyDetailMyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectMyVeggieListResponse; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectMyVeggieProfileResponse; @@ -16,6 +17,7 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -77,6 +79,14 @@ public BaseResponseDto updateMyVeggie( return BaseResponseDto.of(SuccessCode.SUCCESS, null); } - + @PostMapping("/success") + public BaseResponseDto successFarming( + @RequestPart("requestDto") SuccessFarmingRequestDto requestDto, + @RequestPart("image") final MultipartFile image, + @AuthenticationPrincipal CustomUser user + ){ + myVeggieGardenService.successFarming(requestDto, image, user.getUserId()); + return BaseResponseDto.of(SuccessCode.SUCCESS, null); + } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SuccessFarmingRequestDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SuccessFarmingRequestDto.java new file mode 100644 index 00000000..97c994a8 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/request/SuccessFarmingRequestDto.java @@ -0,0 +1,7 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request; + +public record SuccessFarmingRequestDto( + Long myVeggieId, + String content +) { +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java index 211037ca..f2f51e65 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java @@ -1,5 +1,6 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.helper; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenErrorCode; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.MyVeggieGardenBaseException; @@ -38,4 +39,8 @@ public boolean checkMyVeggie(Long userId) { public void deleteMyVeggie(Long id) { myVeggieRepository.deleteById(id); } + + public List getDiariesByMyVeggie(MyVeggie myVeggie) { + return myVeggieRepository.findDiariesByMyVeggie(myVeggie); + } } 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 3d715c21..fbea6720 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 @@ -1,10 +1,14 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.service; +import com.modernfarmer.farmusspring.domain.history.document.HistoryVeggieDetail; +import com.modernfarmer.farmusspring.domain.history.helper.HistoryHelper; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.DeleteMyVeggieRequest; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.MyVeggieUpdate; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRequest; +import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SuccessFarmingRequestDto; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.*; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.myveggiegarden.helper.MyVeggieHelper; import com.modernfarmer.farmusspring.domain.myveggiegarden.util.DateManager; import com.modernfarmer.farmusspring.domain.user.entity.User; import com.modernfarmer.farmusspring.domain.veggieinfo.entity.VeggieInfo; @@ -12,13 +16,16 @@ import com.modernfarmer.farmusspring.domain.veggieinfo.vo.VeggieInfoVo; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; 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 com.modernfarmer.farmusspring.domain.myveggiegarden.repository.MyVeggieRepository; +import org.springframework.web.multipart.MultipartFile; +import java.time.LocalDateTime; import java.util.Date; import java.util.List; @@ -30,6 +37,9 @@ public class MyVeggieGardenService { private final MyVeggieRepository myVeggieRepository; private final MyDetailMyVeggieDto myDetailMyVeggieDto; private final VeggieInfoHelper veggieInfoHelper; + private final MyVeggieHelper myVeggieHelper; + private final HistoryHelper historyHelper; + private final S3Service s3Service; @@ -84,7 +94,17 @@ public void myVeggieUpdate(MyVeggieUpdate myVeggieUpdate) { myVeggieRepository.updateMyVeggie(myVeggieUpdate.getMyVeggieId(), myVeggieUpdate.getNickname(), myVeggieUpdate.getBirth()); } - + @Transactional + public void successFarming(SuccessFarmingRequestDto requestDto, MultipartFile image, Long userId) { + String imageUrl = s3Service.uploadImage(image, "farm-result"); + HistoryVeggieDetail.HistoryPost farmResult = HistoryVeggieDetail.createHistoryPost( + imageUrl, + requestDto.content(), + DateManager.parsingDotDateTime(LocalDateTime.now()) + ); + historyHelper.createVeggieHistoryDetail(userId, requestDto.myVeggieId(), farmResult); + myVeggieHelper.deleteMyVeggie(requestDto.myVeggieId()); + } public int checkFarmClubAffiliation(MyVeggie myVeggie){ if(myVeggie.getUserFarmClub() == null)