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,31 +1,31 @@
package dev.naman.productservice.controllers;

import dev.naman.productservice.dtos.ExceptionDto;
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.dtos.ProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
import dev.naman.productservice.services.ProductService;
import org.springframework.beans.factory.annotation.Qualifier;
import dev.naman.productservice.services.ProductServiceImpl;
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;
private ProductServiceImpl productServiceImpl;
// ....;
// ...;



// constructor injection
// @Autowired
public ProductController(ProductService productService) {
this.productService = productService;
public ProductController(ProductServiceImpl productServiceImpl) {
this.productServiceImpl = productServiceImpl;
}
//

Expand All @@ -38,32 +38,43 @@ public ProductController(ProductService productService) {
// GET /products {}
@GetMapping
public List<GenericProductDto> getAllProducts() {
return productService.getAllProducts();
return productServiceImpl.getAllProducts();
}

// localhost:8080/products/{id}
// localhost:8080/products/123
@GetMapping("{id}")
public GenericProductDto getProductById(@PathVariable("id") Long id) throws NotFoundException {
return productService.getProductById(id);
@GetMapping("/{id}")
public GenericProductDto getProductById(@PathVariable("id") UUID id) throws NotFoundException {
return productServiceImpl.getProductById(id);
}

@DeleteMapping("{id}")
public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") Long id) {
public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") UUID id) {
return new ResponseEntity<>(
productService.deleteProduct(id),
productServiceImpl.deleteProductById(id),
HttpStatus.OK
);
}

@PostMapping
public GenericProductDto createProduct(@RequestBody GenericProductDto product) {
// System.out.println(product.name);
return productService.createProduct(product);
return productServiceImpl.createProduct(product);
}

@PutMapping("{id}")
public void updateProductById() {
public GenericProductDto updateProductById(@RequestBody GenericProductDto genericProductDto, @PathVariable("id") UUID id) {
return productServiceImpl.updateProductById(genericProductDto,id);
}

@GetMapping("/categories/{uuid}")
public List<ProductDto> getCategoryById(@PathVariable("uuid") String uuid){
return productServiceImpl.getCategoryById(uuid);
}

// get all categories
@GetMapping("/categories")
public List<String> getAllCategories(){
return productServiceImpl.getAllCategories();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.ArrayList;
import java.util.List;


@Repository("fakeStoreProductService")
@Service
//@Repository("fakeStoreProductService")
public class FakeStoreProductService implements ProductService {

private FakeStoryProductServiceClient fakeStoryProductServiceClient;
Expand All @@ -36,6 +36,11 @@ public FakeStoreProductService(FakeStoryProductServiceClient fakeStoryProductSer
}


@Override
public GenericProductDto updateProductById(GenericProductDto genericProductDto, Long id) {
return null;
}

@Override
public GenericProductDto createProduct(GenericProductDto product) {
return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.createProduct(product));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import dev.naman.productservice.exceptions.NotFoundException;

import java.util.List;
import java.util.UUID;

public interface ProductService {

GenericProductDto updateProductById(GenericProductDto genericProductDto,Long id);

GenericProductDto createProduct(GenericProductDto product);

GenericProductDto getProductById(Long id) throws NotFoundException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.dtos.ProductDto;

import java.util.List;
import java.util.UUID;

public interface ProductServiceImpl {
GenericProductDto createProduct(GenericProductDto product);

GenericProductDto getProductById(UUID id);

List<GenericProductDto> getAllProducts();

GenericProductDto deleteProductById(UUID id);

GenericProductDto updateProductById(GenericProductDto genericProductDto,UUID id);

List<ProductDto> getCategoryById(String categoryName);

List<String> getAllCategories();
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,133 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.dtos.ProductDto;
import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Price;
import dev.naman.productservice.models.Product;
import dev.naman.productservice.repositories.CategoryRepository;
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;
import java.util.stream.Collectors;

@Primary
@Service("selfProductServiceImpl")
public class SelfProductServiceImpl implements ProductService {
public class SelfProductServiceImpl implements ProductServiceImpl {
private ProductRepository productRepository;
private final CategoryRepository categoryRepository;

public SelfProductServiceImpl(ProductRepository productRepository) {
public SelfProductServiceImpl(ProductRepository productRepository, CategoryRepository categoryRepository) {
this.productRepository = productRepository;
this.categoryRepository = categoryRepository;
}


private GenericProductDto convertProductToProductDto(Product newProduct) {
GenericProductDto product = new GenericProductDto();
product.setTitle(newProduct.getTitle());
product.setDescription(newProduct.getDescription());
product.setImage(newProduct.getImage());

return product;

}
@Override
public GenericProductDto getProductById(Long id) {
return null;
public GenericProductDto getProductById(UUID id) {
Optional<Product> product = productRepository.findById(id);
if(product.isEmpty()) return null;
return convertProductToProductDto(product.get());
}

@Override
public GenericProductDto createProduct(GenericProductDto product) {
return null;
Category category = new Category();
category.setName(product.getCategory());

Price price = new Price();
price.setPrice(product.getPrice());

Product newProduct = new Product();
newProduct.setTitle(product.getTitle());
newProduct.setDescription(product.getDescription());
newProduct.setImage(product.getImage());
newProduct.setCategory(category);
newProduct.setPrice(price);

productRepository.save(newProduct);

return convertProductToProductDto(newProduct);

}



@Override
public List<GenericProductDto> getAllProducts() {
return null;
List<Product> products = productRepository.findAll().stream().collect(Collectors.toList());
List<GenericProductDto> genericProductDtos = new ArrayList<>();

for(Product product:products){
genericProductDtos.add(convertProductToProductDto(product));
}
return genericProductDtos;
}

@Override
public GenericProductDto deleteProduct(Long id) {
return null;
public GenericProductDto deleteProductById(UUID id) {
GenericProductDto genericProductDto = new GenericProductDto();
Optional<Product> product = productRepository.findById(id);
if(product.isEmpty()) return null;
productRepository.deleteById(id);

return convertProductToProductDto(product.get());
}

@Override
public GenericProductDto updateProductById(GenericProductDto genericProductDto, UUID id) {
Optional<Product> product = productRepository.findById(id);
if(product.isEmpty()) return null;
Product updatedProduct = new Product();
updatedProduct.setTitle(genericProductDto.getTitle());
updatedProduct.setDescription(genericProductDto.getDescription());
updatedProduct.setImage(genericProductDto.getImage());

productRepository.save(updatedProduct);
return convertProductToProductDto(updatedProduct);
}

@Override
public List<ProductDto> getCategoryById(String categoryName) {
Optional<Category> category = categoryRepository.findById(UUID.fromString(categoryName));
if(category.isEmpty()) return null;
Category category1 = category.get();
List<ProductDto> productDtos = new ArrayList<>();

for(Product product : category1.getProducts()){
ProductDto productDto = new ProductDto();
productDto.setTitle(product.getTitle());
productDto.setDescription(product.getDescription());
productDto.setImage(product.getImage());
}

return productDtos;
}

@Override
public List<String> getAllCategories() {
List<Category> categories = categoryRepository.findAll().stream().collect(Collectors.toList());
List<Product> products = productRepository.findAllByCategoryIn(categories);
List<String> titles = new ArrayList<>();

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

return titles;
}
}