Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat :: todo
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Aug 26, 2024
1 parent c94b9a1 commit d2a3a73
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.crescendoserver.domain.auth.dto.LoginRequest;
import com.example.crescendoserver.domain.auth.dto.ReissueRequest;
import com.example.crescendoserver.domain.auth.dto.SignUpRequest;
import com.example.crescendoserver.domain.user.domain.User;
import com.example.crescendoserver.domain.user.dto.UserResponse;
import com.example.crescendoserver.global.security.jwt.dto.Jwt;

Expand All @@ -15,4 +16,6 @@ public interface AuthService {

UserResponse me();

User getUser();

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ public UserResponse me() {

return new UserResponse(user.getId(), user.getUsername(), user.getRole());
}

@Override
public User getUser() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();

return userRepository.findByUsername(username)
.orElseThrow(() -> new CustomException(CustomErrorCode.USER_NOT_FOUND));
}
}

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);
}
}
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;
}
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) {
}
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) {
}
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);
}
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);
}
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);
}
}
12 changes: 12 additions & 0 deletions crescendo-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ jwt:

server:
port: 8088


springdoc:
api-docs:
enabled: true
version: openapi_3_0
path: /v3/api-docs
default-consumes-media-type: application/json
auto-tag-classes: true
swagger-ui:
operationsSorter: method
path: /swagger-ui.html

0 comments on commit d2a3a73

Please sign in to comment.