diff --git a/src/main/java/dev/naman/productservice/controllers/CategoryController.java b/src/main/java/dev/naman/productservice/controllers/CategoryController.java index be875bb..f1fd7ec 100644 --- a/src/main/java/dev/naman/productservice/controllers/CategoryController.java +++ b/src/main/java/dev/naman/productservice/controllers/CategoryController.java @@ -1,13 +1,13 @@ package dev.naman.productservice.controllers; -import dev.naman.productservice.dtos.GetProductTitlesRequestDto; -import dev.naman.productservice.dtos.ProductDto; -import dev.naman.productservice.models.Category; -import dev.naman.productservice.models.Product; +import dev.naman.productservice.dtos.GenericProductDto; +import dev.naman.productservice.exceptions.NotFoundException; import dev.naman.productservice.services.CategoryService; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; -import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -20,30 +20,15 @@ public CategoryController(CategoryService categoryService) { this.categoryService = categoryService; } - @GetMapping("/{uuid}") - public List getCategory(@PathVariable("uuid") String uuid) { - List products = categoryService.getCategory(uuid).getProducts(); - - List productDtos = new ArrayList<>(); - - for (Product product: products) { - ProductDto productDto = new ProductDto(); - productDto.setDescription(product.getDescription()); - productDto.setTitle(product.getTitle()); - productDto.setImage(product.getImage()); - productDto.setPrice(product.getPrice()); - productDtos.add(productDto); -// productDto.se - } - - return productDtos; + @GetMapping + public List getAllCategory(){ + return categoryService.getAllCategory(); } - @GetMapping("/titles/") - public List getProductTitles(@RequestBody GetProductTitlesRequestDto requestDto) { + @GetMapping("/{category}") + public List getProductsInCategory(@PathVariable("category") String categoryName) throws NotFoundException { + return categoryService.getProductsInCategory(categoryName); + } - List uuids = requestDto.getUuids(); - return categoryService.getProductTitles(uuids); - } } diff --git a/src/main/java/dev/naman/productservice/controllers/ProductController.java b/src/main/java/dev/naman/productservice/controllers/ProductController.java index af619d5..c7f198e 100644 --- a/src/main/java/dev/naman/productservice/controllers/ProductController.java +++ b/src/main/java/dev/naman/productservice/controllers/ProductController.java @@ -1,69 +1,50 @@ package dev.naman.productservice.controllers; -import dev.naman.productservice.dtos.ExceptionDto; +import dev.naman.productservice.dtos.CategoryDto; import dev.naman.productservice.dtos.GenericProductDto; import dev.naman.productservice.exceptions.NotFoundException; +import dev.naman.productservice.models.Product; import dev.naman.productservice.services.ProductService; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.UUID; @RestController @RequestMapping("/products") public class ProductController { -// @Autowired - // field injection - private ProductService productService; - // ....; - // ...; - + @Qualifier("selfProductServiceImpl") + private ProductService productService; - // constructor injection -// @Autowired - public ProductController(ProductService productService) { + public ProductController(ProductService productService){; this.productService = productService; } -// - // setter injection -// @Autowired -// public void setProductService(ProductService productService) { -// this.productService = productService; -// } - - // GET /products {} @GetMapping - public List getAllProducts() { + public List getAllProducts(){ return productService.getAllProducts(); } - // localhost:8080/products/{id} - // localhost:8080/products/123 @GetMapping("{id}") - public GenericProductDto getProductById(@PathVariable("id") Long id) throws NotFoundException { + public GenericProductDto getProductById(@PathVariable("id") String id) throws NotFoundException { return productService.getProductById(id); } @DeleteMapping("{id}") - public ResponseEntity deleteProductById(@PathVariable("id") Long id) { - return new ResponseEntity<>( - productService.deleteProduct(id), - HttpStatus.OK - ); + public void deleteProductById(@PathVariable("id") UUID id){ + productService.deleteProductById(id); + } + + @PutMapping("{id}") + public void updateProductById(@PathVariable("id") UUID id, @RequestBody Product product){ + productService.updateProductById(id, product); } @PostMapping - public GenericProductDto createProduct(@RequestBody GenericProductDto product) { -// System.out.println(product.name); + public GenericProductDto createProduct(@RequestBody Product product){ return productService.createProduct(product); } - @PutMapping("{id}") - public void updateProductById() { - - } } diff --git a/src/main/java/dev/naman/productservice/dtos/ProductDto.java b/src/main/java/dev/naman/productservice/dtos/ProductDto.java index 9f2d39f..977fddf 100644 --- a/src/main/java/dev/naman/productservice/dtos/ProductDto.java +++ b/src/main/java/dev/naman/productservice/dtos/ProductDto.java @@ -17,9 +17,5 @@ public class ProductDto { private String description; private String image; - // P : C - // => L to R: 1 : 1 - // => R to L: m : 1 - // => Ans: m : 1 private Price price; } diff --git a/src/main/java/dev/naman/productservice/repositories/CategoryRepository.java b/src/main/java/dev/naman/productservice/repositories/CategoryRepository.java index 6521d4a..dd693a1 100644 --- a/src/main/java/dev/naman/productservice/repositories/CategoryRepository.java +++ b/src/main/java/dev/naman/productservice/repositories/CategoryRepository.java @@ -3,15 +3,15 @@ import dev.naman.productservice.models.Category; import org.springframework.context.annotation.Lazy; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import java.util.List; import java.util.Optional; import java.util.UUID; @Lazy -public interface CategoryRepository -extends JpaRepository { - +@Repository +public interface CategoryRepository extends JpaRepository { Optional findById(UUID uuid); @Override diff --git a/src/main/java/dev/naman/productservice/repositories/ProductRepository.java b/src/main/java/dev/naman/productservice/repositories/ProductRepository.java index c2bd45c..a685dba 100644 --- a/src/main/java/dev/naman/productservice/repositories/ProductRepository.java +++ b/src/main/java/dev/naman/productservice/repositories/ProductRepository.java @@ -16,9 +16,11 @@ public interface ProductRepository extends JpaRepository { + Product findByUuid(UUID uuid); + Product findByTitleEquals(String title); - Product findByTitleEqualsAndPrice_PriceOrderByPrice_price(String title, double price); + Product findByTitleEqualsAndAndPrice_Price(String title, double price); List findAllByPrice_Currency(String currency); @@ -27,21 +29,14 @@ public interface ProductRepository long countAllByPrice_Currency(String currency); - List findAllByTitleLike(String titleRegex); List readAllByTitleLike(String titleRegex); + List findAllByCategory_NameEquals(String category); - List findAllByCategoryIn(List categories); - - // @Query("select Product from Product where Product .category.uuid in :uuids") -// List findAllByCategoryIn(List uuids); - + List findAllByCategoryIn(List categories); @Query(value = CustomQueries.FIND_ALL_BY_TITLE, nativeQuery = true) List findAllByTitle(String naman); - -// @Query("select Product from Product where Product.price.currency = :currency and Product.title = :naman") -// List doSomething(String naman, String currency); } diff --git a/src/main/java/dev/naman/productservice/services/CategoryService.java b/src/main/java/dev/naman/productservice/services/CategoryService.java index 91d2912..50df2e5 100644 --- a/src/main/java/dev/naman/productservice/services/CategoryService.java +++ b/src/main/java/dev/naman/productservice/services/CategoryService.java @@ -1,5 +1,7 @@ package dev.naman.productservice.services; +import dev.naman.productservice.dtos.GenericProductDto; +import dev.naman.productservice.exceptions.NotFoundException; import dev.naman.productservice.models.Category; import java.util.List; @@ -7,4 +9,8 @@ public interface CategoryService { Category getCategory(String uuid); List getProductTitles(List categoryUUIDs); + + List getAllCategory(); + + List getProductsInCategory(String categoryName) throws NotFoundException; } diff --git a/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java b/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java index b167a55..f7452c6 100644 --- a/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java +++ b/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java @@ -1,5 +1,7 @@ package dev.naman.productservice.services; +import dev.naman.productservice.dtos.GenericProductDto; +import dev.naman.productservice.exceptions.NotFoundException; import dev.naman.productservice.models.Category; import dev.naman.productservice.models.Product; import dev.naman.productservice.repositories.CategoryRepository; @@ -23,6 +25,29 @@ public CategoryServiceImpl(CategoryRepository categoryRepository, this.productRepository = productRepository; } + @Override + public List getAllCategory(){ + List categories = new ArrayList<>(); + List categoryList = categoryRepository.findAll(); + for(Category category : categoryList) + categories.add(category.getName()); + return categories; + } + + public List getProductsInCategory(String categoryName) throws NotFoundException { + List products = productRepository.findAllByCategory_NameEquals(categoryName); + List genericProductDtos = new ArrayList<>(); + for (Product product : products){ + GenericProductDto genericProductDto = new GenericProductDto(); + genericProductDto.setImage(product.getImage()); + genericProductDto.setTitle(product.getTitle()); + genericProductDto.setDescription(product.getDescription()); + genericProductDto.setPrice(product.getPrice().getPrice()); + + genericProductDtos.add(genericProductDto); + } + return genericProductDtos; + } @Override public Category getCategory(String uuid) { Optional categoryOptional = categoryRepository.findById(UUID.fromString(uuid)); @@ -42,7 +67,7 @@ public Category getCategory(String uuid) { public List getProductTitles(List categoryUUIDs) { List uuids = new ArrayList<>(); - for (String uuid: categoryUUIDs) { + for (String uuid : categoryUUIDs) { uuids.add(UUID.fromString(uuid)); } // @@ -70,7 +95,7 @@ public List getProductTitles(List categoryUUIDs) { List titles = new ArrayList<>(); - for (Product p: products) { + for (Product p : products) { titles.add(p.getTitle()); } diff --git a/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java b/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java index ceb58af..c320e00 100644 --- a/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java +++ b/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java @@ -2,6 +2,7 @@ import dev.naman.productservice.dtos.GenericProductDto; import dev.naman.productservice.exceptions.NotFoundException; +import dev.naman.productservice.models.Product; import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoreProductDto; import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoryProductServiceClient; import org.springframework.context.annotation.Primary; @@ -11,11 +12,49 @@ import java.util.ArrayList; import java.util.List; +import java.util.UUID; @Repository("fakeStoreProductService") public class FakeStoreProductService implements ProductService { + /* //to communicate with third party API--fakestoreApi() + private RestTemplateBuilder restTemplateBuilder ; + private String getProductRequestUrl = "https://fakestoreapi.com/products/{id}"; + private String createProductRequestUrl = "https://fakestoreapi.com/products"; + private String deleteProductRequestUrl = "https://fakestoreapi.com/products/{id}"; + + private String updateProductRequestUrl = "https://fakestoreapi.com/products/{id}"; + + //inject templateBuilder bean + public FakeStoreProductServiceImpl(RestTemplateBuilder restTemplateBuilder){ + this.restTemplateBuilder = restTemplateBuilder; + } + public GenericProductDto getProductById(Long id){ + //return new Product(); + RestTemplate restTemplate = restTemplateBuilder.build(); + ResponseEntity response = restTemplate.getForEntity(getProductRequestUrl, FakeStoreProductDto.class, id); + FakeStoreProductDto fakeStoreProductDto = response.getBody(); //retrieving the response + + GenericProductDto genericProductDto = new GenericProductDto(); + genericProductDto.setId(fakeStoreProductDto.getId()); + genericProductDto.setDescription(fakeStoreProductDto.getDescription()); + genericProductDto.setImage(fakeStoreProductDto.getImage()); + genericProductDto.setCategory(fakeStoreProductDto.getCategory()); + genericProductDto.setPrice(fakeStoreProductDto.getPrice()); + + return genericProductDto; + } + + public void deleteProductById(Long id){ + RestTemplate restTemplate = restTemplateBuilder.build(); + restTemplate.delete(deleteProductRequestUrl, id); + } + + public void updateProductById(Long id){ + //RestTemplate restTemplate = restTemplateBuilder.build(); + //ResponseEntity response = restTemplate.put(); + }*/ private FakeStoryProductServiceClient fakeStoryProductServiceClient; private GenericProductDto convertFakeStoreProductIntoGenericProduct(FakeStoreProductDto fakeStoreProductDto) { @@ -37,13 +76,14 @@ public FakeStoreProductService(FakeStoryProductServiceClient fakeStoryProductSer @Override - public GenericProductDto createProduct(GenericProductDto product) { + public GenericProductDto createProduct(Product product) { return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.createProduct(product)); } @Override - public GenericProductDto getProductById(Long id) throws NotFoundException { - return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.getProductById(id)); + public GenericProductDto getProductById(String id) throws NotFoundException { + // return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.getProductById(id)); + return null; } @Override @@ -57,7 +97,10 @@ public List getAllProducts() { } @Override - public GenericProductDto deleteProduct(Long id) { - return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.deleteProduct(id)); + public void deleteProductById(UUID id) { + // return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.deleteProduct(id)); + } + + public void updateProductById(UUID id, Product updateProduct){ } } diff --git a/src/main/java/dev/naman/productservice/services/ProductService.java b/src/main/java/dev/naman/productservice/services/ProductService.java index bc5eb10..3f90018 100644 --- a/src/main/java/dev/naman/productservice/services/ProductService.java +++ b/src/main/java/dev/naman/productservice/services/ProductService.java @@ -2,16 +2,18 @@ import dev.naman.productservice.dtos.GenericProductDto; import dev.naman.productservice.exceptions.NotFoundException; +import dev.naman.productservice.models.Product; import java.util.List; +import java.util.UUID; public interface ProductService { + GenericProductDto createProduct(Product product); - GenericProductDto createProduct(GenericProductDto product); - - GenericProductDto getProductById(Long id) throws NotFoundException; + GenericProductDto getProductById(String id) throws NotFoundException; List getAllProducts(); - GenericProductDto deleteProduct(Long id); + void updateProductById(UUID id, Product updateProduct); + void deleteProductById(UUID id); } diff --git a/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java b/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java index 260a45b..f7dd5c6 100644 --- a/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java +++ b/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java @@ -1,39 +1,91 @@ package dev.naman.productservice.services; import dev.naman.productservice.dtos.GenericProductDto; +import dev.naman.productservice.models.Category; import dev.naman.productservice.models.Product; import dev.naman.productservice.repositories.ProductRepository; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import java.util.UUID; @Primary @Service("selfProductServiceImpl") public class SelfProductServiceImpl implements ProductService { + private ProductRepository productRepository; - public SelfProductServiceImpl(ProductRepository productRepository) { + public SelfProductServiceImpl(ProductRepository productRepository){ this.productRepository = productRepository; } - @Override - public GenericProductDto getProductById(Long id) { - return null; + public GenericProductDto getProductById(String id) { + Optional productOptional = productRepository.findById(UUID.fromString(id)); + if (productOptional.isEmpty()) { + return null; + } + Product product = productOptional.get(); + //Setting Generic product values and returning it to user + GenericProductDto genericProductDto = new GenericProductDto(); + genericProductDto.setPrice(product.getPrice().getPrice()); + genericProductDto.setCategory(product.getCategory().getName()); + genericProductDto.setDescription(product.getDescription()); + genericProductDto.setTitle(product.getTitle()); + genericProductDto.setImage(product.getImage()); + + return genericProductDto; } @Override - public GenericProductDto createProduct(GenericProductDto product) { - return null; + public GenericProductDto createProduct(Product product) { + GenericProductDto genericProductDto = new GenericProductDto(); + Category category = new Category(); + category.setName(category.getName()); + genericProductDto.setCategory(category.getName()); + genericProductDto.setDescription(product.getDescription()); + genericProductDto.setTitle(product.getTitle()); + genericProductDto.setImage(product.getImage()); + productRepository.save(product); + + return genericProductDto; } @Override public List getAllProducts() { - return null; + List genericProductDtos = new ArrayList<>(); + List products = new ArrayList<>(); + products = productRepository.findAll(); + + for (Product product : products) { + GenericProductDto genericProductDto = new GenericProductDto(); + genericProductDto.setPrice(product.getPrice().getPrice()); + genericProductDto.setCategory(product.getCategory().getName()); + genericProductDto.setDescription(product.getDescription()); + genericProductDto.setTitle(product.getTitle()); + genericProductDto.setImage(product.getImage()); + genericProductDtos.add(genericProductDto); + } + return genericProductDtos; + } @Override - public GenericProductDto deleteProduct(Long id) { - return null; + public void deleteProductById(UUID id) { + productRepository.deleteById(id); + + } + + public void updateProductById(UUID id, Product updateProduct){ + Product product = productRepository.findByUuid(id); + product.setImage(updateProduct.getImage()); + product.setTitle(updateProduct.getTitle()); + product.setDescription(updateProduct.getDescription()); + product.setPrice(updateProduct.getPrice()); + product.setCategory(updateProduct.getCategory()); + + productRepository.save(product); } } diff --git a/src/main/java/dev/naman/productservice/thirdpartyclients/productsservice/fakestore/FakeStoryProductServiceClient.java b/src/main/java/dev/naman/productservice/thirdpartyclients/productsservice/fakestore/FakeStoryProductServiceClient.java index 395d3c2..05b6ee3 100644 --- a/src/main/java/dev/naman/productservice/thirdpartyclients/productsservice/fakestore/FakeStoryProductServiceClient.java +++ b/src/main/java/dev/naman/productservice/thirdpartyclients/productsservice/fakestore/FakeStoryProductServiceClient.java @@ -2,6 +2,7 @@ import dev.naman.productservice.dtos.GenericProductDto; import dev.naman.productservice.exceptions.NotFoundException; +import dev.naman.productservice.models.Product; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpMethod; @@ -41,7 +42,7 @@ public FakeStoryProductServiceClient(RestTemplateBuilder restTemplateBuilder, } - public FakeStoreProductDto createProduct(GenericProductDto product) { + public FakeStoreProductDto createProduct(Product product) { RestTemplate restTemplate = restTemplateBuilder.build(); ResponseEntity response = restTemplate.postForEntity( productRequestsBaseUrl, product, FakeStoreProductDto.class diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1f3ba82..0f76754 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,20 +1,12 @@ -productservice.type=fakeStoreProductService -ayush.goyal=fakeStoreProductService -naman.bhalla=abc -server.port=3000 -# hashmap key:value +server.port = 9090 +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:mysql://localhost:3306/productservice +spring.datasource.username=root +spring.datasource.password=root +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.jpa.show-sql=true -# Fakestore Config Values fakestore.api.url=https://fakestoreapi.com fakestore.api.paths.product=/products -server.error.include-stacktrace=never - -spring.jpa.hibernate.ddl-auto=update -spring.datasource.url=jdbc:mysql://localhost:3306/sep23productservice -spring.datasource.username=sep23productservice -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.show-sql=true -#logging.level.org.hibernate.SQL=DEBUG -#logging.level.org.hibernate.type=TRACE