diff --git a/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java index 311912c..7da0f93 100644 --- a/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java @@ -35,6 +35,9 @@ public enum ErrorStatus implements BaseErrorCode { ANSWER_NOT_FOUND(HttpStatus.BAD_REQUEST, "ANSWER4001", "해당하는 회고 답변이 없습니다."), ANSWER_BAD_MATCH(HttpStatus.BAD_REQUEST, "ANSWER4002", "해당하는 회고에 속하는 회고 답변이 아닙니다."), + // 이모티콘 관련 에러 + EMOTICON_NOT_FOUND(HttpStatus.BAD_REQUEST, "EMOTICON4001", "해당하는 이모티콘이 없습니다."), + // 피드 관련 에러 FEED_NOT_FOUND(HttpStatus.BAD_REQUEST, "FEED4001", "해당하는 워라벨 피드가 없습니다."), FEED_NOT_BLANK(HttpStatus.BAD_REQUEST, "FEED4002", "워라벨 피드 내용은 공백일 수 없습니다."), diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/controller/MemoirController.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/controller/MemoirController.java index e66cda4..21bde92 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/controller/MemoirController.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/controller/MemoirController.java @@ -4,12 +4,14 @@ import com.onnoff.onnoff.domain.off.memoir.converter.MemoirConverter; import com.onnoff.onnoff.domain.off.memoir.dto.MemoirRequestDTO; import com.onnoff.onnoff.domain.off.memoir.dto.MemoirResponseDTO; +import com.onnoff.onnoff.domain.off.memoir.entity.Emoticon; import com.onnoff.onnoff.domain.off.memoir.entity.Memoir; import com.onnoff.onnoff.domain.off.memoir.entity.MemoirQuestion; import com.onnoff.onnoff.domain.off.memoir.service.MemoirService; import io.swagger.v3.oas.annotations.Operation; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; @@ -21,42 +23,66 @@ public class MemoirController { private final MemoirService memoirService; - @GetMapping("/memoir-questions") - @Operation(summary = "회고 질문 조회 API",description = "회고 질문 목록을 조회하는 API입니다. Query String으로 사용자 아이디를 입력해 주세요.") - public ApiResponse> getMemoirQuestion(@RequestParam(name = "userId") Long userId){ - List memoirQuestionList = memoirService.getMemoirQuestion(userId); - return ApiResponse.onSuccess(MemoirConverter.toQuestionResultDTOList(memoirQuestionList)); - } - @PostMapping("/memoirs") - @Operation(summary = "회고 작성 API",description = "새로운 회고를 작성하는 API입니다.") - public ApiResponse writeMemoir(@RequestBody @Valid MemoirRequestDTO.WriteDTO request){ + @Operation(summary = "회고 작성 API", description = "새로운 회고를 작성하는 API입니다.") + public ApiResponse writeMemoir(@RequestBody @Valid MemoirRequestDTO.MemoirWriteDTO request) { Memoir memoir = memoirService.writeMemoir(request); - return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir)); + return ApiResponse.onSuccess(MemoirConverter.toMemoirDTO(memoir)); } - @GetMapping("/memoirs") - @Operation(summary = "회고 조회 API",description = "특정한 날짜의 회고를 조회하는 API입니다. Query String으로 사용자 아이디와 날짜를 입력해 주세요.") - public ApiResponse writeMemoir(@RequestParam(name = "userId") Long userId, @RequestParam(name = "date") LocalDate date){ - Memoir memoir = memoirService.getMemoir(userId, date); + @GetMapping("/memoirs/previews") + @Operation(summary = "회고 미리보기 조회 API", description = "특정 날짜의 회고 미리보기를 조회하는 API입니다. Query String으로 날짜를 입력해 주세요.") + public ApiResponse getMemoirPreview(@RequestParam(name = "date") LocalDate date) { + Memoir memoir = memoirService.getMemoirPreview(date); + return ApiResponse.onSuccess(MemoirConverter.toMemoirPreviewDTO(memoir)); + } - if (memoir == null) { - return ApiResponse.onSuccess(null); - } - return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir)); + @GetMapping("/memoirs/{memoirId}") + @Operation(summary = "회고 조회 API", description = "특정 회고를 조회하는 API입니다.") + public ApiResponse getMemoir(@PathVariable(name = "memoirId") Long memoirId) { + Memoir memoir = memoirService.getMemoir(memoirId); + return ApiResponse.onSuccess(MemoirConverter.toMemoirDTO(memoir)); } - @PatchMapping("/memoirs") - @Operation(summary = "회고 수정 API",description = "기존의 회고를 수정하는 API입니다.") - public ApiResponse updateMemoir(@RequestBody @Valid MemoirRequestDTO.UpdateDTO request){ - Memoir memoir = memoirService.updateMemoir(request); - return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir)); + @GetMapping("/memoirs/bookmarks") + @Operation(summary = "북마크 회고 조회 API", description = "북마크 상태의 회고를 조회하는 API이며, 페이징을 포함합니다. Query String으로 페이지 번호를 입력해 주세요. 0번이 1페이지입니다.") + public ApiResponse getBookmarkedMemoir(@RequestParam(name = "pageNumber") Integer pageNumber) { + Page memoirList = memoirService.getBookmarkedMemoir(pageNumber); + return ApiResponse.onSuccess(MemoirConverter.toBookmarkedMemoirPreviewListDTO(memoirList)); + } + + @PatchMapping("/memoirs/{memoirId}") + @Operation(summary = "회고 내용 수정 API", description = "기존의 회고 내용을 수정하는 API입니다.") + public ApiResponse modifyMemoir(@PathVariable(name = "memoirId") Long memoirId, + @RequestBody @Valid MemoirRequestDTO.MemoirUpdateDTO request) { + Memoir memoir = memoirService.modifyMemoir(memoirId, request); + return ApiResponse.onSuccess(MemoirConverter.toMemoirDTO(memoir)); + } + + @PatchMapping("/memoirs/{memoirId}/bookmark") + @Operation(summary = "회고 북마크 및 해제 API", description = "회고를 북마크하거나 북마크 해제하는 API입니다.") + public ApiResponse bookmarkMemoir(@PathVariable(name = "memoirId") Long memoirId) { + Memoir memoir = memoirService.bookmarkMemoir(memoirId); + return ApiResponse.onSuccess(MemoirConverter.toMemoirDTO(memoir)); } @DeleteMapping("/memoirs/{memoirId}") - @Operation(summary = "회고 삭제 API",description = "기존의 회고를 삭제하는 API입니다.") - public ApiResponse deleteMemoir(@PathVariable(name = "memoirId") Long memoirId){ - Memoir memoir = memoirService.deleteMemoir(memoirId); - return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir)); + @Operation(summary = "회고 삭제 API", description = "기존의 회고를 삭제하는 API입니다.") + public ApiResponse deleteMemoir(@PathVariable(name = "memoirId") Long memoirId) { + return ApiResponse.onSuccess(memoirService.deleteMemoir(memoirId)); + } + + @GetMapping("/memoir-questions") + @Operation(summary = "회고 질문 조회 API", description = "회고 질문 목록을 조회하는 API입니다.") + public ApiResponse> getMemoirQuestion() { + List memoirQuestionList = memoirService.getMemoirQuestion(); + return ApiResponse.onSuccess(MemoirConverter.toMemoirQuestionDTOList(memoirQuestionList)); + } + + @GetMapping("/emoticons") + @Operation(summary = "이모티콘 조회 API", description = "이모티콘 목록을 조회하는 API입니다.") + public ApiResponse> getEmoticon() { + List emoticonList = memoirService.getEmoticon(); + return ApiResponse.onSuccess(MemoirConverter.toEmoticonDTOList(emoticonList)); } } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/converter/MemoirConverter.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/converter/MemoirConverter.java index 10fb0d6..c2dc91d 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/converter/MemoirConverter.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/converter/MemoirConverter.java @@ -1,47 +1,45 @@ package com.onnoff.onnoff.domain.off.memoir.converter; -import com.onnoff.onnoff.domain.off.memoir.dto.MemoirRequestDTO; import com.onnoff.onnoff.domain.off.memoir.dto.MemoirResponseDTO; +import com.onnoff.onnoff.domain.off.memoir.entity.Emoticon; import com.onnoff.onnoff.domain.off.memoir.entity.Memoir; import com.onnoff.onnoff.domain.off.memoir.entity.MemoirAnswer; import com.onnoff.onnoff.domain.off.memoir.entity.MemoirQuestion; +import org.springframework.data.domain.Page; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class MemoirConverter { - public static List toQuestionResultDTOList(List memoirQuestionList) { - return memoirQuestionList.stream() - .map(x -> MemoirResponseDTO.QuestionResultDTO.builder() - .questionId(x.getId()) - .question(x.getQuestion()) - .summary(x.getSummary()) - .build()) - .collect(Collectors.toList()); - } - - public static Memoir toMemoir(MemoirRequestDTO.WriteDTO request) { - return Memoir.builder() - .date(request.getDate()) - .icon(request.getIcon()) - .isBookmarked(false) - .build(); + public static MemoirResponseDTO.MemoirPreviewDTO toMemoirPreviewDTO(Memoir memoir) { + if (memoir == null) { + return MemoirResponseDTO.MemoirPreviewDTO.builder() + .memoirId(null) + .written(false) + .build(); + } else { + return MemoirResponseDTO.MemoirPreviewDTO.builder() + .memoirId(memoir.getId()) + .written(true) + .build(); + } } - public static MemoirResponseDTO.ResultDTO toResultDTO(Memoir memoir) { - return MemoirResponseDTO.ResultDTO.builder() + public static MemoirResponseDTO.MemoirDTO toMemoirDTO(Memoir memoir) { + return MemoirResponseDTO.MemoirDTO.builder() .memoirId(memoir.getId()) .date(memoir.getDate()) - .icon(memoir.getIcon()) + .emoticonUrl(memoir.getEmoticon().getImageUrl()) .isBookmarked(memoir.getIsBookmarked()) - .memoirAnswerList(toAnswerResultDTOList(memoir.getMemoirAnswerList())) + .memoirAnswerList(toMemoirAnswerDTOList(memoir.getMemoirAnswerList())) .build(); } - public static List toAnswerResultDTOList(List memoirAnswerList) { + public static List toMemoirAnswerDTOList(List memoirAnswerList) { return memoirAnswerList.stream() - .map(memoirAnswer -> MemoirResponseDTO.AnswerResultDTO.builder() + .map(memoirAnswer -> MemoirResponseDTO.MemoirAnswerDTO.builder() .answerId(memoirAnswer.getId()) .question(memoirAnswer.getMemoirQuestion().getQuestion()) .summary(memoirAnswer.getMemoirQuestion().getSummary()) @@ -49,4 +47,47 @@ public static List toAnswerResultDTOList(List .build()) .collect(Collectors.toList()); } + + public static MemoirResponseDTO.BookmarkedMemoirListDTO toBookmarkedMemoirPreviewListDTO(Page memoirList) { + AtomicInteger index = new AtomicInteger(); + + List memoirDTOList = memoirList.stream() + .map(memoir -> MemoirResponseDTO.BookmarkedMemoirDTO.builder() + .memoirId(memoir.getId()) + .date(memoir.getDate()) + .emoticonUrl(memoir.getEmoticon().getImageUrl()) + .remain(index.getAndIncrement() % 2) + .build()) + .toList(); + + return MemoirResponseDTO.BookmarkedMemoirListDTO.builder() + .memoirList(memoirDTOList) + .pageNumber(memoirList.getNumber()) + .pageSize(memoirList.getSize()) + .totalPages(memoirList.getTotalPages()) + .totalElements(memoirList.getTotalElements()) + .isFirst(memoirList.isFirst()) + .isLast(memoirList.isLast()) + .build(); + } + + public static List toMemoirQuestionDTOList(List memoirQuestionList) { + return memoirQuestionList.stream() + .map(memoirQuestion -> MemoirResponseDTO.MemoirQuestionDTO.builder() + .questionId(memoirQuestion.getId()) + .question(memoirQuestion.getQuestion()) + .summary(memoirQuestion.getSummary()) + .build()) + .collect(Collectors.toList()); + } + + public static List toEmoticonDTOList(List emoticonList) { + return emoticonList.stream() + .map(emoticon -> MemoirResponseDTO.EmoticonDTO.builder() + .emoticonId(emoticon.getId()) + .imageUrl(emoticon.getImageUrl()) + .build()) + .collect(Collectors.toList()); + } + } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirRequestDTO.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirRequestDTO.java index c0a2438..3b414ca 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirRequestDTO.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirRequestDTO.java @@ -10,19 +10,18 @@ public class MemoirRequestDTO { @Getter - public static class WriteDTO { - @NotNull - Long userId; + public static class MemoirWriteDTO { @NotNull + @PastOrPresent LocalDate date; - @Size(max = 255) - String icon; - @NotEmpty - List<@Valid WriteAnswerDTO> memoirAnswerList; + @NotNull + Long emoticonId; + @NotNull + List<@Valid MemoirWriteAnswerDTO> memoirAnswerList; } @Getter - public static class WriteAnswerDTO { + public static class MemoirWriteAnswerDTO { @NotNull Long questionId; @Size(max = 500) @@ -30,17 +29,15 @@ public static class WriteAnswerDTO { } @Getter - public static class UpdateDTO { + public static class MemoirUpdateDTO { + @NotNull + Long emoticonId; @NotNull - Long memoirId; - @Size(max = 255) - String icon; - Boolean isBookmarked; - List<@Valid UpdateAnswerDTO> memoirAnswerList; + List<@Valid MemoirUpdateAnswerDTO> memoirAnswerList; } @Getter - public static class UpdateAnswerDTO { + public static class MemoirUpdateAnswerDTO { @NotNull Long answerId; @Size(max = 500) diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirResponseDTO.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirResponseDTO.java index 4d41e90..14b6cb0 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirResponseDTO.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/dto/MemoirResponseDTO.java @@ -14,33 +14,76 @@ public class MemoirResponseDTO { @Getter @NoArgsConstructor @AllArgsConstructor - public static class QuestionResultDTO{ - Long questionId; + public static class MemoirDTO { + Long memoirId; + LocalDate date; + String emoticonUrl; + Boolean isBookmarked; + List memoirAnswerList; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class MemoirAnswerDTO { + Long answerId; String question; String summary; + String answer; } @Builder @Getter @NoArgsConstructor @AllArgsConstructor - public static class ResultDTO { + public static class MemoirPreviewDTO { + Boolean written; + Long memoirId; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class BookmarkedMemoirListDTO { + List memoirList; + Integer pageNumber; + Integer pageSize; + Integer totalPages; + Long totalElements; + Boolean isFirst; + Boolean isLast; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class BookmarkedMemoirDTO { Long memoirId; LocalDate date; - String icon; - Boolean isBookmarked; - List memoirAnswerList; + String emoticonUrl; + Integer remain; } @Builder @Getter @NoArgsConstructor @AllArgsConstructor - public static class AnswerResultDTO{ - Long answerId; + public static class MemoirQuestionDTO { + Long questionId; String question; String summary; - String answer; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class EmoticonDTO { + Long emoticonId; + String imageUrl; } } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Emoticon.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Emoticon.java new file mode 100644 index 0000000..f4de4ec --- /dev/null +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Emoticon.java @@ -0,0 +1,19 @@ +package com.onnoff.onnoff.domain.off.memoir.entity; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class Emoticon { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false, length = 1024) + private String imageUrl; +} diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java index 37f5383..bb45994 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java @@ -22,7 +22,9 @@ public class Memoir extends BaseEntity { private LocalDate date; - private String icon; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "emoticon_id") + private Emoticon emoticon; @Column(columnDefinition = "boolean default false") private Boolean isBookmarked; @@ -34,8 +36,8 @@ public class Memoir extends BaseEntity { @OneToMany(mappedBy = "memoir", cascade = CascadeType.ALL) private List memoirAnswerList = new ArrayList<>(); - public void setIcon(String icon) { - this.icon = icon; + public void setEmoticon(Emoticon emoticon) { + this.emoticon = emoticon; } public void setIsBookmarked(Boolean isBookmarked) { diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/EmoticonRepository.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/EmoticonRepository.java new file mode 100644 index 0000000..dab6a2c --- /dev/null +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/EmoticonRepository.java @@ -0,0 +1,7 @@ +package com.onnoff.onnoff.domain.off.memoir.repository; + +import com.onnoff.onnoff.domain.off.memoir.entity.Emoticon; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface EmoticonRepository extends JpaRepository { +} diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/MemoirRepository.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/MemoirRepository.java index 38bbd69..5fe5ab4 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/MemoirRepository.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/repository/MemoirRepository.java @@ -2,12 +2,17 @@ import com.onnoff.onnoff.domain.off.memoir.entity.Memoir; import com.onnoff.onnoff.domain.user.User; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import java.time.LocalDate; +import java.util.List; import java.util.Optional; public interface MemoirRepository extends JpaRepository { Optional findByUserAndDate(User user, LocalDate date); + + Page findByUserAndIsBookmarkedOrderByDateDesc(User user, Boolean isBookmarked, PageRequest pageRequest); } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirService.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirService.java index 0466a19..2c2d6be 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirService.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirService.java @@ -1,24 +1,32 @@ package com.onnoff.onnoff.domain.off.memoir.service; import com.onnoff.onnoff.domain.off.memoir.dto.MemoirRequestDTO; +import com.onnoff.onnoff.domain.off.memoir.entity.Emoticon; import com.onnoff.onnoff.domain.off.memoir.entity.Memoir; import com.onnoff.onnoff.domain.off.memoir.entity.MemoirQuestion; +import org.springframework.data.domain.Page; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.util.List; public interface MemoirService { - List getMemoirQuestion(Long userId); - @Transactional - Memoir writeMemoir(MemoirRequestDTO.WriteDTO request); + Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request); - Memoir getMemoir(Long userId, LocalDate date); + Memoir getMemoirPreview(LocalDate date); - @Transactional - Memoir updateMemoir(MemoirRequestDTO.UpdateDTO request); + Memoir getMemoir(Long memoirId); - @Transactional - Memoir deleteMemoir(Long memoirId); + Page getBookmarkedMemoir(Integer pageNumber); + + Memoir modifyMemoir(Long memoirId, MemoirRequestDTO.MemoirUpdateDTO request); + + Memoir bookmarkMemoir(Long memoirId); + + Long deleteMemoir(Long memoirId); + + List getMemoirQuestion(); + + List getEmoticon(); } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java index eade991..40c3e1d 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java @@ -2,17 +2,20 @@ import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus; import com.onnoff.onnoff.apiPayload.exception.GeneralException; -import com.onnoff.onnoff.domain.off.memoir.converter.MemoirConverter; +import com.onnoff.onnoff.auth.UserContext; import com.onnoff.onnoff.domain.off.memoir.dto.MemoirRequestDTO; import com.onnoff.onnoff.domain.off.memoir.entity.Memoir; import com.onnoff.onnoff.domain.off.memoir.entity.MemoirAnswer; +import com.onnoff.onnoff.domain.off.memoir.entity.Emoticon; import com.onnoff.onnoff.domain.off.memoir.entity.MemoirQuestion; import com.onnoff.onnoff.domain.off.memoir.repository.MemoirAnswerRepository; +import com.onnoff.onnoff.domain.off.memoir.repository.EmoticonRepository; import com.onnoff.onnoff.domain.off.memoir.repository.MemoirQuestionRepository; import com.onnoff.onnoff.domain.off.memoir.repository.MemoirRepository; import com.onnoff.onnoff.domain.user.User; -import com.onnoff.onnoff.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -28,24 +31,21 @@ public class MemoirServiceImpl implements MemoirService { private final MemoirRepository memoirRepository; private final MemoirAnswerRepository memoirAnswerRepository; private final MemoirQuestionRepository memoirQuestionRepository; - - private final UserRepository userRepository; - - @Override - @Transactional(readOnly = true) - public List getMemoirQuestion(Long userId) { - return memoirQuestionRepository.findAll(); - } + private final EmoticonRepository emoticonRepository; @Override @Transactional - public Memoir writeMemoir(MemoirRequestDTO.WriteDTO request) { - User user = userRepository.findById(request.getUserId()).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); + public Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request) { + User user = UserContext.getUser(); if (memoirRepository.findByUserAndDate(user, request.getDate()).isPresent()) { throw new GeneralException(ErrorStatus.MEMOIR_EXIST); } - Memoir newMemoir = MemoirConverter.toMemoir(request); + Memoir newMemoir = Memoir.builder() + .date(request.getDate()) + .emoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new GeneralException(ErrorStatus.EMOTICON_NOT_FOUND))) + .isBookmarked(false) + .build(); List newMemoirAnswerList = request.getMemoirAnswerList().stream() .map(memoirAnswer -> MemoirAnswer.builder() @@ -63,27 +63,32 @@ public Memoir writeMemoir(MemoirRequestDTO.WriteDTO request) { @Override @Transactional(readOnly = true) - public Memoir getMemoir(Long userId, LocalDate date) { - User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); + public Memoir getMemoirPreview(LocalDate date) { + User user = UserContext.getUser(); return memoirRepository.findByUserAndDate(user, date).orElse(null); } @Override - @Transactional - public Memoir updateMemoir(MemoirRequestDTO.UpdateDTO request) { - Memoir memoir = memoirRepository.findById(request.getMemoirId()).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND)); + @Transactional(readOnly = true) + public Memoir getMemoir(Long memoirId) { + return memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND)); + } - if (request.getIcon() != null) { - memoir.setIcon(request.getIcon()); - } + @Override + @Transactional(readOnly = true) + public Page getBookmarkedMemoir(Integer pageNumber) { + User user = UserContext.getUser(); + return memoirRepository.findByUserAndIsBookmarkedOrderByDateDesc(user, true, PageRequest.of(pageNumber, 10)); + } - if (request.getIsBookmarked() != null) { - memoir.setIsBookmarked(request.getIsBookmarked()); - } + @Override + @Transactional + public Memoir modifyMemoir(Long memoirId, MemoirRequestDTO.MemoirUpdateDTO request) { + Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND)); - List requestMemoirAnswerList = request.getMemoirAnswerList() == null ? new ArrayList<>() : request.getMemoirAnswerList(); + memoir.setEmoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new GeneralException(ErrorStatus.EMOTICON_NOT_FOUND))); - for (MemoirRequestDTO.UpdateAnswerDTO memoirAnswer: requestMemoirAnswerList) { + for (MemoirRequestDTO.MemoirUpdateAnswerDTO memoirAnswer : request.getMemoirAnswerList()) { MemoirAnswer findMemoirAnswer = memoirAnswerRepository.findById(memoirAnswer.getAnswerId()).orElseThrow(() -> new GeneralException(ErrorStatus.ANSWER_NOT_FOUND)); if (findMemoirAnswer.getMemoir() != memoir) { throw new GeneralException(ErrorStatus.ANSWER_BAD_MATCH); @@ -91,14 +96,36 @@ public Memoir updateMemoir(MemoirRequestDTO.UpdateDTO request) { findMemoirAnswer.setAnswer(memoirAnswer.getAnswer()); } - return memoirRepository.save(memoir); + return memoir; + } + + @Override + @Transactional + public Memoir bookmarkMemoir(Long memoirId) { + Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND)); + memoir.setIsBookmarked(memoir.getIsBookmarked().equals(false)); + + return memoir; } @Override @Transactional - public Memoir deleteMemoir(Long memoirId) { + public Long deleteMemoir(Long memoirId) { Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND)); memoirRepository.delete(memoir); - return memoir; + + return memoir.getId(); + } + + @Override + @Transactional(readOnly = true) + public List getMemoirQuestion() { + return memoirQuestionRepository.findAll(); + } + + @Override + @Transactional(readOnly = true) + public List getEmoticon() { + return emoticonRepository.findAll(); } }