-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Store에 해당하는 식품들을 조회한다.
- Loading branch information
Showing
19 changed files
with
272 additions
and
20 deletions.
There are no files selected for viewing
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
6 changes: 6 additions & 0 deletions
6
src/main/java/flab/nutridiary/product/repository/ProductCrudRepository.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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package flab.nutridiary.product.repository; | ||
|
||
import flab.nutridiary.product.domain.Product; | ||
import org.springframework.data.jdbc.repository.query.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ProductCrudRepository extends CrudRepository<Product, Long> { | ||
int countByProductNormalizedName(String normalizedName); | ||
|
||
@Query("SELECT * FROM product WHERE product_id IN (:productIds)") | ||
List<Product> findByIds(List<Long> productIds); | ||
} |
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
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
6 changes: 0 additions & 6 deletions
6
src/main/java/flab/nutridiary/productStore/ProductStoreRepository.java
This file was deleted.
Oops, something went wrong.
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
39 changes: 39 additions & 0 deletions
39
src/main/java/flab/nutridiary/productStore/dto/StoreProductResponse.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,39 @@ | ||
package flab.nutridiary.productStore.dto; | ||
|
||
import flab.nutridiary.product.domain.Product; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class StoreProductResponse { | ||
private final Long storeId; | ||
private final List<ProductInfo> products; | ||
|
||
public static StoreProductResponse of(Long storeId, List<Product> products) { | ||
List<ProductInfo> productInfos = products.stream() | ||
.map(product -> ProductInfo.of(product.getId(), product.getProductName(), product.getProductCorp())) | ||
.toList(); | ||
|
||
return new StoreProductResponse(storeId, productInfos); | ||
} | ||
|
||
@Getter | ||
public static class ProductInfo { | ||
private final Long productId; | ||
private final String productName; | ||
private final String productCorp; | ||
|
||
private ProductInfo(Long productId, String productName, String productCorp) { | ||
this.productId = productId; | ||
this.productName = productName; | ||
this.productCorp = productCorp; | ||
} | ||
|
||
public static ProductInfo of(Long productId, String productName, String productCorp) { | ||
return new ProductInfo(productId, productName, productCorp); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/flab/nutridiary/productStore/repository/StoreProductCrudRepository.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,10 @@ | ||
package flab.nutridiary.productStore.repository; | ||
|
||
import flab.nutridiary.productStore.domain.StoreProduct; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface StoreProductCrudRepository extends CrudRepository<StoreProduct, Long> { | ||
List<StoreProduct> findByStoreId(Long storeId); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/flab/nutridiary/productStore/repository/StoreProductRepository.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 flab.nutridiary.productStore.repository; | ||
|
||
import flab.nutridiary.productStore.domain.StoreProduct; | ||
|
||
import java.util.List; | ||
|
||
public interface StoreProductRepository { | ||
List<StoreProduct> findByStoreId(Long storeId); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/flab/nutridiary/productStore/repository/StoreProductRepositoryImpl.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,22 @@ | ||
package flab.nutridiary.productStore.repository; | ||
|
||
import flab.nutridiary.productStore.domain.StoreProduct; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Repository | ||
public class StoreProductRepositoryImpl implements StoreProductRepository { | ||
private final StoreProductCrudRepository storeProductCrudRepository; | ||
|
||
@Override | ||
public List<StoreProduct> findByStoreId(Long storeId) { | ||
return storeProductCrudRepository.findByStoreId(storeId); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/flab/nutridiary/store/controller/StoreController.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,29 @@ | ||
package flab.nutridiary.store.controller; | ||
|
||
import flab.nutridiary.commom.dto.ApiResponse; | ||
import flab.nutridiary.productStore.dto.StoreProductResponse; | ||
import flab.nutridiary.store.dto.response.AllStore; | ||
import flab.nutridiary.store.service.StoreReadService; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class StoreController { | ||
|
||
private final StoreReadService storeReadService; | ||
|
||
@GetMapping("/store") | ||
public ApiResponse<AllStore> getAllStore() { | ||
return ApiResponse.success(storeReadService.getAllStore()); | ||
} | ||
|
||
@GetMapping("/store/{store_id}") | ||
public ApiResponse<StoreProductResponse> getStoreProduct(@PathVariable(name = "store_id") Long storeId) { | ||
return ApiResponse.success(storeReadService.getStoreProduct(storeId)); | ||
} | ||
} |
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,20 @@ | ||
package flab.nutridiary.store.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@ToString | ||
@Getter | ||
public class StoreName { | ||
private final Long id; | ||
private final String name; | ||
|
||
private StoreName(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public static StoreName of(Long id, String name) { | ||
return new StoreName(id, name); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/flab/nutridiary/store/dto/response/AllStore.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 flab.nutridiary.store.dto.response; | ||
|
||
import flab.nutridiary.store.dto.StoreName; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class AllStore { | ||
private final List<StoreName> storeNames; | ||
|
||
private AllStore(List<StoreName> storeNames) { | ||
this.storeNames = storeNames; | ||
} | ||
|
||
public static AllStore of(List<StoreName> storeNames) { | ||
return new AllStore(storeNames); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/flab/nutridiary/store/repository/StoreCrudRepository.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,10 @@ | ||
package flab.nutridiary.store.repository; | ||
|
||
import flab.nutridiary.store.domain.Store; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface StoreCrudRepository extends CrudRepository<Store, Long> { | ||
List<Store> findAll(); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/flab/nutridiary/store/repository/StoreRepository.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 flab.nutridiary.store.repository; | ||
|
||
import flab.nutridiary.store.domain.Store; | ||
|
||
import java.util.List; | ||
|
||
public interface StoreRepository { | ||
List<Store> findAll(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/flab/nutridiary/store/repository/StoreRepositoryImpl.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 flab.nutridiary.store.repository; | ||
|
||
import flab.nutridiary.store.domain.Store; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Transactional | ||
@Repository | ||
@RequiredArgsConstructor | ||
public class StoreRepositoryImpl implements StoreRepository { | ||
|
||
private final StoreCrudRepository storeCrudRepository; | ||
|
||
@Override | ||
public List<Store> findAll() { | ||
return storeCrudRepository.findAll(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/flab/nutridiary/store/service/StoreReadService.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,47 @@ | ||
package flab.nutridiary.store.service; | ||
|
||
import flab.nutridiary.commom.exception.BusinessException; | ||
import flab.nutridiary.product.domain.Product; | ||
import flab.nutridiary.product.repository.ProductRepository; | ||
import flab.nutridiary.productStore.dto.StoreProductResponse; | ||
import flab.nutridiary.productStore.repository.StoreProductRepository; | ||
import flab.nutridiary.store.dto.StoreName; | ||
import flab.nutridiary.store.dto.response.AllStore; | ||
import flab.nutridiary.store.repository.StoreRepository; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
import static flab.nutridiary.commom.exception.StatusConst.STORE_PRODUCT_NOT_FOUND; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class StoreReadService { | ||
private final StoreRepository storeRepository; | ||
private final StoreProductRepository storeProductRepository; | ||
private final ProductRepository productRepository; | ||
|
||
public AllStore getAllStore() { | ||
return AllStore.of(storeRepository.findAll() | ||
.stream() | ||
.map(store -> StoreName.of(store.getId(), store.getStoreName())) | ||
.toList()); | ||
} | ||
|
||
public StoreProductResponse getStoreProduct(Long storeId) { | ||
List<Long> productIds = storeProductRepository.findByStoreId(storeId) | ||
.stream() | ||
.map(storeProduct -> storeProduct.getProductId()) | ||
.toList(); | ||
if (productIds.isEmpty()) { | ||
throw new BusinessException(STORE_PRODUCT_NOT_FOUND); | ||
} | ||
List<Product> products = productRepository.findByIds(productIds); | ||
return StoreProductResponse.of(storeId, products); | ||
} | ||
} |
Oops, something went wrong.