Skip to content

Commit

Permalink
Merge pull request #73 from UMC-CommonPlant/feat/#26_Plant
Browse files Browse the repository at this point in the history
[Feat] 식물 수정, 식물 삭제 관련 API
  • Loading branch information
sonshn authored Jan 27, 2024
2 parents 95b5efd + d61a2f5 commit 9adc785
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public class PlantController {
@PostMapping("/plant/add")
public ResponseEntity<JsonResponse> createPlant(@RequestPart("plant") PlantDto.createPlantReq createPlantReq,
@RequestPart("image") MultipartFile file) {

// log.info("=============CREATE PLANT===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -50,9 +47,6 @@ public ResponseEntity<JsonResponse> createPlant(@RequestPart("plant") PlantDto.c
*/
@GetMapping("/plant/{plantIdx}")
public ResponseEntity<JsonResponse> getPlantCard(@PathVariable Long plantIdx) {

// log.info("=============GET PLANT===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -68,9 +62,6 @@ public ResponseEntity<JsonResponse> getPlantCard(@PathVariable Long plantIdx) {
*/
@GetMapping("/user/plantList")
public ResponseEntity<JsonResponse> getPlantList() {

// log.info("=============GET PLANT LIST===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -86,9 +77,6 @@ public ResponseEntity<JsonResponse> getPlantList() {
// */
// @GetMapping("/place/plantList/{placeCode}")
// public ResponseEntity<JsonResponse> getMyGardenPlantList(@PathVariable String placeCode) {
//
// // log.info("=============GET PLANT LIST===============");
//
// String uuid = jwtService.resolveToken();
// User user = userService.getUser(uuid);
//
Expand All @@ -107,24 +95,20 @@ public ResponseEntity<JsonResponse> getPlantList() {
*/
@PutMapping("/plant/update/wateredDate/{plantIdx}")
public ResponseEntity<JsonResponse> updateWateredDate(@PathVariable Long plantIdx){

// System.out.println("=============UPDATE PLANT WATERED DATE===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

String nickname = plantService.updateWateredDate(plantIdx, user);
String nickname = plantService.updateWateredDate(user, plantIdx);

return ResponseEntity.ok(new JsonResponse(true, 200, "updateWateredDate", nickname));
}

/**
* [GET] /plant/update
* @return 수정할 식물의 애칭
* @return 수정하기 전 식물의 애칭
*/
@GetMapping("/plant/update/{plantIdx}")
public ResponseEntity<JsonResponse> getUpdatedPlant(@PathVariable Long plantIdx){

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -141,15 +125,27 @@ public ResponseEntity<JsonResponse> getUpdatedPlant(@PathVariable Long plantIdx)
public ResponseEntity<JsonResponse> updatePlant(@PathVariable Long plantIdx,
@RequestPart("nickname") String nickname,
@RequestPart("image") MultipartFile file){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

// log.info("=============UPDATE PLANT===============");
String updatedPlant = plantService.updatePlant(user, plantIdx, nickname, file);

return ResponseEntity.ok(new JsonResponse(true, 200, "updatePlant", updatedPlant));
}

/**
* [DELETE] /plant/delete
* @param plantIdx
* @return 삭제한 식물의 닉네임
*/
@DeleteMapping("/plant/delete/{plantIdx}")
public ResponseEntity<JsonResponse> deletePlant(@PathVariable Long plantIdx){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

String updatedPlant = plantService.updatePlant(plantIdx, nickname, file);
String deletedPlant = plantService.deletePlant(user, plantIdx);

return ResponseEntity.ok(new JsonResponse(true, 200, "updatePlant", updatedPlant));
return ResponseEntity.ok(new JsonResponse(true, 200, "deletePlant", deletedPlant));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class PlantService {
@Transactional
public String createPlant(User user, PlantDto.createPlantReq req, MultipartFile plantImage) {

// 식물 장소
// TODO: 식물이 있는 장소 -> 장소의 코드로 검색(추후 장소의 이름으로 바뀔 수 있음)
Place plantPlace = placeService.getPlaceByCode(req.getPlace());
placeService.belongUserOnPlace(user, plantPlace.getCode());

// 식물 종
// String plantName = infoService.findInfo(req.getPlantName()).getName();
String plantName = req.getPlantName();
Expand All @@ -74,10 +79,6 @@ public String createPlant(User user, PlantDto.createPlantReq req, MultipartFile
throw new BadRequestException(ErrorResponseStatus.NO_SELECTED_PLANT_IMAGE);
}

// 식물 장소
// TODO: 식물이 있는 장소 -> 장소의 코드로 검색(추후 장소의 이름으로 바뀔 수 있음)
Place plantPlace = placeService.getPlaceByCode(req.getPlace());

// 물주기 기간
String waterCycle = null;
int castedWaterCycle = 0;
Expand Down Expand Up @@ -133,17 +134,13 @@ public PlantDto.getPlantRes getPlant(User user, Long plantIdx) {

List<Info> infoResponse = infoService.getOneInfo(plant.getPlantName());

// log.info("식물의 고유 정보는:" + infoResponse.getScientific_name() + infoResponse.getHumidity());

// DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

String parsedCreatedDate = plant.getCreatedAt().format(dateTimeFormatter);
// log.info("========parsedCreatedDate======== " + parsedCreatedDate);
LocalDateTime createdDateTime = LocalDate.parse(parsedCreatedDate, dateTimeFormatter).atStartOfDay();

String parsedCurrentDate = LocalDate.now().toString();
// log.info("========parsedCurrentDate======== " + parsedCurrentDate);
LocalDateTime currentDateTime = LocalDate.parse(parsedCurrentDate, dateTimeFormatter).atStartOfDay();

// countDate: 식물이 처음 온 날
Expand Down Expand Up @@ -191,20 +188,14 @@ public Long getRemainderDate(Plant plant){

// WateredDate: 마지막으로 물 준 날짜, CurrentDate: 오늘 날짜
String parsedWateredDate = plant.getWateredDate().format(dateTimeFormatter);
// log.info("========parsedWateredDate======== " + parsedWateredDate);
LocalDateTime wateredDateTime = LocalDate.parse(parsedWateredDate, dateTimeFormatter).atStartOfDay();

String parsedCurrentDate = LocalDate.now().toString();
// log.info("========parsedCurrentDate======== " + parsedCurrentDate);
LocalDateTime currentDateTime = LocalDate.parse(parsedCurrentDate, dateTimeFormatter).atStartOfDay();

// remainderDate: D-Day
Long remainderDate = (Long) infoResponse.get(0).getWater_day()
- (Long) Duration.between(wateredDateTime, currentDateTime).toDays();

// log.info(parsedWateredDate);
// log.info(parsedCurrentDate);
// log.info(String.valueOf(remainderDate));
Long remainderDate = (long) plant.getWaterCycle()
- (long) Duration.between(wateredDateTime, currentDateTime).toDays();

return remainderDate;
}
Expand All @@ -216,21 +207,18 @@ public Long getRemainderDate(Plant plant){
* @return
*/
@Transactional
public String updateWateredDate(Long plantIdx, User user) {
public String updateWateredDate(User user, Long plantIdx) {

Plant plant = plantRepository.findByPlantIdx(plantIdx)
.orElseThrow(() -> new BadRequestException(ErrorResponseStatus.NOT_FOUND_PLANT));

// log.info(" 물주기 리셋할 식물은: " + plant.getNickname());
// TODO: 식물이 있는 장소 -> 장소의 코드로 검색(추후 장소의 이름으로 바뀔 수 있음)
Place plantPlace = placeService.getPlaceByCode(plant.getPlace().getCode());
placeService.belongUserOnPlace(user, plantPlace.getCode());

// TODO: 마지막으로 물 준 날짜
plant.setWateredDate(LocalDateTime.now());

// getPlantInfo()의 인자는 식물 종 이름: 식물 조회할 때 식물 종 이름을 보내서 검색하면 됨
// InfoDto.InfoResponse infoResponse = infoService.findInfo(plant.getPlantName());
// Long resetRemainderDate = -1 * (Long) infoResponse.getWater_day();
// plant.setRemainderDate(resetRemainderDate);

return plantRepository.save(plant).getNickname();
}

Expand Down Expand Up @@ -344,11 +332,15 @@ public List<PlantDto.getMyGardenPlantListRes> getMyGardenPlantList(String placeC
* @return
*/
@Transactional
public String updatePlant(Long plantIdx, String nickname, MultipartFile plantImage) {
public String updatePlant(User user, Long plantIdx, String nickname, MultipartFile plantImage) {

Plant plant = plantRepository.findByPlantIdx(plantIdx)
.orElseThrow(() -> new BadRequestException(ErrorResponseStatus.NOT_FOUND_PLANT));

// TODO: 식물이 있는 장소 -> 장소의 코드로 검색(추후 장소의 이름으로 바뀔 수 있음)
Place plantPlace = placeService.getPlaceByCode(plant.getPlace().getCode());
placeService.belongUserOnPlace(user, plantPlace.getCode());

// TODO: 식물의 애칭
String plantNickname = null;

Expand Down Expand Up @@ -386,6 +378,10 @@ public PlantDto.updatePlantRes getUpdatedPlant(User user, Long plantIdx) {
Plant plant = plantRepository.findByPlantIdx(plantIdx)
.orElseThrow(() -> new BadRequestException(ErrorResponseStatus.NOT_FOUND_PLANT));

// TODO: 식물이 있는 장소 -> 장소의 코드로 검색(추후 장소의 이름으로 바뀔 수 있음)
Place plantPlace = placeService.getPlaceByCode(plant.getPlace().getCode());
placeService.belongUserOnPlace(user, plantPlace.getCode());

return new PlantDto.updatePlantRes(
plant.getNickname(),
plant.getImgUrl()
Expand All @@ -404,6 +400,21 @@ public String deletePlant(User user, Long plantIdx) {
Plant plant = plantRepository.findByPlantIdx(plantIdx)
.orElseThrow(() -> new BadRequestException(ErrorResponseStatus.NOT_FOUND_PLANT));

// TODO: 식물이 있는 장소 -> 장소의 코드로 검색(추후 장소의 이름으로 바뀔 수 있음)
Place plantPlace = placeService.getPlaceByCode(plant.getPlace().getCode());
placeService.belongUserOnPlace(user, plantPlace.getCode());

List<MemoDto.GetAllMemo> memoList = memoService.getAllMemoByPlant(plantIdx);

for(MemoDto.GetAllMemo memo : memoList) {
memoService.deleteMemo(user, memo.getMemo_idx());
}

imageService.deleteFileInS3(plant.getImgUrl());

plantRepository.deleteById(plantIdx);

return plant.getNickname();
}

}

0 comments on commit 9adc785

Please sign in to comment.