From 6a672c8324576ff64300cdf554383af6cf049052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:18:11 +0900 Subject: [PATCH 01/62] =?UTF-8?q?feat:=20=EA=B8=B0=EB=B3=B8=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20CRUD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../selab/todo/controller/TodoController.java | 31 +++++++++++++++++++ src/main/java/com/selab/todo/entity/Todo.java | 2 ++ .../com/selab/todo/service/TodoService.java | 30 ++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index bef9fbe..6578bb6 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -1,13 +1,19 @@ package com.selab.todo.controller; import com.selab.todo.dto.request.TodoRegisterRequest; +import com.selab.todo.dto.request.TodoUpdateRequest; import com.selab.todo.dto.response.TodoResponse; import com.selab.todo.service.TodoService; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -27,4 +33,29 @@ public TodoResponse register(@RequestBody TodoRegisterRequest request) { public TodoResponse get(@PathVariable Long id) { return todoService.get(id); } + + // 페이징 조회 --> Request Param + @GetMapping + public Page getAll( + @PageableDefault(page = 0, size = 10) Pageable pageable + ) { + return todoService.getAll(pageable); + } + + // 수정 + @PutMapping("/{id}") // @PatchMapping + public TodoResponse update( + @PathVariable Long id, + @RequestBody TodoUpdateRequest request + ) { + return todoService.update(id, request); + } + + // 삭제 + @DeleteMapping("/{id}") + public void delete( + @PathVariable Long id + ) { + todoService.delete(id); + } } diff --git a/src/main/java/com/selab/todo/entity/Todo.java b/src/main/java/com/selab/todo/entity/Todo.java index 7edcbf3..fec2f59 100644 --- a/src/main/java/com/selab/todo/entity/Todo.java +++ b/src/main/java/com/selab/todo/entity/Todo.java @@ -3,6 +3,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import javax.persistence.Column; import javax.persistence.Entity; @@ -13,6 +14,7 @@ @Entity @Getter +@Setter @Table(name = "todo") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Todo { diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index 21b8f0a..18a21b7 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -1,10 +1,13 @@ package com.selab.todo.service; import com.selab.todo.dto.request.TodoRegisterRequest; +import com.selab.todo.dto.request.TodoUpdateRequest; import com.selab.todo.dto.response.TodoResponse; import com.selab.todo.entity.Todo; import com.selab.todo.repository.TodoRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -43,8 +46,35 @@ public TodoResponse get(Long id) { } // 페이징 조회 + @Transactional(readOnly = true) + public Page getAll(Pageable pageable) { + return todoRepository.findAll(pageable) + .map(todo -> new TodoResponse( + todo.getId(), + todo.getTitle(), + todo.getContent() + )); + } // 수정 + @Transactional + public TodoResponse update(Long id, TodoUpdateRequest request){ + Todo todo = todoRepository.findById(id).get(); + + // 더티체킹 - 영속성 컨텍스트 + todo.setTitle(request.getTitle()); + todo.setContent(request.getContent()); + + return new TodoResponse( + todo.getId(), + todo.getTitle(), + todo.getContent() + ); + } // 삭제 + @Transactional + public void delete(Long id) { + todoRepository.deleteById(id); + } } From daaa823584013c312d9e8b23d444048afb50987a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:31:13 +0900 Subject: [PATCH 02/62] fix: jpa config and webconfig cors --- .../com/selab/todo/common/BaseEntity.java | 21 ++++++++++++++ .../java/com/selab/todo/config/JpaConfig.java | 9 ++++++ .../java/com/selab/todo/config/WebConfig.java | 29 +++++++++++++++++++ .../selab/todo/controller/TodoController.java | 10 +++++-- src/main/java/com/selab/todo/entity/Todo.java | 3 +- 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/selab/todo/common/BaseEntity.java create mode 100644 src/main/java/com/selab/todo/config/JpaConfig.java create mode 100644 src/main/java/com/selab/todo/config/WebConfig.java diff --git a/src/main/java/com/selab/todo/common/BaseEntity.java b/src/main/java/com/selab/todo/common/BaseEntity.java new file mode 100644 index 0000000..ca155c1 --- /dev/null +++ b/src/main/java/com/selab/todo/common/BaseEntity.java @@ -0,0 +1,21 @@ +package com.selab.todo.common; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseEntity { + @CreatedDate + private LocalDateTime createdAt; + + @LastModifiedDate + private LocalDateTime modifiedAt; +} diff --git a/src/main/java/com/selab/todo/config/JpaConfig.java b/src/main/java/com/selab/todo/config/JpaConfig.java new file mode 100644 index 0000000..bca8012 --- /dev/null +++ b/src/main/java/com/selab/todo/config/JpaConfig.java @@ -0,0 +1,9 @@ +package com.selab.todo.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@Configuration +@EnableJpaAuditing +public class JpaConfig { +} diff --git a/src/main/java/com/selab/todo/config/WebConfig.java b/src/main/java/com/selab/todo/config/WebConfig.java new file mode 100644 index 0000000..d14b427 --- /dev/null +++ b/src/main/java/com/selab/todo/config/WebConfig.java @@ -0,0 +1,29 @@ +package com.selab.todo.config; + +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +// CORS --> 가장 많이 발생 +@Configuration +@RequiredArgsConstructor +public class WebConfig implements WebMvcConfigurer { + @Bean + public UrlBasedCorsConfigurationSource corsConfigurationSource() { + var corsConfig = new CorsConfiguration(); + + corsConfig.addAllowedOriginPattern(CorsConfiguration.ALL); + corsConfig.addAllowedHeader(CorsConfiguration.ALL); + corsConfig.addAllowedMethod(CorsConfiguration.ALL); + + corsConfig.setAllowCredentials(true); + corsConfig.setMaxAge(3600L); + + var corsConfigSource = new UrlBasedCorsConfigurationSource(); + corsConfigSource.registerCorsConfiguration("/**", corsConfig); + return corsConfigSource; + } +} diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 6578bb6..80297b5 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -8,6 +8,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController @@ -24,17 +26,19 @@ public class TodoController { private final TodoService todoService; + @ResponseStatus(code = HttpStatus.CREATED) @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public TodoResponse register(@RequestBody TodoRegisterRequest request) { return todoService.register(request); } + @ResponseStatus(code = HttpStatus.OK) @GetMapping("/{id}") public TodoResponse get(@PathVariable Long id) { return todoService.get(id); } - // 페이징 조회 --> Request Param + @ResponseStatus(code = HttpStatus.OK) @GetMapping public Page getAll( @PageableDefault(page = 0, size = 10) Pageable pageable @@ -42,7 +46,7 @@ public Page getAll( return todoService.getAll(pageable); } - // 수정 + @ResponseStatus(code = HttpStatus.OK) @PutMapping("/{id}") // @PatchMapping public TodoResponse update( @PathVariable Long id, @@ -51,7 +55,7 @@ public TodoResponse update( return todoService.update(id, request); } - // 삭제 + @ResponseStatus(code = HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void delete( @PathVariable Long id diff --git a/src/main/java/com/selab/todo/entity/Todo.java b/src/main/java/com/selab/todo/entity/Todo.java index fec2f59..093bf24 100644 --- a/src/main/java/com/selab/todo/entity/Todo.java +++ b/src/main/java/com/selab/todo/entity/Todo.java @@ -1,5 +1,6 @@ package com.selab.todo.entity; +import com.selab.todo.common.BaseEntity; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -17,7 +18,7 @@ @Setter @Table(name = "todo") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Todo { +public class Todo extends BaseEntity { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) From e2809904004d009d862b3c6a6bb75288a60763bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:37:05 +0900 Subject: [PATCH 03/62] =?UTF-8?q?fix:=20=EC=9E=90=EC=9B=90=20=EC=97=86?= =?UTF-8?q?=EC=9D=84=EC=8B=9C=20exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/entity/Todo.java | 6 +++++- src/main/java/com/selab/todo/service/TodoService.java | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/selab/todo/entity/Todo.java b/src/main/java/com/selab/todo/entity/Todo.java index 093bf24..ccf561d 100644 --- a/src/main/java/com/selab/todo/entity/Todo.java +++ b/src/main/java/com/selab/todo/entity/Todo.java @@ -15,7 +15,6 @@ @Entity @Getter -@Setter @Table(name = "todo") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Todo extends BaseEntity { @@ -34,4 +33,9 @@ public Todo(String title, String content) { this.title = title; this.content = content; } + + public void update(String title, String content) { + this.title = title; + this.content = content; + } } diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index 18a21b7..30023a0 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -36,7 +36,8 @@ public TodoResponse register(TodoRegisterRequest request) { // 단건 조회 @Transactional(readOnly = true) public TodoResponse get(Long id) { - Todo todo = todoRepository.findById(id).get(); + Todo todo = todoRepository.findById(id) + .orElseThrow(() -> new RuntimeException("자원이 없습니다.")); return new TodoResponse( todo.getId(), @@ -58,12 +59,12 @@ public Page getAll(Pageable pageable) { // 수정 @Transactional - public TodoResponse update(Long id, TodoUpdateRequest request){ - Todo todo = todoRepository.findById(id).get(); + public TodoResponse update(Long id, TodoUpdateRequest request) { + Todo todo = todoRepository.findById(id) + .orElseThrow(() -> new RuntimeException("자원이 없습니다.")); // 더티체킹 - 영속성 컨텍스트 - todo.setTitle(request.getTitle()); - todo.setContent(request.getContent()); + todo.update(request.getTitle(), request.getContent()); return new TodoResponse( todo.getId(), From 18f5af403c6a004e74e6f7653737694264277b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:39:50 +0900 Subject: [PATCH 04/62] =?UTF-8?q?fix:=20=EC=82=BD=EC=9E=85,=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C,=20=EC=88=98=EC=A0=95=EC=8B=9C=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/service/TodoService.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index 30023a0..ea6605c 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -6,11 +6,13 @@ import com.selab.todo.entity.Todo; import com.selab.todo.repository.TodoRepository; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +@Slf4j @Service @RequiredArgsConstructor public class TodoService { @@ -26,6 +28,8 @@ public TodoResponse register(TodoRegisterRequest request) { Todo savedTodo = todoRepository.save(todo); + log.info("todo 등록했습니다. {}", todo.getId()); + return new TodoResponse( savedTodo.getId(), savedTodo.getTitle(), @@ -66,6 +70,8 @@ public TodoResponse update(Long id, TodoUpdateRequest request) { // 더티체킹 - 영속성 컨텍스트 todo.update(request.getTitle(), request.getContent()); + log.info("todo 수정했습니다. {}", todo.getId()); + return new TodoResponse( todo.getId(), todo.getTitle(), @@ -76,6 +82,9 @@ public TodoResponse update(Long id, TodoUpdateRequest request) { // 삭제 @Transactional public void delete(Long id) { - todoRepository.deleteById(id); + if (todoRepository.existsById(id)) { + todoRepository.deleteById(id); + log.info("todo 삭제했습니다.. {}", id); + } } } From 1b36dafaa1104ff7261f3740684c28a0c2eab1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:42:40 +0900 Subject: [PATCH 05/62] fix: todo --- src/main/java/com/selab/todo/controller/TodoController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 80297b5..95c8569 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -26,6 +26,8 @@ public class TodoController { private final TodoService todoService; + // RestControllerAdvice, ExceptionHandler, ResponseEntity + // TODO : RESPONSE ENTITY, ERROR HANDLING @ResponseStatus(code = HttpStatus.CREATED) @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public TodoResponse register(@RequestBody TodoRegisterRequest request) { From 89ae4617b6ea076be8f34d01e49a944a62f21f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:44:07 +0900 Subject: [PATCH 06/62] fix: todo --- src/main/java/com/selab/todo/controller/TodoController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 95c8569..8d84768 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -40,6 +40,7 @@ public TodoResponse get(@PathVariable Long id) { return todoService.get(id); } + // TODO : sorting -> 생성된 날짜의 내림차순 정렬 @ResponseStatus(code = HttpStatus.OK) @GetMapping public Page getAll( @@ -57,6 +58,7 @@ public TodoResponse update( return todoService.update(id, request); } + // TODO : hard delete, soft delete @ResponseStatus(code = HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void delete( From a57ef58008d4bce59ce14031581fced305a15852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 1 Feb 2023 21:22:31 +0900 Subject: [PATCH 07/62] =?UTF-8?q?feat:=20Response=20wrapping=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/common/dto/PageDto.java | 28 +++++++++++++ .../selab/todo/common/dto/ResponseDto.java | 30 +++++++++++++ .../selab/todo/controller/TodoController.java | 42 +++++++++---------- 3 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/selab/todo/common/dto/PageDto.java create mode 100644 src/main/java/com/selab/todo/common/dto/ResponseDto.java diff --git a/src/main/java/com/selab/todo/common/dto/PageDto.java b/src/main/java/com/selab/todo/common/dto/PageDto.java new file mode 100644 index 0000000..829bec9 --- /dev/null +++ b/src/main/java/com/selab/todo/common/dto/PageDto.java @@ -0,0 +1,28 @@ +package com.selab.todo.common.dto; + +import lombok.Data; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.http.ResponseEntity; + +import java.util.List; + +@Data +@RequiredArgsConstructor +public class PageDto { + private final List data; + private final int page; + private final int size; + private final long totalElements; + + public static ResponseEntity> ok(Page data) { + var response = new PageDto( + data.getContent(), + data.getPageable().getPageNumber(), + data.getPageable().getPageSize(), + data.getTotalElements() + ); + + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/com/selab/todo/common/dto/ResponseDto.java b/src/main/java/com/selab/todo/common/dto/ResponseDto.java new file mode 100644 index 0000000..457ba58 --- /dev/null +++ b/src/main/java/com/selab/todo/common/dto/ResponseDto.java @@ -0,0 +1,30 @@ +package com.selab.todo.common.dto; + +import lombok.Data; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +@Data +@RequiredArgsConstructor +public class ResponseDto { + private final T data; + + public static ResponseEntity> ok(T data) { + var response = new ResponseDto(data); + return ResponseEntity.ok(response); + } + + public static ResponseEntity> created(T data) { + var response = new ResponseDto(data); + return ResponseEntity + .status(HttpStatus.CREATED) + .body(response); + } + + public static ResponseEntity noContent() { + return ResponseEntity + .status(HttpStatus.NO_CONTENT) + .build(); + } +} diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 8d84768..361e21a 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -1,15 +1,16 @@ package com.selab.todo.controller; +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; import com.selab.todo.dto.request.TodoRegisterRequest; import com.selab.todo.dto.request.TodoUpdateRequest; -import com.selab.todo.dto.response.TodoResponse; import com.selab.todo.service.TodoService; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -17,7 +18,6 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController @@ -26,44 +26,40 @@ public class TodoController { private final TodoService todoService; - // RestControllerAdvice, ExceptionHandler, ResponseEntity - // TODO : RESPONSE ENTITY, ERROR HANDLING - @ResponseStatus(code = HttpStatus.CREATED) @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) - public TodoResponse register(@RequestBody TodoRegisterRequest request) { - return todoService.register(request); + public ResponseEntity register(@RequestBody TodoRegisterRequest request) { + var response = todoService.register(request); + return ResponseDto.created(response); } - @ResponseStatus(code = HttpStatus.OK) @GetMapping("/{id}") - public TodoResponse get(@PathVariable Long id) { - return todoService.get(id); + public ResponseEntity get(@PathVariable Long id) { + var response = todoService.get(id); + return ResponseDto.ok(response); } - // TODO : sorting -> 생성된 날짜의 내림차순 정렬 - @ResponseStatus(code = HttpStatus.OK) @GetMapping - public Page getAll( - @PageableDefault(page = 0, size = 10) Pageable pageable + public ResponseEntity getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable ) { - return todoService.getAll(pageable); + var response = todoService.getAll(pageable); + return PageDto.ok(response); } - @ResponseStatus(code = HttpStatus.OK) @PutMapping("/{id}") // @PatchMapping - public TodoResponse update( + public ResponseEntity update( @PathVariable Long id, @RequestBody TodoUpdateRequest request ) { - return todoService.update(id, request); + var response = todoService.update(id, request); + return ResponseDto.ok(response); } - // TODO : hard delete, soft delete - @ResponseStatus(code = HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") - public void delete( + public ResponseEntity delete( @PathVariable Long id ) { todoService.delete(id); + return ResponseDto.noContent(); } } From 6bf134060d8ae975e1d532979cb9e45b561c182e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 1 Feb 2023 21:26:00 +0900 Subject: [PATCH 08/62] =?UTF-8?q?=EC=A0=95=EC=A0=81=20=ED=8E=99=ED=86=A0?= =?UTF-8?q?=EB=A6=AC=20=EB=A7=A4=EC=84=9C=EB=93=9C=20=ED=8C=A8=ED=84=B4=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../selab/todo/dto/response/TodoResponse.java | 9 +++++++ .../com/selab/todo/service/TodoService.java | 24 ++++--------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/selab/todo/dto/response/TodoResponse.java b/src/main/java/com/selab/todo/dto/response/TodoResponse.java index 8b0f0be..f78d269 100644 --- a/src/main/java/com/selab/todo/dto/response/TodoResponse.java +++ b/src/main/java/com/selab/todo/dto/response/TodoResponse.java @@ -1,5 +1,6 @@ package com.selab.todo.dto.response; +import com.selab.todo.entity.Todo; import lombok.Data; @Data @@ -7,4 +8,12 @@ public class TodoResponse { private final Long id; private final String title; private final String content; + + public static TodoResponse from(Todo todo) { + return new TodoResponse( + todo.getId(), + todo.getTitle(), + todo.getContent() + ); + } } diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index ea6605c..a4dafa0 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -30,11 +30,7 @@ public TodoResponse register(TodoRegisterRequest request) { log.info("todo 등록했습니다. {}", todo.getId()); - return new TodoResponse( - savedTodo.getId(), - savedTodo.getTitle(), - savedTodo.getContent() - ); + return TodoResponse.from(savedTodo); } // 단건 조회 @@ -43,22 +39,14 @@ public TodoResponse get(Long id) { Todo todo = todoRepository.findById(id) .orElseThrow(() -> new RuntimeException("자원이 없습니다.")); - return new TodoResponse( - todo.getId(), - todo.getTitle(), - todo.getContent() - ); + return TodoResponse.from(todo); } // 페이징 조회 @Transactional(readOnly = true) public Page getAll(Pageable pageable) { return todoRepository.findAll(pageable) - .map(todo -> new TodoResponse( - todo.getId(), - todo.getTitle(), - todo.getContent() - )); + .map(TodoResponse::from); } // 수정 @@ -72,11 +60,7 @@ public TodoResponse update(Long id, TodoUpdateRequest request) { log.info("todo 수정했습니다. {}", todo.getId()); - return new TodoResponse( - todo.getId(), - todo.getTitle(), - todo.getContent() - ); + return TodoResponse.from(todo); } // 삭제 From f3e4dc0eb6efc9ab292121a8a036686370b40d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 1 Feb 2023 21:33:03 +0900 Subject: [PATCH 09/62] =?UTF-8?q?exception=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todo/exception/BusinessException.java | 23 ++++++++++ .../com/selab/todo/exception/ErrorDto.java | 30 +++++++++++++ .../selab/todo/exception/ErrorMessage.java | 26 +++++++++++ .../exception/GlobalExceptionHandler.java | 44 +++++++++++++++++++ .../selab/todo/exception/TodoException.java | 7 +++ .../com/selab/todo/service/TodoService.java | 5 ++- 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/selab/todo/exception/BusinessException.java create mode 100644 src/main/java/com/selab/todo/exception/ErrorDto.java create mode 100644 src/main/java/com/selab/todo/exception/ErrorMessage.java create mode 100644 src/main/java/com/selab/todo/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/selab/todo/exception/TodoException.java diff --git a/src/main/java/com/selab/todo/exception/BusinessException.java b/src/main/java/com/selab/todo/exception/BusinessException.java new file mode 100644 index 0000000..37b354b --- /dev/null +++ b/src/main/java/com/selab/todo/exception/BusinessException.java @@ -0,0 +1,23 @@ +package com.selab.todo.exception; + +import lombok.Getter; + +@Getter +public abstract class BusinessException extends RuntimeException { + private final ErrorMessage errorMessage; + + public BusinessException(ErrorMessage message) { + super(message.getDescription()); + this.errorMessage = message; + } + + public BusinessException(ErrorMessage message, String reason) { + super(reason); + this.errorMessage = message; + } + + public BusinessException(String reason) { + super(reason); + this.errorMessage = ErrorMessage.CONFLICT_ERROR; + } +} diff --git a/src/main/java/com/selab/todo/exception/ErrorDto.java b/src/main/java/com/selab/todo/exception/ErrorDto.java new file mode 100644 index 0000000..f11f137 --- /dev/null +++ b/src/main/java/com/selab/todo/exception/ErrorDto.java @@ -0,0 +1,30 @@ +package com.selab.todo.exception; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class ErrorDto implements Serializable { + private final String name; + private final String message; + private final String reason; + + public ErrorDto(ErrorMessage message, String reason) { + this.name = message.name(); + this.message = message.getDescription(); + this.reason = reason; + } + + public ErrorDto(ErrorMessage message) { + this.name = message.name(); + this.message = message.getDescription(); + this.reason = ""; + } + + public ErrorDto(String name, String message) { + this.name = name; + this.message = message; + this.reason = ""; + } +} diff --git a/src/main/java/com/selab/todo/exception/ErrorMessage.java b/src/main/java/com/selab/todo/exception/ErrorMessage.java new file mode 100644 index 0000000..f3ecdcd --- /dev/null +++ b/src/main/java/com/selab/todo/exception/ErrorMessage.java @@ -0,0 +1,26 @@ +package com.selab.todo.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum ErrorMessage { + /** + * Server Error Message + */ + CONFLICT_ERROR(HttpStatus.BAD_REQUEST, "예기치 못한 에러가 발생했습니다."), + INTERNAL_SERVER_ERROR_BY_MAPPER_ERROR(HttpStatus.BAD_REQUEST, "예기치 못한 에러가 발생했습니다."), + INTERNAL_SERVER_ERROR_BY_PROPERTIES_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "예기치 못한 에러가 발생했습니다."), + INVALID_REQUEST_PARMAETER_ERROR(HttpStatus.BAD_REQUEST, "잘못된 요청 입니다."), + + /** + * Todo Error Message + */ + TODO_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "Todo 정보를 찾을 수 없습니다."), + ; + + private final HttpStatus status; + private final String description; +} diff --git a/src/main/java/com/selab/todo/exception/GlobalExceptionHandler.java b/src/main/java/com/selab/todo/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..62f52a8 --- /dev/null +++ b/src/main/java/com/selab/todo/exception/GlobalExceptionHandler.java @@ -0,0 +1,44 @@ +package com.selab.todo.exception; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(BusinessException.class) + protected ResponseEntity handleBusinessException(final BusinessException e) { + var message = e.getErrorMessage(); + + log.error("[ERROR] BusinessException -> {}", message.getDescription()); + + return ResponseEntity + .status(message.getStatus()) + .body(new ErrorDto(message)); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + protected ResponseEntity handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) { + var message = ErrorMessage.CONFLICT_ERROR; + + log.error("[ERROR] MethodArgumentNotValidException -> {}", e.getBindingResult()); + + return ResponseEntity + .status(message.getStatus()) + .body(new ErrorDto(message, e.getMessage())); + } + + @ExceptionHandler(Exception.class) + protected ResponseEntity handleException(final Exception e) { + var message = ErrorMessage.CONFLICT_ERROR; + + log.error("[ERROR] Exception -> {}", e.getCause().toString()); + + return ResponseEntity + .status(message.getStatus()) + .body(new ErrorDto(message, e.getMessage())); + } +} diff --git a/src/main/java/com/selab/todo/exception/TodoException.java b/src/main/java/com/selab/todo/exception/TodoException.java new file mode 100644 index 0000000..74c2e5c --- /dev/null +++ b/src/main/java/com/selab/todo/exception/TodoException.java @@ -0,0 +1,7 @@ +package com.selab.todo.exception; + +public class TodoException extends BusinessException { + public TodoException() { + super(ErrorMessage.TODO_NOT_FOUND_ERROR); + } +} diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index a4dafa0..3ec52cd 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -4,6 +4,7 @@ import com.selab.todo.dto.request.TodoUpdateRequest; import com.selab.todo.dto.response.TodoResponse; import com.selab.todo.entity.Todo; +import com.selab.todo.exception.TodoException; import com.selab.todo.repository.TodoRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -37,7 +38,7 @@ public TodoResponse register(TodoRegisterRequest request) { @Transactional(readOnly = true) public TodoResponse get(Long id) { Todo todo = todoRepository.findById(id) - .orElseThrow(() -> new RuntimeException("자원이 없습니다.")); + .orElseThrow(TodoException::new); return TodoResponse.from(todo); } @@ -53,7 +54,7 @@ public Page getAll(Pageable pageable) { @Transactional public TodoResponse update(Long id, TodoUpdateRequest request) { Todo todo = todoRepository.findById(id) - .orElseThrow(() -> new RuntimeException("자원이 없습니다.")); + .orElseThrow(TodoException::new); // 더티체킹 - 영속성 컨텍스트 todo.update(request.getTitle(), request.getContent()); From b7db802ef3f9e8800019db830067ad171bfe74b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B3=E1=86=A8=E1=84=85=E1=85=A1=E1=86=A8?= =?UTF-8?q?=E1=84=8F=E1=85=A9=E1=84=83=E1=85=B5=E1=86=BC?= <50691225+DongGeon0908@users.noreply.github.com> Date: Wed, 1 Feb 2023 21:42:38 +0900 Subject: [PATCH 10/62] =?UTF-8?q?todo=20swagger=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 ++ .../com/selab/todo/config/SwaggerConfig.java | 39 +++++++++++++++++++ .../selab/todo/controller/TodoController.java | 8 ++++ src/main/resources/application.yml | 3 ++ 4 files changed, 54 insertions(+) create mode 100644 src/main/java/com/selab/todo/config/SwaggerConfig.java diff --git a/build.gradle b/build.gradle index 067172e..14abd0f 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,10 @@ dependencies { annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' + + // swagger + implementation "io.springfox:springfox-boot-starter:3.0.0" + } tasks.named('test') { diff --git a/src/main/java/com/selab/todo/config/SwaggerConfig.java b/src/main/java/com/selab/todo/config/SwaggerConfig.java new file mode 100644 index 0000000..78f1e8e --- /dev/null +++ b/src/main/java/com/selab/todo/config/SwaggerConfig.java @@ -0,0 +1,39 @@ +package com.selab.todo.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.oas.annotations.EnableOpenApi; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +import javax.servlet.http.HttpServletRequest; + +@Configuration +@EnableOpenApi +public class SwaggerConfig { + @Bean + public Docket restApi() { + return new Docket(DocumentationType.OAS_30) + .ignoredParameterTypes( + HttpServletRequest.class + ) + .useDefaultResponseMessages(false) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.selab.todo")) + .paths(PathSelectors.regex("/api/.*")) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("Selab Todo api info") + .description("SE TODO API") + .version("1.0.0") + .build(); + } +} diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 361e21a..7f856eb 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -5,6 +5,8 @@ import com.selab.todo.dto.request.TodoRegisterRequest; import com.selab.todo.dto.request.TodoUpdateRequest; import com.selab.todo.service.TodoService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -20,24 +22,28 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +@Api(tags = {"TODO API"}) @RestController @RequestMapping(value = "/api/v1/todos", produces = MediaType.APPLICATION_JSON_VALUE) @RequiredArgsConstructor public class TodoController { private final TodoService todoService; + @ApiOperation(value = "TODO 등록하기") @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity register(@RequestBody TodoRegisterRequest request) { var response = todoService.register(request); return ResponseDto.created(response); } + @ApiOperation(value = "TODO 단건 조회하기") @GetMapping("/{id}") public ResponseEntity get(@PathVariable Long id) { var response = todoService.get(id); return ResponseDto.ok(response); } + @ApiOperation(value = "TODO 전체 조회하기") @GetMapping public ResponseEntity getAll( @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable @@ -46,6 +52,7 @@ public ResponseEntity getAll( return PageDto.ok(response); } + @ApiOperation(value = "TODO 수정하기") @PutMapping("/{id}") // @PatchMapping public ResponseEntity update( @PathVariable Long id, @@ -55,6 +62,7 @@ public ResponseEntity update( return ResponseDto.ok(response); } + @ApiOperation(value = "TODO 삭제하기") @DeleteMapping("/{id}") public ResponseEntity delete( @PathVariable Long id diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 97c2378..349d01c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -15,3 +15,6 @@ spring: open-in-view: false hibernate: ddl-auto: create-drop + mvc: + pathmatch: + matching-strategy: ant_path_matcher From 7da10b1bdc56e5664d9d3d458e381e1ab1863ad4 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Thu, 2 Feb 2023 11:11:34 +0900 Subject: [PATCH 11/62] =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EC=97=B0=EA=B2=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 349d01c..d27265d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,10 +4,10 @@ spring: max-file-size: 10MB max-request-size: 10MB datasource: - url: jdbc:mysql://localhost:3306/se_todo?useUnicode=true&charset=utf8mb4&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Seoul&useSSL=false + url: jdbc:mysql://localhost:3306/todo?useUnicode=true&charset=utf8mb4&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Seoul&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root - password: + password: kk020206** hikari: minimum-idle: 10 maximum-pool-size: 20 From 3ec64f8d2a3a076f51fa3f25633abdf1ef99efc0 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Thu, 2 Feb 2023 11:27:24 +0900 Subject: [PATCH 12/62] =?UTF-8?q?fix=20:=20=EC=9A=94=EA=B5=AC=EB=AA=85?= =?UTF-8?q?=EC=84=B8=EC=84=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/README.md b/README.md index 6261d5b..49f3320 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,61 @@ # SELAB-TODO > basic todo with spring-mvc + +# 요구명세서 +> 일기 작성 서비스 + +## 목차 +1. 소개 + 1. 목적 +2. 일반적인 기술 사항 + 1. 제품의 기능 + 2. 사용자 특성 + 3. 제약사항 + 4. 가정 및 의존성 +3. 상세기능 요구 사항 + 1. 기능적 요구 사항 + 1. 기본적인 CRUD + 1. 등록 + 2. 조회 + 3. 수정 + 4. 삭제 + +## 소개 +### 1.i 목적 +동건이형께 Todo 스터디를 완료했으므로 사이드 프로젝트를 진행하기 위하여 시작함 +## 일반적인 기술 사항 +### 2. i 제품의 기능 +앱은 아래와 같은 기능들을 주요 기능으로 갖는다 +* 단건 조회 +* 범위 조회 +* 전체 조회 +* 수정 +* 등록 +* 삭제 +* 전체 삭제 +### 2.ii 사용자 특정 +사용자는 일기를 생성할 수 있어야 한다. 생성은 Json 파일 형식으로 해야 한다 + + +사용자는 일기 조회를 할 수 있어야 한다. 단건 조회는 ID를 입력했을 때 가능하며, 범위 조회는 +월(Month)를 입력했을 때 가능해야 한다 + + +사용자는 일기 수정이 가능해야 한다. 수정하는 것은 ID로 수정할 수 있도록 한다. + + +사용자는 일기 삭제가 가능해야 한다. 삭제는 ID를 통해 이루어지도록 한다. +### 2.iii 제약사항 +* spring framework를 사용하여 구현한다 +* MySQL과 Jpa를 사용하여 데이터베이스와 연결한다 +* create-drop을 사용하기 때문에 데이터베이스에 데이터베이스 스키마를 만들지 않아도 된다 + +## 상세기능 요구 사항 +### 3.i. 기능적 요구 사항 +### 3.i.a 기본적인 CRUD +### 3.i.a.a 등록 +### 3.i.a.b 조회 +### 3.i.a.c 수정 +### 3.i.a.d 삭제 + + From fe7c7d45479bda9a5f9245721866d0c4a46ee970 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Thu, 2 Feb 2023 11:32:28 +0900 Subject: [PATCH 13/62] fix : request, response --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 49f3320..393d12e 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,19 @@ ### 3.i. 기능적 요구 사항 ### 3.i.a 기본적인 CRUD ### 3.i.a.a 등록 +* request : id, 등록 날짜, 제목, 내용 ### 3.i.a.b 조회 +#### 단건 조회 +* request : id +#### 범위 조회 +* request : Month +* response : page +#### 전체 조회 +* request : none +* response : page ### 3.i.a.c 수정 +* request : id ### 3.i.a.d 삭제 +* request : none From 058728f53688c5ced4f6d2e15c4cb019cbb8499c Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Fri, 3 Feb 2023 12:32:08 +0900 Subject: [PATCH 14/62] fix : Range Search --- build.gradle | 3 ++- .../selab/todo/controller/TodoController.java | 10 ++++++++ .../selab/todo/dto/response/TodoResponse.java | 6 ++++- src/main/java/com/selab/todo/entity/Todo.java | 12 ++++++++++ .../selab/todo/repository/TodoRepository.java | 3 ++- .../com/selab/todo/service/TodoService.java | 23 ++++++++++++++++++- .../selab/todo/SelabTodoApplicationTests.java | 14 +++++++++-- 7 files changed, 65 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 14abd0f..b3e5220 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { group = 'com.selab' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '11' +sourceCompatibility = "11" configurations { compileOnly { @@ -38,3 +38,4 @@ dependencies { tasks.named('test') { useJUnitPlatform() } +targetCompatibility = JavaVersion.VERSION_16 diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 7f856eb..83ffa2b 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -43,6 +43,16 @@ public ResponseEntity get(@PathVariable Long id) { return ResponseDto.ok(response); } + @ApiOperation(value = "TODO 범위 조회") + @GetMapping("/month/{month}") + public ResponseEntity getRange( + @PathVariable int month, + @PageableDefault Pageable pageable + ) { + var response = todoService.getRange(pageable,month); + return PageDto.ok(response); + } + @ApiOperation(value = "TODO 전체 조회하기") @GetMapping public ResponseEntity getAll( diff --git a/src/main/java/com/selab/todo/dto/response/TodoResponse.java b/src/main/java/com/selab/todo/dto/response/TodoResponse.java index f78d269..2970e1b 100644 --- a/src/main/java/com/selab/todo/dto/response/TodoResponse.java +++ b/src/main/java/com/selab/todo/dto/response/TodoResponse.java @@ -3,17 +3,21 @@ import com.selab.todo.entity.Todo; import lombok.Data; +import java.time.LocalDateTime; + @Data public class TodoResponse { private final Long id; private final String title; private final String content; + private final LocalDateTime localDateTime; public static TodoResponse from(Todo todo) { return new TodoResponse( todo.getId(), todo.getTitle(), - todo.getContent() + todo.getContent(), + todo.getCreatedAt() ); } } diff --git a/src/main/java/com/selab/todo/entity/Todo.java b/src/main/java/com/selab/todo/entity/Todo.java index ccf561d..a8aa717 100644 --- a/src/main/java/com/selab/todo/entity/Todo.java +++ b/src/main/java/com/selab/todo/entity/Todo.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.springframework.data.annotation.CreatedDate; import javax.persistence.Column; import javax.persistence.Entity; @@ -12,6 +13,8 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; +import java.time.LocalDateTime; +import java.time.Month; @Entity @Getter @@ -29,6 +32,11 @@ public class Todo extends BaseEntity { @Column(name = "content") private String content; + @Column(name="createdAt") + @CreatedDate + private LocalDateTime createdAt; + + public Todo(String title, String content) { this.title = title; this.content = content; @@ -38,4 +46,8 @@ public void update(String title, String content) { this.title = title; this.content = content; } + + public int getCreatedMonth(){ + return createdAt.getMonthValue(); + } } diff --git a/src/main/java/com/selab/todo/repository/TodoRepository.java b/src/main/java/com/selab/todo/repository/TodoRepository.java index c8f7e15..ef24a18 100644 --- a/src/main/java/com/selab/todo/repository/TodoRepository.java +++ b/src/main/java/com/selab/todo/repository/TodoRepository.java @@ -1,10 +1,11 @@ package com.selab.todo.repository; +import com.selab.todo.dto.response.TodoResponse; import com.selab.todo.entity.Todo; +import org.springframework.data.domain.Page; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface TodoRepository extends JpaRepository { - } diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index 3ec52cd..026cfe2 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -9,10 +9,18 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + + @Slf4j @Service @RequiredArgsConstructor @@ -39,17 +47,30 @@ public TodoResponse register(TodoRegisterRequest request) { public TodoResponse get(Long id) { Todo todo = todoRepository.findById(id) .orElseThrow(TodoException::new); - + log.info("todo 조회했습니다, {}", todo.getId()); return TodoResponse.from(todo); } // 페이징 조회 @Transactional(readOnly = true) public Page getAll(Pageable pageable) { + log.info("todo 전체 조회"); return todoRepository.findAll(pageable) .map(TodoResponse::from); } + //범위 조회 + @Transactional(readOnly = true) + public Page getRange(Pageable pageable, int month) { + log.info("Todo 범위 조회"); + Page allPage = todoRepository.findAll(pageable).map(TodoResponse::from); + List middleProcess = allPage.stream().filter(a->a.getLocalDateTime().getMonth().getValue()==month).collect(Collectors.toList()); + PageRequest pageRequest = PageRequest.of(0,10); + int start = (int) pageRequest.getOffset(); + int end = Math.min((start+pageRequest.getPageSize()), middleProcess.size()); + return new PageImpl<>(middleProcess.subList(start,end),pageRequest,middleProcess.size()); + } + // 수정 @Transactional public TodoResponse update(Long id, TodoUpdateRequest request) { diff --git a/src/test/java/com/selab/todo/SelabTodoApplicationTests.java b/src/test/java/com/selab/todo/SelabTodoApplicationTests.java index 79c47d1..acbb41a 100644 --- a/src/test/java/com/selab/todo/SelabTodoApplicationTests.java +++ b/src/test/java/com/selab/todo/SelabTodoApplicationTests.java @@ -1,13 +1,23 @@ package com.selab.todo; +import com.selab.todo.dto.response.TodoResponse; +import com.selab.todo.repository.TodoRepository; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; + +import java.util.List; +import java.util.stream.Collectors; @SpringBootTest class SelabTodoApplicationTests { - + TodoRepository todoRepository; @Test - void contextLoads() { + void contextLoads(@PageableDefault Pageable pageable) { + Page page = todoRepository.findAll(pageable).map(TodoResponse::from); + List asd = page.stream().filter(a->a.getLocalDateTime().getMonth().getValue()==2).collect(Collectors.toList()); } } From 06413cf1a71dfa07386afb452a9b29bb6d37c7db Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Fri, 3 Feb 2023 12:45:17 +0900 Subject: [PATCH 15/62] complete : today`s feeling --- src/main/java/com/selab/todo/controller/TodoController.java | 6 +++--- .../com/selab/todo/dto/request/TodoRegisterRequest.java | 1 + .../java/com/selab/todo/dto/request/TodoUpdateRequest.java | 1 + src/main/java/com/selab/todo/dto/response/TodoResponse.java | 4 +++- src/main/java/com/selab/todo/entity/Todo.java | 6 +++++- src/main/java/com/selab/todo/service/TodoService.java | 5 +++-- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 83ffa2b..6c7d574 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -30,9 +30,9 @@ public class TodoController { private final TodoService todoService; @ApiOperation(value = "TODO 등록하기") - @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity register(@RequestBody TodoRegisterRequest request) { - var response = todoService.register(request); + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/{feel}") + public ResponseEntity register(@RequestBody TodoRegisterRequest request, @PathVariable String feel) { + var response = todoService.register(request, feel); return ResponseDto.created(response); } diff --git a/src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java b/src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java index ab85829..e9ef441 100644 --- a/src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java +++ b/src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java @@ -6,4 +6,5 @@ public class TodoRegisterRequest { private final String title; private final String content; + private final String feel; } diff --git a/src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java b/src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java index e445bff..20790b3 100644 --- a/src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java +++ b/src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java @@ -6,4 +6,5 @@ public class TodoUpdateRequest { private final String title; private final String content; + private final String feel; } diff --git a/src/main/java/com/selab/todo/dto/response/TodoResponse.java b/src/main/java/com/selab/todo/dto/response/TodoResponse.java index 2970e1b..8d42aa7 100644 --- a/src/main/java/com/selab/todo/dto/response/TodoResponse.java +++ b/src/main/java/com/selab/todo/dto/response/TodoResponse.java @@ -11,13 +11,15 @@ public class TodoResponse { private final String title; private final String content; private final LocalDateTime localDateTime; + private final String feel; public static TodoResponse from(Todo todo) { return new TodoResponse( todo.getId(), todo.getTitle(), todo.getContent(), - todo.getCreatedAt() + todo.getCreatedAt(), + todo.getFeel() ); } } diff --git a/src/main/java/com/selab/todo/entity/Todo.java b/src/main/java/com/selab/todo/entity/Todo.java index a8aa717..0911224 100644 --- a/src/main/java/com/selab/todo/entity/Todo.java +++ b/src/main/java/com/selab/todo/entity/Todo.java @@ -36,10 +36,14 @@ public class Todo extends BaseEntity { @CreatedDate private LocalDateTime createdAt; + @Column(name="feel") + private String feel; - public Todo(String title, String content) { + + public Todo(String title, String content, String feel) { this.title = title; this.content = content; + this.feel = feel; } public void update(String title, String content) { diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index 026cfe2..03db325 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -29,10 +29,11 @@ public class TodoService { // 삽입 @Transactional - public TodoResponse register(TodoRegisterRequest request) { + public TodoResponse register(TodoRegisterRequest request, String feel) { Todo todo = new Todo( request.getTitle(), - request.getContent() + request.getContent(), + feel ); Todo savedTodo = todoRepository.save(todo); From 54d72c0e157a96b4c4e42dea67914ea5dbd49d5a Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Fri, 3 Feb 2023 12:47:08 +0900 Subject: [PATCH 16/62] =?UTF-8?q?fix=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 393d12e..a05731a 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ ### 3.i. 기능적 요구 사항 ### 3.i.a 기본적인 CRUD ### 3.i.a.a 등록 -* request : id, 등록 날짜, 제목, 내용 +* request : id, 등록 날짜, 제목, 내용, 기분 ### 3.i.a.b 조회 #### 단건 조회 * request : id From 970a12371f93de17c2c56ed31154022f88fdd0a5 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Fri, 3 Feb 2023 15:12:28 +0900 Subject: [PATCH 17/62] =?UTF-8?q?fix=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20PathVariable=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/controller/TodoController.java | 6 +++--- src/main/java/com/selab/todo/service/TodoService.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/TodoController.java index 6c7d574..d735e44 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/TodoController.java @@ -30,9 +30,9 @@ public class TodoController { private final TodoService todoService; @ApiOperation(value = "TODO 등록하기") - @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/{feel}") - public ResponseEntity register(@RequestBody TodoRegisterRequest request, @PathVariable String feel) { - var response = todoService.register(request, feel); + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/") + public ResponseEntity register(@RequestBody TodoRegisterRequest request) { + var response = todoService.register(request); return ResponseDto.created(response); } diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java index 03db325..4e1abe9 100644 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ b/src/main/java/com/selab/todo/service/TodoService.java @@ -29,11 +29,11 @@ public class TodoService { // 삽입 @Transactional - public TodoResponse register(TodoRegisterRequest request, String feel) { + public TodoResponse register(TodoRegisterRequest request) { Todo todo = new Todo( request.getTitle(), request.getContent(), - feel + request.getFeel() ); Todo savedTodo = todoRepository.save(todo); From f22869dc104d124f46f940bf8348967fbf05442f Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:13:20 +0900 Subject: [PATCH 18/62] develop : month delete --- ...doController.java => DiaryController.java} | 63 +++++++++++-------- 1 file changed, 36 insertions(+), 27 deletions(-) rename src/main/java/com/selab/todo/controller/{TodoController.java => DiaryController.java} (54%) diff --git a/src/main/java/com/selab/todo/controller/TodoController.java b/src/main/java/com/selab/todo/controller/DiaryController.java similarity index 54% rename from src/main/java/com/selab/todo/controller/TodoController.java rename to src/main/java/com/selab/todo/controller/DiaryController.java index d735e44..4063fa4 100644 --- a/src/main/java/com/selab/todo/controller/TodoController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -2,9 +2,9 @@ import com.selab.todo.common.dto.PageDto; import com.selab.todo.common.dto.ResponseDto; -import com.selab.todo.dto.request.TodoRegisterRequest; -import com.selab.todo.dto.request.TodoUpdateRequest; -import com.selab.todo.service.TodoService; +import com.selab.todo.dto.request.DiaryRegisterRequest; +import com.selab.todo.dto.request.DiaryUpdateRequest; +import com.selab.todo.service.DiaryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -22,62 +22,71 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -@Api(tags = {"TODO API"}) +@Api(tags = {"Diary API"}) @RestController -@RequestMapping(value = "/api/v1/todos", produces = MediaType.APPLICATION_JSON_VALUE) +@RequestMapping(value = "/api/v1/Diarys", produces = MediaType.APPLICATION_JSON_VALUE) @RequiredArgsConstructor -public class TodoController { - private final TodoService todoService; +public class DiaryController { + private final DiaryService diaryService; - @ApiOperation(value = "TODO 등록하기") - @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/") - public ResponseEntity register(@RequestBody TodoRegisterRequest request) { - var response = todoService.register(request); + @ApiOperation(value = "Diary 등록하기") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); return ResponseDto.created(response); } - @ApiOperation(value = "TODO 단건 조회하기") - @GetMapping("/{id}") + @ApiOperation(value = "Diary 단건 조회하기") + @GetMapping("/search/single/{id}") public ResponseEntity get(@PathVariable Long id) { - var response = todoService.get(id); + var response = diaryService.get(id); return ResponseDto.ok(response); } - @ApiOperation(value = "TODO 범위 조회") - @GetMapping("/month/{month}") + @ApiOperation(value = "Diary 범위 조회") + @GetMapping("/search/month/{month}") public ResponseEntity getRange( @PathVariable int month, @PageableDefault Pageable pageable ) { - var response = todoService.getRange(pageable,month); + var response = diaryService.getRange(pageable,month); return PageDto.ok(response); } - @ApiOperation(value = "TODO 전체 조회하기") - @GetMapping + @ApiOperation(value = "Diary 전체 조회하기") + @GetMapping("/search/all") public ResponseEntity getAll( @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable ) { - var response = todoService.getAll(pageable); + var response = diaryService.getAll(pageable); return PageDto.ok(response); } - @ApiOperation(value = "TODO 수정하기") - @PutMapping("/{id}") // @PatchMapping + @ApiOperation(value = "Diary 수정하기") + @PutMapping("/update/{id}") // @PatchMapping public ResponseEntity update( @PathVariable Long id, - @RequestBody TodoUpdateRequest request + @RequestBody DiaryUpdateRequest request ) { - var response = todoService.update(id, request); + var response = diaryService.update(id, request); return ResponseDto.ok(response); } - @ApiOperation(value = "TODO 삭제하기") - @DeleteMapping("/{id}") + @ApiOperation(value = "Diary 삭제하기") + @DeleteMapping("/delete/id/{id}") public ResponseEntity delete( @PathVariable Long id ) { - todoService.delete(id); + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary 월별 삭제하기") + @DeleteMapping("/delete/month/{month}") + public ResponseEntity monthDelete( + @PathVariable int month + ){ + diaryService.monthDelete(month); return ResponseDto.noContent(); } } From 14a39faf5a65b01fc77a298fc48c454f3e5ac7e0 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:13:39 +0900 Subject: [PATCH 19/62] fix : add year, month, day --- .../{TodoRegisterRequest.java => DiaryRegisterRequest.java} | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) rename src/main/java/com/selab/todo/dto/request/{TodoRegisterRequest.java => DiaryRegisterRequest.java} (61%) diff --git a/src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java b/src/main/java/com/selab/todo/dto/request/DiaryRegisterRequest.java similarity index 61% rename from src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java rename to src/main/java/com/selab/todo/dto/request/DiaryRegisterRequest.java index e9ef441..1d2000c 100644 --- a/src/main/java/com/selab/todo/dto/request/TodoRegisterRequest.java +++ b/src/main/java/com/selab/todo/dto/request/DiaryRegisterRequest.java @@ -3,8 +3,11 @@ import lombok.Data; @Data -public class TodoRegisterRequest { +public class DiaryRegisterRequest { private final String title; private final String content; private final String feel; + private int year; + private int month; + private int day; } From bab5a7c2ba089b1703e50f67e02e98418cfbf1ef Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:15:11 +0900 Subject: [PATCH 20/62] =?UTF-8?q?fix=20:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...teRequest.java => DiaryUpdateRequest.java} | 2 +- .../todo/dto/response/DiaryResponse.java | 31 +++++++++++++++++++ .../selab/todo/dto/response/TodoResponse.java | 25 --------------- .../todo/entity/{Todo.java => Diary.java} | 27 +++++++++++----- ...TodoException.java => DiaryException.java} | 4 +-- .../todo/repository/DiaryRepository.java | 9 ++++++ .../selab/todo/repository/TodoRepository.java | 11 ------- 7 files changed, 62 insertions(+), 47 deletions(-) rename src/main/java/com/selab/todo/dto/request/{TodoUpdateRequest.java => DiaryUpdateRequest.java} (82%) create mode 100644 src/main/java/com/selab/todo/dto/response/DiaryResponse.java delete mode 100644 src/main/java/com/selab/todo/dto/response/TodoResponse.java rename src/main/java/com/selab/todo/entity/{Todo.java => Diary.java} (65%) rename src/main/java/com/selab/todo/exception/{TodoException.java => DiaryException.java} (51%) create mode 100644 src/main/java/com/selab/todo/repository/DiaryRepository.java delete mode 100644 src/main/java/com/selab/todo/repository/TodoRepository.java diff --git a/src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java b/src/main/java/com/selab/todo/dto/request/DiaryUpdateRequest.java similarity index 82% rename from src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java rename to src/main/java/com/selab/todo/dto/request/DiaryUpdateRequest.java index 20790b3..6c161a9 100644 --- a/src/main/java/com/selab/todo/dto/request/TodoUpdateRequest.java +++ b/src/main/java/com/selab/todo/dto/request/DiaryUpdateRequest.java @@ -3,7 +3,7 @@ import lombok.Data; @Data -public class TodoUpdateRequest { +public class DiaryUpdateRequest { private final String title; private final String content; private final String feel; diff --git a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java new file mode 100644 index 0000000..92b536a --- /dev/null +++ b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java @@ -0,0 +1,31 @@ +package com.selab.todo.dto.response; + +import com.selab.todo.entity.Diary; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class DiaryResponse { + private final Long id; + private final String title; + private final String content; + private final LocalDateTime localDateTime; + private final String feel; + private final int year; + private final int month; + private final int day; + + public static DiaryResponse from(Diary diary) { + return new DiaryResponse( + diary.getId(), + diary.getTitle(), + diary.getContent(), + diary.getCreatedAt(), + diary.getFeel(), + diary.getYear(), + diary.getMonth(), + diary.getDay() + ); + } +} diff --git a/src/main/java/com/selab/todo/dto/response/TodoResponse.java b/src/main/java/com/selab/todo/dto/response/TodoResponse.java deleted file mode 100644 index 8d42aa7..0000000 --- a/src/main/java/com/selab/todo/dto/response/TodoResponse.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.selab.todo.dto.response; - -import com.selab.todo.entity.Todo; -import lombok.Data; - -import java.time.LocalDateTime; - -@Data -public class TodoResponse { - private final Long id; - private final String title; - private final String content; - private final LocalDateTime localDateTime; - private final String feel; - - public static TodoResponse from(Todo todo) { - return new TodoResponse( - todo.getId(), - todo.getTitle(), - todo.getContent(), - todo.getCreatedAt(), - todo.getFeel() - ); - } -} diff --git a/src/main/java/com/selab/todo/entity/Todo.java b/src/main/java/com/selab/todo/entity/Diary.java similarity index 65% rename from src/main/java/com/selab/todo/entity/Todo.java rename to src/main/java/com/selab/todo/entity/Diary.java index 0911224..eb9b178 100644 --- a/src/main/java/com/selab/todo/entity/Todo.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -4,7 +4,6 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.Setter; import org.springframework.data.annotation.CreatedDate; import javax.persistence.Column; @@ -14,13 +13,12 @@ import javax.persistence.Id; import javax.persistence.Table; import java.time.LocalDateTime; -import java.time.Month; @Entity @Getter @Table(name = "todo") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Todo extends BaseEntity { +public class Diary extends BaseEntity { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -32,26 +30,39 @@ public class Todo extends BaseEntity { @Column(name = "content") private String content; - @Column(name="createdAt") + @Column(name = "createdAt") @CreatedDate private LocalDateTime createdAt; - @Column(name="feel") + @Column(name = "year") + private int year; + + @Column(name = "month") + private int month; + + @Column(name = "day") + private int day; + + @Column(name = "feel") private String feel; - public Todo(String title, String content, String feel) { + public Diary(String title, String content, String feel, int year, int month, int day) { this.title = title; this.content = content; this.feel = feel; + this.year = year; + this.month = month; + this.day = day; } - public void update(String title, String content) { + public void update(String title, String content, String feel) { this.title = title; this.content = content; + this.feel = feel; } - public int getCreatedMonth(){ + public int getCreatedMonth() { return createdAt.getMonthValue(); } } diff --git a/src/main/java/com/selab/todo/exception/TodoException.java b/src/main/java/com/selab/todo/exception/DiaryException.java similarity index 51% rename from src/main/java/com/selab/todo/exception/TodoException.java rename to src/main/java/com/selab/todo/exception/DiaryException.java index 74c2e5c..a14fc0e 100644 --- a/src/main/java/com/selab/todo/exception/TodoException.java +++ b/src/main/java/com/selab/todo/exception/DiaryException.java @@ -1,7 +1,7 @@ package com.selab.todo.exception; -public class TodoException extends BusinessException { - public TodoException() { +public class DiaryException extends BusinessException { + public DiaryException() { super(ErrorMessage.TODO_NOT_FOUND_ERROR); } } diff --git a/src/main/java/com/selab/todo/repository/DiaryRepository.java b/src/main/java/com/selab/todo/repository/DiaryRepository.java new file mode 100644 index 0000000..b05ab55 --- /dev/null +++ b/src/main/java/com/selab/todo/repository/DiaryRepository.java @@ -0,0 +1,9 @@ +package com.selab.todo.repository; + +import com.selab.todo.entity.Diary; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface DiaryRepository extends JpaRepository { +} diff --git a/src/main/java/com/selab/todo/repository/TodoRepository.java b/src/main/java/com/selab/todo/repository/TodoRepository.java deleted file mode 100644 index ef24a18..0000000 --- a/src/main/java/com/selab/todo/repository/TodoRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.selab.todo.repository; - -import com.selab.todo.dto.response.TodoResponse; -import com.selab.todo.entity.Todo; -import org.springframework.data.domain.Page; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface TodoRepository extends JpaRepository { -} From 3d82dee54670803b1aa048593b3a009f6010f748 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:15:38 +0900 Subject: [PATCH 21/62] =?UTF-8?q?fix=20:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=AA=85=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/service/TodoService.java | 97 ------------------- 1 file changed, 97 deletions(-) delete mode 100644 src/main/java/com/selab/todo/service/TodoService.java diff --git a/src/main/java/com/selab/todo/service/TodoService.java b/src/main/java/com/selab/todo/service/TodoService.java deleted file mode 100644 index 4e1abe9..0000000 --- a/src/main/java/com/selab/todo/service/TodoService.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.selab.todo.service; - -import com.selab.todo.dto.request.TodoRegisterRequest; -import com.selab.todo.dto.request.TodoUpdateRequest; -import com.selab.todo.dto.response.TodoResponse; -import com.selab.todo.entity.Todo; -import com.selab.todo.exception.TodoException; -import com.selab.todo.repository.TodoRepository; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - - -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - - -@Slf4j -@Service -@RequiredArgsConstructor -public class TodoService { - private final TodoRepository todoRepository; - - // 삽입 - @Transactional - public TodoResponse register(TodoRegisterRequest request) { - Todo todo = new Todo( - request.getTitle(), - request.getContent(), - request.getFeel() - ); - - Todo savedTodo = todoRepository.save(todo); - - log.info("todo 등록했습니다. {}", todo.getId()); - - return TodoResponse.from(savedTodo); - } - - // 단건 조회 - @Transactional(readOnly = true) - public TodoResponse get(Long id) { - Todo todo = todoRepository.findById(id) - .orElseThrow(TodoException::new); - log.info("todo 조회했습니다, {}", todo.getId()); - return TodoResponse.from(todo); - } - - // 페이징 조회 - @Transactional(readOnly = true) - public Page getAll(Pageable pageable) { - log.info("todo 전체 조회"); - return todoRepository.findAll(pageable) - .map(TodoResponse::from); - } - - //범위 조회 - @Transactional(readOnly = true) - public Page getRange(Pageable pageable, int month) { - log.info("Todo 범위 조회"); - Page allPage = todoRepository.findAll(pageable).map(TodoResponse::from); - List middleProcess = allPage.stream().filter(a->a.getLocalDateTime().getMonth().getValue()==month).collect(Collectors.toList()); - PageRequest pageRequest = PageRequest.of(0,10); - int start = (int) pageRequest.getOffset(); - int end = Math.min((start+pageRequest.getPageSize()), middleProcess.size()); - return new PageImpl<>(middleProcess.subList(start,end),pageRequest,middleProcess.size()); - } - - // 수정 - @Transactional - public TodoResponse update(Long id, TodoUpdateRequest request) { - Todo todo = todoRepository.findById(id) - .orElseThrow(TodoException::new); - - // 더티체킹 - 영속성 컨텍스트 - todo.update(request.getTitle(), request.getContent()); - - log.info("todo 수정했습니다. {}", todo.getId()); - - return TodoResponse.from(todo); - } - - // 삭제 - @Transactional - public void delete(Long id) { - if (todoRepository.existsById(id)) { - todoRepository.deleteById(id); - log.info("todo 삭제했습니다.. {}", id); - } - } -} From ebef5f8ac51b87a1b8edb65e2f68660908a84c04 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:15:53 +0900 Subject: [PATCH 22/62] =?UTF-8?q?develop=20:=20month=20delete=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/service/DiaryService.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/main/java/com/selab/todo/service/DiaryService.java diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java new file mode 100644 index 0000000..281ade5 --- /dev/null +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -0,0 +1,119 @@ +package com.selab.todo.service; + +import com.selab.todo.dto.request.DiaryRegisterRequest; +import com.selab.todo.dto.request.DiaryUpdateRequest; +import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.entity.Diary; +import com.selab.todo.exception.DiaryException; +import com.selab.todo.repository.DiaryRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + + +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + + +@Slf4j +@Service +@RequiredArgsConstructor +public class DiaryService { + private final DiaryRepository diaryRepository; + + // 삽입 + @Transactional + public DiaryResponse register(DiaryRegisterRequest request) { + Diary diary = new Diary( + request.getTitle(), + request.getContent(), + request.getFeel(), + request.getYear(), + request.getMonth(), + request.getDay() + ); + + Diary savedDiary = diaryRepository.save(diary); + + log.info("todo 등록했습니다. {}", diary.getId()); + + return DiaryResponse.from(savedDiary); + } + + // 단건 조회 + @Transactional(readOnly = true) + public DiaryResponse get(Long id) { + Diary diary = diaryRepository.findById(id) + .orElseThrow(DiaryException::new); + log.info("todo 조회했습니다, {}", diary.getId()); + return DiaryResponse.from(diary); + } + + // 페이징 조회 + @Transactional(readOnly = true) + public Page getAll(Pageable pageable) { + log.info("todo 전체 조회"); + return diaryRepository.findAll(pageable) + .map(DiaryResponse::from); + } + + //범위 조회 + @Transactional(readOnly = true) + public Page getRange(Pageable pageable, int month) { + log.info("Todo 범위 조회"); + Page allPage = diaryRepository.findAll(pageable).map(DiaryResponse::from); + List middleProcess = allPage.stream().filter(a -> a.getLocalDateTime().getMonth().getValue() == month).collect(Collectors.toList()); + PageRequest pageRequest = PageRequest.of(0, 10); + int start = (int) pageRequest.getOffset(); + int end = Math.min((start + pageRequest.getPageSize()), middleProcess.size()); + return new PageImpl<>(middleProcess.subList(start, end), pageRequest, middleProcess.size()); + } + + // 수정 + @Transactional + public DiaryResponse update(Long id, DiaryUpdateRequest request) { + Diary diary = diaryRepository.findById(id) + .orElseThrow(DiaryException::new); + + // 더티체킹 - 영속성 컨텍스트 + diary.update(request.getTitle(), request.getContent(), request.getFeel()); + + log.info("todo 수정했습니다. {}", diary.getId()); + + return DiaryResponse.from(diary); + } + + // 삭제 + @Transactional + public void delete(Long id) { + if (diaryRepository.existsById(id)) { + diaryRepository.deleteById(id); + log.info("todo 삭제했습니다.. {}", id); + } + } + + //월별 삭제 + @Transactional + public void monthDelete(int month) { + List deleteId = new LinkedList<>(); + + diaryRepository.findAll().forEach(eachDiary -> { + if (eachDiary.getMonth() == month) { + deleteId.add(eachDiary.getId()); + } + }); + + deleteId.forEach(id -> { + if (diaryRepository.existsById(id)) { + diaryRepository.deleteById(id); + log.info("Month Todo 삭제했습니다. {}", id); + } + }); + } +} From 5f2b58ff6e1c9ac8d8bd90651c20d4604bc7e0f8 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:16:04 +0900 Subject: [PATCH 23/62] =?UTF-8?q?fix=20:=20Diary=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a05731a..918c9df 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # SELAB-TODO -> basic todo with spring-mvc +> basic diary with spring-mvc # 요구명세서 > 일기 작성 서비스 From 60b2f2a154ebbdea53c671c65f95bd8bd9225952 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:16:11 +0900 Subject: [PATCH 24/62] =?UTF-8?q?fix=20:=20Diary=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/config/SwaggerConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/selab/todo/config/SwaggerConfig.java b/src/main/java/com/selab/todo/config/SwaggerConfig.java index 78f1e8e..5a35176 100644 --- a/src/main/java/com/selab/todo/config/SwaggerConfig.java +++ b/src/main/java/com/selab/todo/config/SwaggerConfig.java @@ -31,8 +31,8 @@ public Docket restApi() { private ApiInfo apiInfo() { return new ApiInfoBuilder() - .title("Selab Todo api info") - .description("SE TODO API") + .title("Selab Diary api info") + .description("SE Diary API") .version("1.0.0") .build(); } From 660ea95ae7ef73e974d1d7417728a9f76f07f2fd Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:16:38 +0900 Subject: [PATCH 25/62] fix : delete test --- .../todo/SelabDiaryApplicationTests.java | 17 ++++++++++++++ .../selab/todo/SelabTodoApplicationTests.java | 23 ------------------- 2 files changed, 17 insertions(+), 23 deletions(-) create mode 100644 src/test/java/com/selab/todo/SelabDiaryApplicationTests.java delete mode 100644 src/test/java/com/selab/todo/SelabTodoApplicationTests.java diff --git a/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java b/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java new file mode 100644 index 0000000..7c6cda8 --- /dev/null +++ b/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java @@ -0,0 +1,17 @@ +package com.selab.todo; + +import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.repository.DiaryRepository; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; + +import java.util.List; +import java.util.stream.Collectors; + +@SpringBootTest +class SelabDiaryApplicationTests { + +} diff --git a/src/test/java/com/selab/todo/SelabTodoApplicationTests.java b/src/test/java/com/selab/todo/SelabTodoApplicationTests.java deleted file mode 100644 index acbb41a..0000000 --- a/src/test/java/com/selab/todo/SelabTodoApplicationTests.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.selab.todo; - -import com.selab.todo.dto.response.TodoResponse; -import com.selab.todo.repository.TodoRepository; -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.web.PageableDefault; - -import java.util.List; -import java.util.stream.Collectors; - -@SpringBootTest -class SelabTodoApplicationTests { - TodoRepository todoRepository; - @Test - void contextLoads(@PageableDefault Pageable pageable) { - Page page = todoRepository.findAll(pageable).map(TodoResponse::from); - List asd = page.stream().filter(a->a.getLocalDateTime().getMonth().getValue()==2).collect(Collectors.toList()); - } - -} From b6a633d8163b3779b04248374d43133cc1ca5827 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Feb 2023 21:59:36 +0900 Subject: [PATCH 26/62] fix : month search --- src/main/java/com/selab/todo/entity/Diary.java | 4 ---- src/main/java/com/selab/todo/service/DiaryService.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index eb9b178..14ce116 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -61,8 +61,4 @@ public void update(String title, String content, String feel) { this.content = content; this.feel = feel; } - - public int getCreatedMonth() { - return createdAt.getMonthValue(); - } } diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index 281ade5..be478fe 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -68,7 +68,7 @@ public Page getAll(Pageable pageable) { public Page getRange(Pageable pageable, int month) { log.info("Todo 범위 조회"); Page allPage = diaryRepository.findAll(pageable).map(DiaryResponse::from); - List middleProcess = allPage.stream().filter(a -> a.getLocalDateTime().getMonth().getValue() == month).collect(Collectors.toList()); + List middleProcess = allPage.stream().filter(a -> a.getMonth() == month).collect(Collectors.toList()); PageRequest pageRequest = PageRequest.of(0, 10); int start = (int) pageRequest.getOffset(); int end = Math.min((start + pageRequest.getPageSize()), middleProcess.size()); From 91da852b59ad5ea9791e56e62f862368c7144fc2 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Tue, 7 Feb 2023 21:55:30 +0900 Subject: [PATCH 27/62] =?UTF-8?q?fix=20:=20=EC=8A=A4=EC=9B=A8=EA=B1=B0=20?= =?UTF-8?q?=EB=A7=81=ED=81=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 918c9df..11d12a1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # SELAB-TODO > basic diary with spring-mvc +[테스트링크](http://localhost:8080/swagger-ui/index.html#/TODO%20API) + # 요구명세서 > 일기 작성 서비스 From 6ab4cb35021d0c4ed0a5f5f4d3e00a830f487fe8 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Tue, 7 Feb 2023 21:55:48 +0900 Subject: [PATCH 28/62] =?UTF-8?q?develop=20:=20Feeling=20=ED=95=AD?= =?UTF-8?q?=EB=AA=A9=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../selab/todo/dto/response/DiaryResponse.java | 3 ++- src/main/java/com/selab/todo/entity/Diary.java | 7 ++++--- src/main/java/com/selab/todo/model/Feeling.java | 17 +++++++++++++++++ .../com/selab/todo/service/DiaryService.java | 5 +++-- 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/selab/todo/model/Feeling.java diff --git a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java index 92b536a..a823bef 100644 --- a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java +++ b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java @@ -1,6 +1,7 @@ package com.selab.todo.dto.response; import com.selab.todo.entity.Diary; +import com.selab.todo.model.Feeling; import lombok.Data; import java.time.LocalDateTime; @@ -11,7 +12,7 @@ public class DiaryResponse { private final String title; private final String content; private final LocalDateTime localDateTime; - private final String feel; + private final Feeling feel; private final int year; private final int month; private final int day; diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index 14ce116..62828c3 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -1,6 +1,7 @@ package com.selab.todo.entity; import com.selab.todo.common.BaseEntity; +import com.selab.todo.model.Feeling; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -44,10 +45,10 @@ public class Diary extends BaseEntity { private int day; @Column(name = "feel") - private String feel; + private Feeling feel; - public Diary(String title, String content, String feel, int year, int month, int day) { + public Diary(String title, String content, Feeling feel, int year, int month, int day) { this.title = title; this.content = content; this.feel = feel; @@ -56,7 +57,7 @@ public Diary(String title, String content, String feel, int year, int month, int this.day = day; } - public void update(String title, String content, String feel) { + public void update(String title, String content, Feeling feel) { this.title = title; this.content = content; this.feel = feel; diff --git a/src/main/java/com/selab/todo/model/Feeling.java b/src/main/java/com/selab/todo/model/Feeling.java new file mode 100644 index 0000000..325f0fa --- /dev/null +++ b/src/main/java/com/selab/todo/model/Feeling.java @@ -0,0 +1,17 @@ +package com.selab.todo.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum Feeling { + GOOD("Good", 1), + BAD("Bad", 2), + NOT_BAD("Not Bad",3), + PERFECT("Perfect",4), + WORST("Worst",5); + + private final String feel; + private final int value; +} diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index be478fe..dbbc9c7 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -5,6 +5,7 @@ import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.entity.Diary; import com.selab.todo.exception.DiaryException; +import com.selab.todo.model.Feeling; import com.selab.todo.repository.DiaryRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -33,7 +34,7 @@ public DiaryResponse register(DiaryRegisterRequest request) { Diary diary = new Diary( request.getTitle(), request.getContent(), - request.getFeel(), + Feeling.valueOf(request.getFeel()), request.getYear(), request.getMonth(), request.getDay() @@ -82,7 +83,7 @@ public DiaryResponse update(Long id, DiaryUpdateRequest request) { .orElseThrow(DiaryException::new); // 더티체킹 - 영속성 컨텍스트 - diary.update(request.getTitle(), request.getContent(), request.getFeel()); + diary.update(request.getTitle(), request.getContent(), Feeling.valueOf(request.getFeel())); log.info("todo 수정했습니다. {}", diary.getId()); From ee7e7c79f1d9a17b27e7f5cf944fd410e8983d97 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sun, 19 Feb 2023 10:52:21 +0900 Subject: [PATCH 29/62] =?UTF-8?q?update=20:=20Feeling=20=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20update=20,=20findA?= =?UTF-8?q?ll=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todo/controller/DiaryController.java | 25 +++++++++-- .../{ => diary}/DiaryRegisterRequest.java | 3 +- .../{ => diary}/DiaryUpdateRequest.java | 2 +- .../request/feel/FeelingUpdateRequest.java | 9 ++++ .../todo/dto/response/DiaryResponse.java | 3 +- .../todo/dto/response/FeelingResponse.java | 18 ++++++++ .../java/com/selab/todo/entity/Diary.java | 13 +++--- .../java/com/selab/todo/entity/Feeling.java | 20 +++++++++ .../java/com/selab/todo/model/Feeling.java | 17 -------- .../todo/repository/FeelingRepository.java | 7 ++++ .../com/selab/todo/service/DiaryService.java | 10 ++--- .../selab/todo/service/FeelingService.java | 42 +++++++++++++++++++ 12 files changed, 134 insertions(+), 35 deletions(-) rename src/main/java/com/selab/todo/dto/request/{ => diary}/DiaryRegisterRequest.java (75%) rename src/main/java/com/selab/todo/dto/request/{ => diary}/DiaryUpdateRequest.java (79%) create mode 100644 src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java create mode 100644 src/main/java/com/selab/todo/dto/response/FeelingResponse.java create mode 100644 src/main/java/com/selab/todo/entity/Feeling.java delete mode 100644 src/main/java/com/selab/todo/model/Feeling.java create mode 100644 src/main/java/com/selab/todo/repository/FeelingRepository.java create mode 100644 src/main/java/com/selab/todo/service/FeelingService.java diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index 4063fa4..f2dc156 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -2,9 +2,11 @@ import com.selab.todo.common.dto.PageDto; import com.selab.todo.common.dto.ResponseDto; -import com.selab.todo.dto.request.DiaryRegisterRequest; -import com.selab.todo.dto.request.DiaryUpdateRequest; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -28,6 +30,7 @@ @RequiredArgsConstructor public class DiaryController { private final DiaryService diaryService; + private final FeelingService feelingService; @ApiOperation(value = "Diary 등록하기") @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") @@ -49,7 +52,7 @@ public ResponseEntity getRange( @PathVariable int month, @PageableDefault Pageable pageable ) { - var response = diaryService.getRange(pageable,month); + var response = diaryService.getRange(pageable, month); return PageDto.ok(response); } @@ -85,8 +88,22 @@ public ResponseEntity delete( @DeleteMapping("/delete/month/{month}") public ResponseEntity monthDelete( @PathVariable int month - ){ + ) { diaryService.monthDelete(month); return ResponseDto.noContent(); } + + @ApiOperation(value = "Feeling 수정하기") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/update/feeling") + public ResponseEntity updateFeeling(@RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling 전체 조회하기") + @GetMapping("/serach/feeling/all") + public ResponseEntity getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } } diff --git a/src/main/java/com/selab/todo/dto/request/DiaryRegisterRequest.java b/src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java similarity index 75% rename from src/main/java/com/selab/todo/dto/request/DiaryRegisterRequest.java rename to src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java index 1d2000c..b14322b 100644 --- a/src/main/java/com/selab/todo/dto/request/DiaryRegisterRequest.java +++ b/src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java @@ -1,5 +1,6 @@ -package com.selab.todo.dto.request; +package com.selab.todo.dto.request.diary; +import lombok.AllArgsConstructor; import lombok.Data; @Data diff --git a/src/main/java/com/selab/todo/dto/request/DiaryUpdateRequest.java b/src/main/java/com/selab/todo/dto/request/diary/DiaryUpdateRequest.java similarity index 79% rename from src/main/java/com/selab/todo/dto/request/DiaryUpdateRequest.java rename to src/main/java/com/selab/todo/dto/request/diary/DiaryUpdateRequest.java index 6c161a9..8bdc19f 100644 --- a/src/main/java/com/selab/todo/dto/request/DiaryUpdateRequest.java +++ b/src/main/java/com/selab/todo/dto/request/diary/DiaryUpdateRequest.java @@ -1,4 +1,4 @@ -package com.selab.todo.dto.request; +package com.selab.todo.dto.request.diary; import lombok.Data; diff --git a/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java b/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java new file mode 100644 index 0000000..6b5ee6e --- /dev/null +++ b/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java @@ -0,0 +1,9 @@ +package com.selab.todo.dto.request.feel; + +import lombok.Data; + +@Data +public class FeelingUpdateRequest { + private final Long id; + private final String feel; +} diff --git a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java index a823bef..92b536a 100644 --- a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java +++ b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java @@ -1,7 +1,6 @@ package com.selab.todo.dto.response; import com.selab.todo.entity.Diary; -import com.selab.todo.model.Feeling; import lombok.Data; import java.time.LocalDateTime; @@ -12,7 +11,7 @@ public class DiaryResponse { private final String title; private final String content; private final LocalDateTime localDateTime; - private final Feeling feel; + private final String feel; private final int year; private final int month; private final int day; diff --git a/src/main/java/com/selab/todo/dto/response/FeelingResponse.java b/src/main/java/com/selab/todo/dto/response/FeelingResponse.java new file mode 100644 index 0000000..b2460ae --- /dev/null +++ b/src/main/java/com/selab/todo/dto/response/FeelingResponse.java @@ -0,0 +1,18 @@ +package com.selab.todo.dto.response; + +import com.selab.todo.entity.Diary; +import com.selab.todo.entity.Feeling; +import lombok.Data; + +@Data +public class FeelingResponse { + private final Long id; + private final String feel; + + public static FeelingResponse from(Diary diary){ + return new FeelingResponse( + diary.getId(), + diary.getFeel() + ); + } +} diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index 62828c3..0eaf520 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -1,7 +1,6 @@ package com.selab.todo.entity; import com.selab.todo.common.BaseEntity; -import com.selab.todo.model.Feeling; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -17,7 +16,7 @@ @Entity @Getter -@Table(name = "todo") +@Table(name = "diary") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Diary extends BaseEntity { @Id @@ -45,10 +44,10 @@ public class Diary extends BaseEntity { private int day; @Column(name = "feel") - private Feeling feel; + private String feel; - public Diary(String title, String content, Feeling feel, int year, int month, int day) { + public Diary(String title, String content, String feel, int year, int month, int day) { this.title = title; this.content = content; this.feel = feel; @@ -57,9 +56,13 @@ public Diary(String title, String content, Feeling feel, int year, int month, in this.day = day; } - public void update(String title, String content, Feeling feel) { + public void update(String title, String content, String feel) { this.title = title; this.content = content; this.feel = feel; } + + public void feelingUpdate(String feel){ + this.feel = feel; + } } diff --git a/src/main/java/com/selab/todo/entity/Feeling.java b/src/main/java/com/selab/todo/entity/Feeling.java new file mode 100644 index 0000000..3a63da6 --- /dev/null +++ b/src/main/java/com/selab/todo/entity/Feeling.java @@ -0,0 +1,20 @@ +package com.selab.todo.entity; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Getter +@Table(name = "feeling") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Feeling { + @Id + @Column(name = "feel") + private String feel; +} diff --git a/src/main/java/com/selab/todo/model/Feeling.java b/src/main/java/com/selab/todo/model/Feeling.java deleted file mode 100644 index 325f0fa..0000000 --- a/src/main/java/com/selab/todo/model/Feeling.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.selab.todo.model; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public enum Feeling { - GOOD("Good", 1), - BAD("Bad", 2), - NOT_BAD("Not Bad",3), - PERFECT("Perfect",4), - WORST("Worst",5); - - private final String feel; - private final int value; -} diff --git a/src/main/java/com/selab/todo/repository/FeelingRepository.java b/src/main/java/com/selab/todo/repository/FeelingRepository.java new file mode 100644 index 0000000..98b6d90 --- /dev/null +++ b/src/main/java/com/selab/todo/repository/FeelingRepository.java @@ -0,0 +1,7 @@ +package com.selab.todo.repository; + +import com.selab.todo.entity.Feeling; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FeelingRepository extends JpaRepository { +} diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index dbbc9c7..f319801 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -1,12 +1,12 @@ package com.selab.todo.service; -import com.selab.todo.dto.request.DiaryRegisterRequest; -import com.selab.todo.dto.request.DiaryUpdateRequest; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.entity.Diary; import com.selab.todo.exception.DiaryException; -import com.selab.todo.model.Feeling; import com.selab.todo.repository.DiaryRepository; +import com.selab.todo.repository.FeelingRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -34,7 +34,7 @@ public DiaryResponse register(DiaryRegisterRequest request) { Diary diary = new Diary( request.getTitle(), request.getContent(), - Feeling.valueOf(request.getFeel()), + request.getFeel(), request.getYear(), request.getMonth(), request.getDay() @@ -83,7 +83,7 @@ public DiaryResponse update(Long id, DiaryUpdateRequest request) { .orElseThrow(DiaryException::new); // 더티체킹 - 영속성 컨텍스트 - diary.update(request.getTitle(), request.getContent(), Feeling.valueOf(request.getFeel())); + diary.update(request.getTitle(), request.getContent(), request.getFeel()); log.info("todo 수정했습니다. {}", diary.getId()); diff --git a/src/main/java/com/selab/todo/service/FeelingService.java b/src/main/java/com/selab/todo/service/FeelingService.java new file mode 100644 index 0000000..33e368c --- /dev/null +++ b/src/main/java/com/selab/todo/service/FeelingService.java @@ -0,0 +1,42 @@ +package com.selab.todo.service; + +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.dto.response.FeelingResponse; +import com.selab.todo.entity.Diary; +import com.selab.todo.exception.DiaryException; +import com.selab.todo.repository.DiaryRepository; +import com.selab.todo.repository.FeelingRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@Service +@RequiredArgsConstructor +public class FeelingService { + private final DiaryRepository diaryRepository; + + @Transactional + public FeelingResponse updateFeeling(FeelingUpdateRequest request){ + Diary diary = diaryRepository.findById(request.getId()) + .orElseThrow(DiaryException::new); + + diary.feelingUpdate(request.getFeel()); + + log.info("diary feeling 수정했습니다. {}", diary.getId()); + + return FeelingResponse.from(diary); + } + + @Transactional(readOnly = true) + public Page getAllFeeling(Pageable pageable){ + log.info("feeling 전체 조회"); + return diaryRepository.findAll(pageable) + .map(FeelingResponse::from); + } +} From be2b0e4e017071a381cbff98b67efa0769f85db7 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sun, 19 Feb 2023 11:03:04 +0900 Subject: [PATCH 30/62] =?UTF-8?q?update=20:=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EB=AA=A8=EB=93=88=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/service/DiaryService.java | 22 +++++-------- .../com/selab/todo/service/FindService.java | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/selab/todo/service/FindService.java diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index f319801..16033cd 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -6,12 +6,9 @@ import com.selab.todo.entity.Diary; import com.selab.todo.exception.DiaryException; import com.selab.todo.repository.DiaryRepository; -import com.selab.todo.repository.FeelingRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,7 +16,6 @@ import java.util.LinkedList; import java.util.List; -import java.util.stream.Collectors; @Slf4j @@ -27,6 +23,7 @@ @RequiredArgsConstructor public class DiaryService { private final DiaryRepository diaryRepository; + private final FindService findService; // 삽입 @Transactional @@ -42,7 +39,7 @@ public DiaryResponse register(DiaryRegisterRequest request) { Diary savedDiary = diaryRepository.save(diary); - log.info("todo 등록했습니다. {}", diary.getId()); + log.info("Diary 등록했습니다. {}", diary.getId()); return DiaryResponse.from(savedDiary); } @@ -52,14 +49,14 @@ public DiaryResponse register(DiaryRegisterRequest request) { public DiaryResponse get(Long id) { Diary diary = diaryRepository.findById(id) .orElseThrow(DiaryException::new); - log.info("todo 조회했습니다, {}", diary.getId()); + log.info("Diary 조회했습니다, {}", diary.getId()); return DiaryResponse.from(diary); } // 페이징 조회 @Transactional(readOnly = true) public Page getAll(Pageable pageable) { - log.info("todo 전체 조회"); + log.info("Diary 전체 조회"); return diaryRepository.findAll(pageable) .map(DiaryResponse::from); } @@ -67,13 +64,10 @@ public Page getAll(Pageable pageable) { //범위 조회 @Transactional(readOnly = true) public Page getRange(Pageable pageable, int month) { - log.info("Todo 범위 조회"); + log.info("Diary 범위 조회"); Page allPage = diaryRepository.findAll(pageable).map(DiaryResponse::from); - List middleProcess = allPage.stream().filter(a -> a.getMonth() == month).collect(Collectors.toList()); - PageRequest pageRequest = PageRequest.of(0, 10); - int start = (int) pageRequest.getOffset(); - int end = Math.min((start + pageRequest.getPageSize()), middleProcess.size()); - return new PageImpl<>(middleProcess.subList(start, end), pageRequest, middleProcess.size()); + + return findService.getMonthRange(allPage, month); } // 수정 @@ -113,7 +107,7 @@ public void monthDelete(int month) { deleteId.forEach(id -> { if (diaryRepository.existsById(id)) { diaryRepository.deleteById(id); - log.info("Month Todo 삭제했습니다. {}", id); + log.info("Month Diary 삭제했습니다. {}", id); } }); } diff --git a/src/main/java/com/selab/todo/service/FindService.java b/src/main/java/com/selab/todo/service/FindService.java new file mode 100644 index 0000000..c7d3486 --- /dev/null +++ b/src/main/java/com/selab/todo/service/FindService.java @@ -0,0 +1,32 @@ +package com.selab.todo.service; + +import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.repository.DiaryRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +@Service +@RequiredArgsConstructor +public class FindService { + public Page getMonthRange(Page allPage, int month) { + List middleProcess = allPage + .stream() + .filter(a -> a.getMonth() == month) + .collect(Collectors.toList()); + + PageRequest pageRequest = PageRequest.of(0, 10); + + int start = (int) pageRequest.getOffset(); + int end = Math.min((start + pageRequest.getPageSize()), middleProcess.size()); + return new PageImpl<>(middleProcess.subList(start, end), pageRequest, middleProcess.size()); + } +} \ No newline at end of file From 8ae4591a799cbec3712635778bd1eb6ba29fc9c9 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sun, 19 Feb 2023 11:03:14 +0900 Subject: [PATCH 31/62] fix : log --- src/main/java/com/selab/todo/service/FeelingService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/selab/todo/service/FeelingService.java b/src/main/java/com/selab/todo/service/FeelingService.java index 33e368c..009fa5b 100644 --- a/src/main/java/com/selab/todo/service/FeelingService.java +++ b/src/main/java/com/selab/todo/service/FeelingService.java @@ -28,14 +28,14 @@ public FeelingResponse updateFeeling(FeelingUpdateRequest request){ diary.feelingUpdate(request.getFeel()); - log.info("diary feeling 수정했습니다. {}", diary.getId()); + log.info("Diary feeling 수정했습니다. {}", diary.getId()); return FeelingResponse.from(diary); } @Transactional(readOnly = true) public Page getAllFeeling(Pageable pageable){ - log.info("feeling 전체 조회"); + log.info("Diary feeling 전체 조회"); return diaryRepository.findAll(pageable) .map(FeelingResponse::from); } From 2fed5fbe8b1eeac8592e58549cb29bf0be8ee131 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sun, 19 Feb 2023 11:08:42 +0900 Subject: [PATCH 32/62] fix : URL --- .../todo/controller/DiaryController.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index f2dc156..f106f20 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -4,6 +4,7 @@ import com.selab.todo.common.dto.ResponseDto; import com.selab.todo.dto.request.diary.DiaryRegisterRequest; import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.diary.MonthRequest; import com.selab.todo.dto.request.feel.FeelingUpdateRequest; import com.selab.todo.service.DiaryService; import com.selab.todo.service.FeelingService; @@ -40,24 +41,24 @@ public ResponseEntity register(@RequestBody DiaryRegisterRequest request) { } @ApiOperation(value = "Diary 단건 조회하기") - @GetMapping("/search/single/{id}") + @GetMapping("/{id}") public ResponseEntity get(@PathVariable Long id) { var response = diaryService.get(id); return ResponseDto.ok(response); } @ApiOperation(value = "Diary 범위 조회") - @GetMapping("/search/month/{month}") + @GetMapping(value = "/search-range-month",consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getRange( - @PathVariable int month, + @RequestBody MonthRequest month, @PageableDefault Pageable pageable ) { - var response = diaryService.getRange(pageable, month); + var response = diaryService.getRange(pageable, month.getMonth()); return PageDto.ok(response); } @ApiOperation(value = "Diary 전체 조회하기") - @GetMapping("/search/all") + @GetMapping("/search-all") public ResponseEntity getAll( @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable ) { @@ -66,7 +67,7 @@ public ResponseEntity getAll( } @ApiOperation(value = "Diary 수정하기") - @PutMapping("/update/{id}") // @PatchMapping + @PutMapping("/{id}") // @PatchMapping public ResponseEntity update( @PathVariable Long id, @RequestBody DiaryUpdateRequest request @@ -76,7 +77,7 @@ public ResponseEntity update( } @ApiOperation(value = "Diary 삭제하기") - @DeleteMapping("/delete/id/{id}") + @DeleteMapping("/{id}") public ResponseEntity delete( @PathVariable Long id ) { @@ -85,11 +86,11 @@ public ResponseEntity delete( } @ApiOperation(value = "Diary 월별 삭제하기") - @DeleteMapping("/delete/month/{month}") + @DeleteMapping("/delete-month") public ResponseEntity monthDelete( - @PathVariable int month + @RequestBody MonthRequest request ) { - diaryService.monthDelete(month); + diaryService.monthDelete(request.getMonth()); return ResponseDto.noContent(); } From 0342a14951fd40125004d6b4e5b4db8c907e3c45 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sun, 19 Feb 2023 11:08:51 +0900 Subject: [PATCH 33/62] update : MonthRequest --- .../com/selab/todo/dto/request/diary/MonthRequest.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java diff --git a/src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java b/src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java new file mode 100644 index 0000000..963eaef --- /dev/null +++ b/src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java @@ -0,0 +1,8 @@ +package com.selab.todo.dto.request.diary; + +import lombok.Data; + +@Data +public class MonthRequest { + private final int month; +} From 700f32e22c4e825b96422896a3a2468e174dcfe3 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:37:21 +0900 Subject: [PATCH 34/62] =?UTF-8?q?update=20:=20=EB=8B=A4=EC=96=91=ED=95=9C?= =?UTF-8?q?=20=EA=B2=80=EC=83=89=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todo/controller/SearchController.java | 56 +++++++++++++++++ ...thRequest.java => MonthSearchRequest.java} | 2 +- .../todo/dto/request/diary/SearchRequest.java | 10 +++ .../dto/request/diary/YearSearchRequest.java | 8 +++ .../request/feel/FeelingSearchRequest.java | 8 +++ .../selab/todo/service/DataSearchService.java | 63 +++++++++++++++++++ .../com/selab/todo/service/FindService.java | 32 ---------- 7 files changed, 146 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/selab/todo/controller/SearchController.java rename src/main/java/com/selab/todo/dto/request/diary/{MonthRequest.java => MonthSearchRequest.java} (74%) create mode 100644 src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java create mode 100644 src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java create mode 100644 src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java create mode 100644 src/main/java/com/selab/todo/service/DataSearchService.java delete mode 100644 src/main/java/com/selab/todo/service/FindService.java diff --git a/src/main/java/com/selab/todo/controller/SearchController.java b/src/main/java/com/selab/todo/controller/SearchController.java new file mode 100644 index 0000000..166f921 --- /dev/null +++ b/src/main/java/com/selab/todo/controller/SearchController.java @@ -0,0 +1,56 @@ +package com.selab.todo.controller; + +import com.selab.todo.dto.request.diary.MonthSearchRequest; +import com.selab.todo.dto.request.diary.SearchRequest; +import com.selab.todo.dto.request.diary.YearSearchRequest; +import com.selab.todo.dto.request.feel.FeelingSearchRequest; +import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.DataSearchService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/Diarys/search", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class SearchController { + private final DataSearchService dataSearchService; + private final DiaryService diaryService; + + @ApiOperation("Month 별로 조회하기") + @GetMapping(value = "/month",consumes = MediaType.APPLICATION_JSON_VALUE) + public Page searchMonth(@PageableDefault Pageable pageable, MonthSearchRequest request) { + Page diaryResponses = diaryService.getAll(pageable); + return dataSearchService.searchMonth(diaryResponses, request.getMonth()); + } + + @ApiOperation("Year 별로 조회하기") + @GetMapping(value = "/year",consumes = MediaType.APPLICATION_JSON_VALUE) + public Page searchYear(@PageableDefault Pageable pageable, YearSearchRequest request) { + Page diaryResponses = diaryService.getAll(pageable); + return dataSearchService.searchYear(diaryResponses, request.getYear()); + } + + @ApiOperation("Year, Month, Day 조회") + @GetMapping(value = "/year-month-day",consumes = MediaType.APPLICATION_JSON_VALUE) + public Page searchYear(@PageableDefault Pageable pageable, SearchRequest request) { + Page diaryResponses = diaryService.getAll(pageable); + return dataSearchService.search(diaryResponses, request); + } + + @ApiOperation("Feeling 조회") + @GetMapping(value = "/feel") + public Page searchFeeling(@PageableDefault Pageable pageable, FeelingSearchRequest request){ + Page diaryResponses = diaryService.getAll(pageable); + return dataSearchService.searchFeel(diaryResponses, request.getFeeling()); + } +} diff --git a/src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java b/src/main/java/com/selab/todo/dto/request/diary/MonthSearchRequest.java similarity index 74% rename from src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java rename to src/main/java/com/selab/todo/dto/request/diary/MonthSearchRequest.java index 963eaef..9ccb48f 100644 --- a/src/main/java/com/selab/todo/dto/request/diary/MonthRequest.java +++ b/src/main/java/com/selab/todo/dto/request/diary/MonthSearchRequest.java @@ -3,6 +3,6 @@ import lombok.Data; @Data -public class MonthRequest { +public class MonthSearchRequest { private final int month; } diff --git a/src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java b/src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java new file mode 100644 index 0000000..a1d6f48 --- /dev/null +++ b/src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java @@ -0,0 +1,10 @@ +package com.selab.todo.dto.request.diary; + +import lombok.Data; + +@Data +public class SearchRequest { + private final int year; + private final int month; + private final int day; +} diff --git a/src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java b/src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java new file mode 100644 index 0000000..5ae12b9 --- /dev/null +++ b/src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java @@ -0,0 +1,8 @@ +package com.selab.todo.dto.request.diary; + +import lombok.Data; + +@Data +public class YearSearchRequest { + private final int year; +} diff --git a/src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java b/src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java new file mode 100644 index 0000000..c83603b --- /dev/null +++ b/src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java @@ -0,0 +1,8 @@ +package com.selab.todo.dto.request.feel; + +import lombok.Data; + +@Data +public class FeelingSearchRequest { + private final String feeling; +} diff --git a/src/main/java/com/selab/todo/service/DataSearchService.java b/src/main/java/com/selab/todo/service/DataSearchService.java new file mode 100644 index 0000000..dbc0b54 --- /dev/null +++ b/src/main/java/com/selab/todo/service/DataSearchService.java @@ -0,0 +1,63 @@ +package com.selab.todo.service; + +import com.selab.todo.dto.request.diary.SearchRequest; +import com.selab.todo.dto.response.DiaryResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +@Service +@RequiredArgsConstructor +public class DataSearchService { + public Page searchMonth(Page allPage, int month) { + List middleProcess = allPage.stream() + .filter(a -> a.getMonth() == month) + .collect(Collectors.toList()); + + log.info("Month 조회. {}", month); + return makePage(middleProcess); + } + + public Page searchYear(Page allPage, int year) { + List middleProcess = allPage.stream() + .filter(a -> a.getMonth() == year) + .collect(Collectors.toList()); + + log.info("Year 조회. {}", year); + return makePage(middleProcess); + } + + public Page search(Page allPage, SearchRequest request) { + List middleProcess = allPage.stream() + .filter(a -> a.getYear() == request.getYear()) + .filter(a -> a.getMonth() == request.getMonth()) + .filter(a -> a.getDay() == a.getDay()) + .collect(Collectors.toList()); + + log.info("전체 날짜 조회."); + return makePage(middleProcess); + } + + public Page searchFeel(Page allPage, String feeling) { + List middleProcess = allPage.stream() + .filter(a -> a.getFeel().equals(feeling)) + .collect(Collectors.toList()); + + log.info("Feeling 조회. {}", feeling); + return makePage(middleProcess); + } + + private PageImpl makePage(List process) { + PageRequest pageRequest = PageRequest.of(0, 10); + int start = (int) pageRequest.getOffset(); + int end = Math.min((start + pageRequest.getPageSize()), process.size()); + return new PageImpl<>(process.subList(start, end), pageRequest, process.size()); + } +} \ No newline at end of file diff --git a/src/main/java/com/selab/todo/service/FindService.java b/src/main/java/com/selab/todo/service/FindService.java deleted file mode 100644 index c7d3486..0000000 --- a/src/main/java/com/selab/todo/service/FindService.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.selab.todo.service; - -import com.selab.todo.dto.response.DiaryResponse; -import com.selab.todo.repository.DiaryRepository; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.stream.Collectors; - -@Slf4j -@Service -@RequiredArgsConstructor -public class FindService { - public Page getMonthRange(Page allPage, int month) { - List middleProcess = allPage - .stream() - .filter(a -> a.getMonth() == month) - .collect(Collectors.toList()); - - PageRequest pageRequest = PageRequest.of(0, 10); - - int start = (int) pageRequest.getOffset(); - int end = Math.min((start + pageRequest.getPageSize()), middleProcess.size()); - return new PageImpl<>(middleProcess.subList(start, end), pageRequest, middleProcess.size()); - } -} \ No newline at end of file From d9917b30df7c27d57d603b1a7d92e7de6f28a760 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:38:14 +0900 Subject: [PATCH 35/62] update : id --- .../todo/dto/request/feel/FeelingRegisterRequest.java | 9 +++++++++ .../todo/dto/request/feel/FeelingUpdateRequest.java | 1 - .../com/selab/todo/dto/response/FeelingResponse.java | 7 +++++++ src/main/java/com/selab/todo/entity/Feeling.java | 5 +++++ .../com/selab/todo/repository/FeelingRepository.java | 2 +- 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/selab/todo/dto/request/feel/FeelingRegisterRequest.java diff --git a/src/main/java/com/selab/todo/dto/request/feel/FeelingRegisterRequest.java b/src/main/java/com/selab/todo/dto/request/feel/FeelingRegisterRequest.java new file mode 100644 index 0000000..006c26f --- /dev/null +++ b/src/main/java/com/selab/todo/dto/request/feel/FeelingRegisterRequest.java @@ -0,0 +1,9 @@ +package com.selab.todo.dto.request.feel; + +import lombok.Data; + +@Data +public class FeelingRegisterRequest { + private final Long id; + private final String feel; +} diff --git a/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java b/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java index 6b5ee6e..e101f61 100644 --- a/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java +++ b/src/main/java/com/selab/todo/dto/request/feel/FeelingUpdateRequest.java @@ -4,6 +4,5 @@ @Data public class FeelingUpdateRequest { - private final Long id; private final String feel; } diff --git a/src/main/java/com/selab/todo/dto/response/FeelingResponse.java b/src/main/java/com/selab/todo/dto/response/FeelingResponse.java index b2460ae..5238685 100644 --- a/src/main/java/com/selab/todo/dto/response/FeelingResponse.java +++ b/src/main/java/com/selab/todo/dto/response/FeelingResponse.java @@ -15,4 +15,11 @@ public static FeelingResponse from(Diary diary){ diary.getFeel() ); } + + public static FeelingResponse from(Feeling feeling){ + return new FeelingResponse( + feeling.getId(), + feeling.getFeel() + ); + } } diff --git a/src/main/java/com/selab/todo/entity/Feeling.java b/src/main/java/com/selab/todo/entity/Feeling.java index 3a63da6..6885a68 100644 --- a/src/main/java/com/selab/todo/entity/Feeling.java +++ b/src/main/java/com/selab/todo/entity/Feeling.java @@ -1,6 +1,7 @@ package com.selab.todo.entity; import lombok.AccessLevel; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -13,8 +14,12 @@ @Getter @Table(name = "feeling") @NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor public class Feeling { @Id + @Column(name = "id") + private Long id; + @Column(name = "feel") private String feel; } diff --git a/src/main/java/com/selab/todo/repository/FeelingRepository.java b/src/main/java/com/selab/todo/repository/FeelingRepository.java index 98b6d90..48deff5 100644 --- a/src/main/java/com/selab/todo/repository/FeelingRepository.java +++ b/src/main/java/com/selab/todo/repository/FeelingRepository.java @@ -3,5 +3,5 @@ import com.selab.todo.entity.Feeling; import org.springframework.data.jpa.repository.JpaRepository; -public interface FeelingRepository extends JpaRepository { +public interface FeelingRepository extends JpaRepository { } From c89f25ceb91a0945840b03fc988326300d1047af Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:38:30 +0900 Subject: [PATCH 36/62] =?UTF-8?q?fix=20:=20feeling=20id=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todo/controller/DiaryController.java | 14 ++++++------- .../com/selab/todo/service/DiaryService.java | 17 ++++++++++++--- .../selab/todo/service/FeelingService.java | 21 +++++++++++++------ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index f106f20..4812281 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -4,7 +4,7 @@ import com.selab.todo.common.dto.ResponseDto; import com.selab.todo.dto.request.diary.DiaryRegisterRequest; import com.selab.todo.dto.request.diary.DiaryUpdateRequest; -import com.selab.todo.dto.request.diary.MonthRequest; +import com.selab.todo.dto.request.diary.MonthSearchRequest; import com.selab.todo.dto.request.feel.FeelingUpdateRequest; import com.selab.todo.service.DiaryService; import com.selab.todo.service.FeelingService; @@ -27,7 +27,7 @@ @Api(tags = {"Diary API"}) @RestController -@RequestMapping(value = "/api/v1/Diarys", produces = MediaType.APPLICATION_JSON_VALUE) +@RequestMapping(value = "/api/v1/Diarys/default", produces = MediaType.APPLICATION_JSON_VALUE) @RequiredArgsConstructor public class DiaryController { private final DiaryService diaryService; @@ -50,7 +50,7 @@ public ResponseEntity get(@PathVariable Long id) { @ApiOperation(value = "Diary 범위 조회") @GetMapping(value = "/search-range-month",consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getRange( - @RequestBody MonthRequest month, + @RequestBody MonthSearchRequest month, @PageableDefault Pageable pageable ) { var response = diaryService.getRange(pageable, month.getMonth()); @@ -88,16 +88,16 @@ public ResponseEntity delete( @ApiOperation(value = "Diary 월별 삭제하기") @DeleteMapping("/delete-month") public ResponseEntity monthDelete( - @RequestBody MonthRequest request + @RequestBody MonthSearchRequest request ) { diaryService.monthDelete(request.getMonth()); return ResponseDto.noContent(); } @ApiOperation(value = "Feeling 수정하기") - @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/update/feeling") - public ResponseEntity updateFeeling(@RequestBody FeelingUpdateRequest request) { - var response = feelingService.updateFeeling(request); + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/update-feeling/{id}") + public ResponseEntity updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); return ResponseDto.ok(response); } diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index 16033cd..eee1887 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -4,8 +4,10 @@ import com.selab.todo.dto.request.diary.DiaryUpdateRequest; import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.entity.Diary; +import com.selab.todo.entity.Feeling; import com.selab.todo.exception.DiaryException; import com.selab.todo.repository.DiaryRepository; +import com.selab.todo.repository.FeelingRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -23,7 +25,8 @@ @RequiredArgsConstructor public class DiaryService { private final DiaryRepository diaryRepository; - private final FindService findService; + private final FeelingRepository feelingRepository; + private final DataSearchService dataSearchService; // 삽입 @Transactional @@ -37,9 +40,16 @@ public DiaryResponse register(DiaryRegisterRequest request) { request.getDay() ); + Feeling feeling = new Feeling( + diary.getId(), + request.getFeel() + ); + Diary savedDiary = diaryRepository.save(diary); + feelingRepository.save(feeling); log.info("Diary 등록했습니다. {}", diary.getId()); + log.info("Feeling 등록했습니다. {}", feeling.getFeel()); return DiaryResponse.from(savedDiary); } @@ -67,7 +77,7 @@ public Page getRange(Pageable pageable, int month) { log.info("Diary 범위 조회"); Page allPage = diaryRepository.findAll(pageable).map(DiaryResponse::from); - return findService.getMonthRange(allPage, month); + return dataSearchService.getMonth(allPage, month); } // 수정 @@ -111,4 +121,5 @@ public void monthDelete(int month) { } }); } -} + +} \ No newline at end of file diff --git a/src/main/java/com/selab/todo/service/FeelingService.java b/src/main/java/com/selab/todo/service/FeelingService.java index 009fa5b..f60eb48 100644 --- a/src/main/java/com/selab/todo/service/FeelingService.java +++ b/src/main/java/com/selab/todo/service/FeelingService.java @@ -1,10 +1,12 @@ package com.selab.todo.service; import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingRegisterRequest; import com.selab.todo.dto.request.feel.FeelingUpdateRequest; import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.dto.response.FeelingResponse; import com.selab.todo.entity.Diary; +import com.selab.todo.entity.Feeling; import com.selab.todo.exception.DiaryException; import com.selab.todo.repository.DiaryRepository; import com.selab.todo.repository.FeelingRepository; @@ -20,17 +22,24 @@ @RequiredArgsConstructor public class FeelingService { private final DiaryRepository diaryRepository; + private final FeelingRepository feelingRepository; @Transactional - public FeelingResponse updateFeeling(FeelingUpdateRequest request){ - Diary diary = diaryRepository.findById(request.getId()) - .orElseThrow(DiaryException::new); + public FeelingResponse register(FeelingRegisterRequest request){ + Feeling feeling = new Feeling( + request.getId(), + request.getFeel() + ); + return FeelingResponse.from(feeling); + } - diary.feelingUpdate(request.getFeel()); + @Transactional + public FeelingResponse updateFeeling(Long id, FeelingUpdateRequest request){ + Feeling feeling = feelingRepository.findById(id).orElseThrow(DiaryException::new); - log.info("Diary feeling 수정했습니다. {}", diary.getId()); + log.info("Diary feeling 수정했습니다. {}", feeling.getId()); - return FeelingResponse.from(diary); + return FeelingResponse.from(feeling); } @Transactional(readOnly = true) From b4f3ac027ab7e9ddfe270dda65dff8bacaaa7fde Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:46:43 +0900 Subject: [PATCH 37/62] =?UTF-8?q?fix=20:=20search=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/selab/todo/controller/DiaryController.java | 2 +- .../java/com/selab/todo/controller/SearchController.java | 8 ++++---- .../request/{feel => search}/FeelingSearchRequest.java | 2 +- .../dto/request/{diary => search}/MonthSearchRequest.java | 2 +- .../todo/dto/request/{diary => search}/SearchRequest.java | 2 +- .../dto/request/{diary => search}/YearSearchRequest.java | 2 +- .../java/com/selab/todo/service/DataSearchService.java | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) rename src/main/java/com/selab/todo/dto/request/{feel => search}/FeelingSearchRequest.java (69%) rename src/main/java/com/selab/todo/dto/request/{diary => search}/MonthSearchRequest.java (68%) rename src/main/java/com/selab/todo/dto/request/{diary => search}/SearchRequest.java (76%) rename src/main/java/com/selab/todo/dto/request/{diary => search}/YearSearchRequest.java (67%) diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index 4812281..300b634 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -4,7 +4,7 @@ import com.selab.todo.common.dto.ResponseDto; import com.selab.todo.dto.request.diary.DiaryRegisterRequest; import com.selab.todo.dto.request.diary.DiaryUpdateRequest; -import com.selab.todo.dto.request.diary.MonthSearchRequest; +import com.selab.todo.dto.request.search.MonthSearchRequest; import com.selab.todo.dto.request.feel.FeelingUpdateRequest; import com.selab.todo.service.DiaryService; import com.selab.todo.service.FeelingService; diff --git a/src/main/java/com/selab/todo/controller/SearchController.java b/src/main/java/com/selab/todo/controller/SearchController.java index 166f921..a6a1117 100644 --- a/src/main/java/com/selab/todo/controller/SearchController.java +++ b/src/main/java/com/selab/todo/controller/SearchController.java @@ -1,9 +1,9 @@ package com.selab.todo.controller; -import com.selab.todo.dto.request.diary.MonthSearchRequest; -import com.selab.todo.dto.request.diary.SearchRequest; -import com.selab.todo.dto.request.diary.YearSearchRequest; -import com.selab.todo.dto.request.feel.FeelingSearchRequest; +import com.selab.todo.dto.request.search.MonthSearchRequest; +import com.selab.todo.dto.request.search.SearchRequest; +import com.selab.todo.dto.request.search.YearSearchRequest; +import com.selab.todo.dto.request.search.FeelingSearchRequest; import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.service.DiaryService; import com.selab.todo.service.DataSearchService; diff --git a/src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java b/src/main/java/com/selab/todo/dto/request/search/FeelingSearchRequest.java similarity index 69% rename from src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java rename to src/main/java/com/selab/todo/dto/request/search/FeelingSearchRequest.java index c83603b..8367306 100644 --- a/src/main/java/com/selab/todo/dto/request/feel/FeelingSearchRequest.java +++ b/src/main/java/com/selab/todo/dto/request/search/FeelingSearchRequest.java @@ -1,4 +1,4 @@ -package com.selab.todo.dto.request.feel; +package com.selab.todo.dto.request.search; import lombok.Data; diff --git a/src/main/java/com/selab/todo/dto/request/diary/MonthSearchRequest.java b/src/main/java/com/selab/todo/dto/request/search/MonthSearchRequest.java similarity index 68% rename from src/main/java/com/selab/todo/dto/request/diary/MonthSearchRequest.java rename to src/main/java/com/selab/todo/dto/request/search/MonthSearchRequest.java index 9ccb48f..cfc7f6c 100644 --- a/src/main/java/com/selab/todo/dto/request/diary/MonthSearchRequest.java +++ b/src/main/java/com/selab/todo/dto/request/search/MonthSearchRequest.java @@ -1,4 +1,4 @@ -package com.selab.todo.dto.request.diary; +package com.selab.todo.dto.request.search; import lombok.Data; diff --git a/src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java b/src/main/java/com/selab/todo/dto/request/search/SearchRequest.java similarity index 76% rename from src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java rename to src/main/java/com/selab/todo/dto/request/search/SearchRequest.java index a1d6f48..6b5be1a 100644 --- a/src/main/java/com/selab/todo/dto/request/diary/SearchRequest.java +++ b/src/main/java/com/selab/todo/dto/request/search/SearchRequest.java @@ -1,4 +1,4 @@ -package com.selab.todo.dto.request.diary; +package com.selab.todo.dto.request.search; import lombok.Data; diff --git a/src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java b/src/main/java/com/selab/todo/dto/request/search/YearSearchRequest.java similarity index 67% rename from src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java rename to src/main/java/com/selab/todo/dto/request/search/YearSearchRequest.java index 5ae12b9..19d8f2c 100644 --- a/src/main/java/com/selab/todo/dto/request/diary/YearSearchRequest.java +++ b/src/main/java/com/selab/todo/dto/request/search/YearSearchRequest.java @@ -1,4 +1,4 @@ -package com.selab.todo.dto.request.diary; +package com.selab.todo.dto.request.search; import lombok.Data; diff --git a/src/main/java/com/selab/todo/service/DataSearchService.java b/src/main/java/com/selab/todo/service/DataSearchService.java index dbc0b54..dc6cedb 100644 --- a/src/main/java/com/selab/todo/service/DataSearchService.java +++ b/src/main/java/com/selab/todo/service/DataSearchService.java @@ -1,6 +1,6 @@ package com.selab.todo.service; -import com.selab.todo.dto.request.diary.SearchRequest; +import com.selab.todo.dto.request.search.SearchRequest; import com.selab.todo.dto.response.DiaryResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; From c4000052203e11af01656ed14364601022cbb84f Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:55:32 +0900 Subject: [PATCH 38/62] =?UTF-8?q?update=20:=20=EC=BF=BC=EB=A6=AC=EB=AC=B8?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9B=94=EB=B3=84=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todo/repository/DiaryRepository.java | 12 ++++++++++ .../com/selab/todo/service/DiaryService.java | 24 +------------------ 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/selab/todo/repository/DiaryRepository.java b/src/main/java/com/selab/todo/repository/DiaryRepository.java index b05ab55..7d9d3e6 100644 --- a/src/main/java/com/selab/todo/repository/DiaryRepository.java +++ b/src/main/java/com/selab/todo/repository/DiaryRepository.java @@ -2,8 +2,20 @@ import com.selab.todo.entity.Diary; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface DiaryRepository extends JpaRepository { + + //월별 삭제 쿼리 + @Query( + value = "delete diary" + + "where month > : month" + ,nativeQuery = true + ) + public List deleteDiariesByMonth(@Param(value = "month") int month); } diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index eee1887..034b4c3 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -71,15 +71,6 @@ public Page getAll(Pageable pageable) { .map(DiaryResponse::from); } - //범위 조회 - @Transactional(readOnly = true) - public Page getRange(Pageable pageable, int month) { - log.info("Diary 범위 조회"); - Page allPage = diaryRepository.findAll(pageable).map(DiaryResponse::from); - - return dataSearchService.getMonth(allPage, month); - } - // 수정 @Transactional public DiaryResponse update(Long id, DiaryUpdateRequest request) { @@ -106,20 +97,7 @@ public void delete(Long id) { //월별 삭제 @Transactional public void monthDelete(int month) { - List deleteId = new LinkedList<>(); - - diaryRepository.findAll().forEach(eachDiary -> { - if (eachDiary.getMonth() == month) { - deleteId.add(eachDiary.getId()); - } - }); - - deleteId.forEach(id -> { - if (diaryRepository.existsById(id)) { - diaryRepository.deleteById(id); - log.info("Month Diary 삭제했습니다. {}", id); - } - }); + List deleteId = diaryRepository.deleteDiariesByMonth(month); } } \ No newline at end of file From 0f0e2294f22bf0945866b71472a281fdd6773d90 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:56:24 +0900 Subject: [PATCH 39/62] =?UTF-8?q?fix=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=97=86=EB=8A=94=20=EB=B3=80=EC=88=98=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/service/DiaryService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index 034b4c3..e94d190 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -97,7 +97,7 @@ public void delete(Long id) { //월별 삭제 @Transactional public void monthDelete(int month) { - List deleteId = diaryRepository.deleteDiariesByMonth(month); + diaryRepository.deleteDiariesByMonth(month); } } \ No newline at end of file From d16cb02b84771cdc6482556800cf95230baa5958 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 14:57:11 +0900 Subject: [PATCH 40/62] =?UTF-8?q?fix=20:=20=EC=A4=91=EB=B3=B5=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/controller/DiaryController.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index 300b634..420b411 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -47,16 +47,6 @@ public ResponseEntity get(@PathVariable Long id) { return ResponseDto.ok(response); } - @ApiOperation(value = "Diary 범위 조회") - @GetMapping(value = "/search-range-month",consumes = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity getRange( - @RequestBody MonthSearchRequest month, - @PageableDefault Pageable pageable - ) { - var response = diaryService.getRange(pageable, month.getMonth()); - return PageDto.ok(response); - } - @ApiOperation(value = "Diary 전체 조회하기") @GetMapping("/search-all") public ResponseEntity getAll( From f31e595e9f1ef8f03e4606910bf6f893126b4a7d Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 15:04:35 +0900 Subject: [PATCH 41/62] =?UTF-8?q?fix=20:=20url=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/controller/DiaryController.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index 420b411..648b8a8 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -76,23 +76,21 @@ public ResponseEntity delete( } @ApiOperation(value = "Diary 월별 삭제하기") - @DeleteMapping("/delete-month") - public ResponseEntity monthDelete( - @RequestBody MonthSearchRequest request - ) { - diaryService.monthDelete(request.getMonth()); + @DeleteMapping("/month/{month}") + public ResponseEntity monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); return ResponseDto.noContent(); } @ApiOperation(value = "Feeling 수정하기") - @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/update-feeling/{id}") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") public ResponseEntity updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { var response = feelingService.updateFeeling(id, request); return ResponseDto.ok(response); } @ApiOperation(value = "Feeling 전체 조회하기") - @GetMapping("/serach/feeling/all") + @GetMapping("/feeling-all") public ResponseEntity getAllFeeling(@PageableDefault Pageable pageable) { var response = feelingService.getAllFeeling(pageable); return PageDto.ok(response); From bdccfdd5b52048965e5a3697ea6248c7719a8df5 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 20 Feb 2023 15:04:47 +0900 Subject: [PATCH 42/62] =?UTF-8?q?fix=20:=20ApiOperation=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/selab/todo/controller/SearchController.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/SearchController.java b/src/main/java/com/selab/todo/controller/SearchController.java index a6a1117..7977883 100644 --- a/src/main/java/com/selab/todo/controller/SearchController.java +++ b/src/main/java/com/selab/todo/controller/SearchController.java @@ -1,5 +1,6 @@ package com.selab.todo.controller; +import com.selab.todo.common.dto.PageDto; import com.selab.todo.dto.request.search.MonthSearchRequest; import com.selab.todo.dto.request.search.SearchRequest; import com.selab.todo.dto.request.search.YearSearchRequest; @@ -14,6 +15,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -26,28 +28,28 @@ public class SearchController { private final DataSearchService dataSearchService; private final DiaryService diaryService; - @ApiOperation("Month 별로 조회하기") + @ApiOperation("Month 별로 검색") @GetMapping(value = "/month",consumes = MediaType.APPLICATION_JSON_VALUE) public Page searchMonth(@PageableDefault Pageable pageable, MonthSearchRequest request) { Page diaryResponses = diaryService.getAll(pageable); return dataSearchService.searchMonth(diaryResponses, request.getMonth()); } - @ApiOperation("Year 별로 조회하기") + @ApiOperation("Year 별로 검색") @GetMapping(value = "/year",consumes = MediaType.APPLICATION_JSON_VALUE) public Page searchYear(@PageableDefault Pageable pageable, YearSearchRequest request) { Page diaryResponses = diaryService.getAll(pageable); return dataSearchService.searchYear(diaryResponses, request.getYear()); } - @ApiOperation("Year, Month, Day 조회") + @ApiOperation("Year, Month, Day 검색") @GetMapping(value = "/year-month-day",consumes = MediaType.APPLICATION_JSON_VALUE) public Page searchYear(@PageableDefault Pageable pageable, SearchRequest request) { Page diaryResponses = diaryService.getAll(pageable); return dataSearchService.search(diaryResponses, request); } - @ApiOperation("Feeling 조회") + @ApiOperation("Feeling 검색") @GetMapping(value = "/feel") public Page searchFeeling(@PageableDefault Pageable pageable, FeelingSearchRequest request){ Page diaryResponses = diaryService.getAll(pageable); From 98472353d9964627664b07e6ce10e028e54dc770 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Wed, 22 Feb 2023 11:12:47 +0900 Subject: [PATCH 43/62] =?UTF-8?q?fix=20:=20url=20=EB=8C=80=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/controller/DiaryController.java | 2 +- src/main/java/com/selab/todo/controller/SearchController.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index 648b8a8..2556a5f 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -27,7 +27,7 @@ @Api(tags = {"Diary API"}) @RestController -@RequestMapping(value = "/api/v1/Diarys/default", produces = MediaType.APPLICATION_JSON_VALUE) +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) @RequiredArgsConstructor public class DiaryController { private final DiaryService diaryService; diff --git a/src/main/java/com/selab/todo/controller/SearchController.java b/src/main/java/com/selab/todo/controller/SearchController.java index 7977883..cbb3cf2 100644 --- a/src/main/java/com/selab/todo/controller/SearchController.java +++ b/src/main/java/com/selab/todo/controller/SearchController.java @@ -22,7 +22,7 @@ @Api(tags = {"Diary API"}) @RestController -@RequestMapping(value = "/api/v1/Diarys/search", produces = MediaType.APPLICATION_JSON_VALUE) +@RequestMapping(value = "/api/v1/diaries/search", produces = MediaType.APPLICATION_JSON_VALUE) @RequiredArgsConstructor public class SearchController { private final DataSearchService dataSearchService; From 23e1bda2afa5e1ac84ef97599aadd2dbd60a4824 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Wed, 22 Feb 2023 12:03:52 +0900 Subject: [PATCH 44/62] =?UTF-8?q?fix=20:=20=EC=9B=90=EC=8B=9C=ED=83=80?= =?UTF-8?q?=EC=9E=85=20=EC=B0=B8=EC=A1=B0=ED=83=80=EC=9E=85=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/entity/Diary.java | 11 +++++++---- .../java/com/selab/todo/service/DiaryService.java | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index 0eaf520..6283ca6 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -12,7 +12,10 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; +import java.time.DayOfWeek; import java.time.LocalDateTime; +import java.time.Month; +import java.time.Year; @Entity @Getter @@ -35,19 +38,19 @@ public class Diary extends BaseEntity { private LocalDateTime createdAt; @Column(name = "year") - private int year; + private Year year; @Column(name = "month") - private int month; + private Month month; @Column(name = "day") - private int day; + private DayOfWeek day; @Column(name = "feel") private String feel; - public Diary(String title, String content, String feel, int year, int month, int day) { + public Diary(String title, String content, String feel, Year year, Month month, DayOfWeek day) { this.title = title; this.content = content; this.feel = feel; diff --git a/src/main/java/com/selab/todo/service/DiaryService.java b/src/main/java/com/selab/todo/service/DiaryService.java index e94d190..dee688c 100644 --- a/src/main/java/com/selab/todo/service/DiaryService.java +++ b/src/main/java/com/selab/todo/service/DiaryService.java @@ -16,6 +16,9 @@ import org.springframework.transaction.annotation.Transactional; +import java.time.DayOfWeek; +import java.time.Month; +import java.time.Year; import java.util.LinkedList; import java.util.List; @@ -35,9 +38,9 @@ public DiaryResponse register(DiaryRegisterRequest request) { request.getTitle(), request.getContent(), request.getFeel(), - request.getYear(), - request.getMonth(), - request.getDay() + Year.of(request.getYear()), + Month.of(request.getMonth()), + DayOfWeek.of(request.getDay()) ); Feeling feeling = new Feeling( From 4470800fda9b277bb608566d57b975c4d4ca3136 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:22:06 +0900 Subject: [PATCH 45/62] =?UTF-8?q?update=20:=20persistence=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0=EC=9D=84=20=EC=9C=84=ED=95=9C=20xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/META-INF/persistence.xml | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/resources/META-INF/persistence.xml diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..29b66a6 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 3abb773de7d60c9a79cc79c380d198b32f94daa4 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:22:52 +0900 Subject: [PATCH 46/62] fix : cleanCode --- src/main/java/com/selab/todo/controller/SearchController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/selab/todo/controller/SearchController.java b/src/main/java/com/selab/todo/controller/SearchController.java index cbb3cf2..25ec1d7 100644 --- a/src/main/java/com/selab/todo/controller/SearchController.java +++ b/src/main/java/com/selab/todo/controller/SearchController.java @@ -55,4 +55,4 @@ public Page searchFeeling(@PageableDefault Pageable pageable, Fee Page diaryResponses = diaryService.getAll(pageable); return dataSearchService.searchFeel(diaryResponses, request.getFeeling()); } -} +} \ No newline at end of file From bb141067ebc15b5f4bbad90f18f974896545f49a Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:23:08 +0900 Subject: [PATCH 47/62] =?UTF-8?q?fix=20:=20=EB=B3=80=EC=88=98=ED=98=95=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/selab/todo/dto/response/DiaryResponse.java | 9 ++++++--- .../java/com/selab/todo/service/DataSearchService.java | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java index 92b536a..ad87145 100644 --- a/src/main/java/com/selab/todo/dto/response/DiaryResponse.java +++ b/src/main/java/com/selab/todo/dto/response/DiaryResponse.java @@ -3,7 +3,10 @@ import com.selab.todo.entity.Diary; import lombok.Data; +import java.time.DayOfWeek; import java.time.LocalDateTime; +import java.time.Month; +import java.time.Year; @Data public class DiaryResponse { @@ -12,9 +15,9 @@ public class DiaryResponse { private final String content; private final LocalDateTime localDateTime; private final String feel; - private final int year; - private final int month; - private final int day; + private final Year year; + private final Month month; + private final DayOfWeek day; public static DiaryResponse from(Diary diary) { return new DiaryResponse( diff --git a/src/main/java/com/selab/todo/service/DataSearchService.java b/src/main/java/com/selab/todo/service/DataSearchService.java index dc6cedb..e787432 100644 --- a/src/main/java/com/selab/todo/service/DataSearchService.java +++ b/src/main/java/com/selab/todo/service/DataSearchService.java @@ -18,7 +18,7 @@ public class DataSearchService { public Page searchMonth(Page allPage, int month) { List middleProcess = allPage.stream() - .filter(a -> a.getMonth() == month) + .filter(a -> a.getMonth().getValue() == month) .collect(Collectors.toList()); log.info("Month 조회. {}", month); @@ -27,7 +27,7 @@ public Page searchMonth(Page allPage, int month) { public Page searchYear(Page allPage, int year) { List middleProcess = allPage.stream() - .filter(a -> a.getMonth() == year) + .filter(a -> a.getYear().getValue() == year) .collect(Collectors.toList()); log.info("Year 조회. {}", year); @@ -36,8 +36,8 @@ public Page searchYear(Page allPage, int year) { public Page search(Page allPage, SearchRequest request) { List middleProcess = allPage.stream() - .filter(a -> a.getYear() == request.getYear()) - .filter(a -> a.getMonth() == request.getMonth()) + .filter(a -> a.getYear().getValue() == request.getYear()) + .filter(a -> a.getMonth().getValue() == request.getMonth()) .filter(a -> a.getDay() == a.getDay()) .collect(Collectors.toList()); From 4783817fc444ed316e5637a70e3ecfd9a077fb90 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:23:30 +0900 Subject: [PATCH 48/62] =?UTF-8?q?update=20:=20Persistence=20DB=20=EC=97=B0?= =?UTF-8?q?=EB=8F=99=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../selab/todo/service/CriteriaService.java | 50 +++++++++++++++++++ .../com/selab/todo/CriteriaServiceTest.java | 44 ++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/main/java/com/selab/todo/service/CriteriaService.java create mode 100644 src/test/java/com/selab/todo/CriteriaServiceTest.java diff --git a/src/main/java/com/selab/todo/service/CriteriaService.java b/src/main/java/com/selab/todo/service/CriteriaService.java new file mode 100644 index 0000000..84fdec4 --- /dev/null +++ b/src/main/java/com/selab/todo/service/CriteriaService.java @@ -0,0 +1,50 @@ +package com.selab.todo.service; + +import com.selab.todo.entity.Diary; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import java.time.DayOfWeek; +import java.time.Month; +import java.time.Year; + +@Slf4j +@Service +@RequiredArgsConstructor +public class CriteriaService { + + @Transactional + public void get(){ + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); + + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + EntityTransaction transaction = entityManager.getTransaction(); + transaction.begin(); + + try{ + Diary diary = new Diary( + "Test", + "test", + "Good", + Year.now(), + Month.of(2), + DayOfWeek.MONDAY + ); + + entityManager.persist(diary); + }catch (Exception e){ + transaction.rollback(); + }finally { + entityManager.close(); + } + + entityManagerFactory.close(); + } +} diff --git a/src/test/java/com/selab/todo/CriteriaServiceTest.java b/src/test/java/com/selab/todo/CriteriaServiceTest.java new file mode 100644 index 0000000..8bafafe --- /dev/null +++ b/src/test/java/com/selab/todo/CriteriaServiceTest.java @@ -0,0 +1,44 @@ +package com.selab.todo; + +import com.selab.todo.entity.Diary; +import org.junit.jupiter.api.Test; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import java.time.DayOfWeek; +import java.time.Month; +import java.time.Year; + +public class CriteriaServiceTest { + + @Test + public void a(){ + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); + + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + EntityTransaction transaction = entityManager.getTransaction(); + transaction.begin(); + + try{ + Diary diary = new Diary( + "Test", + "test", + "Good", + Year.now(), + Month.of(2), + DayOfWeek.MONDAY + ); + + entityManager.persist(diary); + }catch (Exception e){ + transaction.rollback(); + }finally { + entityManager.close(); + } + + entityManagerFactory.close(); + } +} From cb0b4843a96229e4d33b86fe08d85937371d2143 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:24:08 +0900 Subject: [PATCH 49/62] none --- .../java/com/selab/todo/SelabDiaryApplicationTests.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java b/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java index 7c6cda8..825556c 100644 --- a/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java +++ b/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java @@ -1,6 +1,7 @@ package com.selab.todo; import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.entity.Diary; import com.selab.todo.repository.DiaryRepository; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -8,10 +9,15 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; import java.util.List; import java.util.stream.Collectors; @SpringBootTest class SelabDiaryApplicationTests { - } From e2f77e909ad36129610972b05a518d46b4a4e0ec Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:51:30 +0900 Subject: [PATCH 50/62] fix : GenerateValue --- src/main/java/com/selab/todo/entity/Feeling.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/selab/todo/entity/Feeling.java b/src/main/java/com/selab/todo/entity/Feeling.java index 6885a68..eb1a91a 100644 --- a/src/main/java/com/selab/todo/entity/Feeling.java +++ b/src/main/java/com/selab/todo/entity/Feeling.java @@ -5,10 +5,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; @Entity @Getter @@ -17,6 +14,7 @@ @AllArgsConstructor public class Feeling { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; From 01085382ba72ec2f2dc10c2a26859e20c966b354 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:51:43 +0900 Subject: [PATCH 51/62] fix : Entity name --- src/main/java/com/selab/todo/entity/Diary.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index 6283ca6..d1d7a71 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -17,7 +17,7 @@ import java.time.Month; import java.time.Year; -@Entity +@Entity(name = "Diary") @Getter @Table(name = "diary") @NoArgsConstructor(access = AccessLevel.PROTECTED) From 067294b0cf46c2a9157e0008d33f5dcb61bdfb34 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:52:13 +0900 Subject: [PATCH 52/62] update : feelingSearch --- .../selab/todo/service/CriteriaService.java | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/selab/todo/service/CriteriaService.java b/src/main/java/com/selab/todo/service/CriteriaService.java index 84fdec4..4493a2d 100644 --- a/src/main/java/com/selab/todo/service/CriteriaService.java +++ b/src/main/java/com/selab/todo/service/CriteriaService.java @@ -1,8 +1,12 @@ package com.selab.todo.service; +import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.entity.Diary; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -10,41 +14,55 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; import java.time.DayOfWeek; import java.time.Month; import java.time.Year; +import java.util.ArrayList; +import java.util.List; @Slf4j @Service @RequiredArgsConstructor public class CriteriaService { - @Transactional - public void get(){ - EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); + private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); + @Transactional + public Page searchFeel(String feeling){ EntityManager entityManager = entityManagerFactory.createEntityManager(); + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Diary.class); + Root root = criteriaQuery.from(Diary.class); + + Predicate feelEqual = criteriaBuilder.equal(root.get("feeling"), feeling); + + javax.persistence.criteria.Order idDesc = criteriaBuilder.desc(root.get("id")); + + criteriaQuery.select(root) + .where(feelEqual) + .orderBy(idDesc); + + List result = entityManager.createQuery(criteriaQuery).getResultList(); + + List mapping = new ArrayList<>(); + + result.stream() + .forEach(a->{ + mapping.add(DiaryResponse.from(a)); + }); + + return makePage(mapping); + } - EntityTransaction transaction = entityManager.getTransaction(); - transaction.begin(); - - try{ - Diary diary = new Diary( - "Test", - "test", - "Good", - Year.now(), - Month.of(2), - DayOfWeek.MONDAY - ); - - entityManager.persist(diary); - }catch (Exception e){ - transaction.rollback(); - }finally { - entityManager.close(); - } - - entityManagerFactory.close(); + private PageImpl makePage(List process) { + PageRequest pageRequest = PageRequest.of(0, 10); + int start = (int) pageRequest.getOffset(); + int end = Math.min((start + pageRequest.getPageSize()), process.size()); + return new PageImpl<>(process.subList(start, end), pageRequest, process.size()); } } From 98c03745956e7b547cdb3a2f523032b28fcda7f1 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:52:22 +0900 Subject: [PATCH 53/62] update : feelingSearch --- .../todo/controller/CriteriaController.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/com/selab/todo/controller/CriteriaController.java diff --git a/src/main/java/com/selab/todo/controller/CriteriaController.java b/src/main/java/com/selab/todo/controller/CriteriaController.java new file mode 100644 index 0000000..9710909 --- /dev/null +++ b/src/main/java/com/selab/todo/controller/CriteriaController.java @@ -0,0 +1,30 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.service.CriteriaService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries/criteria", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class CriteriaController { + + private final CriteriaService criteriaService; + + @ApiOperation("Creieria Feeling Search") + @GetMapping("/search-feeling/{feeling}") + public ResponseEntity searchFeeling(@PathVariable String feeling){ + var response = criteriaService.searchFeel(feeling); + + return ResponseDto.ok(response); + } +} From 9f94913d783a863e9e0701304a5072dd2270de36 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Sat, 25 Feb 2023 20:52:41 +0900 Subject: [PATCH 54/62] delete --- .../com/selab/todo/CriteriaServiceTest.java | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 src/test/java/com/selab/todo/CriteriaServiceTest.java diff --git a/src/test/java/com/selab/todo/CriteriaServiceTest.java b/src/test/java/com/selab/todo/CriteriaServiceTest.java deleted file mode 100644 index 8bafafe..0000000 --- a/src/test/java/com/selab/todo/CriteriaServiceTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.selab.todo; - -import com.selab.todo.entity.Diary; -import org.junit.jupiter.api.Test; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; -import java.time.DayOfWeek; -import java.time.Month; -import java.time.Year; - -public class CriteriaServiceTest { - - @Test - public void a(){ - EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); - - EntityManager entityManager = entityManagerFactory.createEntityManager(); - - EntityTransaction transaction = entityManager.getTransaction(); - transaction.begin(); - - try{ - Diary diary = new Diary( - "Test", - "test", - "Good", - Year.now(), - Month.of(2), - DayOfWeek.MONDAY - ); - - entityManager.persist(diary); - }catch (Exception e){ - transaction.rollback(); - }finally { - entityManager.close(); - } - - entityManagerFactory.close(); - } -} From e8f6a510fe4884ed34f99f64406f474182087af5 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 27 Feb 2023 21:23:58 +0900 Subject: [PATCH 55/62] fix : cleancode --- .../com/selab/todo/SelabTodoApplication.java | 2 + .../todo/controller/CriteriaController.java | 27 +++++--- .../todo/controller/DiaryController.java | 1 - .../todo/controller/SearchController.java | 2 - .../request/diary/DiaryRegisterRequest.java | 1 - .../java/com/selab/todo/entity/Diary.java | 2 +- .../java/com/selab/todo/entity/Feeling.java | 2 +- .../selab/todo/service/CriteriaService.java | 62 ++++++++++++++++++- 8 files changed, 84 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/selab/todo/SelabTodoApplication.java b/src/main/java/com/selab/todo/SelabTodoApplication.java index b223492..18ac509 100644 --- a/src/main/java/com/selab/todo/SelabTodoApplication.java +++ b/src/main/java/com/selab/todo/SelabTodoApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +@EntityScan(basePackages = {"com.selab.todo.entity"}) @SpringBootApplication public class SelabTodoApplication { public static void main(String[] args) { diff --git a/src/main/java/com/selab/todo/controller/CriteriaController.java b/src/main/java/com/selab/todo/controller/CriteriaController.java index 9710909..536d6c4 100644 --- a/src/main/java/com/selab/todo/controller/CriteriaController.java +++ b/src/main/java/com/selab/todo/controller/CriteriaController.java @@ -1,16 +1,14 @@ package com.selab.todo.controller; import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; import com.selab.todo.service.CriteriaService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Api(tags = {"Diary API"}) @RestController @@ -20,11 +18,24 @@ public class CriteriaController { private final CriteriaService criteriaService; - @ApiOperation("Creieria Feeling Search") - @GetMapping("/search-feeling/{feeling}") - public ResponseEntity searchFeeling(@PathVariable String feeling){ - var response = criteriaService.searchFeel(feeling); + @ApiOperation("Criteria Register") + @PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity register(@RequestBody DiaryRegisterRequest request){ + var response = criteriaService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation("Criteria 단건 조회") + @GetMapping("/{id}") + public ResponseEntity get(@PathVariable Long id){ + var response = criteriaService.get(id); + return ResponseDto.ok(response); + } + @ApiOperation("Criteria 전체 조회") + @GetMapping(value = "/getAll") + public ResponseEntity getAll(){ + var response = criteriaService.getAll(); return ResponseDto.ok(response); } } diff --git a/src/main/java/com/selab/todo/controller/DiaryController.java b/src/main/java/com/selab/todo/controller/DiaryController.java index 2556a5f..73ff99c 100644 --- a/src/main/java/com/selab/todo/controller/DiaryController.java +++ b/src/main/java/com/selab/todo/controller/DiaryController.java @@ -4,7 +4,6 @@ import com.selab.todo.common.dto.ResponseDto; import com.selab.todo.dto.request.diary.DiaryRegisterRequest; import com.selab.todo.dto.request.diary.DiaryUpdateRequest; -import com.selab.todo.dto.request.search.MonthSearchRequest; import com.selab.todo.dto.request.feel.FeelingUpdateRequest; import com.selab.todo.service.DiaryService; import com.selab.todo.service.FeelingService; diff --git a/src/main/java/com/selab/todo/controller/SearchController.java b/src/main/java/com/selab/todo/controller/SearchController.java index 25ec1d7..495b2d5 100644 --- a/src/main/java/com/selab/todo/controller/SearchController.java +++ b/src/main/java/com/selab/todo/controller/SearchController.java @@ -1,6 +1,5 @@ package com.selab.todo.controller; -import com.selab.todo.common.dto.PageDto; import com.selab.todo.dto.request.search.MonthSearchRequest; import com.selab.todo.dto.request.search.SearchRequest; import com.selab.todo.dto.request.search.YearSearchRequest; @@ -15,7 +14,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java b/src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java index b14322b..625ea5f 100644 --- a/src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java +++ b/src/main/java/com/selab/todo/dto/request/diary/DiaryRegisterRequest.java @@ -1,6 +1,5 @@ package com.selab.todo.dto.request.diary; -import lombok.AllArgsConstructor; import lombok.Data; @Data diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index d1d7a71..c7931da 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -17,7 +17,7 @@ import java.time.Month; import java.time.Year; -@Entity(name = "Diary") +@Entity(name = "diary") @Getter @Table(name = "diary") @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/com/selab/todo/entity/Feeling.java b/src/main/java/com/selab/todo/entity/Feeling.java index eb1a91a..e68da0a 100644 --- a/src/main/java/com/selab/todo/entity/Feeling.java +++ b/src/main/java/com/selab/todo/entity/Feeling.java @@ -7,7 +7,7 @@ import javax.persistence.*; -@Entity +@Entity(name = "feeling") @Getter @Table(name = "feeling") @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/com/selab/todo/service/CriteriaService.java b/src/main/java/com/selab/todo/service/CriteriaService.java index 4493a2d..b2f309d 100644 --- a/src/main/java/com/selab/todo/service/CriteriaService.java +++ b/src/main/java/com/selab/todo/service/CriteriaService.java @@ -1,5 +1,6 @@ package com.selab.todo.service; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; import com.selab.todo.dto.response.DiaryResponse; import com.selab.todo.entity.Diary; import lombok.RequiredArgsConstructor; @@ -29,7 +30,66 @@ @RequiredArgsConstructor public class CriteriaService { - private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); + private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("diary"); + + @Transactional + public DiaryResponse register(DiaryRegisterRequest request){ + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + + Diary diary = new Diary( + request.getTitle(), + request.getContent(), + request.getFeel(), + Year.of(request.getYear()), + Month.of(request.getMonth()), + DayOfWeek.of(request.getDay()) + ); + + entityManager.persist(diary); + + return DiaryResponse.from(diary); + } + + @Transactional + public DiaryResponse get(Long id){ + EntityManager entityManager = entityManagerFactory.createEntityManager(); + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Diary.class); + Root root = criteriaQuery.from(Diary.class); + + Predicate idEquals = criteriaBuilder.equal(root.get("id"), id); + + criteriaQuery.select(root).where(idEquals); + + Diary diary = entityManager.createQuery(criteriaQuery).getSingleResult(); + + return DiaryResponse.from(diary); + } + + @Transactional + public Page getAll(){ + EntityManager entityManager = entityManagerFactory.createEntityManager(); + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Diary.class); + Root root = criteriaQuery.from(Diary.class); + + criteriaQuery.select(root); + + List result = entityManager.createQuery(criteriaQuery).getResultList(); + + List mapping = new ArrayList<>(); + + result.stream() + .forEach(a->{ + mapping.add(DiaryResponse.from(a)); + }); + + return makePage(mapping); + } + @Transactional public Page searchFeel(String feeling){ From d5a466108d2ab2115e8da012cae55ca696198cce Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 27 Feb 2023 21:24:13 +0900 Subject: [PATCH 56/62] fix : persistence name --- src/main/resources/META-INF/persistence.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 29b66a6..82a1dd4 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"> - + From 7be9377e22896c2837fdad5f45ffbd39dc313699 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Mar 2023 09:45:59 +0900 Subject: [PATCH 57/62] =?UTF-8?q?fix=20:=20Entity=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/META-INF/persistence.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 82a1dd4..8f7c33c 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -5,6 +5,7 @@ + com.selab.todo.entity.Diary From 0976e814782ec248308a770cbbf82cb5547d2b15 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Mar 2023 09:46:41 +0900 Subject: [PATCH 58/62] update : JpaSpecificationExecutor --- src/main/java/com/selab/todo/repository/DiaryRepository.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/selab/todo/repository/DiaryRepository.java b/src/main/java/com/selab/todo/repository/DiaryRepository.java index 7d9d3e6..4cd6bde 100644 --- a/src/main/java/com/selab/todo/repository/DiaryRepository.java +++ b/src/main/java/com/selab/todo/repository/DiaryRepository.java @@ -2,6 +2,7 @@ import com.selab.todo.entity.Diary; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -9,7 +10,7 @@ import java.util.List; @Repository -public interface DiaryRepository extends JpaRepository { +public interface DiaryRepository extends JpaRepository, JpaSpecificationExecutor { //월별 삭제 쿼리 @Query( From ff93cf7e8b154115a48df614b448d525e57aa2cc Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Mar 2023 09:47:05 +0900 Subject: [PATCH 59/62] fix : cleancode --- .../java/com/selab/todo/entity/Diary.java | 2 +- .../selab/todo/service/CriteriaService.java | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/selab/todo/entity/Diary.java b/src/main/java/com/selab/todo/entity/Diary.java index c7931da..3773bdc 100644 --- a/src/main/java/com/selab/todo/entity/Diary.java +++ b/src/main/java/com/selab/todo/entity/Diary.java @@ -68,4 +68,4 @@ public void update(String title, String content, String feel) { public void feelingUpdate(String feel){ this.feel = feel; } -} +} \ No newline at end of file diff --git a/src/main/java/com/selab/todo/service/CriteriaService.java b/src/main/java/com/selab/todo/service/CriteriaService.java index b2f309d..740b5b3 100644 --- a/src/main/java/com/selab/todo/service/CriteriaService.java +++ b/src/main/java/com/selab/todo/service/CriteriaService.java @@ -30,12 +30,13 @@ @RequiredArgsConstructor public class CriteriaService { - private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("diary"); + private final EntityManagerFactory entityManagerFactory = + Persistence.createEntityManagerFactory("diary"); @Transactional public DiaryResponse register(DiaryRegisterRequest request){ EntityManager entityManager = entityManagerFactory.createEntityManager(); - + EntityTransaction entityTransaction = entityManager.getTransaction(); Diary diary = new Diary( request.getTitle(), @@ -46,7 +47,19 @@ public DiaryResponse register(DiaryRegisterRequest request){ DayOfWeek.of(request.getDay()) ); - entityManager.persist(diary); + try{ + entityTransaction.begin(); + + entityManager.persist(diary); + + entityTransaction.commit(); + }catch (Exception e){ + log.info("Criteria Error. {}", e); + entityTransaction.rollback(); + + }finally { + entityManager.close(); + } return DiaryResponse.from(diary); } From 178fd360c6c6664b86aace1e992372637c304561 Mon Sep 17 00:00:00 2001 From: KimKiHyun0206 Date: Mon, 6 Mar 2023 09:47:22 +0900 Subject: [PATCH 60/62] fix : cleancode --- src/test/java/com/selab/todo/SelabDiaryApplicationTests.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java b/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java index 825556c..7ba6acd 100644 --- a/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java +++ b/src/test/java/com/selab/todo/SelabDiaryApplicationTests.java @@ -9,12 +9,16 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; +import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; +import java.time.DayOfWeek; +import java.time.Month; +import java.time.Year; import java.util.List; import java.util.stream.Collectors; From 59a7c6d230791818ef94ebc8b9d1e723cb91676c Mon Sep 17 00:00:00 2001 From: usr Date: Sun, 22 Oct 2023 21:49:57 +0900 Subject: [PATCH 61/62] update : PersistenceService --- build.gradle | 1 - .../todo/service/PersistenceService.java | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/selab/todo/service/PersistenceService.java diff --git a/build.gradle b/build.gradle index b3e5220..7393a6e 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,6 @@ dependencies { // swagger implementation "io.springfox:springfox-boot-starter:3.0.0" - } tasks.named('test') { diff --git a/src/main/java/com/selab/todo/service/PersistenceService.java b/src/main/java/com/selab/todo/service/PersistenceService.java new file mode 100644 index 0000000..504b2ba --- /dev/null +++ b/src/main/java/com/selab/todo/service/PersistenceService.java @@ -0,0 +1,35 @@ +package com.selab.todo.service; + +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.response.DiaryResponse; +import com.selab.todo.entity.Diary; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.time.DayOfWeek; +import java.time.Month; +import java.time.Year; + +public class PersistenceService { + + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("diaryManager"); + + @Transactional + public DiaryResponse register(DiaryRegisterRequest request){ + Diary diary = new Diary( + request.getTitle(), + request.getContent(), + request.getFeel(), + Year.of(request.getYear()), + Month.of(request.getMonth()), + DayOfWeek.of(request.getDay()) + ); + + EntityManager entityManager = entityManagerFactory.createEntityManager(); + entityManager.persist(diary); + + return DiaryResponse.from(diary); + } +} From c05180be9f39ba85b128898ef85195552b832ca2 Mon Sep 17 00:00:00 2001 From: usr Date: Wed, 15 May 2024 20:19:49 +0900 Subject: [PATCH 62/62] =?UTF-8?q?update=20:=20=EC=A4=84=EB=B0=94=EA=BF=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/selab/todo/common/dto/PageDto.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/selab/todo/common/dto/PageDto.java b/src/main/java/com/selab/todo/common/dto/PageDto.java index 829bec9..6df6c0b 100644 --- a/src/main/java/com/selab/todo/common/dto/PageDto.java +++ b/src/main/java/com/selab/todo/common/dto/PageDto.java @@ -25,4 +25,4 @@ public static ResponseEntity> ok(Page data) { return ResponseEntity.ok(response); } -} +} \ No newline at end of file