Skip to content

Commit

Permalink
Merge pull request #344 from IceButler/refactor/#330-food-searchFood
Browse files Browse the repository at this point in the history
#330 ์‹ํ’ˆ ๊ฒ€์ƒ‰ ๋™์  ์ฟผ๋ฆฌ๋กœ ๋ณ€๊ฒฝ
  • Loading branch information
psyeon1120 authored Aug 1, 2024
2 parents d5f666b + 56a11e2 commit a5a9801
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.example.icebutler_server.food.controller;

import com.example.icebutler_server.cart.dto.request.AddFoodRequest;
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;
import com.example.icebutler_server.food.repository.FoodRepository;
import com.example.icebutler_server.food.service.FoodServiceImpl;
import com.example.icebutler_server.global.dto.response.ResponseCustom;
import com.example.icebutler_server.global.dto.response.SwaggerApiSuccess;
import com.example.icebutler_server.global.sqs.AmazonSQSSender;
import com.example.icebutler_server.global.sqs.FoodData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -34,9 +29,6 @@
public class FoodController {

private final FoodServiceImpl foodService;
private final AmazonSQSSender amazonSQSSender;

private final FoodRepository foodRepository;

@Operation(summary = "์‹ํ’ˆ ๊ฒ€์ƒ‰", description = "์‹ํ’ˆ์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.")
@SwaggerApiSuccess(implementation = FoodRes.class)
Expand All @@ -45,11 +37,7 @@ public class FoodController {
@GetMapping("")
public ResponseCustom<List<FoodRes>> searchFood(@Parameter(name = "category", description = "์‹ํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ") @RequestParam(required = false) String category,
@Parameter(name = "word", description = "๊ฒ€์ƒ‰์–ด") @RequestParam(required = false) String word) {
if (category != null && word != null)
return ResponseCustom.success(foodService.getAllFoodByCategoryAndWord(category, word));
else if (category != null) return ResponseCustom.success(foodService.getAllFoodByCategory(category));
else if (word != null) return ResponseCustom.success(foodService.getAllFoodByWord(word));
else return ResponseCustom.success(foodService.getAllFood());
return ResponseCustom.success(foodService.searchFood(category, word));
}

@Operation(summary = "์‹ํ’ˆ ๋ฐ”์ฝ”๋“œ ์กฐํšŒ", description = "๋ฐ”์ฝ”๋“œ ๋ฒˆํ˜ธ๋กœ ์‹ํ’ˆ์„ ์กฐํšŒํ•œ๋‹ค.")
Expand All @@ -61,15 +49,4 @@ public ResponseCustom<BarcodeFoodRes> searchByBarcode(@RequestParam String code_
return ResponseCustom.success(foodService.searchByBarcode(code_num));
}

@GetMapping("/hihitest")
public void hihiTest() {

AddFoodRequest addFoodRequest = new AddFoodRequest();
addFoodRequest.setFoodName("๋ง›์—†๋Š” ๊ณ ๊ธฐ");
addFoodRequest.setFoodCategory("์œก๋ฅ˜");

Food food = this.foodRepository.save(Food.toEntity(addFoodRequest));
FoodData foodData = FoodData.toDto(food);
amazonSQSSender.sendMessage(FoodData.toDto(food));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.icebutler_server.food.repository;

import com.example.icebutler_server.food.entity.Food;

import java.util.List;

public interface FoodCustom {
List<Food> searchFood(String category, String word);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface FoodRepository extends JpaRepository<Food, Long> {
public interface FoodRepository extends JpaRepository<Food, Long>, FoodCustom {

List<Food> findAllByFoodCategory(FoodCategory foodCategory);
Optional<Food> findByFoodName(String foodName);

Optional<Food> findByFoodName(String foodName);
Food findByFoodNameAndFoodCategory(String foodName, FoodCategory foodCategory);

Food findByFoodNameAndFoodCategory(String foodName, FoodCategory foodCategory);
Optional<Food> findByIdAndIsEnable(Long foodId, boolean status);

List<Food> findByFoodNameContainsAndFoodCategory(String foodName, FoodCategory foodCategory);
Page<Food> findByFoodNameContainsAndIsEnable(String cond, boolean status, Pageable pageable);

List<Food> findByFoodNameContains(String foodName);
Food findByFoodNameAndIsEnable(String foodName, boolean status);

Optional<Food> findByIdAndIsEnable(Long foodId, boolean status);

Page<Food> findByFoodNameContainsAndIsEnable(String cond, boolean status, Pageable pageable);

Food findByFoodNameAndIsEnable(String foodName, boolean status);

Page<Food> findByIsEnableOrderByUpdatedAtDesc(boolean status, Pageable pageable);
Page<Food> findByIsEnableOrderByUpdatedAtDesc(boolean status, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.icebutler_server.food.repository;

import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.entity.FoodCategory;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import static com.example.icebutler_server.food.entity.QFood.food;

@RequiredArgsConstructor
public class FoodRepositoryImpl implements FoodCustom {
private final JPAQueryFactory jpaQueryFactory;


@Override
public List<Food> searchFood(String category, String word) {
return jpaQueryFactory
.selectFrom(food)
.where(categoryEq(category), nameContains(word))
.fetch();
}

private BooleanExpression categoryEq(String category) {
return StringUtils.isEmpty(category) ? null : food.foodCategory.eq(FoodCategory.getFoodCategoryByName(category));
}

private BooleanExpression nameContains(String word) {
return StringUtils.isEmpty(word) ? null : food.foodName.contains(word);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
import com.example.icebutler_server.food.dto.response.BarcodeFoodRes;
import com.example.icebutler_server.food.dto.response.FoodRes;
import org.apache.tomcat.util.json.ParseException;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

public interface FoodService {
void addFood(FoodReq foodReq);
List<FoodRes> getAllFood();
List<FoodRes> getAllFoodByCategory(String categoryName);

BarcodeFoodRes searchByBarcode(String barcodeNum) throws IOException, ParseException, org.json.simple.parser.ParseException;

List<FoodRes> getAllFoodByCategoryAndWord(String categoryName, String word);
List<FoodRes> getAllFoodByWord(String word);
List<FoodRes> searchFood(String category, String word);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
import com.example.icebutler_server.food.entity.FoodCategory;
import com.example.icebutler_server.food.repository.FoodRepository;
import com.example.icebutler_server.global.exception.BaseException;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,29 +28,23 @@
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class FoodServiceImpl implements FoodService{
public class FoodServiceImpl implements FoodService {
//TODO: ๋ฐฐํฌ ์„ค์ • ํ›„ ์ˆ˜์ •์˜ˆ์ •
@Value("${barcode-service-key}")
String serviceKey;

private final FoodRepository foodRepository;

@Transactional
@Override
public void addFood(FoodReq foodReq) {
this.foodRepository.save(Food.toEntity(foodReq));
}

@Override
public List<FoodRes> getAllFood() {
return foodRepository.findAll().stream().map(FoodRes::toDto).collect(Collectors.toList());
public List<FoodRes> searchFood(String category, String word) {
List<Food> searchFoods = foodRepository.searchFood(category, word);
return searchFoods.stream().map(FoodRes::toDto).collect(Collectors.toList());
}

@Transactional
@Override
public List<FoodRes> getAllFoodByCategory(String foodCategoryName) {
FoodCategory foodCategory = FoodCategory.getFoodCategoryByName(foodCategoryName);
return foodRepository.findAllByFoodCategory(foodCategory)
.stream().map(FoodRes::toDto).collect(Collectors.toList());
public void addFood(FoodReq foodReq) {
this.foodRepository.save(Food.toEntity(foodReq));
}

@Override
Expand All @@ -60,19 +53,6 @@ public BarcodeFoodRes searchByBarcode(String barcodeNum) throws IOException, org
return BarcodeFoodRes.toDto(foodDetailName);
}

@Override
public List<FoodRes> getAllFoodByCategoryAndWord(String categoryName, String word) {
FoodCategory foodCategory = FoodCategory.getFoodCategoryByName(categoryName);
return foodRepository.findByFoodNameContainsAndFoodCategory(word, foodCategory)
.stream().map(FoodRes::toDto).collect(Collectors.toList());
}

@Override
public List<FoodRes> getAllFoodByWord(String word) {
return foodRepository.findByFoodNameContains(word)
.stream().map(FoodRes::toDto).collect(Collectors.toList());
}

private String callBarcodeApi(String barcodeNum) throws IOException, ParseException {
URL url = new URL("https://openapi.foodsafetykorea.go.kr/api/" + serviceKey +
"/I2570/json/1/5/BRCD_NO=" + barcodeNum);
Expand All @@ -82,13 +62,13 @@ private String callBarcodeApi(String barcodeNum) throws IOException, ParseExcept
JSONObject result = (JSONObject) obj.get("I2570");
JSONArray row = (JSONArray) result.get("row");
if (row == null) throw new BaseException(NOT_FOUND_BARCODE_FOOD);
JSONObject data = (JSONObject) row.get(0);
JSONObject data = (JSONObject) row.get(0);
return (String) data.get("PRDT_NM");
}

private JSONObject getJsonObjectByParser(StringBuilder sb) throws ParseException {
JSONParser parser = new JSONParser();
return (JSONObject)parser.parse(sb.toString());
return (JSONObject) parser.parse(sb.toString());
}

private StringBuilder callAPI(URL url) throws IOException {
Expand All @@ -97,7 +77,7 @@ private StringBuilder callAPI(URL url) throws IOException {

BufferedReader rd;
// ์„œ๋น„์Šค์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ด๋ฉด 200~300
if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300)
if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300)
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
else
rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
Expand Down

0 comments on commit a5a9801

Please sign in to comment.