Skip to content

Commit

Permalink
Merge pull request #205 from Team-Hankki/develop
Browse files Browse the repository at this point in the history
[feat] 메뉴편집, 둘러보기 추가
  • Loading branch information
Parkjyun authored Oct 13, 2024
2 parents e2d9c0e + 258966a commit 49702d1
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Temporary Items

application-local.yml
application-dev.yml
application-prod.yml

# Qclass
/src/main/generated
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ tasks.register('copyYml', Copy) {
include "*.yml"
into 'src/main/resources'
}
}
}
2 changes: 1 addition & 1 deletion server-yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.hankki.hankkiserver.api.menu.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.dto.HankkiResponse;
import org.hankki.hankkiserver.api.menu.service.MenuCommandService;
import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand;
import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand;
import org.hankki.hankkiserver.api.menu.service.command.MenuPostCommand;
import org.hankki.hankkiserver.api.menu.service.command.MenusPostCommand;
import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse;
import org.hankki.hankkiserver.api.store.controller.request.MenuPostRequest;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class MenuController {

private final MenuCommandService menuCommandService;

@DeleteMapping("/{storeId}/menus/{id}")
public HankkiResponse<Void> deleteMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id) {
menuCommandService.deleteMenu(MenuDeleteCommand.of(storeId, id));
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}

@PatchMapping("/{storeId}/menus/{id}")
public HankkiResponse<Void> updateMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id,
@Valid @RequestBody final MenuPostRequest request) {
menuCommandService.modifyMenu(MenuPatchCommand.of(storeId, id, request.name(), request.price()));
return HankkiResponse.success(CommonSuccessCode.OK);
}

@PostMapping("{storeId}/menus/bulk")
public HankkiResponse<MenusPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final List<MenuPostRequest> request) {
List<MenuPostCommand> command = request.stream()
.map(r -> MenuPostCommand.of(r.name(), r.price()))
.toList();
return HankkiResponse.success(CommonSuccessCode.CREATED, menuCommandService.createMenus(MenusPostCommand.of(storeId, command)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.hankki.hankkiserver.api.menu.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand;
import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand;
import org.hankki.hankkiserver.api.menu.service.command.MenusPostCommand;
import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse;
import org.hankki.hankkiserver.api.store.service.StoreFinder;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuCommandService {

private final MenuDeleter menuDeleter;
private final MenuFinder menuFinder;
private final MenuUpdater menuUpdater;
private final StoreFinder storeFinder;

@Transactional
public void deleteMenu(final MenuDeleteCommand command) {
Menu menu = menuFinder.findByStoreIdAndId(command.storeId(), command.id());
menuDeleter.deleteMenu(menu);
updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId()));
}

@Transactional
public void modifyMenu(final MenuPatchCommand command) {
Menu menu = menuFinder.findByStoreIdAndId(command.storeId(), command.id());
menu.update(command.name(), command.price());
updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId()));
}

@Transactional
public MenusPostResponse createMenus(final MenusPostCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
List<Menu> menus = command.menu().stream()
.filter(c -> !validateMenuConflict(findStore, c.name()))
.map(c -> Menu.create(findStore, c.name(), c.price()))
.toList();
menuUpdater.saveAll(menus);
updateLowestPriceInStore(findStore);
return MenusPostResponse.of(menus);
}

private void updateLowestPriceInStore(final Store findStore) {
findStore.updateLowestPrice(menuFinder.findLowestPriceByStore(findStore));
}

