-
Notifications
You must be signed in to change notification settings - Fork 18
[C팀 이지수] 백엔드 API 과제 제출 #3
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
Open
jisuulee
wants to merge
2
commits into
jinu0328:main
Choose a base branch
from
jisuulee:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/com/example/techeer_partners_api_session/controller/TaskController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| 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.TaskResponseDto; | ||
| 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 | ||
| public ResponseEntity<Map<String, Object>> getAllTasks() { | ||
| List<Task> tasks = taskService.getAllTasks(); | ||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "전체 할 일이 조회되었습니다."); | ||
| response.put("data", tasks); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/completed") | ||
| public ResponseEntity<Map<String, Object>> getCompletedTasks() { | ||
| List<TaskResponseDto> completeTasks = taskService.getCompletedTasks(); | ||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "완료된 일이 조회되었습니다."); | ||
| response.put("data", completeTasks); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/incomplete") | ||
| public ResponseEntity<Map<String, Object>> getIncompleteTasks() { | ||
| List<TaskResponseDto> incompleteTasks = taskService.getIncompleteTasks(); | ||
| Map<String, Object> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "미완료 된 일이 조회되었습니다."); | ||
| response.put("data", incompleteTasks); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<Map<String, String>> updateTask( | ||
| @PathVariable Long id, | ||
| @RequestBody TaskRequestDto dto | ||
| ) { | ||
| taskService.updateTask(id, dto); | ||
| Map<String, String> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "할 일이 수정되었습니다."); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Map<String, String>> deleteTask(@PathVariable Long id) { | ||
| taskService.deleteTask(id); | ||
| Map<String, String> response = new HashMap<>(); | ||
| response.put("status", "success"); | ||
| response.put("message", "할 일이 삭제되었습니다."); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/techeer_partners_api_session/dto/TaskRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/example/techeer_partners_api_session/dto/TaskResponseDto.java
|
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까지 보여드리지는 않았는데 잘 구현해주신 것 같습니다 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.techeer_partners_api_session.dto; | ||
|
|
||
| public class TaskResponseDto { | ||
| private Long id; | ||
| private String title; | ||
| private boolean isDone; | ||
|
|
||
| public TaskResponseDto(Long id, String title, boolean isDone) { | ||
| this.id = id; | ||
| this.title = title; | ||
| this.isDone = isDone; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
| } | ||
|
|
41 changes: 41 additions & 0 deletions
41
src/main/java/com/example/techeer_partners_api_session/entity/Task.java
|
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가 존재한다면 엔티티 클래스에 직접 get 메서드를 적용하기보다 dto 변환 메서드를 추가해줌으로써 엔티티 객체의 안정성을 더 보장해 줄 수 있을 것 같습니다. ex) public TaskResponseDto toDto() {
return new TaskResponseDto(id, title, isDone)
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| 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) // null 허용X | ||
| private String title; | ||
|
|
||
| private boolean isDone = false; | ||
|
|
||
| public Task() {} // JPA가 entity에 접근하기 위해 기본 생성자 필수 | ||
|
|
||
| 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 void setDone(boolean done) { | ||
| isDone = done; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/techeer_partners_api_session/repository/TaskRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 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; | ||
|
|
||
| @Repository | ||
| public interface TaskRepository extends JpaRepository<Task, Long> { | ||
| } |
62 changes: 62 additions & 0 deletions
62
src/main/java/com/example/techeer_partners_api_session/service/TaskService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 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.TaskResponseDto; | ||
| 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; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| public class TaskService { | ||
|
|
||
| private 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<TaskResponseDto> getCompletedTasks() { | ||
| return taskRepository.findAll().stream() | ||
| .filter(task -> task.isDone()) // 완료된 항목 필터링 | ||
| .map(task -> new TaskResponseDto(task.getId(), task.getTitle(), task.isDone())) // Task -> TaskResponseDto 변환 | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<TaskResponseDto> getIncompleteTasks() { | ||
| return taskRepository.findAll().stream() | ||
| .filter(task -> !task.isDone()) // 완료되지 않은 항목 필터링 | ||
| .map(task -> new TaskResponseDto(task.getId(), task.getTitle(), task.isDone())) // Task -> TaskResponseDto 변환 | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public void updateTask(Long id, TaskRequestDto dto) { | ||
| Task task = taskRepository.findById(id) | ||
| .orElseThrow(() -> new IllegalArgumentException("Task not found")); | ||
| task.setTitle(dto.getTitle()); | ||
| task.setDone(dto.isDone()); | ||
| taskRepository.save(task); | ||
| } | ||
|
|
||
| public void deleteTask(Long id) { | ||
| if (!taskRepository.existsById(id)) { | ||
| throw new IllegalArgumentException("Task not found"); | ||
| } | ||
| taskRepository.deleteById(id); | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 브랜치에도 그러한 방식으로 코드가 작성되어있으니 참고해주시면 좋을 것 같습니다! 👍