Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add food option #36

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

}
Loading