-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/KUIT-Couphone/Couphone-Server…
… into feature/member
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/couphoneserver/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/example/couphoneserver/dto/category/GetCategoryResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/couphoneserver/service/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |