-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
backend/src/main/java/site/coduo/todo/controller/TodoController.java
This file contains 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,65 @@ | ||
package site.coduo.todo.controller; | ||
|
||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
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.RestController; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import site.coduo.todo.controller.docs.TodoDocs; | ||
import site.coduo.todo.controller.request.CreateTodoRequest; | ||
import site.coduo.todo.controller.request.UpdateTodoContentRequest; | ||
import site.coduo.todo.controller.request.UpdateTodoOrderRequest; | ||
import site.coduo.todo.controller.response.GetTodoResponse; | ||
import site.coduo.todo.domain.Todo; | ||
import site.coduo.todo.service.TodoService; | ||
|
||
@RequiredArgsConstructor | ||
@CrossOrigin(origins = {"http://localhost:3000", "http://3.35.178.58"}) | ||
@RequestMapping("/api") | ||
@RestController | ||
public class TodoController implements TodoDocs { | ||
|
||
private final TodoService todoService; | ||
|
||
@GetMapping("/{accessCode}/todos") | ||
public List<GetTodoResponse> getTodos(@PathVariable String accessCode) { | ||
final List<Todo> allTodos = todoService.getAllOrderBySort(accessCode); | ||
return IntStream.range(0, allTodos.size()) | ||
.mapToObj(index -> GetTodoResponse.from(allTodos.get(index), index)) | ||
.toList(); | ||
} | ||
|
||
@PostMapping("/{accessCode}/todos") | ||
public void createTodo(@PathVariable String accessCode, @RequestBody CreateTodoRequest request) { | ||
todoService.createTodo(accessCode, request.content()); | ||
} | ||
|
||
@PatchMapping("/todos/{todoId}/contents") | ||
public void updateContent(@PathVariable Long todoId, @RequestBody UpdateTodoContentRequest request) { | ||
todoService.updateTodoContent(todoId, request.content()); | ||
} | ||
|
||
@PatchMapping("/todos/{todoId}/checked") | ||
public void toggleTodoChecked(@PathVariable Long todoId) { | ||
todoService.toggleTodoChecked(todoId); | ||
} | ||
|
||
@PatchMapping("/todos/{todoId}/order") | ||
public void updateTodoOrder(@PathVariable Long todoId, @RequestBody UpdateTodoOrderRequest request) { | ||
todoService.updateTodoSort(todoId, request.order()); | ||
} | ||
|
||
@DeleteMapping("/todos/{todoId}") | ||
public void deleteTodo(@PathVariable Long todoId) { | ||
todoService.deleteTodo(todoId); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/site/coduo/todo/controller/docs/TodoDocs.java
This file contains 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,7 @@ | ||
package site.coduo.todo.controller.docs; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
||
@Tag(name = "투두 API") | ||
public interface TodoDocs { | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/site/coduo/todo/controller/request/CreateTodoRequest.java
This file contains 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,4 @@ | ||
package site.coduo.todo.controller.request; | ||
|
||
public record CreateTodoRequest(String content) { | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/site/coduo/todo/controller/request/UpdateTodoContentRequest.java
This file contains 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,4 @@ | ||
package site.coduo.todo.controller.request; | ||
|
||
public record UpdateTodoContentRequest(String content) { | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/site/coduo/todo/controller/request/UpdateTodoOrderRequest.java
This file contains 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,4 @@ | ||
package site.coduo.todo.controller.request; | ||
|
||
public record UpdateTodoOrderRequest(int order) { | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/site/coduo/todo/controller/response/GetTodoResponse.java
This file contains 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 site.coduo.todo.controller.response; | ||
|
||
import site.coduo.todo.domain.Todo; | ||
|
||
public record GetTodoResponse( | ||
Long id, | ||
String content, | ||
boolean isChecked, | ||
int order | ||
) { | ||
public static GetTodoResponse from(final Todo todo, final int order) { | ||
return new GetTodoResponse( | ||
todo.getId(), | ||
todo.getContent().getContent(), | ||
todo.getIsChecked().isChecked(), | ||
order | ||
); | ||
} | ||
} |