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,49 +1,28 @@
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.GenericCategoryDto;
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 java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/categories")
public class CategoryController {
private CategoryService categoryService;
private final CategoryService categoryService;

public CategoryController(CategoryService categoryService) {
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<GenericCategoryDto> getAllCategories() throws NotFoundException{
return categoryService.getAllCategories();
}

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

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

return categoryService.getProductTitles(uuids);
@GetMapping("/{id}")
public List<GenericProductDto> getProductsByCategory(@PathVariable("id") String id) throws NotFoundException{
return categoryService.getProductByCategory(UUID.fromString(id));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,73 +1,58 @@
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 dev.naman.productservice.services.SelfProductServiceImpl;
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;
// ....;
// ...;


//field injection. Not recommended.

// constructor injection
// @Autowired
public ProductController(ProductService productService) {
//Dependency Injection. Here, ProductService which is a dependency is being injected
private ProductService productService;
private SelfProductServiceImpl selfProductService;
//constructor injection. This is recommended practice.
public ProductController(@Qualifier("fakeStoreProductService") ProductService productService, SelfProductServiceImpl selfProductService){ //if there are more than one implementation for the ProductService interface,
//Spring wouldn't know which one to use. @Qualifier tells Spring which one to use!
this.productService = productService;
this.selfProductService = selfProductService;
}
//

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

// GET /products {}
@GetMapping
public List<GenericProductDto> getAllProducts() {
return productService.getAllProducts();
public List<GenericProductDto> getAllProducts() throws NotFoundException {
return selfProductService.getAllProducts();
}

// 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 NotFoundException
}
return productDto;
@GetMapping("/{id}")
public GenericProductDto getProductById(@PathVariable("id") String id) throws NotFoundException{
return selfProductService.getProductById(UUID.fromString(id));
}

@DeleteMapping("{id}")
public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") Long id) {
return new ResponseEntity<>(
productService.deleteProduct(id),
HttpStatus.OK
);
@DeleteMapping("/{id}")
public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") String id) throws NotFoundException {
return new ResponseEntity<>(selfProductService.deleteProductById(UUID.fromString(id)), HttpStatus.NOT_FOUND);
}

//@RequestBody converts whatever is in the request body to GenericProductDto
@PostMapping
public GenericProductDto createProduct(@RequestBody GenericProductDto product) {
// System.out.println(product.name);
return productService.createProduct(product);
public GenericProductDto createProduct(@RequestBody GenericProductDto genericProductDto){
return selfProductService.createProduct(genericProductDto);
}

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

@PutMapping("/{id}")
public GenericProductDto updateProductById(
@PathVariable("id") String id, @RequestBody GenericProductDto genericProduct) throws NotFoundException{
return selfProductService.updateProductById(UUID.fromString(id),genericProduct);
}
}
}
14 changes: 0 additions & 14 deletions src/main/java/dev/naman/productservice/dtos/CategoryDto.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.naman.productservice.dtos;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GenericCategoryDto {
String name;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.naman.productservice.dtos;

import dev.naman.productservice.models.Category;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GenericProductDto {
private Long id;
private String id;
private String title;
private String description;
private String image;
private String category;
private double price;
}
private String currency;
}
8 changes: 5 additions & 3 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.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.id.factory.spi.GenerationTypeStrategy;

import java.util.UUID;

@Getter
@Setter
@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;
}
}
18 changes: 2 additions & 16 deletions src/main/java/dev/naman/productservice/models/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import java.util.ArrayList;
import java.util.List;

@Entity
Expand All @@ -19,18 +18,5 @@ public class Category extends BaseModel {

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

// this is the same relation being mapped by category attribute in the other (Product) class
}
// class Group {
// m:m
// List<User> members;
// m:m
// List<User> admins;
//
// 1----> 1
// m<---- 1
// m : 1
// User creator;
// }
private List<Product> products;
}
24 changes: 5 additions & 19 deletions src/main/java/dev/naman/productservice/models/Product.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package dev.naman.productservice.models;

import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Getter
@Setter
@Entity //Associates Product with DB
@NoArgsConstructor
@AllArgsConstructor
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")
@JoinTable(name = "category_id")
private Category category;

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

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> {

Optional<Category> findById(UUID uuid);

@Override
List<Category> findAllById(Iterable<UUID> uuids);
}
@Repository
public interface CategoryRepository extends JpaRepository<Category, UUID> {
public <S extends Category> S save(S entity);
public Optional<Category> findById(UUID uuid);
public Optional<Category> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
package dev.naman.productservice.repositories;

import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Product;
import org.springframework.data.domain.Example;
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.Optional;
import java.util.UUID;
import java.util.function.Function;

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

Product findByTitleEquals(String title);

Product findByTitleEqualsAndPrice_PriceOrderByPrice_price(String title, double price);

List<Product> findAllByPrice_Currency(String currency);

@Override
void delete(Product entity);

long countAllByPrice_Currency(String currency);


List<Product> findAllByTitleLike(String titleRegex);

List<Product> readAllByTitleLike(String titleRegex);


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

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


@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);
}
public interface ProductRepository extends JpaRepository<Product, UUID> {
<S extends Product> S save (S entity);
List<Product> findAll();
Optional<Product> findById(UUID uuid);
void deleteById(UUID id);
List<Product> findAllByCategory_Name(String name);
}
Loading