Skip to content

Commit acda443

Browse files
authored
Merge pull request #291 from GTable/feature/#290-public-code
Feature: public code 추가
2 parents 27b935d + 3cfd573 commit acda443

10 files changed

Lines changed: 80 additions & 32 deletions

File tree

nowait-app-admin-api/src/main/java/com/nowait/applicationadmin/store/dto/StoreCreateRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nowait.applicationadmin.store.dto;
22

3+
import com.nowait.common.token.TokenGenerator;
34
import com.nowait.domaincorerdb.store.entity.Store;
45

56
import jakarta.validation.constraints.NotBlank;
@@ -25,6 +26,7 @@ public class StoreCreateRequest {
2526

2627
public Store toEntity() {
2728
return Store.builder()
29+
.publicCode(TokenGenerator.base62(12))
2830
.departmentId(departmentId)
2931
.name(name)
3032
.location(location)

nowait-app-admin-api/src/main/java/com/nowait/applicationadmin/store/dto/StoreCreateResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
public class StoreCreateResponse {
1515

1616
private Long storeId;
17+
private String publicCode;
1718
private Long departmentId;
1819
private String name;
1920
private String location;
@@ -29,6 +30,7 @@ public static StoreCreateResponse fromEntity(Store store) {
2930
return StoreCreateResponse.builder()
3031
.createdAt(store.getCreatedAt())
3132
.storeId(store.getStoreId())
33+
.publicCode(store.getPublicCode())
3234
.departmentId(store.getDepartmentId())
3335
.name(store.getName())
3436
.location(store.getLocation())

nowait-app-user-api/src/main/java/com/nowait/applicationuser/menu/controller/MenuController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,32 @@ public class MenuController {
2525

2626
private final MenuService menuService;
2727

28-
@GetMapping("/all-menus/stores/{storeId}")
28+
@GetMapping("/all-menus/stores/{publicCode}")
2929
@Operation(summary = "가게의 모든 메뉴 조회", description = "특정 가게의 모든 메뉴를 조회합니다.")
3030
@ApiResponse(responseCode = "200", description = "모든 메뉴를 조회 성공")
31-
public ResponseEntity<?> getMenusByStoreId(@PathVariable Long storeId) {
31+
public ResponseEntity<?> getMenusByStoreId(@PathVariable String publicCode) {
3232
return ResponseEntity
3333
.status(HttpStatus.OK)
3434
.body(
3535
ApiUtils.success(
36-
menuService.getAllMenusByStoreId(storeId)
36+
menuService.getAllMenusByStoreId(publicCode)
3737
)
3838
);
3939
}
4040

41-
@GetMapping("/{storeId}/{menuId}")
41+
@GetMapping("/{publicCode}/{menuId}")
4242
@Operation(
4343
summary = "메뉴 ID로 메뉴 조회", description = "특정 가게의 특정 메뉴를 ID로 조회합니다.")
4444
@ApiResponse(responseCode = "200", description = "메뉴 조회 성공")
4545
public ResponseEntity<?> getMenuById(
46-
@PathVariable Long storeId,
46+
@PathVariable String publicCode,
4747
@PathVariable Long menuId
4848
) {
4949
return ResponseEntity
5050
.status(HttpStatus.OK)
5151
.body(
5252
ApiUtils.success(
53-
menuService.getMenuById(storeId, menuId)
53+
menuService.getMenuById(publicCode, menuId)
5454
)
5555
);
5656
}

nowait-app-user-api/src/main/java/com/nowait/applicationuser/menu/service/MenuService.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ public class MenuService {
3030

3131

3232
@Transactional(readOnly = true)
33-
public MenuReadResponse getAllMenusByStoreId(Long storeId) {
34-
if (storeId == null) {
33+
public MenuReadResponse getAllMenusByStoreId(String publicCode) {
34+
if (publicCode == null) {
3535
throw new MenuParamEmptyException();
3636
}
3737

38-
Store store = storeRepository.findById(storeId)
38+
Store store = storeRepository.findByPublicCodeAndDeletedFalse(publicCode)
3939
.orElseThrow(StoreNotFoundException::new);
4040

4141
String storeName = store.getName();
42+
Long storeId = store.getStoreId();
4243
List<Menu> menus = menuRepository.findAllByStoreIdAndDeletedFalseOrderBySortOrder(storeId);
4344

4445
List<MenuReadDto> menuReadResponse = menus.stream()
@@ -55,15 +56,15 @@ public MenuReadResponse getAllMenusByStoreId(Long storeId) {
5556
}
5657

5758
@Transactional(readOnly = true)
58-
public MenuReadDto getMenuById(Long storeId, Long menuId) {
59-
if (storeId == null || menuId == null) {
59+
public MenuReadDto getMenuById(String publicCode, Long menuId) {
60+
if (publicCode == null || menuId == null) {
6061
throw new MenuParamEmptyException();
6162
}
6263

63-
storeRepository.findById(storeId)
64+
Store store = storeRepository.findByPublicCodeAndDeletedFalse(publicCode)
6465
.orElseThrow(StoreNotFoundException::new);
6566

66-
Menu menu = menuRepository.findByStoreIdAndIdAndDeletedFalse(storeId, menuId)
67+
Menu menu = menuRepository.findByStoreIdAndIdAndDeletedFalse(store.getStoreId(), menuId)
6768
.orElseThrow(MenuNotFoundException::new);
6869

6970
List<MenuImage> images = menuImageRepository.findByMenu(menu);

nowait-app-user-api/src/main/java/com/nowait/applicationuser/order/controller/OrderController.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,48 @@
3232
public class OrderController {
3333
private final OrderService orderService;
3434

35-
@PostMapping("/create/{storeId}/{tableId}")
35+
// @PostMapping("/create/{storeId}/{tableId}")
36+
// @Operation(summary = "주문 생성", description = "특정 주점 - 특정 테이블에 대한 주문 생성")
37+
// @ApiResponse(responseCode = "201", description = "주문 생성")
38+
// public ResponseEntity<?> createOrder(
39+
// @PathVariable Long storeId,
40+
// @PathVariable Long tableId,
41+
// @RequestBody @Valid OrderCreateRequestDto orderCreateRequestDto,
42+
// HttpSession session
43+
// ) {
44+
// String sessionId = session.getId();
45+
// OrderCreateResponseDto response = orderService.createOrder(storeId, tableId, orderCreateRequestDto, sessionId);
46+
// return ResponseEntity
47+
// .status(HttpStatus.CREATED)
48+
// .body(
49+
// ApiUtils.success(response)
50+
// );
51+
// }
52+
53+
@PostMapping("/create/{publicCode}/{tableId}")
3654
@Operation(summary = "주문 생성", description = "특정 주점 - 특정 테이블에 대한 주문 생성")
3755
@ApiResponse(responseCode = "201", description = "주문 생성")
3856
public ResponseEntity<?> createOrder(
39-
@PathVariable Long storeId,
57+
@PathVariable String publicCode,
4058
@PathVariable Long tableId,
4159
@RequestBody @Valid OrderCreateRequestDto orderCreateRequestDto,
4260
HttpSession session
43-
) {
61+
) {
4462
String sessionId = session.getId();
45-
OrderCreateResponseDto response = orderService.createOrder(storeId,tableId,orderCreateRequestDto,sessionId);
63+
OrderCreateResponseDto response = orderService.createOrder(publicCode, tableId, orderCreateRequestDto,
64+
sessionId);
4665
return ResponseEntity
4766
.status(HttpStatus.CREATED)
4867
.body(
4968
ApiUtils.success(response)
5069
);
5170
}
5271

53-
@GetMapping("/items/{storeId}/{tableId}")
72+
@GetMapping("/items/{publicCode}/{tableId}")
5473
@Operation(summary = "테이블별 주문 아이템 조회", description = "비로그인(세션) 기준으로 테이블의 내 주문 목록만 조회")
5574
@ApiResponse(responseCode = "200", description = "주문 조회")
5675
public ResponseEntity<?> getOrderItems(
57-
@PathVariable Long storeId,
76+
@PathVariable String publicCode,
5877
@PathVariable Long tableId,
5978
HttpServletRequest request
6079
) {
@@ -64,7 +83,7 @@ public ResponseEntity<?> getOrderItems(
6483
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.success(List.of()));
6584
}
6685
String sessionId = session.getId();
67-
List<OrderResponseDto> orderItems = orderService.getOrderItemsGroupByOrderId(storeId, tableId, sessionId);
86+
List<OrderResponseDto> orderItems = orderService.getOrderItemsGroupByOrderId(publicCode, tableId, sessionId);
6887
return ResponseEntity.
6988
status(HttpStatus.OK)
7089
.body(

nowait-app-user-api/src/main/java/com/nowait/applicationuser/order/service/OrderService.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ public class OrderService {
3939
private final MenuRepository menuRepository;
4040
private final OrderItemRepository orderItemRepository;
4141
@Transactional
42-
public OrderCreateResponseDto createOrder(Long storeId, Long tableId,
42+
public OrderCreateResponseDto createOrder(String publicCode, Long tableId,
4343
OrderCreateRequestDto orderCreateRequestDto, String sessionId) {
44-
parameterValidation(storeId, tableId, orderCreateRequestDto);
44+
parameterValidation(publicCode, tableId, orderCreateRequestDto);
4545

4646
// 💡 [중복 주문 방지] signature 생성 및 체크
47-
String signature = generateOrderSignature(storeId, tableId, orderCreateRequestDto.getItems());
47+
String signature = generateOrderSignature(publicCode, tableId, orderCreateRequestDto.getItems());
4848
checkDuplicateOrderSignature(signature);
4949

5050
// 1. Store 조회
51-
Store store = storeRepository.findById(storeId)
51+
Store store = storeRepository.findByPublicCodeAndDeletedFalse(publicCode)
5252
.orElseThrow(() -> new IllegalArgumentException("store not found"));
5353

5454
// 2. UserOrder 생성 및 signature 저장
@@ -95,9 +95,9 @@ public OrderCreateResponseDto createOrder(Long storeId, Long tableId,
9595

9696
@Transactional(readOnly = true)
9797
public List<OrderResponseDto> getOrderItemsGroupByOrderId(
98-
Long storeId, Long tableId, String sessionId) {
98+
String publicCode, Long tableId, String sessionId) {
9999

100-
List<UserOrder> userOrders = orderRepository.findByStore_StoreIdAndTableIdAndSessionId(storeId, tableId, sessionId);
100+
List<UserOrder> userOrders = orderRepository.findByStore_PublicCodeAndTableIdAndSessionId(publicCode, tableId, sessionId);
101101

102102
// orderId 기준으로 바로 변환
103103
return userOrders.stream()
@@ -116,8 +116,8 @@ public List<OrderResponseDto> getOrderItemsGroupByOrderId(
116116
}
117117

118118

119-
private static void parameterValidation(Long storeId, Long tableId, OrderCreateRequestDto orderCreateRequestDto) {
120-
if (storeId == null || tableId == null || orderCreateRequestDto == null) {
119+
private static void parameterValidation(String publicCode, Long tableId, OrderCreateRequestDto orderCreateRequestDto) {
120+
if (publicCode == null || tableId == null || orderCreateRequestDto == null) {
121121
throw new OrderParameterEmptyException();
122122
}
123123
if (orderCreateRequestDto.getItems() == null || orderCreateRequestDto.getItems().isEmpty()) {
@@ -130,7 +130,7 @@ private static void parameterValidation(Long storeId, Long tableId, OrderCreateR
130130
throw new IllegalArgumentException("Depositor name is too long");
131131
}
132132
}
133-
private String generateOrderSignature(Long storeId, Long tableId, List<CartItemDto> items) {
133+
private String generateOrderSignature(String storeId, Long tableId, List<CartItemDto> items) {
134134
String cartString = items.stream()
135135
.sorted((a, b) -> a.getMenuId().compareTo(b.getMenuId())) // 메뉴 ID 기준 정렬
136136
.map(item -> item.getMenuId() + ":" + item.getQuantity())
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.nowait.common.token;
2+
3+
import java.security.SecureRandom;
4+
5+
public final class TokenGenerator {
6+
private static final SecureRandom RNG = new SecureRandom();
7+
private static final char[] ALPHABET =
8+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
9+
10+
// 12~16 권장
11+
public static String base62(int len) {
12+
char[] out = new char[len];
13+
for (int i = 0; i < len; i++) out[i] = ALPHABET[RNG.nextInt(ALPHABET.length)];
14+
return new String(out);
15+
}
16+
private TokenGenerator() {}
17+
}

nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/order/repository/OrderRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public interface OrderRepository extends JpaRepository<UserOrder, Long> {
1414
boolean existsBySignatureAndCreatedAtAfter(String signature, LocalDateTime createdAt);
1515

16-
List<UserOrder> findByStore_StoreIdAndTableIdAndSessionId(Long storeId, Long tableId, String sessionId);
16+
List<UserOrder> findByStore_PublicCodeAndTableIdAndSessionId(String publicCode, Long tableId, String sessionId);
1717

1818
@EntityGraph(attributePaths = {"orderItems", "orderItems.menu"})
1919
List<UserOrder> findAllByStore_StoreIdAndCreatedAtBetween(Long storeId, LocalDateTime startDateTime, LocalDateTime endDateTime);

nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/store/entity/Store.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class Store extends BaseTimeEntity {
2828
@GeneratedValue(strategy = GenerationType.IDENTITY)
2929
private Long storeId;
3030

31+
@Column(nullable = false, unique = true, updatable = false, length = 12)
32+
private String publicCode;
33+
3134
@Column(name = "department_id", nullable = false)
3235
private Long departmentId;
3336

@@ -56,7 +59,8 @@ public class Store extends BaseTimeEntity {
5659
private Boolean deleted;
5760

5861
public Store(LocalDateTime createdAt, Long storeId, Long departmentId, String name, String location,
59-
String description, String noticeTitle, String noticeContent, String openTime, Boolean isActive, Boolean deleted) {
62+
String description, String noticeTitle, String noticeContent, String openTime, Boolean isActive,
63+
Boolean deleted) {
6064
super(createdAt);
6165
this.storeId = storeId;
6266
this.departmentId = departmentId;
@@ -70,7 +74,8 @@ public Store(LocalDateTime createdAt, Long storeId, Long departmentId, String na
7074
this.deleted = deleted;
7175
}
7276

73-
public void updateInfo(String name, String location, String description, String noticeTitle, String notice, String openTime) {
77+
public void updateInfo(String name, String location, String description, String noticeTitle, String notice,
78+
String openTime) {
7479
if (name != null)
7580
this.name = name;
7681
if (location != null)

nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/store/repository/StoreRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public interface StoreRepository extends JpaRepository<Store, Long>, StoreCustom
1818

1919
Optional<Store> findByStoreIdAndDeletedFalse(Long storeId);
2020

21+
Optional<Store> findByPublicCodeAndDeletedFalse(String publicCode);
22+
2123
Slice<Store> findAllByDeletedFalseOrderByStoreIdAsc(Pageable pageable);
2224

2325
@Query(value = """

0 commit comments

Comments
 (0)