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 @@ -39,11 +40,24 @@ public List<ProductDto> getCategory(@PathVariable("uuid") String uuid) {
return productDtos;
}

@GetMapping
List<String> getAllCategoty(){

return categoryService.getAllCategory();
}

@GetMapping("/titles/")
public List<String> getProductTitles(@RequestBody GetProductTitlesRequestDto requestDto) {

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

return categoryService.getProductTitles(uuids);
}

@GetMapping("/path/{id}")
public CategoryDto getCategotyById(@PathVariable ("id") Long id){

return categoryService.findCategoryById(id);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ProductController(ProductService productService) {
// GET /products {}
@GetMapping
public List<GenericProductDto> getAllProducts() {

return productService.getAllProducts();
}

Expand All @@ -62,8 +63,8 @@ public GenericProductDto createProduct(@RequestBody GenericProductDto product) {
return productService.createProduct(product);
}

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

@PutMapping("/update")
public GenericProductDto updateProductById(@RequestBody GenericProductDto genericProductDto) {
return productService.updateProductById(genericProductDto);
}
}
10 changes: 6 additions & 4 deletions src/main/java/dev/naman/productservice/models/BaseModel.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package dev.naman.productservice.models;

import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.id.factory.spi.GenerationTypeStrategy;

import java.util.UUID;

@Data
@MappedSuperclass
public class BaseModel {
@Id
@GeneratedValue(generator = "naman")
@GenericGenerator(name = "naman", strategy = "uuid2")
@Column(name = "id", columnDefinition = "binary(16)", nullable = false, updatable = false)
private UUID uuid;
@GeneratedValue(strategy = GenerationType.SEQUENCE)
// @GenericGenerator(name = "naman", strategy = "uuid2")
//@Column(name = "id", columnDefinition = "binary(16)", nullable = false, updatable = false)
private Long id;
}
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
5 changes: 3 additions & 2 deletions src/main/java/dev/naman/productservice/models/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public class Product extends BaseModel {
// => L to R: 1 : 1
// => R to L: m : 1
// => Ans: m : 1
@ManyToOne(cascade = {CascadeType.PERSIST})
@ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.MERGE})
@JoinColumn(name = "category")
private Category category;

@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.LAZY)
@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE,CascadeType.MERGE})
// @Fetch(FetchMode.JOIN)
@JoinColumn(name = "price")
private Price price;
// private double price;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

