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

import dev.naman.productservice.dtos.CategoryDto;
import dev.naman.productservice.dtos.GetProductTitlesRequestDto;
import dev.naman.productservice.dtos.ProductDto;
import dev.naman.productservice.models.Category;
Expand Down Expand Up @@ -33,7 +34,6 @@ public List<ProductDto> getCategory(@PathVariable("uuid") String uuid) {
productDto.setImage(product.getImage());
productDto.setPrice(product.getPrice());
productDtos.add(productDto);
// productDto.se
}

return productDtos;
Expand All @@ -46,4 +46,9 @@ public List<String> getProductTitles(@RequestBody GetProductTitlesRequestDto req

return categoryService.getProductTitles(uuids);
}
@GetMapping()
public List<CategoryDto> getAllCategories(@RequestBody GetProductTitlesRequestDto requestDto) {
List<String> uuids = requestDto.getUuids();
return categoryService.getAllCategories(uuids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.naman.productservice.controllers;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
import dev.naman.productservice.services.FakeProductService;
import dev.naman.productservice.services.FakeStoreProductService;
import dev.naman.productservice.services.ProductService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fakeproducts")
public class FakeProductController {
// @Autowired
// field injection
private FakeProductService fakeProductService;
// ....;
// ...;



// constructor injection
// @Autowired
public FakeProductController(FakeProductService fakeProductService) {
this.fakeProductService = fakeProductService;
}
//

// setter injection
// @Autowired
// public void setProductService(ProductService productService) {
// this.productService = productService;
// }

// GET /products {}
@GetMapping
public List<GenericProductDto> getAllProducts() {
return fakeProductService.getAllProducts();
}

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

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

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

// @PutMapping("{id}")
// public void updateProductById() {
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import dev.naman.productservice.dtos.ExceptionDto;
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.dtos.GetProductTitlesRequestDto;
import dev.naman.productservice.dtos.ProductDto;
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;
Expand All @@ -14,56 +17,49 @@
@RestController
@RequestMapping("/products")
public class ProductController {
// @Autowired
// field injection
private ProductService productService;
// ....;
// ...;



// constructor injection
// @Autowired
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() {
return productService.getAllProducts();
public List<ProductDto> getAllProducts(@RequestBody GetProductTitlesRequestDto requestDto) {
List<String> uuids = requestDto.getUuids();
return productService.getAllProducts(uuids);
}

// localhost:8080/products/{id}
// localhost:8080/products/123
@GetMapping("/category/{categoryId}")
public List<ProductDto> getProductsInCategory(@PathVariable("categoryId") String categoryId) throws NotFoundException {
return productService.getProductsInCategory(categoryId);
}

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

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

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

@PutMapping("{id}")
public void updateProductById() {

public ProductDto updateProductById(@RequestBody ProductDto product,@PathVariable("id") String id) {
return productService.updateProduct(product,id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@Setter
public class CategoryDto {
private String name;

private List<ProductDto> products;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Category extends BaseModel {
private String name;

@OneToMany(mappedBy = "category")
@Fetch(FetchMode.SELECT)
@Fetch(FetchMode.SUBSELECT)
private List<Product> products = new ArrayList<>();

// this is the same relation being mapped by category attribute in the other (Product) class
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/dev/naman/productservice/models/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class Product extends BaseModel {
@JoinColumn(name = "category")
private Category category;

@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.LAZY)
@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
// @Fetch(FetchMode.JOIN)
private Price price;
// private double price;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.naman.productservice.repositories;

import dev.naman.productservice.dtos.ProductDto;
import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Product;
import org.springframework.data.domain.Example;
Expand All @@ -9,12 +10,15 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;

@Repository
public interface ProductRepository
extends JpaRepository<Product, UUID> {
@Override
Optional<Product> findById(UUID uuid);

Product findByTitleEquals(String title);

Expand All @@ -25,6 +29,7 @@ public interface ProductRepository
@Override
void delete(Product entity);


long countAllByPrice_Currency(String currency);


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.CategoryDto;
import dev.naman.productservice.models.Category;

import java.util.List;

public interface CategoryService {
Category getCategory(String uuid);
List<String> getProductTitles(List<String> categoryUUIDs);
List<CategoryDto> getAllCategories(List<String> categoryUUIds);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.CategoryDto;
import dev.naman.productservice.dtos.ProductDto;
import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Product;
import dev.naman.productservice.repositories.CategoryRepository;
import dev.naman.productservice.repositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -16,7 +19,6 @@
public class CategoryServiceImpl implements CategoryService {
private CategoryRepository categoryRepository;
private final ProductRepository productRepository;

public CategoryServiceImpl(CategoryRepository categoryRepository,
ProductRepository productRepository) {
this.categoryRepository = categoryRepository;
Expand Down Expand Up @@ -76,4 +78,33 @@ public List<String> getProductTitles(List<String> categoryUUIDs) {

return titles;
}

@Override
public List<CategoryDto> getAllCategories(List<String> categoryUUIds) {
List<UUID> uuids = new ArrayList<>();
for(String categoryId: categoryUUIds){
uuids.add(UUID.fromString(categoryId));
}
List<Category> categories = categoryRepository.findAllById(uuids);
List<CategoryDto> categoryDtos = new ArrayList<>();
for(Category category:categories){
CategoryDto categoryDto = new CategoryDto();
categoryDto.setName(category.getName());
List<Product> products = category.getProducts();
List<ProductDto> productDtos = new ArrayList<>();
products.forEach(
product -> {
ProductDto productDto = new ProductDto();
productDto.setTitle(product.getTitle());
productDto.setDescription(product.getDescription());
productDto.setImage(product.getImage());
productDto.setPrice(product.getPrice());
productDtos.add(productDto);
}
);
categoryDto.setProducts(productDtos);
categoryDtos.add(categoryDto);
}
return categoryDtos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;

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

public interface FakeProductService {

GenericProductDto createProduct(GenericProductDto product);

GenericProductDto getProductById(Long id) throws NotFoundException;

List<GenericProductDto> getAllProducts();

GenericProductDto deleteProduct(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


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

private FakeStoryProductServiceClient fakeStoryProductServiceClient;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.dtos.ProductDto;
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(GenericProductDto product);
ProductDto createProduct(Product product);

GenericProductDto getProductById(Long id) throws NotFoundException;
ProductDto getProductById(String id) throws NotFoundException;
List<ProductDto> getProductsInCategory(String id) throws NotFoundException;

List<GenericProductDto> getAllProducts();
List<ProductDto> getAllProducts(List<String> categories);

GenericProductDto deleteProduct(Long id);
ProductDto deleteProduct(String id);
ProductDto updateProduct(ProductDto productDto,String id);
}
Loading