Skip to content

Commit

Permalink
Merge pull request #36 from 28th-meetup/feat/addFoodOption
Browse files Browse the repository at this point in the history
Feat/add food option
  • Loading branch information
summit45 committed Nov 16, 2023
2 parents f851d61 + 6c8da41 commit a6600a4
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/kusitms/jipbap/food/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ public class Food extends DateEntity {

@Column(name = "food_name")
private String name;
private Long price;

private Long dollarPrice;
private Long canadaPrice;
private String image;
private String description;
private Long recommendCount;
private String image;
private String foodPackage; // 배달포장 모두 가능, 배달 모두 가능, 포장 모두 가능


}
9 changes: 8 additions & 1 deletion src/main/java/com/kusitms/jipbap/food/FoodController.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ public CommonResponse<FoodDto> registerFood(
@Operation(summary = "메뉴 하나 상세조회")
@GetMapping("/{foodId}")
@ResponseStatus(HttpStatus.OK)
public CommonResponse<FoodDto> getFoodDetail(@PathVariable Long foodId) {
public CommonResponse<FoodDetailResponse> getFoodDetail(@PathVariable Long foodId) {
return new CommonResponse<>(foodService.getFoodDetail(foodId));
}

@Operation(summary = "메뉴 당 옵션 상세조회")
@GetMapping("/{foodId}/option")
@ResponseStatus(HttpStatus.OK)
public CommonResponse<List<FoodOptionResponse>> getFoodDetailByOption(@PathVariable Long foodId ) {
return new CommonResponse<>(foodService.getFoodDetailByOption(foodId));
}

@Operation(summary = "홈에서 현재 지역 내에서 인기메뉴 조회하기")
@GetMapping("/home")
@ResponseStatus(HttpStatus.OK)
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/FoodOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kusitms.jipbap.food;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "tb_food_option")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class FoodOption {

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "food_id")
private Food food;

private String name;

private Long dollarPrice;

private Long canadaPrice;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.kusitms.jipbap.food;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface FoodOptionRepository extends JpaRepository<FoodOption, Long> {
List<FoodOption> findAllByFood(Food food);
}
51 changes: 45 additions & 6 deletions src/main/java/com/kusitms/jipbap/food/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class FoodService {
private final FoodRepository foodRepository;
private final CategoryRepository categoryRepository;
private final OrderRepository orderRepository;
private final FoodOptionRepository foodOptionRepository;

private final AmazonS3 amazonS3;

Expand Down Expand Up @@ -72,14 +73,50 @@ public FoodDto registerFood(String email, RegisterFoodRequestDto dto, MultipartF
}

Food food = foodRepository.save(
new Food(null, store, category, dto.getName(), dto.getPrice(), dto.getDescription(), 0L, imageUri)
Food.builder()
.store(store)
.category(category)
.name(dto.getName())
.dollarPrice(dto.getDollarPrice())
.canadaPrice(dto.getCanadaPrice())
.description(dto.getDescription())
.recommendCount(0L)
.image(imageUri)
.foodPackage(dto.getFoodPackage())
.build()
);
return new FoodDto(food.getId(), store.getId(), category.getId(), food.getName(), food.getPrice(), food.getDescription(), food.getImage());

// FoodOption 저장
if (dto.getFoodOptionRequestList() != null && !dto.getFoodOptionRequestList().isEmpty()) {
for (FoodOptionRequest foodOptionRequest : dto.getFoodOptionRequestList()) {
FoodOption foodOption = FoodOption.builder()
.food(food)
.name(foodOptionRequest.getName())
.dollarPrice(foodOptionRequest.getDollarPrice())
.canadaPrice(foodOptionRequest.getCanadaPrice())
.build();

foodOptionRepository.save(foodOption);
}
}

return new FoodDto(food.getId(), store.getId(), category.getId(), food.getName(), food.getDollarPrice(), food.getCanadaPrice(), food.getDescription(), food.getImage());
}

public FoodDto getFoodDetail(Long foodId) {
public FoodDetailResponse getFoodDetail(Long foodId) {
Food food = foodRepository.findById(foodId).orElseThrow(()-> new FoodNotExistsException("해당 음식 Id는 유효하지 않습니다."));
return new FoodDto(food.getId(), food.getStore().getId(), food.getCategory().getId(), food.getName(), food.getPrice(), food.getDescription(), food.getImage());
List<FoodOptionResponse> foodOptionResponseList = foodOptionRepository.findAllByFood(food).stream()
.map(foodOption -> new FoodOptionResponse(foodOption.getId(), foodOption.getName(), foodOption.getDollarPrice(), foodOption.getCanadaPrice()))
.collect(Collectors.toList());
return new FoodDetailResponse(food.getId(), food.getStore().getId(), food.getCategory().getId(), food.getName(), food.getDollarPrice(), food.getCanadaPrice(), food.getDescription(), food.getImage(), foodOptionResponseList);
}

public List<FoodOptionResponse> getFoodDetailByOption(Long foodId) {
Food food = foodRepository.findById(foodId).orElseThrow(()-> new FoodNotExistsException("해당 음식 Id는 유효하지 않습니다."));
List<FoodOptionResponse> foodOptionResponseList = foodOptionRepository.findAllByFood(food).stream()
.map(foodOption -> new FoodOptionResponse(foodOption.getId(), foodOption.getName(), foodOption.getDollarPrice(), foodOption.getCanadaPrice()))
.collect(Collectors.toList());
return foodOptionResponseList;
}

public List<BestSellingFoodResponse> getBestSellingFoodByRegion(String email) {
Expand All @@ -91,7 +128,8 @@ public List<BestSellingFoodResponse> getBestSellingFoodByRegion(String email) {
.map(food -> new BestSellingFoodResponse(
food.getName(),
food.getStore().getName(),
food.getPrice()
food.getDollarPrice(),
food.getCanadaPrice()
))
.collect(Collectors.toList());

Expand All @@ -109,7 +147,8 @@ public List<FoodDto> getFoodByCategory(Long categoryId){
food.getStore().getId(),
food.getCategory().getId(),
food.getName(),
food.getPrice(),
food.getDollarPrice(),
food.getCanadaPrice(),
food.getDescription(),
food.getImage()
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
public class BestSellingFoodResponse {
private String name;
private String storeName;
private Long price;
private Long dollarPrice;
private Long canadaPrice;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ public class FoodDetailByStoreResponse {
private Long id;
private Long categoryId;
private String name;
private Long price;
private Long dollarPrice;
private Long canadaPrice;
private String description;
private Long recommendCount;

public FoodDetailByStoreResponse(Food food){
this.id = food.getId();
this.categoryId = food.getCategory().getId();
this.name = food.getName();
this.price = food.getPrice();
this.dollarPrice = food.getDollarPrice();
this.canadaPrice = food.getCanadaPrice();
this.description = food.getDescription();
this.recommendCount = food.getRecommendCount();
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/FoodDetailResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.kusitms.jipbap.food.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FoodDetailResponse {
private Long id;
private Long storeId;
private Long categoryId;
private String name;
private Long dollarPrice;
private Long canadaPrice;
private String description;
private String image;
private List<FoodOptionResponse> foodOptionResponseList;
}
3 changes: 2 additions & 1 deletion src/main/java/com/kusitms/jipbap/food/dto/FoodDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class FoodDto {
private Long storeId;
private Long categoryId;
private String name;
private Long price;
private Long dollarPrice;
private Long canadaPrice;
private String description;
private String image;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/FoodOptionRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.kusitms.jipbap.food.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FoodOptionRequest {
private String name;

private Long dollarPrice;

private Long canadaPrice;
}
17 changes: 17 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/FoodOptionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kusitms.jipbap.food.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FoodOptionResponse {
private Long id;
private String name;
private Long dollarPrice;
private Long canadaPrice;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.kusitms.jipbap.food.dto;

import com.kusitms.jipbap.food.Category;
import com.kusitms.jipbap.food.FoodOption;
import com.kusitms.jipbap.food.FoodRepository;
import com.kusitms.jipbap.store.Store;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
Expand All @@ -16,6 +20,10 @@ public class RegisterFoodRequestDto {
private Long storeId;
private Long categoryId;
private String name;
private Long price;
private Long dollarPrice;
private Long canadaPrice;
private String description;
private List<FoodOptionRequest> foodOptionRequestList;
private String foodPackage;

}

0 comments on commit a6600a4

Please sign in to comment.