Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: 회고 관련 API 수정 #42

Merged
merged 14 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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", "워라벨 피드 내용은 공백일 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -21,42 +22,66 @@ public class MemoirController {

private final MemoirService memoirService;

@GetMapping("/memoir-questions")
@Operation(summary = "회고 질문 조회 API",description = "회고 질문 목록을 조회하는 API입니다. Query String으로 사용자 아이디를 입력해 주세요.")
public ApiResponse<List<MemoirResponseDTO.QuestionResultDTO>> getMemoirQuestion(@RequestParam(name = "userId") Long userId){
List<MemoirQuestion> memoirQuestionList = memoirService.getMemoirQuestion(userId);
return ApiResponse.onSuccess(MemoirConverter.toQuestionResultDTOList(memoirQuestionList));
}

@PostMapping("/memoirs")
@Operation(summary = "회고 작성 API",description = "새로운 회고를 작성하는 API입니다.")
public ApiResponse<MemoirResponseDTO.ResultDTO> writeMemoir(@RequestBody @Valid MemoirRequestDTO.WriteDTO request){
@Operation(summary = "회고 작성 API", description = "새로운 회고를 작성하는 API입니다.")
public ApiResponse<MemoirResponseDTO.MemoirResultDTO> writeMemoir(@RequestBody @Valid MemoirRequestDTO.MemoirWriteDTO request) {
Memoir memoir = memoirService.writeMemoir(request);
return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir));
return ApiResponse.onSuccess(MemoirConverter.toMemoirResultDTO(memoir));
}

@GetMapping("/memoirs")
@Operation(summary = "회고 조회 API",description = "특정한 날짜의 회고를 조회하는 API입니다. Query String으로 사용자 아이디와 날짜를 입력해 주세요.")
public ApiResponse<MemoirResponseDTO.ResultDTO> 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<MemoirResponseDTO.MemoirPreviewResultDTO> getMemoirPreview(@RequestParam(name = "date") LocalDate date) {
Memoir memoir = memoirService.getMemoirPreview(date);
return ApiResponse.onSuccess(MemoirConverter.toMemoirPreviewResultDTO(memoir));
}

if (memoir == null) {
return ApiResponse.onSuccess(null);
}
return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir));
@GetMapping("/memoirs/{memoirId}")
@Operation(summary = "회고 조회 API", description = "특정 회고를 조회하는 API입니다.")
public ApiResponse<MemoirResponseDTO.MemoirResultDTO> getMemoir(@PathVariable(name = "memoirId") Long memoirId) {
Memoir memoir = memoirService.getMemoir(memoirId);
return ApiResponse.onSuccess(MemoirConverter.toMemoirResultDTO(memoir));
}

@PatchMapping("/memoirs")
@Operation(summary = "회고 수정 API",description = "기존의 회고를 수정하는 API입니다.")
public ApiResponse<MemoirResponseDTO.ResultDTO> 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입니다.")
public ApiResponse<List<MemoirResponseDTO.BookmarkedMemoirResultDTO>> getBookmarkedMemoir() {
List<Memoir> memoirList = memoirService.getBookmarkedMemoir();
return ApiResponse.onSuccess(MemoirConverter.toBookmarkedMemoirResultDTOList(memoirList));
}

@PatchMapping("/memoirs/{memoirId}")
@Operation(summary = "회고 내용 수정 API", description = "기존의 회고 내용을 수정하는 API입니다.")
public ApiResponse<MemoirResponseDTO.MemoirResultDTO> modifyMemoir(@PathVariable(name = "memoirId") Long memoirId,
@RequestBody @Valid MemoirRequestDTO.MemoirUpdateDTO request) {
Memoir memoir = memoirService.modifyMemoir(memoirId, request);
return ApiResponse.onSuccess(MemoirConverter.toMemoirResultDTO(memoir));
}

@PatchMapping("/memoirs/{memoirId}/bookmark")
@Operation(summary = "회고 북마크 및 해제 API", description = "회고를 북마크하거나 북마크 해제하는 API입니다.")
public ApiResponse<MemoirResponseDTO.MemoirResultDTO> bookmarkMemoir(@PathVariable(name = "memoirId") Long memoirId) {
Memoir memoir = memoirService.bookmarkMemoir(memoirId);
return ApiResponse.onSuccess(MemoirConverter.toMemoirResultDTO(memoir));
}

