Skip to content

Commit

Permalink
[refactor] put data in deletedMenu Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Nov 12, 2024
1 parent 1b4d567 commit 124957a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
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.DeletedMenu;
import org.hankki.hankkiserver.domain.menu.repository.DeletedMenuRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DeletedMenuUpdater {

private final DeletedMenuRepository deletedMenuRepository;

public void save(final DeletedMenu deletedMenu) {
deletedMenuRepository.save(deletedMenu);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse;
import org.hankki.hankkiserver.api.store.service.StoreFinder;
import org.hankki.hankkiserver.api.store.service.StoreUpdater;
import org.hankki.hankkiserver.domain.menu.model.DeletedMenu;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.hankki.hankkiserver.event.EventPublisher;
Expand All @@ -25,6 +26,7 @@ public class MenuCommandService {
private final MenuUpdater menuUpdater;
private final StoreFinder storeFinder;
private final StoreUpdater storeUpdater;
private final DeletedMenuUpdater deletedMenuUpdater;
private final EventPublisher publisher;

@Transactional
Expand All @@ -33,7 +35,7 @@ public void deleteMenu(final MenuDeleteCommand command) {
Menu menu = menuFinder.findByStoreIdAndId(findStore.getId(), command.id());
menuDeleter.deleteMenu(menu);
updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId()));
checkNoMenuInStore(findStore, command.userId());
checkNoMenuInStore(findStore, command.userId(), menu);
}

@Transactional
Expand Down Expand Up @@ -70,10 +72,16 @@ private boolean validateMenuConflict(final Store store, final String menuName) {
return menuFinder.existsByStoreAndName(store, menuName);
}

private void checkNoMenuInStore(final Store store, final long userId) {
private void checkNoMenuInStore(final Store store, final long userId, final Menu menu) {
if (!menuFinder.existsByStoreId(store.getId())) {
storeUpdater.deleteStore(store.getId());
DeletedMenu deletedMenu = convertToDeletedMenu(menu, store.getId());
deletedMenuUpdater.save(deletedMenu);
publisher.publish(DeleteStoreEvent.of(store.getName(), userId));
}
}

private DeletedMenu convertToDeletedMenu(final Menu menu, final long storeId) {
return DeletedMenu.create(menu.getName(), menu.getPrice(), storeId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.hankki.hankkiserver.domain.menu.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hankki.hankkiserver.domain.common.BaseCreatedAtEntity;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "deleted_menu")
public class DeletedMenu extends BaseCreatedAtEntity {

@Id
@Column(name = "deleted_menu_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "price", nullable = false)
private int price;

@Column(name = "store_id", nullable = false)
private Long storeId;

@Builder
private DeletedMenu(final String name, final int price, final Long storeId) {
this.name = name;
this.price = price;
this.storeId = storeId;
}

public static DeletedMenu create(final String name, final int price, final Long storeId) {
return DeletedMenu.builder()
.name(name)
.price(price)
.storeId(storeId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hankki.hankkiserver.domain.menu.repository;

import org.hankki.hankkiserver.domain.menu.model.DeletedMenu;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DeletedMenuRepository extends JpaRepository<DeletedMenu, Long> {
}

0 comments on commit 124957a

Please sign in to comment.