forked from codesquad-members-2022/sidedish
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from Louie-03/dev-BE
[Team-03][BE][루이&쿠킴] - 특정 음식 타입 조회 기능
- Loading branch information
Showing
26 changed files
with
848 additions
and
14 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
BE/src/main/java/sidedish/com/controller/ProductsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sidedish.com.controller; | ||
|
||
import java.util.List; | ||
import javax.validation.constraints.NotEmpty; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import sidedish.com.controller.model.ProductMealTypeResponse; | ||
import sidedish.com.service.ProductsService; | ||
|
||
@RestController | ||
@RequestMapping("/api/products") | ||
public class ProductsController { | ||
|
||
private final ProductsService productsService; | ||
|
||
public ProductsController(ProductsService productsService) { | ||
this.productsService = productsService; | ||
} | ||
|
||
@GetMapping | ||
public List<ProductMealTypeResponse> findProductsMealType( | ||
@RequestParam @NotEmpty String meal) { | ||
return productsService.findByMealType(meal); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
BE/src/main/java/sidedish/com/controller/ProductsDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sidedish.com.controller; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Component; | ||
import sidedish.com.controller.model.ProductMealTypeResponse; | ||
import sidedish.com.domain.Product; | ||
|
||
@Component | ||
public class ProductsDtoMapper { | ||
|
||
public List<ProductMealTypeResponse> toProductsMealTypeResponseFromDomain( | ||
List<Product> products) { | ||
return products.stream() | ||
.map(this::toProductMealTypeResponseFromDomain) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private ProductMealTypeResponse toProductMealTypeResponseFromDomain( | ||
Product product) { | ||
return new ProductMealTypeResponse( | ||
product.getId(), | ||
product.getImages().get(0).getImageUrl(), | ||
product.getProductName(), | ||
product.getDescription(), | ||
product.getFixedPrice(), | ||
product.getOriginalPrice(), | ||
product.getDiscountPolicy().getPolicyName()); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
BE/src/main/java/sidedish/com/controller/model/ProductMealTypeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package sidedish.com.controller.model; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ProductMealTypeResponse { | ||
|
||
private Long id; | ||
private String image; | ||
private String productName; | ||
private String description; | ||
private long fixedPrice; | ||
private long originalPrice; | ||
private String event; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sidedish.com.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class DiscountPolicy { | ||
|
||
private String policyName; | ||
private float discountRate; | ||
|
||
public long calculateFixedPrice(long originalPrice) { | ||
return (long) (originalPrice * ((100 - discountRate) / 100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package sidedish.com.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class Image { | ||
|
||
private String imageUrl; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sidedish.com.domain; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Product { | ||
private Long id; | ||
private DiscountPolicy discountPolicy; | ||
private List<Image> images; | ||
private String productName; | ||
private String description; | ||
private long fixedPrice; | ||
private long originalPrice; | ||
private String mealCategory; | ||
private String bestCategory; | ||
|
||
public Product(Long id, DiscountPolicy discountPolicy, | ||
List<Image> images, String productName, String description, long originalPrice, | ||
String mealCategory, String bestCategory) { | ||
this.id = id; | ||
this.discountPolicy = discountPolicy; | ||
this.images = images; | ||
this.productName = productName; | ||
this.description = description; | ||
this.originalPrice = originalPrice; | ||
this.mealCategory = mealCategory; | ||
this.bestCategory = bestCategory; | ||
this.fixedPrice = calculateFixedPrice(); | ||
} | ||
|
||
private long calculateFixedPrice() { | ||
return discountPolicy.calculateFixedPrice(originalPrice); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
BE/src/main/java/sidedish/com/exception/NoSuchProductsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package sidedish.com.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(value = HttpStatus.NOT_FOUND) | ||
public class NoSuchProductsException extends RuntimeException { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
BE/src/main/java/sidedish/com/repository/DiscountPolicyRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package sidedish.com.repository; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import org.springframework.data.repository.CrudRepository; | ||
import sidedish.com.repository.entity.DiscountPolicyEntity; | ||
|
||
public interface DiscountPolicyRepository extends CrudRepository<DiscountPolicyEntity, Long> { | ||
|
||
@Override | ||
List<DiscountPolicyEntity> findAll(); | ||
} |
69 changes: 69 additions & 0 deletions
69
BE/src/main/java/sidedish/com/repository/DomainEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package sidedish.com.repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Component; | ||
import sidedish.com.domain.DiscountPolicy; | ||
import sidedish.com.domain.Image; | ||
import sidedish.com.domain.Product; | ||
import sidedish.com.repository.entity.DiscountPolicyEntity; | ||
import sidedish.com.repository.entity.ProductEntity; | ||
|
||
@Component | ||
public class DomainEntityMapper { | ||
|
||
public List<Product> toDomainFromProductsEntity(List<ProductEntity> productEntities, | ||
List<DiscountPolicyEntity> discountPolicies) { | ||
List<Product> products = new ArrayList<>(); | ||
for (ProductEntity productEntity : productEntities) { | ||
Long discountPolicyId = productEntity.getDiscountPolicyId(); | ||
DiscountPolicyEntity discountPolicyEntity = searchDiscountPolicyById( | ||
discountPolicies, discountPolicyId); | ||
|
||
Product product = toDomainFromProductEntity(productEntity, discountPolicyEntity); | ||
products.add(product); | ||
} | ||
return products; | ||
} | ||
|
||
private DiscountPolicyEntity searchDiscountPolicyById( | ||
List<DiscountPolicyEntity> discountPolicies, Long discountPolicyId) { | ||
for (DiscountPolicyEntity discountPolicyEntity : discountPolicies) { | ||
if (discountPolicyEntity.isEqualsId(discountPolicyId)) { | ||
return discountPolicyEntity; | ||
} | ||
} | ||
return new DiscountPolicyEntity(null, null, 0); | ||
} | ||
|
||
private Product toDomainFromProductEntity(ProductEntity productEntity, | ||
DiscountPolicyEntity discountPolicyEntity) { | ||
DiscountPolicy discountPolicy = toDomainFromDiscountPolicyEntity(discountPolicyEntity); | ||
|
||
List<Image> images = toDomainFromImageEntity(productEntity); | ||
|
||
return new Product( | ||
productEntity.getId(), | ||
discountPolicy, | ||
images, | ||
productEntity.getProductName(), | ||
productEntity.getDescription(), | ||
productEntity.getOriginalPrice(), | ||
productEntity.getMealCategory(), | ||
productEntity.getBestCategory()); | ||
} | ||
|
||
private List<Image> toDomainFromImageEntity(ProductEntity productEntity) { | ||
return productEntity.getImageEntities().stream() | ||
.map(imageEntity -> new Image(imageEntity.getImageUrl())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private DiscountPolicy toDomainFromDiscountPolicyEntity( | ||
DiscountPolicyEntity discountPolicyEntity) { | ||
return new DiscountPolicy( | ||
discountPolicyEntity.getPolicyName(), | ||
discountPolicyEntity.getDiscountRate()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
BE/src/main/java/sidedish/com/repository/ProductsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sidedish.com.repository; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jdbc.repository.query.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
import sidedish.com.repository.entity.ProductEntity; | ||
|
||
@Repository | ||
public interface ProductsRepository extends CrudRepository<ProductEntity, Long> { | ||
|
||
@Query("select id, discount_policy_id, product_name, description, original_price, meal_category " | ||
+ "from PRODUCT where meal_category = :mealType") | ||
List<ProductEntity> findByMealType(@Param("mealType") String mealType); | ||
} |
27 changes: 27 additions & 0 deletions
27
BE/src/main/java/sidedish/com/repository/entity/DiscountPolicyEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sidedish.com.repository.entity; | ||
|
||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.PersistenceConstructor; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
@Getter | ||
@Table("DISCOUNT_POLICY") | ||
public class DiscountPolicyEntity { | ||
|
||
@Id | ||
private final Long id; | ||
private final String policyName; | ||
private final float discountRate; | ||
|
||
@PersistenceConstructor | ||
public DiscountPolicyEntity(Long id, String policyName, float discountRate) { | ||
this.id = id; | ||
this.policyName = policyName; | ||
this.discountRate = discountRate; | ||
} | ||
|
||
public boolean isEqualsId(Long id) { | ||
return this.id.equals(id); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
BE/src/main/java/sidedish/com/repository/entity/ImageEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sidedish.com.repository.entity; | ||
|
||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.PersistenceConstructor; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
@Getter | ||
@Table("PRODUCT_IMAGE") | ||
public class ImageEntity { | ||
|
||
@Id | ||
private final Long id; | ||
private final String imageUrl; | ||
|
||
@PersistenceConstructor | ||
public ImageEntity(Long id, String imageUrl) { | ||
this.id = id; | ||
this.imageUrl = imageUrl; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
BE/src/main/java/sidedish/com/repository/entity/ProductEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sidedish.com.repository.entity; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.PersistenceConstructor; | ||
import org.springframework.data.relational.core.mapping.MappedCollection; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
@Getter | ||
@Table("PRODUCT") | ||
public class ProductEntity { | ||
|
||
@Id | ||
private final Long id; | ||
private final Long discountPolicyId; | ||
@MappedCollection(idColumn = "ID", keyColumn = "ID") | ||
private final List<ImageEntity> imageEntities; | ||
private final String productName; | ||
private final String description; | ||
private final long originalPrice; | ||
private final String mealCategory; | ||
private final String bestCategory; | ||
|
||
@PersistenceConstructor | ||
public ProductEntity(Long id, Long discountPolicyId, | ||
List<ImageEntity> imageEntities, String productName, String description, long originalPrice, | ||
String mealCategory, String bestCategory) { | ||
this.id = id; | ||
this.discountPolicyId = discountPolicyId; | ||
this.imageEntities = imageEntities; | ||
this.productName = productName; | ||
this.description = description; | ||
this.originalPrice = originalPrice; | ||
this.mealCategory = mealCategory; | ||
this.bestCategory = bestCategory; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
BE/src/main/java/sidedish/com/service/ProductsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package sidedish.com.service; | ||
|
||
import java.util.List; | ||
import org.springframework.stereotype.Service; | ||
import sidedish.com.controller.ProductsDtoMapper; | ||
import sidedish.com.controller.model.ProductMealTypeResponse; | ||
import sidedish.com.domain.Product; | ||
import sidedish.com.exception.NoSuchProductsException; | ||
import sidedish.com.repository.DiscountPolicyRepository; | ||
import sidedish.com.repository.DomainEntityMapper; | ||
import sidedish.com.repository.ProductsRepository; | ||
|
||
@Service | ||
public class ProductsService { | ||
|
||
private final ProductsRepository productsRepository; | ||
private final DiscountPolicyRepository discountPolicyRepository; | ||
private final ProductsDtoMapper productsDtoMapper; | ||
private final DomainEntityMapper domainEntityMapper; | ||
|
||
public ProductsService(ProductsRepository productsRepository, | ||
DiscountPolicyRepository discountPolicyRepository, | ||
ProductsDtoMapper productsDtoMapper, | ||
DomainEntityMapper domainEntityMapper) { | ||
this.productsRepository = productsRepository; | ||
this.discountPolicyRepository = discountPolicyRepository; | ||
this.productsDtoMapper = productsDtoMapper; | ||
this.domainEntityMapper = domainEntityMapper; | ||
} | ||
|
||
public List<ProductMealTypeResponse> findByMealType(String meal) { | ||
List<Product> products = domainEntityMapper.toDomainFromProductsEntity( | ||
productsRepository.findByMealType(meal), | ||
discountPolicyRepository.findAll()); | ||
|
||
validProducts(products); | ||
|
||
return productsDtoMapper.toProductsMealTypeResponseFromDomain(products); | ||
} | ||
|
||
private void validProducts(List<Product> products) { | ||
if (products.isEmpty()) { | ||
throw new NoSuchProductsException(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.