diff --git a/src/main/java/swyp/team5/greening/petPlant/controller/DailyRecordController.java b/src/main/java/swyp/team5/greening/petPlant/controller/DailyRecordController.java index 79028d7..b50e043 100644 --- a/src/main/java/swyp/team5/greening/petPlant/controller/DailyRecordController.java +++ b/src/main/java/swyp/team5/greening/petPlant/controller/DailyRecordController.java @@ -5,9 +5,11 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; @@ -15,6 +17,7 @@ import swyp.team5.greening.common.dto.response.ApiResponseDto; import swyp.team5.greening.common.resolver.LogIn; import swyp.team5.greening.petPlant.dto.request.CreateDailyRecordRequestDto; +import swyp.team5.greening.petPlant.dto.request.UpdateDailyRecordRequestDto; import swyp.team5.greening.petPlant.dto.response.CreateDailyRecordResponseDto; import swyp.team5.greening.petPlant.dto.response.FindDailyRecordResponseDto; import swyp.team5.greening.petPlant.service.DailyRecordCommandService; @@ -58,4 +61,25 @@ public ApiResponseDto getDailyRecord( )); } + @Operation(summary = "특정 애완 식물 오늘의 기록 수정 API") + @PutMapping("/daily-record/{dailyRecordId}") + @ResponseStatus(HttpStatus.OK) + public void updateDailyRecord( + @LogIn Long userId, + @PathVariable Long dailyRecordId, + @Validated @RequestBody UpdateDailyRecordRequestDto requestDto + ) { + dailyRecordCommandService.updateDailyRecord(userId, dailyRecordId, requestDto); + } + + @Operation(summary = "특정 애완 식물 오늘의 기록 삭제 API") + @DeleteMapping("/daily-record/{dailyRecordId}") + @ResponseStatus(HttpStatus.OK) + public void deleteDailyRecord( + @LogIn Long userId, + @PathVariable Long dailyRecordId + ) { + dailyRecordCommandService.deleteDailyRecord(userId, dailyRecordId); + } + } diff --git a/src/main/java/swyp/team5/greening/petPlant/domain/entity/DailyRecord.java b/src/main/java/swyp/team5/greening/petPlant/domain/entity/DailyRecord.java index d194449..b3f74b3 100644 --- a/src/main/java/swyp/team5/greening/petPlant/domain/entity/DailyRecord.java +++ b/src/main/java/swyp/team5/greening/petPlant/domain/entity/DailyRecord.java @@ -58,6 +58,10 @@ public DailyRecord( this.petPlantId = petPlantId; } + public void updateTitle(String newTitle) { + this.title = newTitle; + } + public void updateContent(List contents) { dailyRecordContents.clear(); @@ -68,4 +72,8 @@ public void updateContent(List contents) { content.setSequence(index); } } + + public void delete() { + this.state = DailyRecordState.DELETED; + } } diff --git a/src/main/java/swyp/team5/greening/petPlant/domain/repository/DailyRecordRepository.java b/src/main/java/swyp/team5/greening/petPlant/domain/repository/DailyRecordRepository.java index d2a3506..06579f1 100644 --- a/src/main/java/swyp/team5/greening/petPlant/domain/repository/DailyRecordRepository.java +++ b/src/main/java/swyp/team5/greening/petPlant/domain/repository/DailyRecordRepository.java @@ -1,10 +1,17 @@ package swyp.team5.greening.petPlant.domain.repository; +import java.time.LocalDate; +import java.util.Optional; import swyp.team5.greening.petPlant.domain.entity.DailyRecord; +import swyp.team5.greening.petPlant.domain.entity.DailyRecordState; public interface DailyRecordRepository { DailyRecord save(DailyRecord dailyRecord); + boolean existsByPetPlantIdAndWriteDateAndState(Long petPlantId, LocalDate writeDate, DailyRecordState state); + + Optional findByIdAndState(Long dailyRecordId, DailyRecordState state); + void deleteAll(); } diff --git a/src/main/java/swyp/team5/greening/petPlant/dto/request/UpdateDailyRecordRequestDto.java b/src/main/java/swyp/team5/greening/petPlant/dto/request/UpdateDailyRecordRequestDto.java new file mode 100644 index 0000000..2744825 --- /dev/null +++ b/src/main/java/swyp/team5/greening/petPlant/dto/request/UpdateDailyRecordRequestDto.java @@ -0,0 +1,16 @@ +package swyp.team5.greening.petPlant.dto.request; + +import jakarta.validation.constraints.NotEmpty; +import java.util.List; +import swyp.team5.greening.petPlant.dto.DailyRecordContentDto; + +public record UpdateDailyRecordRequestDto( + + @NotEmpty + String title, + + @NotEmpty + List content +) { + +} diff --git a/src/main/java/swyp/team5/greening/petPlant/exception/PetPlantExceptionMessage.java b/src/main/java/swyp/team5/greening/petPlant/exception/PetPlantExceptionMessage.java index 20c3d6f..dfa0d6c 100644 --- a/src/main/java/swyp/team5/greening/petPlant/exception/PetPlantExceptionMessage.java +++ b/src/main/java/swyp/team5/greening/petPlant/exception/PetPlantExceptionMessage.java @@ -12,6 +12,7 @@ public enum PetPlantExceptionMessage implements ExceptionMessage { BAD_REQUEST_PET_PLANT_WRITER("내가 등록한 애완 식물이 아닙니다.", "400"), NOT_FOUND_DAILY_RECORD("해당 날짜의 기록이 존재하지 않습니다.", "404"), + ALREADY_EXISTS_DAILY_RECORD("이미 작성한 오늘의 기록이 존재합니다", "409"), INVALID_DATE_ACCESS("해당 날짜에만 접근 가능합니다.", "400"); diff --git a/src/main/java/swyp/team5/greening/petPlant/service/DailyRecordCommandService.java b/src/main/java/swyp/team5/greening/petPlant/service/DailyRecordCommandService.java index 3e5503a..c638cfb 100644 --- a/src/main/java/swyp/team5/greening/petPlant/service/DailyRecordCommandService.java +++ b/src/main/java/swyp/team5/greening/petPlant/service/DailyRecordCommandService.java @@ -17,6 +17,7 @@ import swyp.team5.greening.petPlant.domain.repository.DailyRecordRepository; import swyp.team5.greening.petPlant.domain.repository.PetPlantRepository; import swyp.team5.greening.petPlant.dto.request.CreateDailyRecordRequestDto; +import swyp.team5.greening.petPlant.dto.request.UpdateDailyRecordRequestDto; import swyp.team5.greening.petPlant.dto.response.CreateDailyRecordResponseDto; import swyp.team5.greening.petPlant.exception.PetPlantExceptionMessage; @@ -33,7 +34,8 @@ public class DailyRecordCommandService { //1. 애완 식물 조회 //2. 사용자 유효성 검증 //3. 오늘 날짜 여부 확인 - //4. 오늘의 기록 저장 + //4. 이미 오늘 작성한 오늘의 기록이 존재하는지 확인 + //5. 오늘의 기록 작성 @Transactional public CreateDailyRecordResponseDto createDailyRecord( Long userId, @@ -57,6 +59,12 @@ public CreateDailyRecordResponseDto createDailyRecord( } //4 + if (dailyRecordRepository.existsByPetPlantIdAndWriteDateAndState(petPlantId, + nowDate.get(), DailyRecordState.IN_PROGRESS)) { + throw new GreeningGlobalException(PetPlantExceptionMessage.ALREADY_EXISTS_DAILY_RECORD); + } + + //5 DailyRecord dailyRecord = DailyRecord.builder() .title(requestDto.title()) .writeDate(requestDto.today()) @@ -78,4 +86,70 @@ public CreateDailyRecordResponseDto createDailyRecord( return new CreateDailyRecordResponseDto(saved.getId()); } + //오늘의 기록 수정 + //1. 오늘의 기록 조회 + //2. 권한 확인 + //3. 제목 수정 + //4. 새로운 본문 저장 + @Transactional + public void updateDailyRecord( + Long userId, + Long dailyRecordId, + UpdateDailyRecordRequestDto requestDto + + ) { + DailyRecord dailyRecord = dailyRecordRepository.findByIdAndState(dailyRecordId, + DailyRecordState.IN_PROGRESS) + .orElseThrow(() -> new GreeningGlobalException( + PetPlantExceptionMessage.NOT_FOUND_PET_PLANT)); + + PetPlant petPlant = petPlantRepository.findByIdAndState(dailyRecord.getPetPlantId(), + PetPlantState.IN_PROGRESS) + .orElseThrow(() -> new GreeningGlobalException( + PetPlantExceptionMessage.NOT_FOUND_PET_PLANT)); + + if (!Objects.equals(petPlant.getUserId(), userId)) { + throw new GreeningGlobalException( + PetPlantExceptionMessage.BAD_REQUEST_PET_PLANT_WRITER); + } + + dailyRecord.updateTitle(requestDto.title()); + + List contents = requestDto.content().stream() + .map(dto -> DailyRecordContent.builder() + .content(dto.value()) + .type(DailyRecordContentType.valueOf(dto.type().toUpperCase())) + .build()) + .toList(); + + dailyRecord.updateContent(contents); + } + + //오늘의 기록 삭제 + //1. 오늘의 기록 조회 + //2. 권한 확인 + //3. 삭제 + @Transactional + public void deleteDailyRecord( + Long userId, + Long dailyRecordId + ) { + DailyRecord dailyRecord = dailyRecordRepository.findByIdAndState(dailyRecordId, + DailyRecordState.IN_PROGRESS) + .orElseThrow(() -> new GreeningGlobalException( + PetPlantExceptionMessage.NOT_FOUND_PET_PLANT)); + + PetPlant petPlant = petPlantRepository.findByIdAndState(dailyRecord.getPetPlantId(), + PetPlantState.IN_PROGRESS) + .orElseThrow(() -> new GreeningGlobalException( + PetPlantExceptionMessage.NOT_FOUND_PET_PLANT)); + + if (!Objects.equals(petPlant.getUserId(), userId)) { + throw new GreeningGlobalException( + PetPlantExceptionMessage.BAD_REQUEST_PET_PLANT_WRITER); + } + + dailyRecord.delete(); + } + }