Skip to content

Commit

Permalink
Fix[#37] 사용자 모든 카테고리 조회 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
eunseo5343 authored Jul 11, 2024
2 parents ba201dc + 680cbcb commit 8cd5252
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jaksim/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
// implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// lombok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import lombok.RequiredArgsConstructor;
import org.sopt.jaksim.category.dto.CategoryCheckResponse;
import org.sopt.jaksim.category.dto.CategoryCreateRequest;
import org.sopt.jaksim.category.dto.FilteredResourceResponse;
import org.sopt.jaksim.category.service.CategoryService;
Expand All @@ -13,6 +14,7 @@
import org.sopt.jaksim.task.service.TaskService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
Expand All @@ -38,4 +40,10 @@ public ResponseEntity<BaseResponse<?>> retrieve(@RequestParam @DateTimeFormat(is
List<FilteredResourceResponse> response = categoryTaskFacade.getAllResources(startDate, endDate);
return ApiResponseUtil.success(SuccessMessage.SUCCESS, response);
}

@GetMapping("/categories")
public ResponseEntity<BaseResponse<?>> getCategoriesByUserId() {
List<CategoryCheckResponse> categories = categoryService.getCategoriesByUserId(); // categoryRepository를 통해 데이터베이스에서 특정 사용자의 카테고리를 조회
return ApiResponseUtil.success(SuccessMessage.SUCCESS, categories); // 성공 응답 반환
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.jaksim.category.dto;

import java.time.LocalDate;

public record CategoryCheckResponse(
Long id,
String name,
LocalDate startDate,
LocalDate endDate) {
public static CategoryCheckResponse of(Long id, String name, LocalDate startDate, LocalDate endDate) {
return new CategoryCheckResponse(id, name, startDate, endDate);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.sopt.jaksim.category.repository;

import org.sopt.jaksim.category.domain.Category;
import org.sopt.jaksim.category.domain.CategoryTask;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("select e from Category e where " +
Expand All @@ -14,4 +16,6 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
"or e.endDate between :idxStartDate and :idxEndDate " +
"or (e.startDate <= :idxStartDate and e.endDate >= :idxEndDate)")
List<Category> findByUserIdWithRange(Long userId, LocalDate idxStartDate, LocalDate idxEndDate);

Optional<List<Category>> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sopt.jaksim.category.domain.Category;
import org.sopt.jaksim.category.dto.CategoryCheckResponse;
import org.sopt.jaksim.category.domain.CategoryTask;
import org.sopt.jaksim.category.dto.CategoryCreateRequest;
import org.sopt.jaksim.category.dto.CategoryTaskLink;
import org.sopt.jaksim.category.repository.CategoryRepository;
import org.sopt.jaksim.category.repository.CategoryTaskRepository;
import org.sopt.jaksim.global.common.DateUtil;
import org.sopt.jaksim.global.exception.NotFoundException;
import org.sopt.jaksim.global.message.ErrorMessage;
import org.sopt.jaksim.mset.service.MsetService;

import org.sopt.jaksim.task.domain.Task;
import org.sopt.jaksim.task.repository.TaskRepository;
import org.sopt.jaksim.task.service.TaskService;
import org.sopt.jaksim.user.domain.User;
import org.sopt.jaksim.user.facade.UserFacade;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
Expand Down Expand Up @@ -70,4 +75,19 @@ public boolean isContains(Category category, LocalDate idxDate) {
category.getStartDate().equals(idxDate) ||
category.getEndDate().equals(idxDate);
}


public List<CategoryCheckResponse> getCategoriesByUserId() {
// userId -> user pk -> Long -> SecurityContextHolder Authentication 객체
// principal handler
Long userId = userFacade.getUserByPrincipal().getId();
List<Category> categories = categoryRepository.findByUserId(userId).orElseThrow(
() -> new NotFoundException(ErrorMessage.NOT_FOUND)
);
return categories.stream() //리스트를 스트림으로 변환
//각 category 객체를 CategoryCheckResponse 객체로 변환
.map(category -> CategoryCheckResponse.of(category.getId(), category.getName(), category.getStartDate(), category.getEndDate()))
.collect(Collectors.toList()); //변환된 스트림을 리스트로 수집
}
}

0 comments on commit 8cd5252

Please sign in to comment.