diff --git a/src/main/java/dev/naman/productservice/controller/CategoryController.java b/src/main/java/dev/naman/productservice/controller/CategoryController.java new file mode 100644 index 0000000..cd3a850 --- /dev/null +++ b/src/main/java/dev/naman/productservice/controller/CategoryController.java @@ -0,0 +1,47 @@ +package dev.naman.productservice.controller; + +import dev.pranay.productservice.dtos.ProductDto; +import dev.pranay.productservice.models.Product; +import dev.pranay.productservice.services.CategoryServiceDB; +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; +@RestController +@RequestMapping("/categories") +public class CategoryController { + private final CategoryServiceDB categoryServiceDB; + + public CategoryController(CategoryServiceDB categoryServiceDB) { + this.categoryServiceDB = categoryServiceDB; + } + + + @GetMapping("/{uuid}") + public List getCategory(@PathVariable("uuid") String uuid) { + List products = categoryServiceDB.getCategoryById(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); + } + + return productDtos; + } + + // get all categories + @GetMapping("/all") + public List getAllCategories(){ + + return categoryServiceDB.getAllCategories(); + } +} diff --git a/src/main/java/dev/naman/productservice/controller/ProductController.java b/src/main/java/dev/naman/productservice/controller/ProductController.java new file mode 100644 index 0000000..12902e7 --- /dev/null +++ b/src/main/java/dev/naman/productservice/controller/ProductController.java @@ -0,0 +1,59 @@ +package dev.naman.productservice.controller; + + +import dev.pranay.productservice.dtos.GenericProductDto; +import dev.pranay.productservice.exception.NotFoundException; +import dev.pranay.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("products") +public class ProductController { + + private ProductService productService; + + public ProductController(ProductService productService){ + this.productService = productService; + } + + + + //GET /products {} + @GetMapping + 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 { + return productService.getProductById(id); + + } + + + @DeleteMapping("{id}") + public ResponseEntity deleteProductById(@PathVariable("id") Long id) { + return new ResponseEntity<>( + productService.deleteProduct(id), + HttpStatus.OK + ); + + } + + @PostMapping + public GenericProductDto createProduct(@RequestBody GenericProductDto product) { + return productService.createProduct(product); + + } + + @PutMapping("{id}") + public GenericProductDto updateProductById(@PathVariable("id")Long id, @RequestBody GenericProductDto product) { + return productService.updateProductById(id,product); + } +} diff --git a/src/main/java/dev/naman/productservice/controller/ProductControllerDB.java b/src/main/java/dev/naman/productservice/controller/ProductControllerDB.java new file mode 100644 index 0000000..0e00a83 --- /dev/null +++ b/src/main/java/dev/naman/productservice/controller/ProductControllerDB.java @@ -0,0 +1,58 @@ +package dev.naman.productservice.controller; + +import dev.pranay.productservice.dtos.GenericProductDtoDB; +import dev.pranay.productservice.exception.NotFoundException; +import dev.pranay.productservice.services.ProductServiceDB; +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("/dbproducts") +public class ProductControllerDB { + private ProductServiceDB productServiceDB; + + public ProductControllerDB(ProductServiceDB productServiceDB){ + this.productServiceDB = productServiceDB; + } + + + + //GET /products {} + @GetMapping + public List getAllProducts() { + return productServiceDB.getAllProducts(); + } + + // localhost:8080/products/{id} + // localhost:8080/products/123 + @GetMapping("{id}") + public GenericProductDtoDB getProductById(@PathVariable("id") String id) throws NotFoundException { + return productServiceDB.getProductById(UUID.fromString(id)); + + } + + + @DeleteMapping("{id}") + public ResponseEntity deleteProductById(@PathVariable("id") String id) throws NotFoundException { + return new ResponseEntity<>( + productServiceDB.deleteProductById(UUID.fromString(id)), + HttpStatus.OK + ); + + } + + @PostMapping + public GenericProductDtoDB createProduct(@RequestBody GenericProductDtoDB product) { + return productServiceDB.createProduct(product); + + } + + @PutMapping("{id}") + public GenericProductDtoDB updateProductById(@RequestBody GenericProductDtoDB product, @PathVariable("id")String id) { + return productServiceDB.updateProductById(product, UUID.fromString(id)); + } +} diff --git a/src/main/java/dev/naman/productservice/controllers/CategoryController.java b/src/main/java/dev/naman/productservice/controllers/CategoryController.java deleted file mode 100644 index be875bb..0000000 --- a/src/main/java/dev/naman/productservice/controllers/CategoryController.java +++ /dev/null @@ -1,49 +0,0 @@ -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.services.CategoryService; -import org.springframework.web.bind.annotation.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -@RestController -@RequestMapping("/categories") -public class CategoryController { - private CategoryService categoryService; - - 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("/titles/") - public List getProductTitles(@RequestBody GetProductTitlesRequestDto requestDto) { - - 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 deleted file mode 100644 index e73f2f2..0000000 --- a/src/main/java/dev/naman/productservice/controllers/ProductController.java +++ /dev/null @@ -1,100 +0,0 @@ -package dev.naman.productservice.controllers; - -import dev.naman.productservice.dtos.ExceptionDto; -import dev.naman.productservice.dtos.GenericProductDto; -import dev.naman.productservice.exceptions.NotFoundException; -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.ArrayList; -import java.util.Comparator; -import java.util.List; - -@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 ResponseEntity> getAllProducts() { - List productDtos = productService.getAllProducts(); - if (productDtos.isEmpty()) { - return new ResponseEntity<>( - productDtos, - HttpStatus.NOT_FOUND - ); - } - - List genericProductDtos = new ArrayList<>(); - - for (GenericProductDto gpd: productDtos) { - genericProductDtos.add(gpd); - }; - -// genericProductDtos.remove(genericProductDtos.get(0)); - - return new ResponseEntity<>(genericProductDtos, HttpStatus.OK); - -// productDtos.get(0).setId(1001L); -// -// return new ResponseEntity<>(productDtos, HttpStatus.OK); - } - - // localhost:8080/products/{id} - // localhost:8080/products/123 - @GetMapping("{id}") - public GenericProductDto getProductById(@PathVariable("id") Long id) throws NotFoundException { - GenericProductDto productDto = productService.getProductById(id); - if (productDto == null) { - throw new NotFoundException("Product Doesn't Exist"); - } - -// GenericProductDto genericProductDto = new GenericProductDto(); -// genericProductDto.setTitle(productDto.getTitle()); - return productDto; - -// Comparator - } - - @DeleteMapping("{id}") - public ResponseEntity deleteProductById(@PathVariable("id") Long id) { - return new ResponseEntity<>( - productService.deleteProduct(id), - HttpStatus.OK - ); - } - - @PostMapping - public GenericProductDto createProduct(@RequestBody GenericProductDto product) { -// System.out.println(product.name); - return productService.createProduct(product); - } - - @PutMapping("{id}") - public void updateProductById() { - - } -} diff --git a/src/main/java/dev/naman/productservice/dtos/GetProductTitlesRequestDto.java b/src/main/java/dev/naman/productservice/dtos/CategoryDtoDB.java similarity index 50% rename from src/main/java/dev/naman/productservice/dtos/GetProductTitlesRequestDto.java rename to src/main/java/dev/naman/productservice/dtos/CategoryDtoDB.java index bb61e86..f34a63a 100644 --- a/src/main/java/dev/naman/productservice/dtos/GetProductTitlesRequestDto.java +++ b/src/main/java/dev/naman/productservice/dtos/CategoryDtoDB.java @@ -7,6 +7,8 @@ @Getter @Setter -public class GetProductTitlesRequestDto { - private List uuids; +public class CategoryDtoDB { + private String id; + private String name; + private List products; } diff --git a/src/main/java/dev/naman/productservice/dtos/ExceptionDto.java b/src/main/java/dev/naman/productservice/dtos/ExceptionDto.java index 8fe5a05..6fc7859 100644 --- a/src/main/java/dev/naman/productservice/dtos/ExceptionDto.java +++ b/src/main/java/dev/naman/productservice/dtos/ExceptionDto.java @@ -3,7 +3,6 @@ import lombok.Getter; import lombok.Setter; import org.springframework.http.HttpStatus; - @Getter @Setter public class ExceptionDto { diff --git a/src/main/java/dev/naman/productservice/dtos/GenericProductDto.java b/src/main/java/dev/naman/productservice/dtos/GenericProductDto.java index ea81d6c..75d5fc2 100644 --- a/src/main/java/dev/naman/productservice/dtos/GenericProductDto.java +++ b/src/main/java/dev/naman/productservice/dtos/GenericProductDto.java @@ -1,9 +1,6 @@ package dev.naman.productservice.dtos; - -import dev.naman.productservice.models.Category; import lombok.Getter; import lombok.Setter; - @Getter @Setter public class GenericProductDto { diff --git a/src/main/java/dev/naman/productservice/dtos/GenericProductDtoDB.java b/src/main/java/dev/naman/productservice/dtos/GenericProductDtoDB.java new file mode 100644 index 0000000..c0903dc --- /dev/null +++ b/src/main/java/dev/naman/productservice/dtos/GenericProductDtoDB.java @@ -0,0 +1,18 @@ +package dev.naman.productservice.dtos; + +import lombok.Getter; +import lombok.Setter; + +import java.util.UUID; +@Getter +@Setter +public class GenericProductDtoDB { + private UUID id; + private String title; + private String description; + private String image; + private String category; + private Double price; + private String currency; + +} diff --git a/src/main/java/dev/naman/productservice/dtos/ProductDto.java b/src/main/java/dev/naman/productservice/dtos/ProductDto.java index 9f2d39f..155734e 100644 --- a/src/main/java/dev/naman/productservice/dtos/ProductDto.java +++ b/src/main/java/dev/naman/productservice/dtos/ProductDto.java @@ -1,16 +1,15 @@ package dev.naman.productservice.dtos; -import dev.naman.productservice.models.Category; -import dev.naman.productservice.models.Price; -import jakarta.persistence.CascadeType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToOne; +import dev.pranay.productservice.models.Price; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter +@AllArgsConstructor +@NoArgsConstructor public class ProductDto { private String title; diff --git a/src/main/java/dev/naman/productservice/exception/ControllerAdvices.java b/src/main/java/dev/naman/productservice/exception/ControllerAdvices.java new file mode 100644 index 0000000..5f40dd9 --- /dev/null +++ b/src/main/java/dev/naman/productservice/exception/ControllerAdvices.java @@ -0,0 +1,14 @@ +package dev.naman.productservice.exception; + +import org.springframework.web.bind.annotation.ControllerAdvice; + +@ControllerAdvice +public class ControllerAdvices { + +// @ExceptionHandler(NotFoundException.class) +// private ResponseEntity handleNotfoundException(NotFoundException notFoundException){ +// return new ResponseEntity<> +// (new ExceptionDto(HttpStatus.NOT_FOUND, notFoundException.getMessage()), +// HttpStatus.NOT_FOUND); +// } +} diff --git a/src/main/java/dev/naman/productservice/exceptions/NotFoundException.java b/src/main/java/dev/naman/productservice/exception/NotFoundException.java similarity index 56% rename from src/main/java/dev/naman/productservice/exceptions/NotFoundException.java rename to src/main/java/dev/naman/productservice/exception/NotFoundException.java index 0986b0e..9517214 100644 --- a/src/main/java/dev/naman/productservice/exceptions/NotFoundException.java +++ b/src/main/java/dev/naman/productservice/exception/NotFoundException.java @@ -1,12 +1,13 @@ -package dev.naman.productservice.exceptions; +package dev.naman.productservice.exception; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(value = HttpStatus.NOT_FOUND) -public class NotFoundException extends Exception { +public class NotFoundException extends Exception{ - public NotFoundException(String message) { + public NotFoundException(String message){ super(message); } + } diff --git a/src/main/java/dev/naman/productservice/exceptions/ControllerAdvices.java b/src/main/java/dev/naman/productservice/exceptions/ControllerAdvices.java deleted file mode 100644 index a1c2361..0000000 --- a/src/main/java/dev/naman/productservice/exceptions/ControllerAdvices.java +++ /dev/null @@ -1,32 +0,0 @@ -package dev.naman.productservice.exceptions; - -import dev.naman.productservice.dtos.ExceptionDto; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; - -@ControllerAdvice -public class ControllerAdvices { -// @ExceptionHandler(NotFoundException.class) -// private ResponseEntity handleNotFoundException( -// NotFoundException notFoundException -// ) { -// -// return new ResponseEntity( -// new ExceptionDto(HttpStatus.NOT_FOUND, notFoundException.getMessage()), -// HttpStatus.NOT_FOUND -// ); -// } -// -// @ExceptionHandler(ArrayIndexOutOfBoundsException.class) -// private ResponseEntity handleArrayIndexOutOfBound( -// ArrayIndexOutOfBoundsException notFoundException -// ) { -// -// return new ResponseEntity( -// new ExceptionDto(HttpStatus.NOT_FOUND, notFoundException.getMessage()), -// HttpStatus.NOT_FOUND -// ); -// } -} diff --git a/src/main/java/dev/naman/productservice/models/BaseModel.java b/src/main/java/dev/naman/productservice/models/BaseModel.java index c8ada58..862edbb 100644 --- a/src/main/java/dev/naman/productservice/models/BaseModel.java +++ b/src/main/java/dev/naman/productservice/models/BaseModel.java @@ -1,16 +1,20 @@ package dev.naman.productservice.models; -import jakarta.persistence.*; +import jakarta.persistence.Column; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; +import lombok.Data; import org.hibernate.annotations.GenericGenerator; -import org.hibernate.id.factory.spi.GenerationTypeStrategy; import java.util.UUID; @MappedSuperclass +@Data public class BaseModel { @Id - @GeneratedValue(generator = "naman") - @GenericGenerator(name = "naman", strategy = "uuid2") + @GeneratedValue(generator = "uuidgenerator") + @GenericGenerator(name = "uuidgenerator", strategy = "uuid2") @Column(name = "id", columnDefinition = "binary(16)", nullable = false, updatable = false) private UUID uuid; } diff --git a/src/main/java/dev/naman/productservice/models/Category.java b/src/main/java/dev/naman/productservice/models/Category.java index 4fd8f7f..dd4f6c5 100644 --- a/src/main/java/dev/naman/productservice/models/Category.java +++ b/src/main/java/dev/naman/productservice/models/Category.java @@ -1,36 +1,22 @@ package dev.naman.productservice.models; -import jakarta.persistence.*; -import lombok.*; -import org.hibernate.annotations.Fetch; -import org.hibernate.annotations.FetchMode; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.OneToMany; +import lombok.Getter; +import lombok.Setter; -import java.util.ArrayList; import java.util.List; @Entity @Getter @Setter -@AllArgsConstructor -@NoArgsConstructor -public class Category extends BaseModel { +public class Category extends BaseModel{ @Column private String name; + @OneToMany(fetch = FetchType.EAGER, mappedBy = "category") + private List products; - @OneToMany(mappedBy = "category") - @Fetch(FetchMode.SELECT) - private List products = new ArrayList<>(); - // this is the same relation being mapped by category attribute in the other (Product) class } -// class Group { -// m:m -// List members; -// m:m -// List admins; -// -// 1----> 1 -// m<---- 1 -// m : 1 -// User creator; -// } \ No newline at end of file diff --git a/src/main/java/dev/naman/productservice/models/Order.java b/src/main/java/dev/naman/productservice/models/Order.java index 9ec7d46..08e9dbb 100644 --- a/src/main/java/dev/naman/productservice/models/Order.java +++ b/src/main/java/dev/naman/productservice/models/Order.java @@ -1,6 +1,9 @@ package dev.naman.productservice.models; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/dev/naman/productservice/models/Price.java b/src/main/java/dev/naman/productservice/models/Price.java index 2b0d1a3..3d3960a 100644 --- a/src/main/java/dev/naman/productservice/models/Price.java +++ b/src/main/java/dev/naman/productservice/models/Price.java @@ -11,7 +11,8 @@ @Setter @AllArgsConstructor @NoArgsConstructor -public class Price extends BaseModel { +public class Price extends BaseModel{ + String currency; double price; } diff --git a/src/main/java/dev/naman/productservice/models/Product.java b/src/main/java/dev/naman/productservice/models/Product.java index ad6621b..e205665 100644 --- a/src/main/java/dev/naman/productservice/models/Product.java +++ b/src/main/java/dev/naman/productservice/models/Product.java @@ -1,34 +1,27 @@ package dev.naman.productservice.models; -import jakarta.annotation.Nullable; import jakarta.persistence.*; -import lombok.*; -import org.hibernate.annotations.Fetch; -import org.hibernate.annotations.FetchMode; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; @Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor -public class Product extends BaseModel { - +public class Product extends BaseModel{ private String title; - private String description; - private String image; - // P : C - // => L to R: 1 : 1 - // => R to L: m : 1 - // => Ans: m : 1 + @ManyToOne(cascade = {CascadeType.PERSIST}) @JoinColumn(name = "category") private Category category; - @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.LAZY) -// @Fetch(FetchMode.JOIN) - private Price price; - private int inventoryCount; -// private double price; + @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) + 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..e0e8651 100644 --- a/src/main/java/dev/naman/productservice/repositories/CategoryRepository.java +++ b/src/main/java/dev/naman/productservice/repositories/CategoryRepository.java @@ -1,19 +1,23 @@ package dev.naman.productservice.repositories; -import dev.naman.productservice.models.Category; -import org.springframework.context.annotation.Lazy; +import dev.pranay.productservice.models.Category; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +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 List findAllById(Iterable uuids); + + Category findByName(String category); + + @Query(nativeQuery = true, value = "Select distinct c.name from category c") + List listCategoryByName(); + } diff --git a/src/main/java/dev/naman/productservice/repositories/CustomQueries.java b/src/main/java/dev/naman/productservice/repositories/CustomQueries.java index 161151e..23e637d 100644 --- a/src/main/java/dev/naman/productservice/repositories/CustomQueries.java +++ b/src/main/java/dev/naman/productservice/repositories/CustomQueries.java @@ -1,6 +1,10 @@ package dev.naman.productservice.repositories; public interface CustomQueries { + String GET_ALL_PRODUCT_BY_CATEGORY = "Select p.* from product p left join category c on p.category = c.id where c.name=:categoryName"; + String GET_ALL_PRODUCT_CATEGORY = "Select distinct c.name from product p left join category c on p.category = c.id"; String FIND_ALL_BY_TITLE = "select * from product join product_orders " + "on product.id = product_orders.product_id where title = :naman"; + + String FIND_ALL_PRODUCT = "SELECT DISTINCT p FROM Product p LEFT JOIN FETCH p.category LEFT JOIN FETCH p.price"; } diff --git a/src/main/java/dev/naman/productservice/repositories/PriceRepository.java b/src/main/java/dev/naman/productservice/repositories/PriceRepository.java index bd06799..62f4faf 100644 --- a/src/main/java/dev/naman/productservice/repositories/PriceRepository.java +++ b/src/main/java/dev/naman/productservice/repositories/PriceRepository.java @@ -1,8 +1,11 @@ package dev.naman.productservice.repositories; -import dev.naman.productservice.models.Price; +import dev.pranay.productservice.models.Price; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; +@Repository +public interface PriceRepository extends JpaRepository { -public interface PriceRepository -extends JpaRepository { } diff --git a/src/main/java/dev/naman/productservice/repositories/ProductRepository.java b/src/main/java/dev/naman/productservice/repositories/ProductRepository.java index c2bd45c..59d820c 100644 --- a/src/main/java/dev/naman/productservice/repositories/ProductRepository.java +++ b/src/main/java/dev/naman/productservice/repositories/ProductRepository.java @@ -1,20 +1,24 @@ package dev.naman.productservice.repositories; -import dev.naman.productservice.models.Category; -import dev.naman.productservice.models.Product; -import org.springframework.data.domain.Example; +import dev.pranay.productservice.models.Category; +import dev.pranay.productservice.models.Product; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.FluentQuery; import org.springframework.stereotype.Repository; import java.util.List; import java.util.UUID; -import java.util.function.Function; - @Repository -public interface ProductRepository -extends JpaRepository { +public interface ProductRepository extends JpaRepository { + @Query(value = CustomQueries.FIND_ALL_PRODUCT) + List findAllProducts(); + + + @Query(nativeQuery = true, value = CustomQueries.GET_ALL_PRODUCT_CATEGORY) + List getAllProductCategory(); + + @Query(nativeQuery = true, value = CustomQueries.GET_ALL_PRODUCT_BY_CATEGORY) + List getAllProductByCategory(String categoryName); Product findByTitleEquals(String title); @@ -33,7 +37,7 @@ public interface ProductRepository List readAllByTitleLike(String titleRegex); - List findAllByCategoryIn(List categories); + List findAllByCategoryIn(List categories); // @Query("select Product from Product where Product .category.uuid in :uuids") // List findAllByCategoryIn(List uuids); @@ -44,4 +48,5 @@ public interface ProductRepository // @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 deleted file mode 100644 index 91d2912..0000000 --- a/src/main/java/dev/naman/productservice/services/CategoryService.java +++ /dev/null @@ -1,10 +0,0 @@ -package dev.naman.productservice.services; - -import dev.naman.productservice.models.Category; - -import java.util.List; - -public interface CategoryService { - Category getCategory(String uuid); - List getProductTitles(List categoryUUIDs); -} diff --git a/src/main/java/dev/naman/productservice/services/CategoryServiceDB.java b/src/main/java/dev/naman/productservice/services/CategoryServiceDB.java new file mode 100644 index 0000000..3c12109 --- /dev/null +++ b/src/main/java/dev/naman/productservice/services/CategoryServiceDB.java @@ -0,0 +1,11 @@ +package dev.naman.productservice.services; + +import dev.pranay.productservice.models.Category; + +import java.util.List; + +public interface CategoryServiceDB { + Category getCategoryById(String uuid); + + List getAllCategories(); +} diff --git a/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java b/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java index b167a55..ad34b89 100644 --- a/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java +++ b/src/main/java/dev/naman/productservice/services/CategoryServiceImpl.java @@ -1,11 +1,9 @@ package dev.naman.productservice.services; -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 dev.pranay.productservice.models.Category; +import dev.pranay.productservice.repositories.CategoryRepository; +import dev.pranay.productservice.repositories.ProductRepository; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.RequestBody; import java.util.ArrayList; import java.util.List; @@ -13,18 +11,17 @@ import java.util.UUID; @Service -public class CategoryServiceImpl implements CategoryService { - private CategoryRepository categoryRepository; +public class CategoryServiceImpl implements CategoryServiceDB{ private final ProductRepository productRepository; + private final CategoryRepository categoryRepository; - public CategoryServiceImpl(CategoryRepository categoryRepository, - ProductRepository productRepository) { - this.categoryRepository = categoryRepository; + public CategoryServiceImpl(ProductRepository productRepository, CategoryRepository categoryRepository) { this.productRepository = productRepository; + this.categoryRepository = categoryRepository; } @Override - public Category getCategory(String uuid) { + public Category getCategoryById(String uuid) { Optional categoryOptional = categoryRepository.findById(UUID.fromString(uuid)); if (categoryOptional.isEmpty()) { @@ -33,47 +30,17 @@ public Category getCategory(String uuid) { Category category = categoryOptional.get(); - List products = category.getProducts(); - +// List products = category.getProducts(); return category; } - public List getProductTitles(List categoryUUIDs) { - List uuids = new ArrayList<>(); - - for (String uuid: categoryUUIDs) { - uuids.add(UUID.fromString(uuid)); - } -// -// List categories = categoryRepository.findAllById(uuids); -// -// -// List titles = new ArrayList<>(); -// -// categories.forEach( -// category -> { -// category.getProducts().forEach( -// product -> { -// titles.add(product.getTitle()); -// } -// ); -// } -// ); -// -// -// return titles; - - List categories = categoryRepository.findAllById(uuids); - - List products = productRepository.findAllByCategoryIn(categories); - - List titles = new ArrayList<>(); - - for (Product p: products) { - titles.add(p.getTitle()); - } - - return titles; + @Override + public List getAllCategories() { + List categories = new ArrayList<>(); + List categoryList = categoryRepository.findAll(); + for(Category category : categoryList) + categories.add(category.getName()); + return categories; } } diff --git a/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java b/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java index d629446..e35ee74 100644 --- a/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java +++ b/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java @@ -1,22 +1,19 @@ package dev.naman.productservice.services; -import dev.naman.productservice.dtos.GenericProductDto; -import dev.naman.productservice.exceptions.NotFoundException; -import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoreProductDto; -import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoryProductServiceClient; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Repository; +import dev.pranay.productservice.dtos.GenericProductDto; +import dev.pranay.productservice.exception.NotFoundException; +import dev.pranay.productservice.thirdpartyclients.productservice.fakestore.FakeStoreProductDto; +import dev.pranay.productservice.thirdpartyclients.productservice.fakestore.FakeStoreProductServiceClient; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; -@Repository("fakeStoreProductService") -public class FakeStoreProductService implements ProductService { +@Service("fakeStoreProductService") +public class FakeStoreProductService implements ProductService{ - private FakeStoryProductServiceClient fakeStoryProductServiceClient; + private FakeStoreProductServiceClient fakeStoreProductServiceClient; private GenericProductDto convertFakeStoreProductIntoGenericProduct(FakeStoreProductDto fakeStoreProductDto) { @@ -31,34 +28,41 @@ private GenericProductDto convertFakeStoreProductIntoGenericProduct(FakeStorePro return product; } - public FakeStoreProductService(FakeStoryProductServiceClient fakeStoryProductServiceClient) { - this.fakeStoryProductServiceClient = fakeStoryProductServiceClient; + + public FakeStoreProductService(FakeStoreProductServiceClient fakeStoreProductServiceClient) { + this.fakeStoreProductServiceClient = fakeStoreProductServiceClient; + } + + public GenericProductDto getProductById(Long id) throws NotFoundException { + return convertFakeStoreProductIntoGenericProduct(fakeStoreProductServiceClient.getProductById(id)); } - @Override public GenericProductDto createProduct(GenericProductDto product) { - return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.createProduct(product)); + return convertFakeStoreProductIntoGenericProduct(fakeStoreProductServiceClient.createProduct(product)); } - @Override - public GenericProductDto getProductById(Long id) throws NotFoundException { - System.out.println("In product service"); - return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.getProductById(id)); - } - @Override public List getAllProducts() { List genericProductDtos = new ArrayList<>(); - for (FakeStoreProductDto fakeStoreProductDto: fakeStoryProductServiceClient.getAllProducts()) { + for(FakeStoreProductDto fakeStoreProductDto : fakeStoreProductServiceClient.getAllProducts()){ genericProductDtos.add(convertFakeStoreProductIntoGenericProduct(fakeStoreProductDto)); } return genericProductDtos; } - @Override + public GenericProductDto deleteProduct(Long id) { - return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.deleteProduct(id)); + return convertFakeStoreProductIntoGenericProduct(fakeStoreProductServiceClient.deleteProduct(id)); + } + + + @Override + public GenericProductDto updateProductById(Long id, GenericProductDto product) { + return convertFakeStoreProductIntoGenericProduct(fakeStoreProductServiceClient.updateProductById(id,product)); + } + + } diff --git a/src/main/java/dev/naman/productservice/services/ProductService.java b/src/main/java/dev/naman/productservice/services/ProductService.java index bc5eb10..d582751 100644 --- a/src/main/java/dev/naman/productservice/services/ProductService.java +++ b/src/main/java/dev/naman/productservice/services/ProductService.java @@ -1,17 +1,17 @@ package dev.naman.productservice.services; -import dev.naman.productservice.dtos.GenericProductDto; -import dev.naman.productservice.exceptions.NotFoundException; +import dev.pranay.productservice.dtos.GenericProductDto; +import dev.pranay.productservice.exception.NotFoundException; import java.util.List; public interface ProductService { - GenericProductDto createProduct(GenericProductDto product); - GenericProductDto getProductById(Long id) throws NotFoundException; + GenericProductDto createProduct(GenericProductDto product); List getAllProducts(); - GenericProductDto deleteProduct(Long id); + GenericProductDto deleteProduct(Long id); + GenericProductDto updateProductById(Long id, GenericProductDto Product); } diff --git a/src/main/java/dev/naman/productservice/services/ProductServiceDB.java b/src/main/java/dev/naman/productservice/services/ProductServiceDB.java new file mode 100644 index 0000000..306c60c --- /dev/null +++ b/src/main/java/dev/naman/productservice/services/ProductServiceDB.java @@ -0,0 +1,20 @@ +package dev.naman.productservice.services; + +import dev.pranay.productservice.dtos.GenericProductDtoDB; +import dev.pranay.productservice.exception.NotFoundException; + +import java.util.List; +import java.util.UUID; + +public interface ProductServiceDB { + GenericProductDtoDB createProduct(GenericProductDtoDB product); + + GenericProductDtoDB getProductById(UUID id)throws NotFoundException; + + List getAllProducts(); + + GenericProductDtoDB deleteProductById(UUID id)throws NotFoundException;; + + GenericProductDtoDB updateProductById(GenericProductDtoDB genericProductDto,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 2cb1071..a6ed6a0 100644 --- a/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java +++ b/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java @@ -1,42 +1,121 @@ package dev.naman.productservice.services; -import dev.naman.productservice.dtos.GenericProductDto; -import dev.naman.productservice.models.Product; -import dev.naman.productservice.repositories.ProductRepository; +import dev.pranay.productservice.dtos.GenericProductDtoDB; +import dev.pranay.productservice.exception.NotFoundException; +import dev.pranay.productservice.models.Category; +import dev.pranay.productservice.models.Price; +import dev.pranay.productservice.models.Product; +import dev.pranay.productservice.repositories.CategoryRepository; +import dev.pranay.productservice.repositories.PriceRepository; +import dev.pranay.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 class SelfProductServiceImpl implements ProductServiceDB{ + private final ProductRepository productRepository; + private final CategoryRepository categoryRepository; + private final PriceRepository priceRepository; - public SelfProductServiceImpl(ProductRepository productRepository) { + public SelfProductServiceImpl(ProductRepository productRepository, CategoryRepository categoryRepository, PriceRepository priceRepository) { this.productRepository = productRepository; + this.categoryRepository = categoryRepository; + this.priceRepository = priceRepository; + } + private GenericProductDtoDB convertProductToGenericProduct(Product product) { + + GenericProductDtoDB genericProductDtoDB = new GenericProductDtoDB(); + genericProductDtoDB.setId(product.getUuid()); + genericProductDtoDB.setImage(product.getImage()); + genericProductDtoDB.setDescription(product.getDescription()); + genericProductDtoDB.setTitle(product.getTitle()); + genericProductDtoDB.setPrice(product.getPrice().getPrice()); + genericProductDtoDB.setCategory(product.getCategory().getName()); + genericProductDtoDB.setCurrency(product.getPrice().getCurrency()); + + return genericProductDtoDB; } + @Override - public GenericProductDto getProductById(Long id) { - System.out.println("In product service"); + public GenericProductDtoDB createProduct(GenericProductDtoDB genericProductDtoDB) { + Category category = categoryRepository.findByName(genericProductDtoDB.getCategory()); + if(category == null){ + category = new Category(); + category.setName(genericProductDtoDB.getCategory()); + categoryRepository.save(category); + } + Product product = new Product(); + String currency = (genericProductDtoDB.getCurrency() != null) ? genericProductDtoDB.getCurrency() : "USD"; + Price price = new Price(currency, genericProductDtoDB.getPrice()); + + product.setImage(genericProductDtoDB.getImage()); + product.setDescription(genericProductDtoDB.getDescription()); + product.setCategory(category); + product.setPrice(price); + product.setImage(genericProductDtoDB.getImage()); + product.setCategory(category); + product.setTitle(genericProductDtoDB.getTitle()); - return new GenericProductDto(); + priceRepository.save(price); + Product savedProduct = productRepository.save(product); + return convertProductToGenericProduct(savedProduct); } @Override - public GenericProductDto createProduct(GenericProductDto product) { - return null; + public GenericProductDtoDB getProductById(UUID id) throws NotFoundException { + Optional product = productRepository.findById(id); + if (product.isEmpty()) { + return null; + } + return convertProductToGenericProduct(product.get()); } @Override - public List getAllProducts() { - return null; + public List getAllProducts() { + List products = productRepository.findAll(); + List product = new ArrayList<>(); + products.forEach(prod -> { + GenericProductDtoDB genericproduct = convertProductToGenericProduct(prod); + product.add(genericproduct); + }); + return product; } @Override - public GenericProductDto deleteProduct(Long id) { - return null; + public GenericProductDtoDB deleteProductById(UUID id) throws NotFoundException { + Optional product = productRepository.findById(id); + if (product.isEmpty()) { + throw new NotFoundException("Product with ID " + id + " not found"); + } + Product productInfo = product.get(); + productRepository.delete(productInfo); + return convertProductToGenericProduct(productInfo); + } + + @Override + public GenericProductDtoDB updateProductById(GenericProductDtoDB genericProductDto, UUID id) { + + //Product product = ProductConvertor.getProductDto(genericProductDto); + Product product = productRepository.getById(id); + + product.setTitle(genericProductDto.getTitle()); + product.setDescription(genericProductDto.getDescription()); + Category category = new Category(); + category.setName(genericProductDto.getCategory()); + product.setCategory(category); + Price price = new Price(); + price.setPrice(genericProductDto.getPrice()); + product.setPrice(price); + product.setImage(genericProductDto.getImage()); + + productRepository.save(product); + return convertProductToGenericProduct(product); } }