From d2a3a73d623f90679bbe7e710aa9cfa41a2fb04b Mon Sep 17 00:00:00 2001 From: su080915 Date: Mon, 26 Aug 2024 21:33:54 +0900 Subject: [PATCH] feat :: todo --- .../domain/auth/service/AuthService.java | 3 + .../domain/auth/service/AuthServiceImpl.java | 9 +++ .../todo/controller/TodoController.java | 58 +++++++++++++++++ .../domain/todo/domain/Todo.java | 45 +++++++++++++ .../todo/dto/request/TodoCreateRequest.java | 6 ++ .../todo/dto/request/TodoEditRequest.java | 6 ++ .../todo/repository/TodoRepository.java | 11 ++++ .../domain/todo/service/TodoService.java | 21 ++++++ .../domain/todo/service/TodoServiceImpl.java | 64 +++++++++++++++++++ .../src/main/resources/application.yml | 12 ++++ 10 files changed, 235 insertions(+) create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/controller/TodoController.java create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/domain/Todo.java create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoCreateRequest.java create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoEditRequest.java create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/repository/TodoRepository.java create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoService.java create mode 100644 crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoServiceImpl.java diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthService.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthService.java index 0db7a5c..5523e1c 100644 --- a/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthService.java +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthService.java @@ -3,6 +3,7 @@ import com.example.crescendoserver.domain.auth.dto.LoginRequest; import com.example.crescendoserver.domain.auth.dto.ReissueRequest; import com.example.crescendoserver.domain.auth.dto.SignUpRequest; +import com.example.crescendoserver.domain.user.domain.User; import com.example.crescendoserver.domain.user.dto.UserResponse; import com.example.crescendoserver.global.security.jwt.dto.Jwt; @@ -15,4 +16,6 @@ public interface AuthService { UserResponse me(); + User getUser(); + } diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthServiceImpl.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthServiceImpl.java index bb244ba..66bb2ce 100644 --- a/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthServiceImpl.java +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/auth/service/AuthServiceImpl.java @@ -98,4 +98,13 @@ public UserResponse me() { return new UserResponse(user.getId(), user.getUsername(), user.getRole()); } + + @Override + public User getUser() { + String username = SecurityContextHolder.getContext().getAuthentication().getName(); + + return userRepository.findByUsername(username) + .orElseThrow(() -> new CustomException(CustomErrorCode.USER_NOT_FOUND)); + } } + diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/controller/TodoController.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/controller/TodoController.java new file mode 100644 index 0000000..835b1bd --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/controller/TodoController.java @@ -0,0 +1,58 @@ +package com.example.crescendoserver.domain.todo.controller; + +import com.example.crescendoserver.domain.todo.domain.Todo; +import com.example.crescendoserver.domain.todo.dto.request.TodoCreateRequest; +import com.example.crescendoserver.domain.todo.dto.request.TodoEditRequest; +import com.example.crescendoserver.domain.todo.service.TodoService; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + + +@Tag(name = "투두", description = "ToDo") +@RestController +@RequestMapping("/posts") +@RequiredArgsConstructor +public class TodoController { + private final TodoService todoService; + + @PostMapping + public void createTodo(@RequestBody TodoCreateRequest todoCreateRequest) { + todoService.createTodo(todoCreateRequest); + } + + @GetMapping + public List getAllTodos() { + return todoService.getTodos(); + } + + @GetMapping("/{todoId}") + public Todo getTodo(@PathVariable Long todoId) { + return todoService.getTodo(todoId); + } + + @DeleteMapping("/{todoId}") + public void deleteTodo(@PathVariable Long todoId) { + todoService.deleteTodo(todoId); + } + + @PostMapping("/{todoId}") + public void checkTodo(@PathVariable Long todoId) { + todoService.todoCheck(todoId); + } + + @PatchMapping("/{todoId}") + public void editTodo(@PathVariable Long todoId, @RequestBody TodoEditRequest request) { + todoService.editTodo(todoId, request); + } +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/domain/Todo.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/domain/Todo.java new file mode 100644 index 0000000..014e19c --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/domain/Todo.java @@ -0,0 +1,45 @@ +package com.example.crescendoserver.domain.todo.domain; + + +import com.example.crescendoserver.domain.user.domain.User; +import com.example.crescendoserver.global.common.BaseEntity; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; + +import java.time.LocalDate; + +@Entity +@Getter +@Setter +@SuperBuilder +@Table(name = "todos") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Todo extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String title; + + @ManyToOne() + @JoinColumn(name = "author_id", nullable = false) + private User author; + + @Column(nullable = false) + private Boolean checked = false; + + @Column(nullable = false) + private LocalDate date; +} \ No newline at end of file diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoCreateRequest.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoCreateRequest.java new file mode 100644 index 0000000..e2e99e2 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoCreateRequest.java @@ -0,0 +1,6 @@ +package com.example.crescendoserver.domain.todo.dto.request; + +import java.time.LocalDate; + +public record TodoCreateRequest(String title, LocalDate date) { +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoEditRequest.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoEditRequest.java new file mode 100644 index 0000000..de2e0df --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoEditRequest.java @@ -0,0 +1,6 @@ +package com.example.crescendoserver.domain.todo.dto.request; + +import java.time.LocalDate; + +public record TodoEditRequest(String title, LocalDate date) { +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/repository/TodoRepository.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/repository/TodoRepository.java new file mode 100644 index 0000000..c0528c4 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/repository/TodoRepository.java @@ -0,0 +1,11 @@ +package com.example.crescendoserver.domain.todo.repository; + +import com.example.crescendoserver.domain.todo.domain.Todo; +import com.example.crescendoserver.domain.user.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface TodoRepository extends JpaRepository { + List findAllByAuthorOrderByIdDesc(User author); +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoService.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoService.java new file mode 100644 index 0000000..f11962d --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoService.java @@ -0,0 +1,21 @@ +package com.example.crescendoserver.domain.todo.service; + +import com.example.crescendoserver.domain.todo.domain.Todo; +import com.example.crescendoserver.domain.todo.dto.request.TodoCreateRequest; +import com.example.crescendoserver.domain.todo.dto.request.TodoEditRequest; + +import java.util.List; + +public interface TodoService { + void createTodo(TodoCreateRequest request); + + void deleteTodo(Long todoId); + + void editTodo(Long todoId, TodoEditRequest request); + + List getTodos(); + + Todo getTodo(Long todoId); + + void todoCheck(Long todoId); +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoServiceImpl.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoServiceImpl.java new file mode 100644 index 0000000..da36073 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoServiceImpl.java @@ -0,0 +1,64 @@ +package com.example.crescendoserver.domain.todo.service; + +import com.example.crescendoserver.domain.auth.service.AuthService; +import com.example.crescendoserver.domain.todo.domain.Todo; +import com.example.crescendoserver.domain.todo.dto.request.TodoCreateRequest; +import com.example.crescendoserver.domain.todo.dto.request.TodoEditRequest; +import com.example.crescendoserver.domain.todo.repository.TodoRepository; +import com.example.crescendoserver.domain.user.domain.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + + +@Service +@RequiredArgsConstructor +public class TodoServiceImpl implements TodoService { + private TodoRepository todoRepository; + private final AuthService authService; + + @Override + public void createTodo(TodoCreateRequest request) { + User user = authService.getUser(); + Todo todo = Todo.builder() + .title(request.title()) + .author(user) + .date(request.date()) + .checked(false) + .build(); + todoRepository.save(todo); + } + + @Override + public void deleteTodo(Long todoId) { + todoRepository.deleteById(todoId); + } + + @Override + public void editTodo(Long todoId, TodoEditRequest request) { + Todo todo = todoRepository.findById(todoId).orElse(null); + todo.setTitle(request.title()); + todo.setDate(request.date()); + + todoRepository.save(todo); + } + + @Override + public List getTodos() { + User user = authService.getUser(); + return todoRepository.findAllByAuthorOrderByIdDesc(user); + } + + @Override + public Todo getTodo(Long todoId) { + return todoRepository.findById(todoId).orElse(null); + } + + @Override + public void todoCheck(Long todoId) { + Todo todo = todoRepository.findById(todoId).orElse(null); + todo.setChecked(!todo.getChecked()); + todoRepository.save(todo); + } +} diff --git a/crescendo-server/src/main/resources/application.yml b/crescendo-server/src/main/resources/application.yml index 1ffb5e3..478a5e6 100644 --- a/crescendo-server/src/main/resources/application.yml +++ b/crescendo-server/src/main/resources/application.yml @@ -34,3 +34,15 @@ jwt: server: port: 8088 + + +springdoc: + api-docs: + enabled: true + version: openapi_3_0 + path: /v3/api-docs + default-consumes-media-type: application/json + auto-tag-classes: true + swagger-ui: + operationsSorter: method + path: /swagger-ui.html \ No newline at end of file