Skip to content

Commit

Permalink
Merge pull request #326 from IceButler/refactor/#325-modify-cart-foodโ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆ-user

#325 food, cart, user exception ๊ตฌ์กฐ ๋ณ€๊ฒฝ ๋ฐ assembler ์‚ญ์ œ
  • Loading branch information
psyeon1120 authored Jul 8, 2024
2 parents cb33243 + 3940587 commit a346697
Show file tree
Hide file tree
Showing 43 changed files with 228 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.example.icebutler_server.admin.dto.request.ModifyFoodRequest;
import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.entity.FoodCategory;
import com.example.icebutler_server.food.exception.DuplicateFoodNameException;
import com.example.icebutler_server.global.exception.BaseException;
import org.springframework.stereotype.Component;

import static com.example.icebutler_server.global.exception.ReturnCode.ALREADY_EXIST_FOOD_NAME;

@Component
public class AdminAssembler {
public Food toUpdateFoodInfo(Food food, ModifyFoodRequest request) {
Expand All @@ -16,6 +18,6 @@ public Food toUpdateFoodInfo(Food food, ModifyFoodRequest request) {
}

public void validateFoodName(Food checkFood, String foodName) {
if(checkFood.getFoodName().equals(foodName)) throw new DuplicateFoodNameException();
if (checkFood.getFoodName().equals(foodName)) throw new BaseException(ALREADY_EXIST_FOOD_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@


import com.example.icebutler_server.admin.dto.assembler.AdminAssembler;
import com.example.icebutler_server.admin.dto.request.*;
import com.example.icebutler_server.admin.dto.request.JoinRequest;
import com.example.icebutler_server.admin.dto.request.LoginRequest;
import com.example.icebutler_server.admin.dto.request.ModifyFoodRequest;
import com.example.icebutler_server.admin.dto.response.AdminResponse;
import com.example.icebutler_server.admin.dto.response.SearchFoodsResponse;
import com.example.icebutler_server.admin.exception.AlreadyExistEmailException;
import com.example.icebutler_server.admin.exception.FoodNotFoundException;
import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.repository.FoodRepository;
import com.example.icebutler_server.admin.dto.response.PostAdminRes;
import com.example.icebutler_server.admin.dto.response.SearchFoodsResponse;
import com.example.icebutler_server.admin.dto.response.UserResponse;
import com.example.icebutler_server.admin.entity.Admin;
import com.example.icebutler_server.admin.exception.AdminNotFoundException;
import com.example.icebutler_server.admin.exception.AlreadyExistEmailException;
import com.example.icebutler_server.admin.exception.FoodNotFoundException;
import com.example.icebutler_server.admin.exception.PasswordNotMatchException;
import com.example.icebutler_server.admin.repository.AdminRepository;
import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.repository.FoodRepository;
import com.example.icebutler_server.global.exception.BaseException;
import com.example.icebutler_server.global.feign.dto.AdminReq;
import com.example.icebutler_server.global.feign.feignClient.RecipeServerClient;
import com.example.icebutler_server.global.feign.publisher.RecipeServerEventPublisherImpl;
import com.example.icebutler_server.global.util.redis.RedisTemplateService;
import com.example.icebutler_server.global.util.TokenUtils;
import com.example.icebutler_server.global.util.redis.RedisTemplateService;
import com.example.icebutler_server.user.entity.User;
import com.example.icebutler_server.user.exception.UserNotFoundException;
import com.example.icebutler_server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import static com.example.icebutler_server.global.exception.ReturnCode.NOT_FOUND_USER;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -73,7 +76,7 @@ public PostAdminRes login(LoginRequest request)
@Override
public void logout(Long adminIdx)
{
Admin admin = adminRepository.findByIdAndIsEnable(adminIdx, true).orElseThrow(UserNotFoundException::new);
Admin admin = adminRepository.findByIdAndIsEnable(adminIdx, true).orElseThrow(() -> new BaseException(NOT_FOUND_USER));
admin.logout();
redisTemplateService.deleteUserRefreshToken(admin.getId().toString());
}
Expand All @@ -92,7 +95,7 @@ public Page<UserResponse> search(
public void withdraw(Long userIdx, Long adminIdx, String authorization)
{
adminRepository.findByIdAndIsEnable(adminIdx,true).orElseThrow(AdminNotFoundException::new);
User user = userRepository.findById(userIdx).orElseThrow(UserNotFoundException::new);
User user = userRepository.findById(userIdx).orElseThrow(() -> new BaseException(NOT_FOUND_USER));
HashMap<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("Authorization", authorization);
recipeServerClient.withdrawUser(userIdx, requestHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class CartController {
@ApiResponses(value = {
@ApiResponse(responseCode = "403", description = "๋ƒ‰์žฅ๊ณ ์˜ ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "404", description = "์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
@ApiResponse(responseCode = "404", description = "(U0005)ํ•ด๋‹น ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ๋ƒ‰์žฅ๊ณ ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
"(C0000)์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
})
@Auth
Expand All @@ -53,10 +53,10 @@ public ResponseCustom<List<CartResponse>> getCartFoods(@Parameter(name = "๋ƒ‰์žฅ
@ApiResponses(value = {
@ApiResponse(responseCode = "403", description = "๋ƒ‰์žฅ๊ณ ์˜ ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "404", description = "์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
@ApiResponse(responseCode = "404", description = "(U0005)ํ•ด๋‹น ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ๋ƒ‰์žฅ๊ณ ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.",
"(C0000)์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"(F0000)์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
})
@Auth
Expand All @@ -73,10 +73,10 @@ public ResponseCustom<?> addCartFoods(@Parameter(name = "๋ƒ‰์žฅ๊ณ  ID") @PathVar
@ApiResponses(value = {
@ApiResponse(responseCode = "403", description = "๋ƒ‰์žฅ๊ณ ์˜ ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "404", description = "์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
@ApiResponse(responseCode = "404", description = "(U0005)ํ•ด๋‹น ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ๋ƒ‰์žฅ๊ณ ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์š”์ฒญํ•œ id๋ฅผ ๊ฐ€์ง„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.",
"(C0000)์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\t\n" +
"(F0000)์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
})
@Auth
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public Cart(Fridge fridge) {
this.fridge = fridge;
}

public static Cart toEntity(Fridge fridge) {
return Cart.builder()
.fridge(fridge)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public CartFood(Food food, Cart cart) {
this.cart = cart;
}

public static CartFood toEntity(Cart cart, Food food) {
return CartFood.builder()
.cart(cart)
.food(food)
.build();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.example.icebutler_server.cart.service;

import com.example.icebutler_server.cart.dto.assembler.CartFoodAssembler;
import com.example.icebutler_server.cart.dto.request.AddFoodRequest;
import com.example.icebutler_server.cart.dto.request.AddFoodToCartRequest;
import com.example.icebutler_server.cart.dto.request.RemoveFoodFromCartRequest;
import com.example.icebutler_server.cart.dto.response.CartResponse;
import com.example.icebutler_server.cart.entity.Cart;
import com.example.icebutler_server.cart.entity.CartFood;
import com.example.icebutler_server.cart.exception.CartNotFoundException;
import com.example.icebutler_server.cart.repository.CartFoodRepository;
import com.example.icebutler_server.cart.repository.CartRepository;
import com.example.icebutler_server.food.dto.assembler.FoodAssembler;
import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.entity.FoodCategory;
import com.example.icebutler_server.food.repository.FoodRepository;
Expand All @@ -19,18 +16,22 @@
import com.example.icebutler_server.fridge.exception.FridgeUserNotFoundException;
import com.example.icebutler_server.fridge.repository.FridgeRepository;
import com.example.icebutler_server.fridge.repository.FridgeUserRepository;
import com.example.icebutler_server.global.exception.BaseException;
import com.example.icebutler_server.global.sqs.AmazonSQSSender;
import com.example.icebutler_server.global.sqs.FoodData;
import com.example.icebutler_server.user.entity.User;
import com.example.icebutler_server.user.exception.UserNotFoundException;
import com.example.icebutler_server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.icebutler_server.global.exception.ReturnCode.NOT_FOUND_CART;
import static com.example.icebutler_server.global.exception.ReturnCode.NOT_FOUND_USER;


@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -43,8 +44,6 @@ public class CartServiceImpl implements CartService {
private final FridgeRepository fridgeRepository;
private final FridgeUserRepository fridgeUserRepository;
private final CartRepository cartRepository;
private final FoodAssembler foodAssembler;
private final CartFoodAssembler cartFoodAssembler;
private final AmazonSQSSender amazonSQSSender;

// ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์‹ํ’ˆ ์กฐํšŒ
Expand Down Expand Up @@ -74,7 +73,7 @@ public void addCartFoods(Long fridgeIdx, AddFoodToCartRequest request, Long user
for(AddFoodRequest foodRequest : request.getFoodRequests()) {
Food food = this.foodRepository.findByFoodNameAndFoodCategory(foodRequest.getFoodName(), FoodCategory.getFoodCategoryByName(foodRequest.getFoodCategory()));
if(food == null) {
food = this.foodRepository.save(foodAssembler.toEntity(foodRequest));
food = this.foodRepository.save(Food.toEntity(foodRequest));
amazonSQSSender.sendMessage(FoodData.toDto(food));
}
foodRequests.add(food);
Expand All @@ -89,7 +88,7 @@ public void addCartFoods(Long fridgeIdx, AddFoodToCartRequest request, Long user
for (Long foodInIdx : foodsInNowCart) if(foodInIdx.equals(f.getId())) return false;
return true;
})
.map((food) -> cartFoodAssembler.toEntity(cart, food))
.map((food) -> CartFood.toEntity(cart, food))
.collect(Collectors.toList());

cartFoodRepository.saveAll(cartFoods);
Expand All @@ -105,9 +104,9 @@ public void deleteCartFoods(Long fridgeIdx, RemoveFoodFromCartRequest request, L
}

private Cart getCart(Long userIdx, Long fridgeIdx) {
User user = userRepository.findByIdAndIsEnable(userIdx, true).orElseThrow(UserNotFoundException::new);
User user = userRepository.findByIdAndIsEnable(userIdx, true).orElseThrow(() -> new BaseException(NOT_FOUND_USER));
Fridge fridge = fridgeRepository.findByIdAndIsEnable(fridgeIdx, true).orElseThrow(FridgeNotFoundException::new);
fridgeUserRepository.findByUserAndFridgeAndIsEnable(user, fridge, true).orElseThrow(FridgeUserNotFoundException::new);
return cartRepository.findByFridge_IdAndIsEnable(fridgeIdx, true).orElseThrow(CartNotFoundException::new);
return cartRepository.findByFridge_IdAndIsEnable(fridgeIdx, true).orElseThrow(() -> new BaseException(NOT_FOUND_CART));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.icebutler_server.food.controller;

import com.example.icebutler_server.cart.dto.request.AddFoodRequest;
import com.example.icebutler_server.food.dto.assembler.FoodAssembler;
import com.example.icebutler_server.food.dto.response.BarcodeFoodRes;
import com.example.icebutler_server.food.dto.response.FoodRes;
import com.example.icebutler_server.food.entity.Food;
Expand Down Expand Up @@ -38,11 +37,10 @@ public class FoodController {
private final AmazonSQSSender amazonSQSSender;

private final FoodRepository foodRepository;
private final FoodAssembler foodAssembler;

@Operation(summary = "์‹ํ’ˆ ๊ฒ€์ƒ‰", description = "์‹ํ’ˆ์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.")
@SwaggerApiSuccess(implementation = FoodRes.class)
@ApiResponse(responseCode = "400", description = "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.",
@ApiResponse(responseCode = "404", description = "(F0000)์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class)))
@GetMapping("")
public ResponseCustom<List<FoodRes>> searchFood(@Parameter(name = "category", description = "์‹ํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ") @RequestParam(required = false) String category,
Expand All @@ -55,7 +53,7 @@ public ResponseCustom<List<FoodRes>> searchFood(@Parameter(name = "category", de

@Operation(summary = "์‹ํ’ˆ ๋ฐ”์ฝ”๋“œ ์กฐํšŒ", description = "๋ฐ”์ฝ”๋“œ ๋ฒˆํ˜ธ๋กœ ์‹ํ’ˆ์„ ์กฐํšŒํ•œ๋‹ค.")
@SwaggerApiSuccess(implementation = BarcodeFoodRes.class)
@ApiResponse(responseCode = "400", description = "ํ•ด๋‹น ๋ฐ”์ฝ”๋“œ์˜ ์ƒํ’ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
@ApiResponse(responseCode = "404", description = "(F00001)ํ•ด๋‹น ๋ฐ”์ฝ”๋“œ์˜ ์ƒํ’ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class)))
@GetMapping("/barcode")
public ResponseCustom<BarcodeFoodRes> searchByBarcode(@RequestParam String code_num) throws IOException, org.json.simple.parser.ParseException {
Expand All @@ -69,7 +67,7 @@ public void hihiTest() {
addFoodRequest.setFoodName("๋ง›์—†๋Š” ๊ณ ๊ธฐ");
addFoodRequest.setFoodCategory("์œก๋ฅ˜");

Food food = this.foodRepository.save(foodAssembler.toEntity(addFoodRequest));
Food food = this.foodRepository.save(Food.toEntity(addFoodRequest));
FoodData foodData = FoodData.toDto(food);
amazonSQSSender.sendMessage(FoodData.toDto(food));
}
Expand Down

This file was deleted.

Loading

0 comments on commit a346697

Please sign in to comment.