Skip to content

Commit b4890ef

Browse files
committed
feat: 조회 기능
1 parent 5cab4a9 commit b4890ef

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

src/main/java/com/back/domain/study/todo/controller/TodoController.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import io.swagger.v3.oas.annotations.tags.Tag;
1010
import jakarta.validation.Valid;
1111
import lombok.RequiredArgsConstructor;
12+
import org.springframework.format.annotation.DateTimeFormat;
1213
import org.springframework.http.ResponseEntity;
1314
import org.springframework.security.core.annotation.AuthenticationPrincipal;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.RequestBody;
16-
import org.springframework.web.bind.annotation.RequestMapping;
17-
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.bind.annotation.*;
16+
17+
import java.time.LocalDate;
18+
import java.util.List;
1819

1920
@RestController
2021
@RequiredArgsConstructor
@@ -23,6 +24,7 @@
2324
public class TodoController {
2425
private final TodoService todoService;
2526

27+
// ==================== 생성 ===================
2628
@PostMapping
2729
@Operation(summary = "할 일 생성", description = "새로운 할 일을 생성합니다.")
2830
public ResponseEntity<RsData<TodoResponseDto>> createTodo(
@@ -33,4 +35,32 @@ public ResponseEntity<RsData<TodoResponseDto>> createTodo(
3335
return ResponseEntity.ok(RsData.success("할 일이 생성되었습니다.", response));
3436
}
3537

38+
// ==================== 조회 ===================
39+
// 특정 날짜 조회
40+
@GetMapping
41+
@Operation(summary = "할 일 목록 조회", description = "조건에 따라 할 일 목록을 조회합니다. " +
42+
"date만 제공시 해당 날짜, startDate와 endDate 제공시 기간별, 아무것도 없으면 전체 조회")
43+
public ResponseEntity<RsData<List<TodoResponseDto>>> getTodos(
44+
@AuthenticationPrincipal CustomUserDetails userDetails,
45+
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date
46+
) {
47+
List<TodoResponseDto> response = todoService.getTodosByDate(userDetails.getUserId(), date);
48+
49+
return ResponseEntity.ok(RsData.success("할 일 목록을 조회했습니다.", response));
50+
}
51+
52+
// 사용자의 모든 할 일 조회
53+
@GetMapping("/all")
54+
@Operation(summary = "모든 할 일 조회", description = "사용자의 모든 할 일을 조회합니다.")
55+
public ResponseEntity<RsData<List<TodoResponseDto>>> getAllTodos(
56+
@AuthenticationPrincipal CustomUserDetails userDetails
57+
) {
58+
List<TodoResponseDto> response = todoService.getAllTodos(userDetails.getUserId());
59+
return ResponseEntity.ok(RsData.success("모든 할 일을 조회했습니다.", response));
60+
}
61+
62+
// ==================== 수정 ===================
63+
64+
// ==================== 삭제 ===================
65+
3666
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.back.domain.study.todo.repository;
22

33
import com.back.domain.study.todo.entity.Todo;
4+
import com.back.domain.user.entity.User;
45
import org.springframework.data.jpa.repository.JpaRepository;
56
import org.springframework.stereotype.Repository;
67

8+
import java.time.LocalDate;
9+
import java.util.List;
10+
711
@Repository
812
public interface TodoRepository extends JpaRepository<Todo, Long> {
13+
List<Todo> findByUserIdAndDate(Long userId, LocalDate date);
14+
List<Todo> findByUserId(Long userId);
915
}

src/main/java/com/back/domain/study/todo/service/TodoService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import org.springframework.stereotype.Service;
1515
import org.springframework.transaction.annotation.Transactional;
1616

17+
import java.time.LocalDate;
18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
1721
@Service
1822
@RequiredArgsConstructor
1923
@Transactional(readOnly = true)
@@ -36,7 +40,20 @@ public TodoResponseDto createTodo(Long userId, TodoRequestDto request) {
3640
}
3741

3842
// ==================== 조회 ===================
39-
43+
// 유저의 특정 날짜의 모든 할 일 조회
44+
public List<TodoResponseDto> getTodosByDate(Long userId, LocalDate date) {
45+
List<Todo> todos = todoRepository.findByUserIdAndDate(userId, date);
46+
return todos.stream()
47+
.map(TodoResponseDto::from)
48+
.collect(Collectors.toList());
49+
}
50+
// 유저의 전체 할 일 조회
51+
public List<TodoResponseDto> getAllTodos(Long userId) {
52+
List<Todo> todos = todoRepository.findByUserId(userId);
53+
return todos.stream()
54+
.map(TodoResponseDto::from)
55+
.collect(Collectors.toList());
56+
}
4057
// ==================== 수정 ===================
4158

4259
// ==================== 삭제 ===================

0 commit comments

Comments
 (0)