From 41674588529b228919a9cea54ebc3b53391c589b Mon Sep 17 00:00:00 2001 From: HyunWoo9930 Date: Wed, 27 Nov 2024 20:50:34 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EA=B0=80=EA=B2=8C=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20api=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../menu/controller/MenuController.java | 7 +-- .../menu/repository/MenuRepository.java | 2 +- .../domain/menu/service/MenuService.java | 6 ++- .../jangburich/domain/store/domain/Store.java | 20 ++++++-- .../store/domain/StoreCreateRequestDTO.java | 24 +++++++--- .../store/domain/StoreGetResponseDTO.java | 47 ++++++++++++++----- .../domain/store/service/StoreService.java | 12 +++-- .../jangburich/utils/DayOfWeekConverter.java | 28 +++++++++++ 8 files changed, 113 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/jangburich/utils/DayOfWeekConverter.java diff --git a/src/main/java/com/jangburich/domain/menu/controller/MenuController.java b/src/main/java/com/jangburich/domain/menu/controller/MenuController.java index dfeffd3..c694ffa 100644 --- a/src/main/java/com/jangburich/domain/menu/controller/MenuController.java +++ b/src/main/java/com/jangburich/domain/menu/controller/MenuController.java @@ -3,7 +3,6 @@ import java.util.List; import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -15,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController; import com.jangburich.domain.menu.domain.MenuCreateRequestDTO; -import com.jangburich.domain.menu.domain.MenuGetResponseDTO; import com.jangburich.domain.menu.domain.MenuResponse; import com.jangburich.domain.menu.domain.MenuUpdateRequestDTO; import com.jangburich.domain.menu.service.MenuService; @@ -58,9 +56,8 @@ public ResponseCustom deleteMenu( } @GetMapping("") - public ResponseCustom> getMenu( - Authentication authentication, Pageable pageable) { - Page menu = menuService.getMenu(AuthenticationParser.parseUserId(authentication), pageable); + public ResponseCustom> getMenu(Authentication authentication) { + List menu = menuService.getMenu(AuthenticationParser.parseUserId(authentication)); return ResponseCustom.OK(menu); } } diff --git a/src/main/java/com/jangburich/domain/menu/repository/MenuRepository.java b/src/main/java/com/jangburich/domain/menu/repository/MenuRepository.java index 20a5bf9..6e9a47c 100644 --- a/src/main/java/com/jangburich/domain/menu/repository/MenuRepository.java +++ b/src/main/java/com/jangburich/domain/menu/repository/MenuRepository.java @@ -11,7 +11,7 @@ import org.springframework.data.jpa.repository.Query; public interface MenuRepository extends JpaRepository { - Page findAllByStore(Store store, Pageable pageable); + List findAllByStore(Store store); @Query("SELECT m.id, m.price FROM Menu m WHERE m.id IN :menuIds") List findPricesByMenuIds(List menuIds); diff --git a/src/main/java/com/jangburich/domain/menu/service/MenuService.java b/src/main/java/com/jangburich/domain/menu/service/MenuService.java index d42fc6f..b21be79 100644 --- a/src/main/java/com/jangburich/domain/menu/service/MenuService.java +++ b/src/main/java/com/jangburich/domain/menu/service/MenuService.java @@ -1,5 +1,7 @@ package com.jangburich.domain.menu.service; +import java.util.List; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -74,7 +76,7 @@ public void deleteMenu(String customOAuthUser, Long id) { menuRepository.delete(menu); } - public Page getMenu(String customOAuthUser, Pageable pageable) { + public List getMenu(String customOAuthUser) { User user = userRepository.findByProviderId(customOAuthUser) .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); @@ -84,6 +86,6 @@ public Page getMenu(String customOAuthUser, Pageable pageable) { Store store = storeRepository.findByOwner(owner) .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_STORE_ID)); - return menuRepository.findAllByStore(store, pageable); + return menuRepository.findAllByStore(store); } } diff --git a/src/main/java/com/jangburich/domain/store/domain/Store.java b/src/main/java/com/jangburich/domain/store/domain/Store.java index 04b92fe..4bd5741 100644 --- a/src/main/java/com/jangburich/domain/store/domain/Store.java +++ b/src/main/java/com/jangburich/domain/store/domain/Store.java @@ -2,6 +2,7 @@ import java.time.DayOfWeek; import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.List; import com.fasterxml.jackson.annotation.JsonFormat; @@ -99,7 +100,9 @@ public static Store create(Owner owner) { return newOwner; } - public static Store of(Owner owner, StoreCreateRequestDTO storeCreateRequestDTO) { + public static Store of(Owner owner, StoreCreateRequestDTO storeCreateRequestDTO, List dayOfWeeks) { + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm"); + Store newStore = new Store(); newStore.setOwner(owner); newStore.setName(storeCreateRequestDTO.getStoreName()); @@ -108,11 +111,18 @@ public static Store of(Owner owner, StoreCreateRequestDTO storeCreateRequestDTO) newStore.setLatitude(storeCreateRequestDTO.getLatitude()); newStore.setLongitude(storeCreateRequestDTO.getLongitude()); newStore.setAddress(storeCreateRequestDTO.getAddress()); - newStore.setAddress(storeCreateRequestDTO.getAddress()); newStore.setLocation(storeCreateRequestDTO.getLocation()); - newStore.setWorkDays(storeCreateRequestDTO.getDayOfWeek()); - newStore.setOpenTime(storeCreateRequestDTO.getOpenTime()); - newStore.setCloseTime(storeCreateRequestDTO.getCloseTime()); + newStore.setWorkDays(dayOfWeeks); + newStore.setOpenTime( + storeCreateRequestDTO.getOpenTime() != null + ? LocalTime.parse(storeCreateRequestDTO.getOpenTime(), timeFormatter) + : null + ); + newStore.setCloseTime( + storeCreateRequestDTO.getCloseTime() != null + ? LocalTime.parse(storeCreateRequestDTO.getCloseTime(), timeFormatter) + : null + ); newStore.setContactNumber(storeCreateRequestDTO.getPhoneNumber()); newStore.setReservationAvailable(storeCreateRequestDTO.getReservationAvailable()); newStore.setMaxReservation(storeCreateRequestDTO.getMaxReservation()); diff --git a/src/main/java/com/jangburich/domain/store/domain/StoreCreateRequestDTO.java b/src/main/java/com/jangburich/domain/store/domain/StoreCreateRequestDTO.java index 14d1172..f7066f4 100644 --- a/src/main/java/com/jangburich/domain/store/domain/StoreCreateRequestDTO.java +++ b/src/main/java/com/jangburich/domain/store/domain/StoreCreateRequestDTO.java @@ -1,8 +1,8 @@ package com.jangburich.domain.store.domain; -import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.List; import com.jangburich.domain.menu.domain.MenuCreateRequestDTO; @@ -17,6 +17,7 @@ @Setter @RequiredArgsConstructor public class StoreCreateRequestDTO { + private String storeName; private String phoneNumber; private String businessName; @@ -30,16 +31,14 @@ public class StoreCreateRequestDTO { @Enumerated(EnumType.STRING) private Category category; - // address private Double latitude; private Double longitude; private String address; private String location; - // business hour - private List dayOfWeek; - private LocalTime openTime; - private LocalTime closeTime; + private String dayOfWeek; + private String openTime; + private String closeTime; private List menuCreateRequestDTOS; @@ -47,4 +46,17 @@ public class StoreCreateRequestDTO { private Long prepaymentDuration; private Boolean reservationAvailable; private Long maxReservation; + + private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm"); + + public void setOpenTime(LocalTime openTime) { + this.openTime = openTime != null ? openTime.format(TIME_FORMATTER) : null; + } + + public void setCloseTime(LocalTime closeTime) { + this.closeTime = closeTime != null ? closeTime.format(TIME_FORMATTER) : null; + } } + + + diff --git a/src/main/java/com/jangburich/domain/store/domain/StoreGetResponseDTO.java b/src/main/java/com/jangburich/domain/store/domain/StoreGetResponseDTO.java index e2a0c13..a611407 100644 --- a/src/main/java/com/jangburich/domain/store/domain/StoreGetResponseDTO.java +++ b/src/main/java/com/jangburich/domain/store/domain/StoreGetResponseDTO.java @@ -1,8 +1,11 @@ package com.jangburich.domain.store.domain; import java.time.DayOfWeek; -import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.List; +import java.util.stream.Collectors; + +import com.jangburich.domain.menu.domain.MenuResponse; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -31,16 +34,19 @@ public class StoreGetResponseDTO { private Double longitude; private String address; private String location; - private List dayOfWeek; - private LocalTime openTime; - private LocalTime closeTime; + private String dayOfWeek; + private String openTime; + private String closeTime; + private List menuResponses; + + private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm"); public StoreGetResponseDTO(Long id, String ownerId, String name, Category category, String representativeImage, Boolean reservationAvailable, Long maxReservation, Long minPrepayment, Long prepaymentDuration, String introduction, - Double latitude, Double longitude, String address, String location, List dayOfWeek, - LocalTime openTime, - LocalTime closeTime) { + Double latitude, Double longitude, String address, String location, String dayOfWeek, + String openTime, + String closeTime, List menuResponses) { this.id = id; this.ownerId = ownerId; this.name = name; @@ -58,9 +64,27 @@ public StoreGetResponseDTO(Long id, String ownerId, String name, Category catego this.dayOfWeek = dayOfWeek; this.openTime = openTime; this.closeTime = closeTime; + this.menuResponses = menuResponses; } - public StoreGetResponseDTO of(Store store) { + private String convertDayOfWeekToKorean(DayOfWeek dayOfWeek) { + return switch (dayOfWeek) { + case MONDAY -> "월"; + case TUESDAY -> "화"; + case WEDNESDAY -> "수"; + case THURSDAY -> "목"; + case FRIDAY -> "금"; + case SATURDAY -> "토"; + case SUNDAY -> "일"; + default -> throw new IllegalArgumentException("Invalid DayOfWeek"); + }; + } + + public StoreGetResponseDTO of(Store store, List menuResponses) { + String dayOfWeekString = store.getWorkDays().stream() + .map(this::convertDayOfWeekToKorean) + .collect(Collectors.joining(", ")); + return new StoreGetResponseDTO( store.getId(), store.getOwner().getUser().getProviderId(), @@ -76,9 +100,10 @@ public StoreGetResponseDTO of(Store store) { store.getLongitude(), store.getAddress(), store.getLocation(), - store.getWorkDays(), - store.getOpenTime(), - store.getCloseTime() + dayOfWeekString, + store.getOpenTime().format(TIME_FORMATTER), + store.getCloseTime().format(TIME_FORMATTER), + menuResponses ); } } diff --git a/src/main/java/com/jangburich/domain/store/service/StoreService.java b/src/main/java/com/jangburich/domain/store/service/StoreService.java index 69218e6..42273af 100644 --- a/src/main/java/com/jangburich/domain/store/service/StoreService.java +++ b/src/main/java/com/jangburich/domain/store/service/StoreService.java @@ -1,5 +1,6 @@ package com.jangburich.domain.store.service; +import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; @@ -13,6 +14,7 @@ import com.jangburich.domain.menu.domain.Menu; import com.jangburich.domain.menu.domain.MenuCreateRequestDTO; +import com.jangburich.domain.menu.domain.MenuResponse; import com.jangburich.domain.menu.repository.MenuRepository; import com.jangburich.domain.order.domain.Cart; import com.jangburich.domain.order.domain.OrderResponse; @@ -49,6 +51,7 @@ import com.jangburich.global.config.s3.S3Service; import com.jangburich.global.error.DefaultNullPointerException; import com.jangburich.global.payload.ErrorCode; +import com.jangburich.utils.DayOfWeekConverter; import lombok.RequiredArgsConstructor; @@ -79,7 +82,9 @@ public void createStore(String authentication, StoreCreateRequestDTO storeCreate Owner owner = ownerRepository.findByUser(user) .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); - Store store = storeRepository.save(Store.of(owner, storeCreateRequestDTO)); + List dayOfWeekList = DayOfWeekConverter.convertStringToDayOfWeekList(storeCreateRequestDTO.getDayOfWeek()); + + Store store = storeRepository.save(Store.of(owner, storeCreateRequestDTO, dayOfWeekList)); store.setRepresentativeImage(s3Service.uploadImageToS3(image)); @@ -104,7 +109,6 @@ public void createStore(String authentication, StoreCreateRequestDTO storeCreate } } } - } @Transactional @@ -206,7 +210,9 @@ public StoreGetResponseDTO getStoreInfo(String authentication) { throw new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION); } - return new StoreGetResponseDTO().of(store); + List menus = menuRepository.findAllByStore(store); + + return new StoreGetResponseDTO().of(store, menus); } public List getPaymentGroup(String userId) { diff --git a/src/main/java/com/jangburich/utils/DayOfWeekConverter.java b/src/main/java/com/jangburich/utils/DayOfWeekConverter.java new file mode 100644 index 0000000..743f0d4 --- /dev/null +++ b/src/main/java/com/jangburich/utils/DayOfWeekConverter.java @@ -0,0 +1,28 @@ +package com.jangburich.utils; + +import java.time.DayOfWeek; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class DayOfWeekConverter { + + public static DayOfWeek convertKoreanToDayOfWeek(String koreanDay) { + return switch (koreanDay.trim()) { + case "월" -> DayOfWeek.MONDAY; + case "화" -> DayOfWeek.TUESDAY; + case "수" -> DayOfWeek.WEDNESDAY; + case "목" -> DayOfWeek.THURSDAY; + case "금" -> DayOfWeek.FRIDAY; + case "토" -> DayOfWeek.SATURDAY; + case "일" -> DayOfWeek.SUNDAY; + default -> throw new IllegalArgumentException("Invalid day: " + koreanDay); + }; + } + + public static List convertStringToDayOfWeekList(String daysString) { + return Arrays.stream(daysString.split(",")) + .map(DayOfWeekConverter::convertKoreanToDayOfWeek) + .collect(Collectors.toList()); + } +}