diff --git a/src/main/java/UMC/campusNote/classSideNote/controller/ClassSideNoteController.java b/src/main/java/UMC/campusNote/classSideNote/controller/ClassSideNoteController.java index 56f4b1e..6b7195b 100644 --- a/src/main/java/UMC/campusNote/classSideNote/controller/ClassSideNoteController.java +++ b/src/main/java/UMC/campusNote/classSideNote/controller/ClassSideNoteController.java @@ -1,13 +1,14 @@ package UMC.campusNote.classSideNote.controller; -import UMC.campusNote.classSideNote.dto.ClassSideNoteRequest; -import UMC.campusNote.classSideNote.dto.ClassSideNoteResponse; +import UMC.campusNote.classSideNote.converter.ClassSideNoteConverter; +import UMC.campusNote.classSideNote.dto.ClassSideNoteRequestDTO.ClassSideNoteCreateDTO; +import UMC.campusNote.classSideNote.dto.ClassSideNoteRequestDTO.ClassSideNoteUpdateDTO; +import UMC.campusNote.classSideNote.dto.ClassSideNoteResponseDTO.ClassSideNoteResultDTO; import UMC.campusNote.classSideNote.entity.ClassSideNote; import UMC.campusNote.classSideNote.service.ClassSideNoteService; import UMC.campusNote.common.ApiResponse; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import lombok.AllArgsConstructor; @@ -29,91 +30,65 @@ public class ClassSideNoteController { ClassSideNoteService classSideNoteService; + ClassSideNoteConverter classSideNoteConverter; @PostMapping("/create/") - public ApiResponse createClassSideNote( + public ApiResponse createClassSideNote( @RequestParam Long userLessonId, - @RequestBody ClassSideNoteRequest request) { - ClassSideNote classSideNote = classSideNoteService.createClassSideNote(userLessonId, request); - - ClassSideNoteResponse response = convert(classSideNote); + @RequestBody ClassSideNoteCreateDTO request) { + ClassSideNoteResultDTO response = classSideNoteService.createClassSideNote(userLessonId, request); return ApiResponse.onSuccess(response); } @GetMapping("/{classSideNoteId}") - public ApiResponse getClassSideNoteById( + public ApiResponse getClassSideNoteById( @PathVariable Long classSideNoteId) { - ClassSideNote classSideNote = classSideNoteService.getClassSideNoteById(classSideNoteId); - - ClassSideNoteResponse response = convert(classSideNote); + ClassSideNoteResultDTO response = classSideNoteService.getClassSideNoteById(classSideNoteId); return ApiResponse.onSuccess(response); - } @GetMapping("/{userLessonId}/list") - public ApiResponse> getClassSideNoteListByUserLessonId( + public ApiResponse> getClassSideNoteListByUserLessonId( @PathVariable("userLessonId") Long userLessonId ) { - List classSideNotes = + List classSideNoteList = classSideNoteService.getClassSideNoteListByUserLessonId(userLessonId); - List classSideNoteResponseList = new ArrayList<>(); - for (ClassSideNote classSideNote : classSideNotes) { - classSideNoteResponseList.add(convert(classSideNote)); - } - return ApiResponse.onSuccess(classSideNoteResponseList); + List classSideNoteResultDTOList = classSideNoteConverter.toClassSideNoteResultDTOList(classSideNoteList); + return ApiResponse.onSuccess(classSideNoteResultDTOList); } @PutMapping("/{classSideNoteId}") - public ApiResponse updateClassSideNote( + public ApiResponse updateClassSideNote( @PathVariable Long classSideNoteId, - @RequestBody ClassSideNoteRequest request) { - ClassSideNote classSideNote = classSideNoteService.updateClassSideNote(classSideNoteId, request); - - ClassSideNoteResponse response = convert(classSideNote); + @RequestBody ClassSideNoteUpdateDTO request) { + ClassSideNoteResultDTO response = classSideNoteService.updateClassSideNote(classSideNoteId, request); return ApiResponse.onSuccess(response); } @PatchMapping("/{classSideNoteId}") - public ApiResponse updateClassSideNoteContent( + public ApiResponse updateClassSideNoteContent( @PathVariable Long classSideNoteId, @RequestParam String content) { - ClassSideNote classSideNote = classSideNoteService.getClassSideNoteById(classSideNoteId); - classSideNote.updateContent(content); - - ClassSideNoteResponse response = convert(classSideNote); + ClassSideNoteResultDTO response = classSideNoteService.updateClassSideNoteContent(classSideNoteId, content); return ApiResponse.onSuccess(response); } @DeleteMapping("/{classSideNoteId}") - public ApiResponse deleteClassSideNoteById( + public ApiResponse deleteClassSideNoteById( @PathVariable Long classSideNoteId) { - ClassSideNote classSideNote = classSideNoteService.getClassSideNoteById(classSideNoteId); + ClassSideNoteResultDTO response = classSideNoteService.getClassSideNoteById(classSideNoteId); classSideNoteService.deleteById(classSideNoteId); - ClassSideNoteResponse response = convert(classSideNote); - return ApiResponse.onSuccess(response); // 삭제된 내용 반환 - - } - private ClassSideNoteResponse convert(ClassSideNote classSideNote) { - return ClassSideNoteResponse.builder() - .id(classSideNote.getId()) - .content(classSideNote.getContent()) - .isTodo(classSideNote.getIsTodo()) - .colorCode(classSideNote.getColorCode()) - .deadline(classSideNote.getDeadline() != null ? - classSideNote.getDeadline().toString() : null) - .build(); - } } diff --git a/src/main/java/UMC/campusNote/classSideNote/converter/ClassSideNoteConverter.java b/src/main/java/UMC/campusNote/classSideNote/converter/ClassSideNoteConverter.java new file mode 100644 index 0000000..7b55aa5 --- /dev/null +++ b/src/main/java/UMC/campusNote/classSideNote/converter/ClassSideNoteConverter.java @@ -0,0 +1,30 @@ +package UMC.campusNote.classSideNote.converter; + +import UMC.campusNote.classSideNote.dto.ClassSideNoteResponseDTO.ClassSideNoteResultDTO; +import UMC.campusNote.classSideNote.entity.ClassSideNote; + +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ClassSideNoteConverter { + public ClassSideNoteResultDTO toClassSideNoteResultDTO(ClassSideNote classSideNote) { + return ClassSideNoteResultDTO.builder() + .id(classSideNote.getId()) + .content(classSideNote.getContent()) + .isTodo(classSideNote.getIsTodo()) + .colorCode(classSideNote.getColorCode()) + .deadline(classSideNote.getDeadline() != null ? + classSideNote.getDeadline().toString() : null) + .build(); + } + + public List toClassSideNoteResultDTOList(List classSideNoteList) { + return classSideNoteList.stream() + .map(this::toClassSideNoteResultDTO) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteRequest.java b/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteRequest.java deleted file mode 100644 index 08cd91a..0000000 --- a/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteRequest.java +++ /dev/null @@ -1,34 +0,0 @@ -package UMC.campusNote.classSideNote.dto; - -import jakarta.validation.constraints.Max; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotBlank; - -import lombok.Builder; -import lombok.Getter; -import lombok.NonNull; -import lombok.Setter; - -import java.time.LocalDate; - - - -@Getter -@Setter -@Builder -public class ClassSideNoteRequest { - - // private Long userLessonId - @NotBlank - private String content; - - @NonNull - private Boolean isTodo; - - @NonNull - @Min(0x000000) - @Max(0xffffff) - private Integer colorCode; - - private LocalDate deadline; -} \ No newline at end of file diff --git a/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteRequestDTO.java b/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteRequestDTO.java new file mode 100644 index 0000000..2fcef16 --- /dev/null +++ b/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteRequestDTO.java @@ -0,0 +1,43 @@ +package UMC.campusNote.classSideNote.dto; + +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; + +import lombok.Builder; +import lombok.Getter; +import lombok.NonNull; +import lombok.Setter; + +import java.time.LocalDate; + + +public class ClassSideNoteRequestDTO { + @Getter + @Setter + @Builder + public static class ClassSideNoteCreateDTO { + @NotBlank + private String content; + + @NonNull + private Boolean isTodo; + + @NonNull + @Min(0x000000) + @Max(0xffffff) + private Integer colorCode; + + private LocalDate deadline; + } + + @Getter + @Setter + @Builder + public static class ClassSideNoteUpdateDTO { + @NonNull + private String content; + + private LocalDate deadline; + } +} \ No newline at end of file diff --git a/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteResponse.java b/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteResponse.java deleted file mode 100644 index cad7744..0000000 --- a/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteResponse.java +++ /dev/null @@ -1,29 +0,0 @@ -package UMC.campusNote.classSideNote.dto; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NonNull; -import lombok.Setter; - -@Getter -@Setter -@Builder -@AllArgsConstructor -public class ClassSideNoteResponse { - - @NonNull - private Long id; - - @NonNull - private String content; - - @NonNull - private Boolean isTodo; - - @NonNull - private Integer colorCode; - - private String deadline; - -} \ No newline at end of file diff --git a/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteResponseDTO.java b/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteResponseDTO.java new file mode 100644 index 0000000..e287543 --- /dev/null +++ b/src/main/java/UMC/campusNote/classSideNote/dto/ClassSideNoteResponseDTO.java @@ -0,0 +1,33 @@ +package UMC.campusNote.classSideNote.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NonNull; +import lombok.Setter; + + +public class ClassSideNoteResponseDTO { + + @Getter + @Setter + @Builder + @AllArgsConstructor + public static class ClassSideNoteResultDTO { + @NonNull + private Long id; + + @NonNull + private String content; + + @NonNull + private Boolean isTodo; + + @NonNull + private Integer colorCode; + + private String deadline; + } + + +} \ No newline at end of file diff --git a/src/main/java/UMC/campusNote/classSideNote/entity/ClassSideNote.java b/src/main/java/UMC/campusNote/classSideNote/entity/ClassSideNote.java index e4d2512..766843d 100644 --- a/src/main/java/UMC/campusNote/classSideNote/entity/ClassSideNote.java +++ b/src/main/java/UMC/campusNote/classSideNote/entity/ClassSideNote.java @@ -1,6 +1,6 @@ package UMC.campusNote.classSideNote.entity; -import UMC.campusNote.classSideNote.dto.ClassSideNoteRequest; +import UMC.campusNote.classSideNote.dto.ClassSideNoteRequestDTO.ClassSideNoteUpdateDTO; import UMC.campusNote.common.BaseEntity; import UMC.campusNote.mapping.UserLesson; import jakarta.persistence.*; @@ -39,15 +39,14 @@ public class ClassSideNote extends BaseEntity { // 할일인 경우에는 deadline 을 받아오고 // 그냥 사이드 노트인 경우에는 deadline이 null 일 수 있다는 생각입니다. - public ClassSideNote updateContent(String content){ + public ClassSideNote updateContent(String content) { this.content = content; return this; } - public ClassSideNote update(ClassSideNoteRequest request) { - content = request.getContent(); - isTodo = request.getIsTodo(); - colorCode = request.getColorCode(); - deadline = request.getDeadline(); + + public ClassSideNote update(ClassSideNoteUpdateDTO request) { + this.content = request.getContent(); + this.deadline = request.getDeadline(); return this; } } diff --git a/src/main/java/UMC/campusNote/classSideNote/service/ClassSideNoteService.java b/src/main/java/UMC/campusNote/classSideNote/service/ClassSideNoteService.java index 664774e..71b0ea4 100644 --- a/src/main/java/UMC/campusNote/classSideNote/service/ClassSideNoteService.java +++ b/src/main/java/UMC/campusNote/classSideNote/service/ClassSideNoteService.java @@ -6,7 +6,10 @@ import static UMC.campusNote.classSideNote.status.ClassSideNoteErrorStatus.CLASS_SIDE_NOTE_NOT_FOUND; import static UMC.campusNote.classSideNote.status.ClassSideNoteErrorStatus.USER_LESSON_NOT_FOUND; -import UMC.campusNote.classSideNote.dto.ClassSideNoteRequest; +import UMC.campusNote.classSideNote.converter.ClassSideNoteConverter; +import UMC.campusNote.classSideNote.dto.ClassSideNoteRequestDTO.ClassSideNoteCreateDTO; +import UMC.campusNote.classSideNote.dto.ClassSideNoteRequestDTO.ClassSideNoteUpdateDTO; +import UMC.campusNote.classSideNote.dto.ClassSideNoteResponseDTO.ClassSideNoteResultDTO; import UMC.campusNote.classSideNote.entity.ClassSideNote; import UMC.campusNote.classSideNote.exception.ClassSideNoteException; import UMC.campusNote.classSideNote.repository.ClassSideNoteRepository; @@ -30,9 +33,10 @@ public class ClassSideNoteService { ClassSideNoteRepository classSideNoteRepository; UserLessonRepository userLessonRepository; + ClassSideNoteConverter classSideNoteConverter; - public ClassSideNote createClassSideNote(Long userLessonId, ClassSideNoteRequest request) { + public ClassSideNoteResultDTO createClassSideNote(Long userLessonId, ClassSideNoteCreateDTO request) { validateUserLesson(userLessonId); UserLesson userLesson = userLessonRepository.findById(userLessonId).orElseThrow(); @@ -48,28 +52,37 @@ public ClassSideNote createClassSideNote(Long userLessonId, ClassSideNoteRequest build(); classSideNoteRepository.save(classSideNote); - return classSideNote; + + return classSideNoteConverter.toClassSideNoteResultDTO(classSideNote); } - public ClassSideNote updateClassSideNote(Long id, ClassSideNoteRequest request) { + public ClassSideNoteResultDTO updateClassSideNote(Long id, ClassSideNoteUpdateDTO request) { validateClassSideNoteId(id); ClassSideNote oldClassSideNote = classSideNoteRepository.findById(id).orElseThrow(); - // 임시로 elseThrow ClassSideNote classSideNote = oldClassSideNote.update(request); validateClassSideNoteId(classSideNote.getId()); - return classSideNote; + return classSideNoteConverter.toClassSideNoteResultDTO(classSideNote); + } + public ClassSideNoteResultDTO updateClassSideNoteContent(Long id, String content){ + validateClassSideNoteId(id); + ClassSideNote oldClassSideNote = classSideNoteRepository.findById(id).orElseThrow(); + + ClassSideNote classSideNote = oldClassSideNote.updateContent(content); + validateClassSideNoteId(classSideNote.getId()); + + return classSideNoteConverter.toClassSideNoteResultDTO(classSideNote); } - public ClassSideNote getClassSideNoteById(Long id) { + public ClassSideNoteResultDTO getClassSideNoteById(Long id) { validateClassSideNoteId(id); ClassSideNote classSideNote = classSideNoteRepository.findById(id).orElseThrow(); // Todo : 검증로직 추가 - return classSideNote; + return classSideNoteConverter.toClassSideNoteResultDTO(classSideNote); } public List getClassSideNoteListByUserLessonId(Long userLessonId) { @@ -120,7 +133,7 @@ private void validateUserLesson(Long userLessonId) { } - private void validateRequest(ClassSideNoteRequest request) { + private void validateRequest(ClassSideNoteCreateDTO request) { if (request == null) { throw new ClassSideNoteException(CLASS_SIDE_NOTE_BAD_REQUEST); }