Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package in.koreatech.koin.domain.ownershop.service;

import java.time.Clock;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -29,6 +31,7 @@
import in.koreatech.koin.domain.shop.model.ShopCategoryMap;
import in.koreatech.koin.domain.shop.model.ShopImage;
import in.koreatech.koin.domain.shop.model.ShopOpen;
import in.koreatech.koin.domain.shop.repository.EventArticleRepository;
import in.koreatech.koin.domain.shop.repository.MenuCategoryMapRepository;
import in.koreatech.koin.domain.shop.repository.MenuCategoryRepository;
import in.koreatech.koin.domain.shop.repository.MenuDetailRepository;
Expand All @@ -47,6 +50,7 @@
@Transactional(readOnly = true)
public class OwnerShopService {

private final Clock clock;
private final ShopRepository shopRepository;
private final OwnerRepository ownerRepository;
private final ShopOpenRepository shopOpenRepository;
Expand All @@ -58,6 +62,7 @@ public class OwnerShopService {
private final MenuCategoryMapRepository menuCategoryMapRepository;
private final MenuImageRepository menuImageRepository;
private final MenuDetailRepository menuDetailRepository;
private final EventArticleRepository eventArticleRepository;

public OwnerShopsResponse getOwnerShops(Long ownerId) {
List<Shop> shops = shopRepository.findAllByOwnerId(ownerId);
Expand Down Expand Up @@ -99,7 +104,8 @@ public void createOwnerShops(Long ownerId, OwnerShopsRequest ownerShopsRequest)

public ShopResponse getShopByShopId(Long ownerId, Long shopId) {
Shop shop = getOwnerShopById(shopId, ownerId);
return ShopResponse.from(shop);
Boolean eventDuration = eventArticleRepository.isEvent(shopId, LocalDate.now(clock));
return ShopResponse.from(shop, eventDuration);
}

private Shop getOwnerShopById(Long shopId, Long ownerId) {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/in/koreatech/koin/domain/shop/dto/ShopResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ public record ShopResponse(

@JsonFormat(pattern = "yyyy-MM-dd")
@Schema(example = "2024-03-01", description = "업데이트 날짜")
LocalDateTime updatedAt
) {
LocalDateTime updatedAt,

public static ShopResponse from(Shop shop) {
@Schema(example = "true", description = "상점 이벤트 진행 여부")
Boolean isEvent
) {

public static ShopResponse from(Shop shop, Boolean isEvent) {
return new ShopResponse(
shop.getAddress(),
shop.getDelivery(),
Expand Down Expand Up @@ -97,7 +99,8 @@ public static ShopResponse from(Shop shop) {
shopCategory.getName()
);
}).toList(),
shop.getUpdatedAt()
shop.getUpdatedAt(),
isEvent
);
}

Expand All @@ -119,6 +122,7 @@ public record InnerShopOpen(
@JsonFormat(pattern = "HH:mm")
LocalTime closeTime
) {

public static InnerShopOpen from(ShopOpen shopOpen) {
return new InnerShopOpen(
shopOpen.getDayOfWeek(),
Expand All @@ -136,6 +140,7 @@ private record InnerShopCategory(
@Schema(example = "중국집", description = "이름")
String name
) {

}

private record InnerMenuCategory(
Expand All @@ -145,5 +150,6 @@ private record InnerMenuCategory(
@Schema(example = "대표 메뉴", description = "이름")
String name
) {

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package in.koreatech.koin.domain.shop.repository;

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

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

import in.koreatech.koin.domain.shop.model.EventArticle;
Expand All @@ -12,4 +14,10 @@ public interface EventArticleRepository extends Repository<EventArticle, Long> {

List<EventArticle> findAllByShopId(Long shopId);

@Query("""
SELECT COUNT(e) > 0 FROM EventArticle e
WHERE :now BETWEEN e.startDate AND e.endDate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service에서 clock now로 지금 시간 가져온담에 db의 start/enddate참조해서 그안에 있으면 참을 반환한다는 의민가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네넹

AND e.shop.id = :shopId
""")
Boolean isEvent(Long shopId, LocalDate now);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package in.koreatech.koin.domain.shop.service;

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

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -30,6 +32,7 @@
@RequiredArgsConstructor
public class ShopService {

private final Clock clock;
private final MenuRepository menuRepository;
private final MenuCategoryRepository menuCategoryRepository;
private final ShopRepository shopRepository;
Expand All @@ -55,7 +58,8 @@ public MenuCategoriesResponse getMenuCategories(Long shopId) {

public ShopResponse getShop(Long shopId) {
Shop shop = shopRepository.getById(shopId);
return ShopResponse.from(shop);
Boolean eventDuration = eventArticleRepository.isEvent(shopId, LocalDate.now(clock));
return ShopResponse.from(shop, eventDuration);
Comment on lines +61 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

}

public ShopMenuResponse getShopMenu(Long shopId) {
Expand Down
93 changes: 64 additions & 29 deletions src/test/java/in/koreatech/koin/acceptance/OwnerShopApiTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package in.koreatech.koin.acceptance;

import static in.koreatech.koin.domain.user.model.UserType.OWNER;
import static java.time.format.DateTimeFormatter.ofPattern;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.Mockito.when;

import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.List;

import org.assertj.core.api.SoftAssertions;
Expand Down Expand Up @@ -43,6 +48,7 @@
import in.koreatech.koin.domain.user.model.User;
import in.koreatech.koin.domain.user.model.UserGender;
import in.koreatech.koin.global.auth.JwtProvider;
import in.koreatech.koin.support.JsonAssertions;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.ExtractableResponse;
Expand Down Expand Up @@ -93,6 +99,9 @@ class OwnerShopApiTest extends AcceptanceTest {

@BeforeEach
void setUp() {
when(clock.instant()).thenReturn(
ZonedDateTime.parse("2024-02-21 18:00:00 KST", ofPattern("yyyy-MM-dd " + "HH:mm:ss z")).toInstant());
when(clock.getZone()).thenReturn(Clock.systemDefaultZone().getZone());

OwnerAttachment attachment = OwnerAttachment.builder()
.url("https://test.com/test.jpg")
Expand Down Expand Up @@ -337,8 +346,8 @@ void getShop() {
.dayOfWeek("FRIDAY")
.build();

ShopOpen newShopOpen1 = shopOpenRepository.save(open1);
ShopOpen newShopOpen2 = shopOpenRepository.save(open2);
shopOpenRepository.save(open1);
shopOpenRepository.save(open2);
Comment on lines +349 to +350
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다 여기 코드는 왜 인스턴스가 선언되어 있지...???

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

바꾸기 이전에는

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

복붙하면서 미사용코드 선언해둬서 미사용 변수 생략했습니다.


ShopCategoryMap shopCategoryMap1 = ShopCategoryMap.builder()
.shop(shop)
Expand All @@ -350,8 +359,8 @@ void getShop() {
.shopCategory(shopCategory2)
.build();

ShopCategoryMap newShopCategoryMap1 = shopCategoryMapRepository.save(shopCategoryMap1);
ShopCategoryMap newShopCategoryMap2 = shopCategoryMapRepository.save(shopCategoryMap2);
shopCategoryMapRepository.save(shopCategoryMap1);
shopCategoryMapRepository.save(shopCategoryMap2);

ShopImage shopImage1 = ShopImage.builder()
.imageUrl("https://test.com/test1.jpg")
Expand All @@ -363,14 +372,14 @@ void getShop() {
.shop(shop)
.build();

ShopImage newShopImage1 = shopImageRepository.save(shopImage1);
ShopImage newShopImage2 = shopImageRepository.save(shopImage2);
shopImageRepository.save(shopImage1);
shopImageRepository.save(shopImage2);

ExtractableResponse<Response> response = RestAssured
.given()
.header("Authorization", "Bearer " + token)
.when()
.get("/owner/shops/1")
.get("/owner/shops/{shopId}", shop.getId())
.then()
.statusCode(HttpStatus.OK.value())
.extract();
Expand All @@ -380,27 +389,53 @@ void getShop() {
List<ShopOpen> savedShopOpens = shopOpenRepository.findAllByShopId(shop.getId());
List<ShopCategoryMap> savedShopCategoryMaps = shopCategoryMapRepository.findAllByShopId(shop.getId());

assertSoftly(
softly -> {
softly.assertThat(response.body().jsonPath().getString("address")).isEqualTo(shop.getAddress());
softly.assertThat(response.body().jsonPath().getBoolean("delivery")).isEqualTo(shop.getDelivery());
softly.assertThat(response.body().jsonPath().getLong("delivery_price"))
.isEqualTo(shop.getDeliveryPrice());
softly.assertThat(response.body().jsonPath().getString("description")).isEqualTo(shop.getDescription());
softly.assertThat(response.body().jsonPath().getLong("id")).isEqualTo(shop.getId());
softly.assertThat(response.body().jsonPath().getString("name")).isEqualTo(shop.getName());
softly.assertThat(response.body().jsonPath().getBoolean("pay_bank")).isEqualTo(shop.getPayBank());
softly.assertThat(response.body().jsonPath().getBoolean("pay_card")).isEqualTo(shop.getPayCard());
softly.assertThat(response.body().jsonPath().getString("phone")).isEqualTo(shop.getPhone());

softly.assertThat(response.body().jsonPath().getList("image_urls")).hasSize(savedShopImages.size());
softly.assertThat(response.body().jsonPath().getList("menu_categories"))
.hasSize(savedMenuCategories.size());
softly.assertThat(response.body().jsonPath().getList("open")).hasSize(savedShopOpens.size());
softly.assertThat(response.body().jsonPath().getList("shop_categories"))
.hasSize(savedShopCategoryMaps.size());
}
);
JsonAssertions.assertThat(response.asPrettyString())
.isEqualTo(String.format("""
{
"address": "대전광역시 유성구 대학로 291",
"delivery": true,
"delivery_price": 3000,
"description": "테스트 상점입니다.",
"id": 1,
"image_urls": [
"https://test.com/test1.jpg",
"https://test.com/test2.jpg"
],
"menu_categories": [

],
"name": "테스트 상점",
"open": [
{
"day_of_week": "MONDAY",
"closed": false,
"open_time": "00:00",
"close_time": "21:00"
},
{
"day_of_week": "FRIDAY",
"closed": false,
"open_time": "00:00",
"close_time": "00:00"
}
],
"pay_bank": true,
"pay_card": true,
"phone": "010-1234-5678",
"shop_categories": [
{
"id": 1,
"name": "테스트1"
},
{
"id": 2,
"name": "테스트2"
}
],
"updated_at": "%s",
"is_event": false
}
""", LocalDateTime.now().format(ofPattern("yyyy-MM-dd"))));
}

@Test
Expand Down Expand Up @@ -656,7 +691,7 @@ void ownerCannotQueryOtherStoresWithoutPermission() {
.given()
.header("Authorization", "Bearer " + otherOwnerToken)
.when()
.get("/owner/shops/1")
.get("/owner/shops/{shopId}", shop.getId())
.then()
.statusCode(HttpStatus.FORBIDDEN.value())
.extract();
Expand Down
Loading