Skip to content

Commit

Permalink
refactor : meal and dish domain
Browse files Browse the repository at this point in the history
  • Loading branch information
JiwonKKang committed Sep 2, 2024
1 parent eec3c95 commit ac76f29
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public Response<Void> createDish(
dishService.createDish(
TargetDishStore.from(storeId),
TargetDishCategory.from(categoryId),
toFile(file),
request.toDishInfo());
request.toContent(),
toFile(file));
return Response.success("반찬 생성 성공");
}

Expand All @@ -72,7 +72,7 @@ public Response<Void> updateDish(
@PathVariable("dishId") Long dishId,
@RequestPart(required = false) MultipartFile file,
@RequestPart("request") DishRequest request) {
dishService.updateDish(TargetDish.from(dishId), toFile(file), request.toDishInfo());
dishService.updateDish(TargetDish.from(dishId), toFile(file), request.toContent());
return Response.success("반찬 수정 성공");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package core.startup.mealtoktok.api.dishstore.request;

import core.startup.mealtoktok.domain.dishstore.DishInfo;
import core.startup.mealtoktok.domain.dishstore.DishContent;

public record DishRequest(String dishName, int dishPrice, int dishQuantity) {
public DishInfo toDishInfo() {
return DishInfo.of(dishName, dishPrice, dishQuantity);

public DishContent toContent() {
return DishContent.of(dishName, dishPrice, dishQuantity);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package core.startup.mealtoktok.api.dishstore.response;

import core.startup.mealtoktok.common.dto.Money;
import core.startup.mealtoktok.domain.dishstore.DishAndImage;
import core.startup.mealtoktok.domain.dishstore.DishState;
import core.startup.mealtoktok.domain.dishstore.DishWithImage;

public record DishResponse(
Long dishId,
Expand All @@ -12,13 +12,13 @@ public record DishResponse(
int dishQuantity,
DishState dishState) {

public static DishResponse from(DishAndImage dishAndImage) {
public static DishResponse from(DishWithImage dishWithImage) {
return new DishResponse(
dishAndImage.dish().getDishId(),
dishAndImage.dish().getDishName(),
dishAndImage.dish().getDishPrice(),
dishAndImage.image().getImageUrl(),
dishAndImage.dish().getDishQuantity(),
dishAndImage.dish().getDishState());
dishWithImage.dish().getDishId(),
dishWithImage.dish().getDishName(),
dishWithImage.dish().getDishPrice(),
dishWithImage.image().getImageUrl(),
dishWithImage.dish().getDishQuantity(),
dishWithImage.dish().getDishState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void addCorsMappings(CorsRegistry registry) {
"Authorization",
SecurityProperties.ACCESS_TOKEN_HEADER,
SecurityProperties.REFRESH_TOKEN_HEADER)
.exposedHeaders("*")
.exposedHeaders("*") // CORS 응답에 대해 클라이언트가 접근할수있도록 허용
.allowCredentials(true)
.maxAge(3600);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import core.startup.mealtoktok.api.global.dto.Response;
import core.startup.mealtoktok.api.meal.dto.MealDishResponse;
import core.startup.mealtoktok.api.meal.dto.MealRequest;
import core.startup.mealtoktok.domain.meal.MealAndDishes;
import core.startup.mealtoktok.domain.meal.MealOwner;
import core.startup.mealtoktok.domain.meal.MealService;
import core.startup.mealtoktok.domain.meal.MealWithDishes;
import core.startup.mealtoktok.domain.meal.TargetMeal;
import core.startup.mealtoktok.domain.user.User;

Expand All @@ -39,16 +39,16 @@ public Response<Void> createMeal(

@GetMapping("/meals/{mealId}")
public Response<MealDishResponse> readMeal(@PathVariable("mealId") Long mealId) {
MealAndDishes mealAndDishes = mealService.readMealAndDishes(TargetMeal.from(mealId));
return Response.success(MealDishResponse.from(mealAndDishes));
MealWithDishes mealWithDishes = mealService.readMealAndDishes(TargetMeal.from(mealId));
return Response.success(MealDishResponse.from(mealWithDishes));
}

@GetMapping("/meals")
public Response<List<MealDishResponse>> readMeals(@AuthenticationPrincipal User currentUser) {
List<MealAndDishes> mealAndDishesList =
List<MealWithDishes> mealWithDishesList =
mealService.readMealAndDishes(MealOwner.from(currentUser));
List<MealDishResponse> mealDishResponses =
mealAndDishesList.stream().map(MealDishResponse::from).toList();
mealWithDishesList.stream().map(MealDishResponse::from).toList();
return Response.success(mealDishResponses);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import java.util.List;

import core.startup.mealtoktok.api.dishstore.response.DishResponse;
import core.startup.mealtoktok.domain.meal.MealAndDishes;
import core.startup.mealtoktok.domain.meal.MealWithDishes;

public record MealDishResponse(MealResponse meal, List<DishResponse> dishes) {

public static MealDishResponse from(MealAndDishes mealAndDishes) {
MealResponse mealResponse = MealResponse.from(mealAndDishes.meal());
public static MealDishResponse from(MealWithDishes mealWithDishes) {
MealResponse mealResponse = MealResponse.from(mealWithDishes.meal());
List<DishResponse> dishResponses =
mealAndDishes.dishes().stream().map(DishResponse::from).toList();
mealWithDishes.dishes().stream().map(DishResponse::from).toList();
return new MealDishResponse(mealResponse, dishResponses);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.common.dto.Image;
import core.startup.mealtoktok.domain.global.File;
import core.startup.mealtoktok.domain.global.FileUploader;
import core.startup.mealtoktok.domain.global.ImageAppender;

@Component
Expand All @@ -13,12 +15,17 @@ public class DishAppender {

private final DishValidator dishValidator;
private final DishRepository dishRepository;
private final FileUploader fileUploader;
private final ImageAppender imageAppender;

public void append(
DishStore dishStore, DishCategory dishCategory, Image image, DishInfo dishInfo) {
dishValidator.validateName(dishStore, dishInfo.dishName());
DishStore dishStore,
DishCategory dishCategory,
DishContent dishContent,
File uploadFile) {
dishValidator.validateName(dishStore, dishContent.dishName());
Image image = fileUploader.upload(uploadFile);
Image saveImage = imageAppender.append(image);
dishRepository.saveDish(dishStore, dishCategory, dishInfo, saveImage);
dishRepository.saveDish(dishStore, dishCategory, dishContent, saveImage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.startup.mealtoktok.domain.dishstore;

import java.math.BigDecimal;

import core.startup.mealtoktok.common.dto.Money;

public record DishContent(String dishName, Money dishPrice, int dishQuantity) {

public static DishContent of(String dishName, int dishPrice, int dishQuantity) {
return new DishContent(dishName, Money.from(BigDecimal.valueOf(dishPrice)), dishQuantity);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.common.dto.Image;
import core.startup.mealtoktok.domain.global.ImageReader;
import core.startup.mealtoktok.domain.global.ImageRemover;
import core.startup.mealtoktok.domain.global.TargetImage;

@Component
@RequiredArgsConstructor
public class DishRemover {

private final DishRepository dishRepository;
private final ImageReader imageReader;
private final ImageRemover imageRemover;

public void remove(Dish dish) {
dishRepository.deleteDish(dish);
Image image = imageReader.read(TargetImage.from(dish.getDishImage().imageId()));
imageRemover.remove(image);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

public interface DishRepository {

void saveDish(DishStore dishStore, DishCategory dishCategory, DishInfo dishInfo, Image image);
void saveDish(
DishStore dishStore, DishCategory dishCategory, DishContent dishContent, Image image);

Dish findDishById(TargetDish targetDish);

void deleteDish(Dish dish);

List<Dish> findAllByCategory(DishCategory dishCategory);

void updateDish(Dish dish, DishInfo dishInfo, Image image);
void updateDish(Dish dish, DishContent dishContent, Image image);

boolean existsByDishStoreIdAndDishName(DishStore dishStore, String dishName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@

import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.common.dto.Image;
import core.startup.mealtoktok.domain.global.File;
import core.startup.mealtoktok.domain.global.FileUploader;
import core.startup.mealtoktok.domain.global.ImageReader;
import core.startup.mealtoktok.domain.global.ImageRemover;
import core.startup.mealtoktok.domain.global.TargetImage;

@Service
@RequiredArgsConstructor
Expand All @@ -24,49 +19,37 @@ public class DishService {
private final DishUpdater dishUpdater;
private final DishStoreReader dishStoreReader;
private final DishCategoryReader dishCategoryReader;
private final FileUploader fileUploader;
private final ImageReader imageReader;
private final ImageRemover imageRemover;
private final DishAndImageWrapper dishAndImageWrapper;
private final DishWithImageFinder dishWithImageFinder;

@Transactional
public void createDish(
TargetDishStore targetDishStore,
TargetDishCategory targetDishCategory,
File uploadFile,
DishInfo dishInfo) {
DishContent dishContent,
File uploadImage) {
DishStore dishStore = dishStoreReader.read(targetDishStore);
DishCategory dishCategory = dishCategoryReader.read(targetDishCategory);
Image image = fileUploader.upload(uploadFile);
dishAppender.append(dishStore, dishCategory, image, dishInfo);
dishAppender.append(dishStore, dishCategory, dishContent, uploadImage);
}

@Transactional
public void deleteDish(TargetDish targetDish) {
Dish dish = dishReader.read(targetDish);
Image image = imageReader.read(TargetImage.from(dish.getDishImage().imageId()));
dishRemover.remove(dish);
imageRemover.remove(image);
}

@Transactional
public void updateDish(TargetDish targetDish, File uploadFile, DishInfo dishInfo) {
public void updateDish(TargetDish targetDish, File uploadImage, DishContent dishContent) {
Dish dish = dishReader.read(targetDish);
DishStore dishStore = dishStoreReader.read(TargetDishStore.from(dish.getDishStoreId()));
Image image = fileUploader.upload(uploadFile);
dishUpdater.update(dishStore, dish, image, dishInfo);
dishUpdater.update(dishStore, dish, uploadImage, dishContent);
}

public List<DishAndImage> readDishes(TargetDishCategory targetDishCategory) {
DishCategory dishCategory = dishCategoryReader.read(targetDishCategory);
List<Dish> dishes = dishReader.readAll(dishCategory);

return dishAndImageWrapper.wrapAll(dishes);
public List<DishWithImage> readDishes(TargetDishCategory targetDishCategory) {
return dishWithImageFinder.find(targetDishCategory);
}

public List<DishAndImage> searchDishes(String keyword) {
List<Dish> dishes = dishReader.search(keyword);

return dishAndImageWrapper.wrapAll(dishes);
public List<DishWithImage> searchDishes(String keyword) {
return dishWithImageFinder.find(keyword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.common.dto.Image;
import core.startup.mealtoktok.domain.global.File;
import core.startup.mealtoktok.domain.global.FileUploader;
import core.startup.mealtoktok.domain.global.ImageReader;
import core.startup.mealtoktok.domain.global.ImageUpdater;
import core.startup.mealtoktok.domain.global.TargetImage;
Expand All @@ -16,15 +18,16 @@
public class DishUpdater {

private final DishValidator dishValidator;
private final FileUploader fileUploader;
private final ImageReader imageReader;
private final ImageUpdater imageUpdater;
private final DishRepository dishRepository;

public void update(DishStore dishStore, Dish dish, Image image, DishInfo dishInfo) {
dishValidator.validateName(dishStore, dish, dishInfo.dishName());

public void update(DishStore dishStore, Dish dish, File uploadImage, DishContent dishContent) {
dishValidator.validateName(dishStore, dish, dishContent.dishName());
Image image = fileUploader.upload(uploadImage);
Image existingImage = imageReader.read(TargetImage.from(dish.getDishImage().imageId()));
Image updatedImage = imageUpdater.update(existingImage, image);
dishRepository.updateDish(dish, dishInfo, updatedImage);
dishRepository.updateDish(dish, dishContent, updatedImage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.startup.mealtoktok.domain.dishstore;

import core.startup.mealtoktok.common.dto.Image;

public record DishWithImage(Dish dish, Image image) {

public static DishWithImage of(Dish dish, Image images) {
return new DishWithImage(dish, images);
}
}
Loading

0 comments on commit ac76f29

Please sign in to comment.