-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature: 카테고리별 케이크 이미지 조회 API 구현
- Loading branch information
Showing
23 changed files
with
337 additions
and
16 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
cakk-api/src/main/java/com/cakk/api/controller/cake/CakeController.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,32 @@ | ||
package com.cakk.api.controller.cake; | ||
|
||
import jakarta.validation.Valid; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import com.cakk.api.dto.request.cake.CakeSearchByCategoryRequest; | ||
import com.cakk.api.dto.response.cake.CakeImageListResponse; | ||
import com.cakk.api.service.cake.CakeService; | ||
import com.cakk.common.response.ApiResponse; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/cakes") | ||
public class CakeController { | ||
|
||
private final CakeService cakeService; | ||
|
||
@GetMapping("/search/categories") | ||
public ApiResponse<CakeImageListResponse> listByCategory( | ||
@Valid @ModelAttribute CakeSearchByCategoryRequest request | ||
) { | ||
final CakeImageListResponse response = cakeService.findCakeImagesByCursorAndCategory(request); | ||
|
||
return ApiResponse.success(response); | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
cakk-api/src/main/java/com/cakk/api/dto/request/cake/CakeSearchByCategoryRequest.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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package com.cakk.api.dto.request.cake; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
import com.cakk.common.enums.CakeDesignCategory; | ||
|
||
public record CakeSearchByCategoryRequest( | ||
Long cakeId, | ||
@NotNull | ||
CakeDesignCategory category, | ||
int pageSize | ||
@NotNull | ||
Integer pageSize | ||
) { | ||
} |
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
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
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
16 changes: 15 additions & 1 deletion
16
cakk-api/src/test/java/com/cakk/api/common/base/IntegrationTest.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
10 changes: 10 additions & 0 deletions
10
cakk-api/src/test/java/com/cakk/api/config/MockCustomUser.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,10 @@ | ||
package com.cakk.api.config; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class) | ||
public @interface MockCustomUser { | ||
} |
32 changes: 32 additions & 0 deletions
32
cakk-api/src/test/java/com/cakk/api/config/WithMockCustomUserSecurityContextFactory.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,32 @@ | ||
package com.cakk.api.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.test.context.support.WithSecurityContextFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.cakk.api.vo.OAuthUserDetails; | ||
import com.cakk.domain.entity.user.User; | ||
import com.cakk.domain.repository.reader.UserReader; | ||
|
||
@Component | ||
public class WithMockCustomUserSecurityContextFactory implements WithSecurityContextFactory<MockCustomUser> { | ||
|
||
@Autowired | ||
private UserReader userReader; | ||
|
||
@Override | ||
public SecurityContext createSecurityContext(MockCustomUser annotation) { | ||
final SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); | ||
|
||
final User user = userReader.findByUserId(1L); | ||
final OAuthUserDetails userDetails = new OAuthUserDetails(user); | ||
final UsernamePasswordAuthenticationToken authenticationToken = | ||
new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); | ||
|
||
securityContext.setAuthentication(authenticationToken); | ||
return securityContext; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
cakk-api/src/test/java/com/cakk/api/integration/cake/CakeIntegrationTest.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,125 @@ | ||
package com.cakk.api.integration.cake; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.context.jdbc.SqlGroup; | ||
import org.springframework.web.util.UriComponents; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import com.cakk.api.common.base.IntegrationTest; | ||
import com.cakk.api.dto.response.cake.CakeImageListResponse; | ||
import com.cakk.common.enums.CakeDesignCategory; | ||
import com.cakk.common.enums.ReturnCode; | ||
import com.cakk.common.response.ApiResponse; | ||
import com.cakk.domain.dto.param.cake.CakeImageResponseParam; | ||
import com.cakk.domain.entity.cake.CakeCategory; | ||
import com.cakk.domain.repository.reader.CakeCategoryReader; | ||
|
||
@SqlGroup({ | ||
@Sql(scripts = { | ||
"/sql/insert-test-user.sql", | ||
"/sql/insert-cake.sql" | ||
}, executionPhase = BEFORE_TEST_METHOD), | ||
@Sql(scripts = "/sql/delete-all.sql", executionPhase = AFTER_TEST_METHOD) | ||
}) | ||
class CakeIntegrationTest extends IntegrationTest { | ||
|
||
private static final String API_URL = "/api/v1/cakes"; | ||
|
||
@Autowired | ||
private CakeCategoryReader cakeCategoryReader; | ||
|
||
@Test | ||
void 카테고리로_케이크_이미지_조회에_성공한다() { | ||
// given | ||
final String url = "%s%d%s/search/categories".formatted(BASE_URL, port, API_URL); | ||
final UriComponents uriComponents = UriComponentsBuilder | ||
.fromUriString(url) | ||
.queryParam("category", CakeDesignCategory.FLOWER) | ||
.queryParam("pageSize", 5) | ||
.build(); | ||
|
||
// when | ||
final ResponseEntity<ApiResponse> responseEntity = restTemplate.getForEntity(uriComponents.toUriString(), ApiResponse.class); | ||
|
||
// then | ||
final ApiResponse response = objectMapper.convertValue(responseEntity.getBody(), ApiResponse.class); | ||
final CakeImageListResponse data = objectMapper.convertValue(response.getData(), CakeImageListResponse.class); | ||
|
||
assertEquals(HttpStatusCode.valueOf(200), responseEntity.getStatusCode()); | ||
assertEquals(ReturnCode.SUCCESS.getCode(), response.getReturnCode()); | ||
assertEquals(ReturnCode.SUCCESS.getMessage(), response.getReturnMessage()); | ||
|
||
Long lastCakeId = data.cakeImages().stream().map(CakeImageResponseParam::cakeId).min(Long::compareTo).orElse(null); | ||
assertEquals(lastCakeId, data.lastCakeId()); | ||
assertEquals(5, data.size()); | ||
data.cakeImages().forEach(cakeImage -> { | ||
CakeCategory cakeCategory = cakeCategoryReader.findByCakeId(cakeImage.cakeId()); | ||
assertEquals(CakeDesignCategory.FLOWER, cakeCategory.getCakeDesignCategory()); | ||
}); | ||
} | ||
|
||
@Test | ||
void 카테고리로_다음_페이지_케이크_이미지_조회에_성공한다() { | ||
// given | ||
final String url = "%s%d%s/search/categories".formatted(BASE_URL, port, API_URL); | ||
final UriComponents uriComponents = UriComponentsBuilder | ||
.fromUriString(url) | ||
.queryParam("cakeId", 6) | ||
.queryParam("category", CakeDesignCategory.FLOWER) | ||
.queryParam("pageSize", 5) | ||
.build(); | ||
|
||
// when | ||
final ResponseEntity<ApiResponse> responseEntity = restTemplate.getForEntity(uriComponents.toUriString(), ApiResponse.class); | ||
|
||
// then | ||
final ApiResponse response = objectMapper.convertValue(responseEntity.getBody(), ApiResponse.class); | ||
final CakeImageListResponse data = objectMapper.convertValue(response.getData(), CakeImageListResponse.class); | ||
|
||
assertEquals(HttpStatusCode.valueOf(200), responseEntity.getStatusCode()); | ||
assertEquals(ReturnCode.SUCCESS.getCode(), response.getReturnCode()); | ||
assertEquals(ReturnCode.SUCCESS.getMessage(), response.getReturnMessage()); | ||
|
||
Long lastCakeId = data.cakeImages().stream().map(CakeImageResponseParam::cakeId).min(Long::compareTo).orElse(null); | ||
assertEquals(lastCakeId, data.lastCakeId()); | ||
assertEquals(5, data.size()); | ||
data.cakeImages().forEach(cakeImage -> { | ||
CakeCategory cakeCategory = cakeCategoryReader.findByCakeId(cakeImage.cakeId()); | ||
assertEquals(CakeDesignCategory.FLOWER, cakeCategory.getCakeDesignCategory()); | ||
}); | ||
} | ||
|
||
@Test | ||
void 카테고리로_케이크_이미지_조회_시_데이터가_없으면_빈_배열을_반환한다() { | ||
// given | ||
final String url = "%s%d%s/search/categories".formatted(BASE_URL, port, API_URL); | ||
final UriComponents uriComponents = UriComponentsBuilder | ||
.fromUriString(url) | ||
.queryParam("cakeId", 1) | ||
.queryParam("category", CakeDesignCategory.FLOWER) | ||
.queryParam("pageSize", 5) | ||
.build(); | ||
|
||
// when | ||
final ResponseEntity<ApiResponse> responseEntity = restTemplate.getForEntity(uriComponents.toUriString(), ApiResponse.class); | ||
|
||
// then | ||
final ApiResponse response = objectMapper.convertValue(responseEntity.getBody(), ApiResponse.class); | ||
final CakeImageListResponse data = objectMapper.convertValue(response.getData(), CakeImageListResponse.class); | ||
|
||
assertEquals(HttpStatusCode.valueOf(200), responseEntity.getStatusCode()); | ||
assertEquals(ReturnCode.SUCCESS.getCode(), response.getReturnCode()); | ||
assertEquals(ReturnCode.SUCCESS.getMessage(), response.getReturnMessage()); | ||
|
||
assertEquals(0, data.cakeImages().size()); | ||
assertNull(data.lastCakeId()); | ||
assertEquals(0, data.size()); | ||
} | ||
} |
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,13 @@ | ||
delete from cake_like; | ||
delete from cake_tag; | ||
delete from tag; | ||
delete from cake_category; | ||
delete from cake; | ||
|
||
delete from cake_shop_like; | ||
delete from cake_shop_link; | ||
delete from cake_shop_operation; | ||
delete from business_information; | ||
delete from cake_shop; | ||
|
||
delete from users; |
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,27 @@ | ||
insert into cake_shop (shop_id, thumbnail_url, shop_name, shop_bio, shop_description, latitude, longitude, like_count, linked_flag, | ||
created_at, updated_at) | ||
values (1, 'thumbnail_url', '케이크 맛집', '케이크 맛집입니다.', '케이크 맛집입니다.', 37.123456, 127.123456, 0, false, now(), now()); | ||
|
||
insert into cake (cake_id, shop_id, cake_name, cake_image_url, like_count, created_at, updated_at) | ||
values (1, 1, '케이크1', 'cake_image_url1', 0, now(), now()), | ||
(2, 1, '케이크2', 'cake_image_url2', 0, now(), now()), | ||
(3, 1, '케이크3', 'cake_image_url3', 0, now(), now()), | ||
(4, 1, '케이크4', 'cake_image_url4', 0, now(), now()), | ||
(5, 1, '케이크5', 'cake_image_url5', 0, now(), now()), | ||
(6, 1, '케이크6', 'cake_image_url6', 0, now(), now()), | ||
(7, 1, '케이크7', 'cake_image_url7', 0, now(), now()), | ||
(8, 1, '케이크8', 'cake_image_url8', 0, now(), now()), | ||
(9, 1, '케이크9', 'cake_image_url9', 0, now(), now()), | ||
(10, 1, '케이크10', 'cake_image_url10', 0, now(), now()); | ||
|
||
insert into cake_category (cake_category_id, cake_id, cake_design_category, created_at) | ||
values (1, 1, 'FLOWER', now()), | ||
(2, 2, 'FLOWER', now()), | ||
(3, 3, 'FLOWER', now()), | ||
(4, 4, 'FLOWER', now()), | ||
(5, 5, 'FLOWER', now()), | ||
(6, 6, 'FLOWER', now()), | ||
(7, 7, 'FLOWER', now()), | ||
(8, 8, 'FLOWER', now()), | ||
(9, 9, 'FLOWER', now()), | ||
(10, 10, 'FLOWER', now()); |
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,3 @@ | ||
insert into users (user_id, provider, provider_id, nickname, profile_image_url, email, gender, birthday, role, created_at, updated_at, | ||
deleted_at) | ||
values (1, 'GOOGLE', '123456', '테스트 유저', 'image_url', 'test@google.com', 'MALE', '1998-01-01', 'USER', now(), now(), null); |
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
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
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
Oops, something went wrong.