@Lazy
public interface CategoryRepository
extends JpaRepository<Category, UUID> {
extends JpaRepository<Category, Long> {

Optional<Category> findById(UUID uuid);
// Optional<Category> findById(UUID uuid);

@Override
List<Category> findAllById(Iterable<UUID> uuids);
// @Override
// List<Category> findAllById(Iterable<Long> uuids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Repository
public interface ProductRepository
extends JpaRepository<Product, UUID> {
extends JpaRepository<Product, Long> {

Product findByTitleEquals(String title);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
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<String> getAllCategory();

CategoryDto findCategoryById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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;
Expand All @@ -11,6 +13,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
Expand All @@ -25,18 +28,19 @@ public CategoryServiceImpl(CategoryRepository categoryRepository,

@Override
public Category getCategory(String uuid) {
Optional<Category> categoryOptional = categoryRepository.findById(UUID.fromString(uuid));
// Optional<Category> categoryOptional = categoryRepository.findById(UUID.fromString(uuid));

if (categoryOptional.isEmpty()) {
return null;
}
// if (categoryOptional.isEmpty()) {
// return null;
// }

Category category = categoryOptional.get();
// Category category = categoryOptional.get();

List<Product> products = category.getProducts();
// List<Product> products = category.getProducts();


return category;
// return category;
return null;
}

public List<String> getProductTitles(List<String> categoryUUIDs) {
Expand Down Expand Up @@ -64,16 +68,54 @@ public List<String> getProductTitles(List<String> categoryUUIDs) {
//
// return titles;

List<Category> categories = categoryRepository.findAllById(uuids);
// List<Category> categories = categoryRepository.findAllById(UUID.fromString(" "));

List<Product> products = productRepository.findAllByCategoryIn(categories);
// List<Product> products = productRepository.findAllByCategoryIn(categories);

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

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

return titles;
}

@Override
public List<String> getAllCategory() {

List<Category> categoty = categoryRepository.findAll();

List<String> result = categoty.stream().map(category -> category.getName()).collect(Collectors.toList());
return result;
}

@Override
public CategoryDto findCategoryById(Long id) {
Category category = null;
CategoryDto categoryDto = new CategoryDto();
try {
category= categoryRepository.findById(id).get();
}catch (Exception ex){
System.out.println(ex.getMessage());
}
return convert(category);
}

private CategoryDto convert(Category category){
CategoryDto categoryDto= new CategoryDto();
ProductDto productDto= new ProductDto();
List<ProductDto> products= new ArrayList<>();
categoryDto.setName(category.getName());
for (Product product: category.getProducts()){
productDto.setDescription(product.getDescription());
productDto.setImage(product.getImage());
productDto.setPrice(product.getPrice());
productDto.setTitle(product.getTitle());
products.add(productDto);
}

categoryDto.setProducts(products);
return categoryDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public List<GenericProductDto> getAllProducts() {
public GenericProductDto deleteProduct(Long id) {
return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.deleteProduct(id));
}

@Override
public GenericProductDto updateProductById(GenericProductDto genericProductDto) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface ProductService {
List<GenericProductDto> getAllProducts();

GenericProductDto deleteProduct(Long id);

GenericProductDto updateProductById(GenericProductDto genericProductDto);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Price;
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.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Primary
@Service("selfProductServiceImpl")
Expand All @@ -19,21 +23,71 @@ public SelfProductServiceImpl(ProductRepository productRepository) {

@Override
public GenericProductDto getProductById(Long id) {
return null;
return convertGenericProductDtoToProduct(productRepository.findById(id).get());
}

@Override
public GenericProductDto createProduct(GenericProductDto product) {
return null;
public GenericProductDto createProduct(GenericProductDto genericProductDto) {

Product product = new Product();
Category category = new Category();
Price price= new Price();
price.setCurrency("INR");
price.setPrice(genericProductDto.getPrice());
category.setName(genericProductDto.getCategory());
product.setTitle(genericProductDto.getTitle());
product.setDescription(genericProductDto.getDescription());
product.setImage(genericProductDto.getImage());
product.setCategory(category);
product.setPrice(price);
Product in = productRepository.save(product);
return convertGenericProductDtoToProduct(in);
}

private GenericProductDto convertGenericProductDtoToProduct(Product product) {
GenericProductDto genericProductDto= new GenericProductDto();
genericProductDto.setId(product.getId());
genericProductDto.setCategory(product.getCategory().getName());
genericProductDto.setPrice(product.getPrice().getPrice());
genericProductDto.setTitle(product.getTitle());
genericProductDto.setDescription(product.getDescription());
genericProductDto.setImage(product.getImage());
return genericProductDto;
}

@Override
public List<GenericProductDto> getAllProducts() {
return null;
List<Product> allProducts = productRepository.findAll();
return allProducts.stream().map(product -> convertGenericProductDtoToProduct(product))
.collect(Collectors.toList());
}

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

@Override
public GenericProductDto updateProductById(GenericProductDto genericProductDto) {
Product product = new Product();
Category category = new Category();
Price price= new Price();
price.setCurrency("INR");
price.setPrice(genericProductDto.getPrice());
category.setName(genericProductDto.getCategory());
product.setId(genericProductDto.getId());
product.setTitle(genericProductDto.getTitle());
product.setDescription(genericProductDto.getDescription());
product.setImage(genericProductDto.getImage());
product.setCategory(category);
product.setPrice(price);
Product in = productRepository.save(product);
return convertGenericProductDtoToProduct(in);
}
}
}

2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ naman.bhalla=abc
server.port=3000
# hashmap key:value


//
# Fakestore Config Values
fakestore.api.url=https://fakestoreapi.com
fakestore.api.paths.product=/products
Expand Down