Skip to content

Commit

Permalink
Merge pull request #79 from Mojacknong/fix_42/유저-히스토리-생성-기능
Browse files Browse the repository at this point in the history
Feature 78 채소 히스토리 생성 로직
  • Loading branch information
MinchoGreenT authored Aug 15, 2024
2 parents b7a683e + dff6f86 commit 0551f1d
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public static HistoryVeggieDetail createHistoryDetail(List<HistoryPost> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.modernfarmer.farmusspring.domain.history.dto.req;

public record VeggieHistoryResultPostRequestDto(
String historyDetailId,
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
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;
import org.bson.types.ObjectId;
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;
Expand Down Expand Up @@ -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<Diary> diaries = myVeggieHelper.getDiariesByMyVeggie(myVeggie);
List<HistoryVeggieDetail.HistoryPost> 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<HistoryFarmClubDetail.HistoryClubPost> getHistoryClubPostList(List<MissionPostHistoryVo> missionPostHistoryList, List<StepVo> stepList) {
Expand All @@ -107,6 +125,13 @@ private static List<HistoryFarmClubDetail.HistoryClubPost> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +24,7 @@
public class HistoryService {

private final HistoryHelper historyHelper;
private final S3Service s3Service;

public void createHistory(Long userId) {
historyHelper.createUserHistory(userId);
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request;

public record SuccessFarmingRequestDto(
Long myVeggieId,
String content
) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,4 +39,8 @@ public boolean checkMyVeggie(Long userId) {
public void deleteMyVeggie(Long id) {
myVeggieRepository.deleteById(id);
}

public List<Diary> getDiariesByMyVeggie(MyVeggie myVeggie) {
return myVeggieRepository.findDiariesByMyVeggie(myVeggie);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
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;
import com.modernfarmer.farmusspring.domain.veggieinfo.helper.VeggieInfoHelper;
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;

Expand All @@ -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;



Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0551f1d

Please sign in to comment.