Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,30 +20,15 @@ public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}

@GetMapping("/{uuid}")
public List<ProductDto> getCategory(@PathVariable("uuid") String uuid) {
List<Product> products = categoryService.getCategory(uuid).getProducts();

List<ProductDto> 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<String> getAllCategory(){
return categoryService.getAllCategory();
}

@GetMapping("/titles/")
public List<String> getProductTitles(@RequestBody GetProductTitlesRequestDto requestDto) {
@GetMapping("/{category}")
public List<GenericProductDto> getProductsInCategory(@PathVariable("category") String categoryName) throws NotFoundException {
return categoryService.getProductsInCategory(categoryName);
}

List<String> uuids = requestDto.getUuids();

return categoryService.getProductTitles(uuids);
}
}
Original file line number Diff line number Diff line change
@@ -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<GenericProductDto> getAllProducts() {
public List<GenericProductDto> 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<GenericProductDto> 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() {

}
}
4 changes: 0 additions & 4 deletions src/main/java/dev/naman/productservice/dtos/ProductDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category, UUID> {

@Repository
public interface CategoryRepository extends JpaRepository<Category, UUID> {
Optional<Category> findById(UUID uuid);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
public interface ProductRepository
extends JpaRepository<Product, UUID> {

Product findByUuid(UUID uuid);

Product findByTitleEquals(String title);

Product findByTitleEqualsAndPrice_PriceOrderByPrice_price(String title, double price);
Product findByTitleEqualsAndAndPrice_Price(String title, double price);

List<Product> findAllByPrice_Currency(String currency);

Expand All @@ -27,21 +29,14 @@ public interface ProductRepository

long countAllByPrice_Currency(String currency);


List<Product> findAllByTitleLike(String titleRegex);

List<Product> readAllByTitleLike(String titleRegex);

List<Product> findAllByCategory_NameEquals(String category);

List<Product> findAllByCategoryIn(List<Category> categories);

// @Query("select Product from Product where Product .category.uuid in :uuids")
// List<Product> findAllByCategoryIn(List<UUID> uuids);

List<Product> findAllByCategoryIn(List<Category> categories);

@Query(value = CustomQueries.FIND_ALL_BY_TITLE, nativeQuery = true)
List<Product> findAllByTitle(String naman);

// @Query("select Product from Product where Product.price.currency = :currency and Product.title = :naman")
// List<Product> doSomething(String naman, String currency);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
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;

public interface CategoryService {
Category getCategory(String uuid);
List<String> getProductTitles(List<String> categoryUUIDs);

List<String> getAllCategory();

List<GenericProductDto> getProductsInCategory(String categoryName) throws NotFoundException;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +25,29 @@ public CategoryServiceImpl(CategoryRepository categoryRepository,
this.productRepository = productRepository;
}

@Override
public List<String> getAllCategory(){
List<String> categories = new ArrayList<>();
List<Category> categoryList = categoryRepository.findAll();
for(Category category : categoryList)
categories.add(category.getName());
return categories;
}

public List<GenericProductDto> getProductsInCategory(String categoryName) throws NotFoundException {
List<Product> products = productRepository.findAllByCategory_NameEquals(categoryName);
List<GenericProductDto> 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<Category> categoryOptional = categoryRepository.findById(UUID.fromString(uuid));
Expand All @@ -42,7 +67,7 @@ public Category getCategory(String uuid) {
public List<String> getProductTitles(List<String> categoryUUIDs) {
List<UUID> uuids = new ArrayList<>();

for (String uuid: categoryUUIDs) {
for (String uuid : categoryUUIDs) {
uuids.add(UUID.fromString(uuid));
}
//
Expand Down Expand Up @@ -70,7 +95,7 @@ public List<String> getProductTitles(List<String> categoryUUIDs) {

List<String> titles = new ArrayList<>();

for (Product p: products) {
for (Product p : products) {
titles.add(p.getTitle());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<FakeStoreProductDto> 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<FakeStoreProductDto> response = restTemplate.put();
}*/
private FakeStoryProductServiceClient fakeStoryProductServiceClient;

private GenericProductDto convertFakeStoreProductIntoGenericProduct(FakeStoreProductDto fakeStoreProductDto) {
Expand All @@ -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
Expand All @@ -57,7 +97,10 @@ public List<GenericProductDto> 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){
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenericProductDto> getAllProducts();

GenericProductDto deleteProduct(Long id);
void updateProductById(UUID id, Product updateProduct);
void deleteProductById(UUID id);
}
Loading