Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into feature/member
  • Loading branch information
versatile0010 committed Aug 3, 2023
2 parents 6c0ed01 + fc2f628 commit 22b5140
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.couphoneserver.controller;

import com.example.couphoneserver.common.response.BaseErrorResponse;
import com.example.couphoneserver.common.response.BaseResponse;
import com.example.couphoneserver.dto.category.GetCategoryResponse;
import com.example.couphoneserver.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
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.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "categories", description = "카테고리 관련 API")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/categories")
public class CategoryController {

private final CategoryService categoryService;

@GetMapping("")
@Operation(summary = "카테고리 정보 제공", description = "카테고리 정보를 제공합니다.", responses = {
@ApiResponse(responseCode = "200", description = "쿼리가 null인 경우, 모든 카테고리 조회 성공", content = @Content(schema = @Schema(implementation = GetCategoryResponse.class))),
@ApiResponse(responseCode = "200", description = "쿼리에 이름을 넣은 경우, 카테고리 조회 성공", content = @Content(schema = @Schema(implementation = GetCategoryResponse.class))),
@ApiResponse(responseCode = "400", description = "존재하지 않은 리소스 접근", content = @Content(schema = @Schema(implementation = BaseErrorResponse.class)))
})
public BaseResponse<List<GetCategoryResponse>> getCategory(@Parameter(name = "query", description = "카테고리의 이름(선택)", in = ParameterIn.QUERY)
@RequestParam(required = false) String query){
return new BaseResponse<>(categoryService.findCategory(query));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.couphoneserver.dto.category;

import lombok.Builder;
import lombok.Data;

@Builder
@Data
public class GetCategoryResponse {
private Long CategoryId;
private String CategoryName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.example.couphoneserver.domain.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface CategoryRepository extends JpaRepository<Category, Long> {
Optional<Category> findById(Long id);

List<Category> findByNameLike(String query);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.couphoneserver.service;

import com.example.couphoneserver.common.exception.CategoryException;
import com.example.couphoneserver.domain.entity.Category;
import com.example.couphoneserver.dto.category.GetCategoryResponse;
import com.example.couphoneserver.repository.CategoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.example.couphoneserver.common.response.status.BaseExceptionResponseStatus.CATEGORY_NOT_FOUND;

@Service
@Slf4j
@Transactional(readOnly=true)
@RequiredArgsConstructor
public class CategoryService {

private final CategoryRepository categoryRepository;

public List<GetCategoryResponse> findCategory(String query) {
List<GetCategoryResponse> resultCategories = new ArrayList<>();
if(query == null) query = "%%";
List<Category> categories = categoryRepository.findByNameLike(query);
if(categories.size() < 1)
throw new CategoryException(CATEGORY_NOT_FOUND);
for (Category category:categories)
resultCategories.add(GetCategoryResponse.builder()
.CategoryId(category.getId())
.CategoryName(category.getName()).build());
return resultCategories;
}


}

0 comments on commit 22b5140

Please sign in to comment.