-
Notifications
You must be signed in to change notification settings - Fork 18
[B팀 김유현] 백엔드 API 과제 제출 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.example.techeer_partners_api_session.controller; | ||
|
|
||
| import com.example.techeer_partners_api_session.dto.TaskRequestDto; | ||
| import com.example.techeer_partners_api_session.dto.TaskUpdateDto; | ||
| import com.example.techeer_partners_api_session.entity.Task; | ||
| import com.example.techeer_partners_api_session.service.TaskService; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/tasks") | ||
| public class TaskController { | ||
| private final TaskService taskService; | ||
|
|
||
| public TaskController(TaskService taskService) { | ||
| this.taskService = taskService; | ||
| } | ||
|
|
||
| // 할 일 생성 | ||
| @PostMapping | ||
| public ResponseEntity<Map<String, String>> createTask (@RequestBody TaskRequestDto dto) { | ||
| taskService.createTask(dto); | ||
|
|
||
| Map<String, String> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message" , "할 일이 생성되었습니다."); | ||
|
|
||
| return ResponseEntity.status(201).body(response); | ||
| } | ||
|
|
||
|
|
||
| @GetMapping("/completed") | ||
| public ResponseEntity<Map<String, Object>> getCompletedTasks() { | ||
| List<Task> completedTasks = taskService.getDoneTasks(); | ||
|
|
||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "완료 된 일이 조회되었습니다."); | ||
| response.put("data", completedTasks); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 응답에서 별도의 응답 dto가 아닌 Task 엔티티 객체를 직접 반환하고 있습니다. 이 방식은 직관적이고 현재 Task의 멤버가 적은 만큼 큰 단점이 없어보이지만 엔티티가 복잡해질 수록 꼭 엔티티의 모든 필드가 아닌 선택적으로 몇가지 필드만 클라이언트에게 반환해야하는 경우가 생기게 됩니다. 이를 위해 요청 dto뿐만 아닌 응답용 dto를 구현해서 활용하는 것도 좋을 것 같습니다 😄 |
||
|
|
||
| @GetMapping("/incompleted") | ||
| public ResponseEntity<Map<String, Object>> getInCompletedTasks() { | ||
| List<Task> inCompletedTasks = taskService.getNotDoneTasks(); | ||
|
|
||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "미완료 된 일이 조회되었습니다."); | ||
| response.put("data", inCompletedTasks); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PatchMapping("/{id}") | ||
| public ResponseEntity<Map<String, Object>> updateTask(@PathVariable Long id, @RequestBody TaskUpdateDto dto) { | ||
| Task updateTask = taskService.updateTask(id, dto); | ||
|
|
||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message" , "할 일이 수정되었습니다.."); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Map<String, Object>> delteTask(@PathVariable Long id) { | ||
| taskService.deleteTask(id); | ||
|
|
||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message" , "할 일이 삭제되었습니다."); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.example.techeer_partners_api_session.dto; | ||
|
|
||
| public class TaskRequestDto { | ||
| private final String title; | ||
| private final boolean isDone; | ||
|
|
||
| public TaskRequestDto(String title, boolean isDone){ | ||
| this.title = title; | ||
| this.isDone = isDone; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
| } |
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TaskRequestDto와 TaskUpdateDto의 구조가 같은 것으로 보입니다. 엔티티 객체와 로직이 확장 될수록 Dto를 세분화하는 것은 옳은 방식이지만 현재는 구조가 동일한 만큼 TaskRequestDto만으로 충분할 것 같습니다 👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.example.techeer_partners_api_session.dto; | ||
|
|
||
| public class TaskUpdateDto { | ||
| private String title; | ||
| private Boolean isDone; | ||
|
|
||
| public TaskUpdateDto() {} | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
| public void setTitle(){ | ||
| this.title = title; | ||
| } | ||
|
|
||
| public Boolean getIsDone() { | ||
| return isDone; | ||
| } | ||
|
|
||
| public void setIsDone(Boolean isDone) { | ||
| this.isDone = isDone; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.example.techeer_partners_api_session.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
|
|
||
| @Entity | ||
| public class Task { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(nullable = false) | ||
| private String title; | ||
|
|
||
| private boolean isDone = false; | ||
|
|
||
| public Task() {} | ||
|
|
||
| public Task(String title) { | ||
| this.title = title; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
|
|
||
| public void setTitle(String title) { | ||
| this.title = title; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
|
|
||
| public void setIsDone(Boolean isDone) { | ||
| this.isDone = isDone; | ||
| } | ||
| } | ||
|
|
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. findByIsDone(Boolean isDone)과 같이 하나의 메서드로 통일 시킬 수 있을 것 같습니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example.techeer_partners_api_session.repository; | ||
|
|
||
| import com.example.techeer_partners_api_session.entity.Task; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface TaskRepository extends JpaRepository<Task, Long> { | ||
| List<Task> findByIsDoneTrue(); | ||
| List<Task> findByIsDoneFalse(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.example.techeer_partners_api_session.service; | ||
|
|
||
| import com.example.techeer_partners_api_session.dto.TaskRequestDto; | ||
| import com.example.techeer_partners_api_session.dto.TaskUpdateDto; | ||
| import com.example.techeer_partners_api_session.entity.Task; | ||
| import com.example.techeer_partners_api_session.repository.TaskRepository; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| @Service | ||
| public class TaskService { | ||
| private final TaskRepository taskRepository; | ||
|
|
||
| public TaskService(TaskRepository taskRepository) { | ||
| this.taskRepository = taskRepository; | ||
| } | ||
|
|
||
| public void createTask(TaskRequestDto dto) { | ||
| Task task = new Task(dto.getTitle()); | ||
| taskRepository.save(task); | ||
| } | ||
|
|
||
| public List<Task> getAllTasks() { | ||
| return taskRepository.findAll(); | ||
| } | ||
|
|
||
| public List<Task> getDoneTasks() { | ||
| return taskRepository.findByIsDoneTrue(); | ||
| } | ||
|
|
||
| public List<Task> getNotDoneTasks() { | ||
| return taskRepository.findByIsDoneFalse(); | ||
| } | ||
|
|
||
| public Task updateTask(Long id, TaskUpdateDto dto) { | ||
| Task task = taskRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 ID를 가진 할 일이 존재하지 않습니다." + id)); | ||
|
|
||
| if (dto.getTitle() != null) { | ||
| task.setTitle(dto.getTitle()); | ||
| } | ||
|
|
||
| if (dto.getIsDone() != null) { | ||
| task.setIsDone(dto.getIsDone()); | ||
| } | ||
| return taskRepository.save(task); | ||
| } | ||
|
|
||
| public void deleteTask(Long id) { | ||
| Task task = taskRepository.findById(id).orElseThrow(() -> new RuntimeException("해당 ID를 가진 할 일이 존재하지 않습니다." + id)); | ||
|
|
||
| taskRepository.delete(task); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API 응답의 ResponseEntity 구조가 거의 동일하게 반복되고 있습니다. status, message, data를 사용하는 구조를 반복적으로 작성하는 대신, 이를 하나의 공통 응답 객체로 관리하면 코드의 재사용성과 가독성을 크게 향상시킬 수 있을 것 같습니다. 현재 main 브랜치에도 그러한 방식으로 코드가 작성되어있으니 참고해주시면 좋을 것 같습니다! 👍