@DeleteMapping("/memoirs/{memoirId}")
@Operation(summary = "회고 삭제 API",description = "기존의 회고를 삭제하는 API입니다.")
public ApiResponse<MemoirResponseDTO.ResultDTO> deleteMemoir(@PathVariable(name = "memoirId") Long memoirId){
Memoir memoir = memoirService.deleteMemoir(memoirId);
return ApiResponse.onSuccess(MemoirConverter.toResultDTO(memoir));
@Operation(summary = "회고 삭제 API", description = "기존의 회고를 삭제하는 API입니다.")
public ApiResponse<Long> deleteMemoir(@PathVariable(name = "memoirId") Long memoirId) {
return ApiResponse.onSuccess(memoirService.deleteMemoir(memoirId));
}

@GetMapping("/memoir-questions")
@Operation(summary = "회고 질문 조회 API", description = "회고 질문 목록을 조회하는 API입니다.")
public ApiResponse<List<MemoirResponseDTO.MemoirQuestionResultDTO>> getMemoirQuestion() {
List<MemoirQuestion> memoirQuestionList = memoirService.getMemoirQuestion();
return ApiResponse.onSuccess(MemoirConverter.toMemoirQuestionResultDTOList(memoirQuestionList));
}

@GetMapping("/emoticons")
@Operation(summary = "이모티콘 조회 API", description = "이모티콘 목록을 조회하는 API입니다.")
public ApiResponse<List<MemoirResponseDTO.EmoticonResultDTO>> getEmoticon() {
List<Emoticon> emoticonList = memoirService.getEmoticon();
return ApiResponse.onSuccess(MemoirConverter.toEmoticonResultDTOList(emoticonList));
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,84 @@
package com.onnoff.onnoff.domain.off.memoir.converter;

import com.onnoff.onnoff.apiPayload.ApiResponse;
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 java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class MemoirConverter {

public static List<MemoirResponseDTO.QuestionResultDTO> toQuestionResultDTOList(List<MemoirQuestion> 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.MemoirPreviewResultDTO toMemoirPreviewResultDTO(Memoir memoir) {
if (memoir == null) {
return MemoirResponseDTO.MemoirPreviewResultDTO.builder()
.memoirId(null)
.written(false)
.build();
} else {
return MemoirResponseDTO.MemoirPreviewResultDTO.builder()
.memoirId(memoir.getId())
.written(true)
.build();
}
}

public static MemoirResponseDTO.ResultDTO toResultDTO(Memoir memoir) {
return MemoirResponseDTO.ResultDTO.builder()
public static MemoirResponseDTO.MemoirResultDTO toMemoirResultDTO(Memoir memoir) {
return MemoirResponseDTO.MemoirResultDTO.builder()
.memoirId(memoir.getId())
.date(memoir.getDate())
.icon(memoir.getIcon())
.emoticonUrl(memoir.getEmoticon().getImageUrl())
.isBookmarked(memoir.getIsBookmarked())
.memoirAnswerList(toAnswerResultDTOList(memoir.getMemoirAnswerList()))
.memoirAnswerList(toMemoirAnswerResultDTOList(memoir.getMemoirAnswerList()))
.build();
}

public static List<MemoirResponseDTO.AnswerResultDTO> toAnswerResultDTOList(List<MemoirAnswer> memoirAnswerList) {
public static List<MemoirResponseDTO.MemoirAnswerResultDTO> toMemoirAnswerResultDTOList(List<MemoirAnswer> memoirAnswerList) {
return memoirAnswerList.stream()
.map(memoirAnswer -> MemoirResponseDTO.AnswerResultDTO.builder()
.map(memoirAnswer -> MemoirResponseDTO.MemoirAnswerResultDTO.builder()
.answerId(memoirAnswer.getId())
.question(memoirAnswer.getMemoirQuestion().getQuestion())
.summary(memoirAnswer.getMemoirQuestion().getSummary())
.answer(memoirAnswer.getAnswer())
.build())
.collect(Collectors.toList());
}

public static List<MemoirResponseDTO.BookmarkedMemoirResultDTO> toBookmarkedMemoirResultDTOList(List<Memoir> memoirList) {
AtomicInteger index = new AtomicInteger();

return memoirList.stream()
.map(memoir -> MemoirResponseDTO.BookmarkedMemoirResultDTO.builder()
.memoirId(memoir.getId())
.date(memoir.getDate())
.emoticonUrl(memoir.getEmoticon().getImageUrl())
.remain(index.getAndIncrement() % 2)
.build())
.collect(Collectors.toList());
}

public static List<MemoirResponseDTO.MemoirQuestionResultDTO> toMemoirQuestionResultDTOList(List<MemoirQuestion> memoirQuestionList) {
return memoirQuestionList.stream()
.map(memoirQuestion -> MemoirResponseDTO.MemoirQuestionResultDTO.builder()
.questionId(memoirQuestion.getId())
.question(memoirQuestion.getQuestion())
.summary(memoirQuestion.getSummary())
.build())
.collect(Collectors.toList());
}

public static List<MemoirResponseDTO.EmoticonResultDTO> toEmoticonResultDTOList(List<Emoticon> emoticonList) {
return emoticonList.stream()
.map(emoticon -> MemoirResponseDTO.EmoticonResultDTO.builder()
.emoticonId(emoticon.getId())
.imageUrl(emoticon.getImageUrl())
.build())
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,34 @@
public class MemoirRequestDTO {

@Getter
public static class WriteDTO {
@NotNull
Long userId;
public static class MemoirWriteDTO {
@NotNull
@PastOrPresent
LocalDate date;
@Size(max = 255)
String icon;
@NotNull
Long emoticonId;
@NotEmpty
List<@Valid WriteAnswerDTO> memoirAnswerList;
List<@Valid MemoirWriteAnswerDTO> memoirAnswerList;
}

@Getter
public static class WriteAnswerDTO {
public static class MemoirWriteAnswerDTO {
@NotNull
Long questionId;
@Size(max = 500)
String answer;
}

@Getter
public static class UpdateDTO {
public static class MemoirUpdateDTO {
@NotNull
Long memoirId;
@Size(max = 255)
String icon;
Boolean isBookmarked;
List<@Valid UpdateAnswerDTO> memoirAnswerList;
Long emoticonId;
@NotEmpty
List<@Valid MemoirUpdateAnswerDTO> memoirAnswerList;
}

@Getter
public static class UpdateAnswerDTO {
public static class MemoirUpdateAnswerDTO {
@NotNull
Long answerId;
@Size(max = 500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,62 @@ public class MemoirResponseDTO {
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class QuestionResultDTO{
Long questionId;
public static class MemoirResultDTO {
Long memoirId;
LocalDate date;
String emoticonUrl;
Boolean isBookmarked;
List<MemoirAnswerResultDTO> memoirAnswerList;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MemoirAnswerResultDTO {
Long answerId;
String question;
String summary;
String answer;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ResultDTO {
public static class MemoirPreviewResultDTO {
Boolean written;
Long memoirId;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class BookmarkedMemoirResultDTO {
Long memoirId;
LocalDate date;
String icon;
Boolean isBookmarked;
List<AnswerResultDTO> memoirAnswerList;
String emoticonUrl;
Integer remain;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class AnswerResultDTO{
Long answerId;
public static class MemoirQuestionResultDTO {
Long questionId;
String question;
String summary;
String answer;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class EmoticonResultDTO {
Long emoticonId;
String imageUrl;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,8 +36,8 @@ public class Memoir extends BaseEntity {
@OneToMany(mappedBy = "memoir", cascade = CascadeType.ALL)
private List<MemoirAnswer> memoirAnswerList = new ArrayList<>();

public void setIcon(String icon) {
this.icon = icon;
public void setEmoticon(Emoticon emoticon) {
this.emoticon = emoticon;
}

public void setIsBookmarked(Boolean isBookmarked) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Emoticon, Long> {
}
Loading