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 all 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,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;
Expand All @@ -21,42 +23,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.MemoirDTO> 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<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.MemoirPreviewDTO> 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<MemoirResponseDTO.MemoirDTO> 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<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이며, 페이징을 포함합니다. Query String으로 페이지 번호를 입력해 주세요. 0번이 1페이지입니다.")
public ApiResponse<MemoirResponseDTO.BookmarkedMemoirListDTO> getBookmarkedMemoir(@RequestParam(name = "pageNumber") Integer pageNumber) {
Page<Memoir> memoirList = memoirService.getBookmarkedMemoir(pageNumber);
return ApiResponse.onSuccess(MemoirConverter.toBookmarkedMemoirPreviewListDTO(memoirList));
}

@PatchMapping("/memoirs/{memoirId}")
@Operation(summary = "회고 내용 수정 API", description = "기존의 회고 내용을 수정하는 API입니다.")
public ApiResponse<MemoirResponseDTO.MemoirDTO> 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<MemoirResponseDTO.MemoirDTO> 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<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.MemoirQuestionDTO>> getMemoirQuestion() {
List<MemoirQuestion> memoirQuestionList = memoirService.getMemoirQuestion();
return ApiResponse.onSuccess(MemoirConverter.toMemoirQuestionDTOList(memoirQuestionList));
}

@GetMapping("/emoticons")
@Operation(summary = "이모티콘 조회 API", description = "이모티콘 목록을 조회하는 API입니다.")
public ApiResponse<List<MemoirResponseDTO.EmoticonDTO>> getEmoticon() {
List<Emoticon> emoticonList = memoirService.getEmoticon();
return ApiResponse.onSuccess(MemoirConverter.toEmoticonDTOList(emoticonList));
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,93 @@
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<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.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<MemoirResponseDTO.AnswerResultDTO> toAnswerResultDTOList(List<MemoirAnswer> memoirAnswerList) {
public static List<MemoirResponseDTO.MemoirAnswerDTO> toMemoirAnswerDTOList(List<MemoirAnswer> 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())
.answer(memoirAnswer.getAnswer())
.build())
.collect(Collectors.toList());
}

public static MemoirResponseDTO.BookmarkedMemoirListDTO toBookmarkedMemoirPreviewListDTO(Page<Memoir> memoirList) {
AtomicInteger index = new AtomicInteger();

List<MemoirResponseDTO.BookmarkedMemoirDTO> 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<MemoirResponseDTO.MemoirQuestionDTO> toMemoirQuestionDTOList(List<MemoirQuestion> 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<MemoirResponseDTO.EmoticonDTO> toEmoticonDTOList(List<Emoticon> emoticonList) {
return emoticonList.stream()
.map(emoticon -> MemoirResponseDTO.EmoticonDTO.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;
@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)
String answer;
}

@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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemoirAnswerDTO> 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<BookmarkedMemoirDTO> 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<AnswerResultDTO> 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;
}

}
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;
}
Loading
Loading