Skip to content

Commit

Permalink
Merge pull request #46 from BudgetBuddiesTeam/feat/#39
Browse files Browse the repository at this point in the history
[feat] 유저 별 카테고리 조회 API
  • Loading branch information
ryogaeng authored Jul 27, 2024
2 parents 5a48dfa + 6711522 commit 1d0444c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.bbteam.budgetbuddies.domain.category.controller;

import com.bbteam.budgetbuddies.domain.category.dto.CategoryRequestDTO;
import com.bbteam.budgetbuddies.domain.category.dto.CategoryResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public interface CategoryApi {

@Operation(summary = "카테고리 추가", description = "사용자가 직접 카테고리를 추가합니다.")
@ApiResponses({
@ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@ApiResponse(responseCode = "AUTH003", description = "access 토큰을 주세요!", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class)))
})
@PostMapping("/add")
ResponseEntity<CategoryResponseDTO> createCategory(
@Parameter(description = "user_id, name(사용자가 입력한 카테고리명), is_default(default 카테고리 여부)로 request")
@RequestBody CategoryRequestDTO categoryRequestDTO);


@Operation(summary = "유저 개인의 카테고리 조회", description = "유저의 카테고리(default + 개인 custom)를 조회합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "403", description = "Forbidden"),
@ApiResponse(responseCode = "404", description = "Not Found")
})
@GetMapping("/get/{userId}")
ResponseEntity<List<CategoryResponseDTO>> getUserCategories(@PathVariable Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@
import com.bbteam.budgetbuddies.domain.category.dto.CategoryRequestDTO;
import com.bbteam.budgetbuddies.domain.category.dto.CategoryResponseDTO;
import com.bbteam.budgetbuddies.domain.category.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/categories")
public class CategoryController {
public class CategoryController implements CategoryApi {

private final CategoryService categoryService;

@Operation(summary = "카테고리 추가", description = "사용자가 직접 카테고리를 추가합니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH003", description = "access 토큰을 주세요!", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class)))
})
@Override
@PostMapping("/add")
public ResponseEntity<CategoryResponseDTO> createCategory(
@Parameter(description = "user_id, name(사용자가 입력한 카테고리명), is_default(default 카테고리 여부)로 request")
@RequestBody CategoryRequestDTO categoryRequestDTO
) {
public ResponseEntity<CategoryResponseDTO> createCategory(@RequestBody CategoryRequestDTO categoryRequestDTO) {
CategoryResponseDTO response = categoryService.createCategory(categoryRequestDTO);
return ResponseEntity.ok(response);
}

@Override
@GetMapping("/get/{userId}")
public ResponseEntity<List<CategoryResponseDTO>> getUserCategories(@PathVariable Long userId) {
List<CategoryResponseDTO> response = categoryService.getUserCategories(userId);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.bbteam.budgetbuddies.domain.category.dto.CategoryRequestDTO;
import com.bbteam.budgetbuddies.domain.category.dto.CategoryResponseDTO;

import java.util.List;

public interface CategoryService {
CategoryResponseDTO createCategory(CategoryRequestDTO categoryRequestDTO);
List<CategoryResponseDTO> getUserCategories(Long userId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -35,4 +38,16 @@ public CategoryResponseDTO createCategory(CategoryRequestDTO categoryRequestDTO)

return categoryConverter.toCategoryResponseDTO(savedCategory);
}

@Override
public List<CategoryResponseDTO> getUserCategories(Long userId) {

userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("User not found with id: " + userId));

List<Category> categories = categoryRepository.findUserCategoryByUserId(userId);
return categories.stream()
.map(categoryConverter::toCategoryResponseDTO)
.collect(Collectors.toList());
}
}

0 comments on commit 1d0444c

Please sign in to comment.