-
Notifications
You must be signed in to change notification settings - Fork 18
[F팀 장주리] 백엔드 API 과제 제출 #7
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,27 @@ | ||
| package com.example.techeer_partners_api_session.config; | ||
|
|
||
| import io.swagger.v3.oas.models.Components; | ||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.info.Info; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
|
||
| @Configuration | ||
| @EnableWebMvc | ||
| public class SwaggerConfig { | ||
|
|
||
| @Bean | ||
| public OpenAPI openAPI() { | ||
| return new OpenAPI() | ||
| .components(new Components()) | ||
| .info(apiInfo()); | ||
| } | ||
|
|
||
| private Info apiInfo() { | ||
| return new Info() | ||
| .title("API Test") // API의 제목 | ||
| .description("Todo list Sawgger UI") // API에 대한 설명 | ||
| .version("3.0"); // API의 버전 | ||
| } | ||
| } |
|
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. API 응답의 ResponseEntity 구조가 거의 동일하게 반복되고 있습니다. status, message, data를 사용하는 구조를 반복적으로 작성하는 대신, 이를 하나의 공통 응답 객체로 관리하면 코드의 재사용성과 가독성을 크게 향상시킬 수 있을 것 같습니다. 현재 main 브랜치에도 그러한 방식으로 코드가 작성되어있으니 참고해주시면 좋을 것 같습니다! 👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| 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.repository.TaskRepository; | ||
| import com.example.techeer_partners_api_session.service.TaskService; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/tasks") | ||
| public class TaskController { | ||
| private final TaskService taskService; | ||
| private final TaskRepository taskRepository; | ||
|
|
||
| public TaskController(TaskService taskService, TaskRepository taskRepository) { | ||
| this.taskService = taskService; | ||
| this.taskRepository = taskRepository; | ||
| } | ||
|
|
||
| private <T> ResponseEntity<Map<String, Object>> createResponse(String status, String message, T data) { | ||
| Map<String, Object> response = new LinkedHashMap<>(); | ||
| response.put("status", status); | ||
| response.put("message", message); | ||
| if(data != null){ | ||
| response.put("data", data); | ||
| } | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| //할 일 등록 | ||
| @Operation(summary = "할 일 등록") | ||
| @PostMapping | ||
| public ResponseEntity<Map<String, Object>> createTask(@RequestBody TaskRequestDto dto) { | ||
| if (dto.getTitle() == null || dto.getTitle().isBlank()) { | ||
| return createResponse("fail", "title은 필수 입력입니다.", null); | ||
| } | ||
|
|
||
| taskService.createTask(dto); | ||
| return createResponse("success", "할 일이 생성 되었습니다.", null); | ||
| } | ||
|
|
||
| // 전체 조회 | ||
| @Operation(summary = "할 일 전체 조회") | ||
| @GetMapping | ||
| public ResponseEntity<Map<String, Object>> getTasks() { | ||
| List<Task> tasks = taskService.getAllTask(); | ||
| List<TaskResponseDto> taskResponseDtos = tasks.stream() | ||
| .map(task -> new TaskResponseDto(task.getId(), task.getTitle(), task.isDone())) | ||
| .collect(Collectors.toList()); | ||
| return createResponse("success", "전체 할 일이 조회되었습니다.", taskResponseDtos); | ||
| } | ||
|
|
||
| // 완료된 일만 조회 | ||
| @Operation(summary = "완료된 일 조회") | ||
| @GetMapping("/completed") | ||
| public ResponseEntity<Map<String, Object>> getCompletedTasks() { | ||
| List<Task> tasks = taskService.getCompletedTask(); | ||
| List<TaskResponseDto> taskResponseDtos = tasks.stream() | ||
| .map(task -> new TaskResponseDto(task.getId(), task.getTitle(), task.isDone())) | ||
| .collect(Collectors.toList()); | ||
| return createResponse("success", "완료된 일이 조회되었습니다.", taskResponseDtos); | ||
| } | ||
|
|
||
| // 미완료된 일만 조회 | ||
| @Operation(summary = "미완료된 일 조회") | ||
| @GetMapping("/inCompleted") | ||
| public ResponseEntity<Map<String, Object>> getInCompletedTasks() { | ||
| List<Task> tasks = taskService.getInCompletedTask(); | ||
| List<TaskResponseDto> taskResponseDtos = tasks.stream() | ||
| .map(task -> new TaskResponseDto(task.getId(), task.getTitle(), task.isDone())) | ||
| .collect(Collectors.toList()); | ||
| return createResponse("success", "미완료된 일이 조회되었습니다.", taskResponseDtos); | ||
| } | ||
|
|
||
|
|
||
| //할 일 수정 | ||
| @Operation(summary = "할 일 수정") | ||
| @PutMapping("/{id}") | ||
| public ResponseEntity<Map<String, Object>> updateTask(@PathVariable Long id, @RequestBody TaskRequestDto dto){ | ||
| Optional<Task> task = taskRepository.findById(id); | ||
| if (task.isPresent()) { | ||
| taskService.updateTask(id, dto); // 작업이 성공적으로 이루어지면 응답 | ||
| return createResponse("success", "할 일이 수정 되었습니다.", null); | ||
| } else { | ||
| return createResponse("fail", "해당 할 일이 존재하지 않습니다.", null); | ||
| } | ||
| } | ||
|
|
||
| //할 일 삭제 | ||
| @Operation(summary = "할 일 삭제") | ||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Map<String, Object>> deleteTask(@PathVariable Long id){ | ||
| Optional<Task> task = taskRepository.findById(id); | ||
| if (task.isPresent()) { | ||
|
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. 여기서 taskRespository를 활용하기에 controller에서도 tastRepository의 의존성을 갖게 된 것으로 보입니다. 이미 taskService가 taskRespository에 대한 의존성을 가지고 있는 만큼 controller에서 중복된 의존성을 가질 필요는 없다고 생각합니다. task가 존재하는 지 확인하는 로직도 service 메서드 내로 옮긴다면 controller에서 service에 대한 의존성만 가져도 충분할 것 같습니다 😄 |
||
| taskService.deleteTask(id); | ||
| return createResponse("success", "할 일이 삭제 되었습니다.", null); | ||
| } else { | ||
| return createResponse("fail", "해당 할 일이 존재하지 않습니다.", null); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.example.techeer_partners_api_session.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class TaskRequestDto { | ||
| private final String title; | ||
| private final boolean isDone; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.example.techeer_partners_api_session.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class TaskResponseDto { | ||
| private Long id; | ||
| private String title; | ||
| private boolean 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. dto를 잘 구성해주신 만큼 엔티티 클래스에 직접 get 메서드를 적용하기보다 dto 변환 메서드를 추가해줌으로써 엔티티 객체의 안정성을 더 보장해 줄 수 있을 것 같습니다. ex) public TaskResponseDto toDto() { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.techeer_partners_api_session.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Setter | ||
| 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; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 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> findByIsDone(boolean isDone); //완료 여부 필터링 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.example.techeer_partners_api_session.service; | ||
|
|
||
| import com.example.techeer_partners_api_session.dto.TaskRequestDto; | ||
| 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.Optional; | ||
|
|
||
| @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> getAllTask(){ | ||
| return taskRepository.findAll(); | ||
| } | ||
|
|
||
| //완료된 일 조회 | ||
| public List<Task> getCompletedTask(){ | ||
| return taskRepository.findByIsDone(true); | ||
| } | ||
|
|
||
| //미완료된 일 조회 | ||
| public List<Task> getInCompletedTask(){ | ||
| return taskRepository.findByIsDone(false); | ||
| } | ||
|
|
||
| //할 일 수정 | ||
| public void updateTask(Long id, TaskRequestDto dto){ | ||
| Optional<Task> task = taskRepository.findById(id); | ||
|
|
||
| if(task.isPresent()){ //task에 값이 존재하는지 확인 | ||
| Task existingTask = task.get(); //존재하면 해당 task get | ||
|
|
||
| //할 일 수정 | ||
| if(dto.getTitle() != null){ | ||
| existingTask.setTitle(dto.getTitle()); | ||
| } | ||
| existingTask.setDone(dto.isDone()); | ||
| taskRepository.save(existingTask); | ||
| } | ||
| } | ||
|
|
||
| //할 일 삭제 | ||
| public void deleteTask(Long id){ | ||
| taskRepository.deleteById(id); | ||
| } | ||
| } | ||
|
|
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.
스웨거 연동까지 해주셨군요 👍