diff --git a/src/main/java/com/aidiary/domain/emotion/application/EmotionService.java b/src/main/java/com/aidiary/domain/emotion/application/EmotionService.java index 4be5f0e..c1f1a99 100644 --- a/src/main/java/com/aidiary/domain/emotion/application/EmotionService.java +++ b/src/main/java/com/aidiary/domain/emotion/application/EmotionService.java @@ -2,6 +2,7 @@ import com.aidiary.domain.diary.domain.Diary; import com.aidiary.domain.diary.domain.repository.DiaryRepository; +import com.aidiary.domain.emotion.dto.DiaryEditEmotionRes; import com.aidiary.domain.emotion.dto.DiarysByEmotionRes; import com.aidiary.domain.emotion.dto.EmotionStatRes; import com.aidiary.domain.user.domain.User; @@ -16,7 +17,6 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; -import java.util.List; @Service @RequiredArgsConstructor @@ -76,4 +76,28 @@ public Slice findDiarys(UserPrincipal userPrincipal, String Slice allByEmotionAndUserId = diaryRepository.findAllByEmotionAndUserId(emotion, userPrincipal.getId(), pageable); return allByEmotionAndUserId; } + + @Transactional + public DiaryEditEmotionRes editEmotion(Long userId, String emotion, Long diaryId) { + Diary diary = diaryRepository.findById(diaryId) + .orElseThrow(EntityNotFoundException::new); + + if (!diary.getUser().getId().equals(userId)) { + return DiaryEditEmotionRes + .builder() + .diaryId(diaryId) + .isEdited(false) + .emotion(diary.getEmotion()) + .build(); + } + + diary.updateEmotion(emotion); + + return DiaryEditEmotionRes + .builder() + .diaryId(diaryId) + .isEdited(true) + .emotion(emotion) + .build(); + } } diff --git a/src/main/java/com/aidiary/domain/emotion/dto/DiaryEditEmotionRes.java b/src/main/java/com/aidiary/domain/emotion/dto/DiaryEditEmotionRes.java new file mode 100644 index 0000000..4a8cafb --- /dev/null +++ b/src/main/java/com/aidiary/domain/emotion/dto/DiaryEditEmotionRes.java @@ -0,0 +1,11 @@ +package com.aidiary.domain.emotion.dto; + +import lombok.Builder; + +@Builder +public record DiaryEditEmotionRes( + Long diaryId, + boolean isEdited, + String emotion +) { +} diff --git a/src/main/java/com/aidiary/domain/emotion/presentation/EmotionController.java b/src/main/java/com/aidiary/domain/emotion/presentation/EmotionController.java index 2122d7b..3cc46dc 100644 --- a/src/main/java/com/aidiary/domain/emotion/presentation/EmotionController.java +++ b/src/main/java/com/aidiary/domain/emotion/presentation/EmotionController.java @@ -108,4 +108,18 @@ public ResponseCustom> findDiarysByEmotion( ) { return ResponseCustom.OK(emotionService.findDiarys(userPrincipal, emotion, pageable)); } + + @Operation(summary = "일기에 저장된 감정 수정", description = "일기에 저장된 감정을 수정합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "감정 수정 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = DiarysByEmotionRes.class))}), + @ApiResponse(responseCode = "400", description = "감정 수정 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}), + }) + @PatchMapping("{diaryId}/{emotion}") + public ResponseCustom editDiaryEmotion( + @Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal, + @Parameter(description = "수정할 감정(행복, 우울, 화남, 불안, 지루함)을 입력해주세요.", required = true) @PathVariable String emotion, + @Parameter(description = "감정을 수정할 일기의 id를 입력해주세요.", required = true) @PathVariable Long diaryId + ) { + return ResponseCustom.OK(emotionService.editEmotion(userPrincipal.getId(), emotion, diaryId)); + } }