This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
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
10 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
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
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
58 changes: 58 additions & 0 deletions
58
...rver/src/main/java/com/example/crescendoserver/domain/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,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<Todo> 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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
crescendo-server/src/main/java/com/example/crescendoserver/domain/todo/domain/Todo.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,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; | ||
} |
6 changes: 6 additions & 0 deletions
6
.../src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoCreateRequest.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,6 @@ | ||
package com.example.crescendoserver.domain.todo.dto.request; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record TodoCreateRequest(String title, LocalDate date) { | ||
} |
6 changes: 6 additions & 0 deletions
6
...er/src/main/java/com/example/crescendoserver/domain/todo/dto/request/TodoEditRequest.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,6 @@ | ||
package com.example.crescendoserver.domain.todo.dto.request; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record TodoEditRequest(String title, LocalDate date) { | ||
} |
11 changes: 11 additions & 0 deletions
11
...rver/src/main/java/com/example/crescendoserver/domain/todo/repository/TodoRepository.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,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<Todo, Long> { | ||
List<Todo> findAllByAuthorOrderByIdDesc(User author); | ||
} |
21 changes: 21 additions & 0 deletions
21
...ndo-server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoService.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,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<Todo> getTodos(); | ||
|
||
Todo getTodo(Long todoId); | ||
|
||
void todoCheck(Long todoId); | ||
} |
64 changes: 64 additions & 0 deletions
64
...server/src/main/java/com/example/crescendoserver/domain/todo/service/TodoServiceImpl.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,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<Todo> 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); | ||
} | ||
} |
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