private boolean validateMenuConflict(Store store, String menuName) {
return menuFinder.existsByStoreAndName(store, menuName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hankki.hankkiserver.api.menu.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.menu.repository.MenuRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MenuDeleter {

private final MenuRepository menuRepository;

protected void deleteMenu(final Menu menu) {
menuRepository.delete(menu);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.hankki.hankkiserver.api.menu.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.code.MenuErrorCode;
import org.hankki.hankkiserver.common.exception.NotFoundException;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.menu.repository.MenuRepository;
import org.hankki.hankkiserver.domain.store.model.Store;
Expand All @@ -17,4 +19,16 @@ public class MenuFinder {
public List<Menu> findAllByStore(final Store store) {
return menuRepository.findAllByStore(store);
}

protected Menu findByStoreIdAndId(final Long storeId, final Long id) {
return menuRepository.findByStoreIdAndId(storeId,id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND));
}

protected boolean existsByStoreAndName(final Store store, final String name) {
return menuRepository.existsByStoreAndName(store, name);
}

protected int findLowestPriceByStore(Store store) {
return menuRepository.findLowestPriceByStore(store);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public class MenuUpdater {
public void saveAll(final List<Menu> menus) {
menuRepository.saveAll(menus);
}

protected void save(final Menu menu) {
menuRepository.save(menu);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.hankki.hankkiserver.api.menu.service.command;

public record MenuDeleteCommand(
long storeId,
long id
) {
public static MenuDeleteCommand of(long storeId, long id) {
return new MenuDeleteCommand(storeId, id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hankki.hankkiserver.api.menu.service.command;

public record MenuPatchCommand(
long storeId,
long id,
String name,
int price
) {
public static MenuPatchCommand of(final long storeId, final long id, final String name, final int price) {
return new MenuPatchCommand(storeId, id, name, price);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.hankki.hankkiserver.api.menu.service.command;

public record MenuPostCommand(
String name,
int price
) {
public static MenuPostCommand of(final String name, final int price) {
return new MenuPostCommand(name, price);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hankki.hankkiserver.api.menu.service.command;

import java.util.List;

public record MenusPostCommand (
long storeId,
List<MenuPostCommand> menu
){
public static MenusPostCommand of (final long storeId, final List<MenuPostCommand> menu) {
return new MenusPostCommand(storeId, menu);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hankki.hankkiserver.api.menu.service.response;

import org.hankki.hankkiserver.domain.menu.model.Menu;

public record MenuPostResponse (
long id,
String name,
int price
) {

public static MenuPostResponse of(final Menu menu) {
return new MenuPostResponse(menu.getId(), menu.getName(), menu.getPrice());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.hankki.hankkiserver.api.menu.service.response;

import org.hankki.hankkiserver.domain.menu.model.Menu;

import java.util.List;

public record MenusPostResponse(
List<MenuPostResponse> menuList
) {
public static MenusPostResponse of(List<Menu> menus) {
List<MenuPostResponse> menuResponses = menus.stream()
.map(MenuPostResponse::of)
.toList();
return new MenusPostResponse(menuResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import org.hankki.hankkiserver.domain.menu.model.Menu;

public record MenuResponse(
Long id,
String name,
int price
) {
public static MenuResponse of(Menu menu) {
return new MenuResponse(menu.getName(), menu.getPrice());
return new MenuResponse(menu.getId(), menu.getName(), menu.getPrice());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public class SecurityConfig {
private final JwtValidator jwtValidator;
private final JwtProvider jwtProvider;

private static final String[] whiteList = {"/api/v1/auth/login", "/api/v1/auth/reissue", "/actuator/health"};
private static final String[] authWhiteList = {"/api/v1/auth/login", "/api/v1/auth/reissue", "/actuator/health"};
private static final String[] businessLogicWhileList = {"/api/v1/stores/categories", "/api/v1/stores/sort-options", "/api/v1/stores/price-categories",
"/api/v1/stores", "/api/v1/stores/pins", "/api/v1/stores/{articleId:\\d+}/thumbnail", "/api/v1/universities"};

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand All @@ -50,6 +52,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return web -> web.ignoring().requestMatchers(whiteList);
return web -> web.ignoring()
.requestMatchers(authWhiteList)
.requestMatchers(businessLogicWhileList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.hankki.hankkiserver.common.code;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor
public enum MenuErrorCode implements ErrorCode {

MENU_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 메뉴입니다."),
ALREADY_EXISTED_MENU(HttpStatus.CONFLICT, "이미 존재하는 메뉴입니다.");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public static Menu create(final Store store, final String name, final int price)
.price(price)
.build();
}

public void update(final String name, final int price) {
this.name = name;
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface MenuRepository extends JpaRepository<Menu, Long> {
List<Menu> findAllByStore(Store store);
Optional<Menu> findByStoreIdAndId(Long storeId, Long id);
boolean existsByStoreAndName(Store store, String name);

@Query("SELECT MIN(m.price) FROM Menu m WHERE m.store = :store")
int findLowestPriceByStore(Store store);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public void softDelete() {
public String getImageUrlOrElseNull() {
return images.isEmpty() ? null : images.get(0).getImageUrl();
}

public void updateLowestPrice(int lowestPrice) {
this.lowestPrice = lowestPrice;
}
}

0 comments on commit 49702d1

Please sign in to comment.