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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<version>9.22.2</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
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;
import dev.naman.productservice.models.Product;

import dev.naman.productservice.exceptions.NotFoundException;
import dev.naman.productservice.services.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/categories")
public class CategoryController {

@Autowired
private CategoryService categoryService;

public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}

// #4 - Get in category
@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
public CategoryDto getCategory(@PathVariable("uuid") String uuid) throws NotFoundException {
return categoryService.getCategory(uuid);

}


// #3 - Get all Categories
@GetMapping
public List<CategoryDto> getAllCategory(@RequestParam("name") String name) throws NotFoundException {
if (StringUtils.isNotEmpty(name)) {
// #4.1 - Get in category by Name
return categoryService.getCategoryByName(name);
}

return productDtos;
return categoryService.getAllCategories();
}

@GetMapping("/titles/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
import dev.naman.productservice.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -14,60 +15,44 @@
@RestController
@RequestMapping("/products")
public class ProductController {
// @Autowired
// field injection
@Autowired
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 {}
// #1 - Get All Products
@GetMapping
public List<GenericProductDto> getAllProducts() {
return productService.getAllProducts();
}

// localhost:8080/products/{id}
// localhost:8080/products/123
// #2 - Get a single Product
@GetMapping("{id}")
public GenericProductDto getProductById(@PathVariable("id") Long id) throws NotFoundException {
GenericProductDto productDto = productService.getProductById(id);
if (productDto == null) {
// throw NotFoundException
}
return productDto;
public GenericProductDto getProductById(@PathVariable("id") String id) throws NotFoundException {
return productService.getProductById(id);
}

// #7 - Delete a product
@DeleteMapping("{id}")
public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") Long id) {
public ResponseEntity<String> deleteProductById(@PathVariable("id") String id) throws NotFoundException {
return new ResponseEntity<>(
productService.deleteProduct(id),
HttpStatus.OK
);
}

// #5 - Add new Product
@PostMapping
public GenericProductDto createProduct(@RequestBody GenericProductDto product) {
public String createProduct(@RequestBody GenericProductDto product) {
// System.out.println(product.name);
return productService.createProduct(product);
}

// #6 - Update a product
@PutMapping("{id}")
public void updateProductById() {

public void updateProductById(@RequestBody GenericProductDto product) throws NotFoundException {
productService.updateProduct(product);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
@Setter
public class GenericProductDto {
private Long id;
private String id;
private String title;
private String description;
private String image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public class ProductDto {
// => L to R: 1 : 1
// => R to L: m : 1
// => Ans: m : 1
private Price price;
private double price;
}
32 changes: 32 additions & 0 deletions src/main/java/dev/naman/productservice/mapper/CategoryMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.naman.productservice.mapper;

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 java.util.List;

public class CategoryMapper {

public static CategoryDto convertCategoryEntityToCategoryDto(Category category) {

CategoryDto categoryDto = new CategoryDto();
categoryDto.setName(category.getName());
List<ProductDto> productDtoList = category.getProducts().stream()
.map(CategoryMapper::convertCategoryProductToProduct).toList();

categoryDto.setProducts(productDtoList);

return categoryDto;
}
private static ProductDto convertCategoryProductToProduct(Product product) {
ProductDto productDto = new ProductDto();
productDto.setTitle(product.getTitle());
productDto.setDescription(product.getDescription());
productDto.setImage(product.getImage());
productDto.setPrice(product.getPrice().getPrice());

return productDto;
}
}
46 changes: 46 additions & 0 deletions src/main/java/dev/naman/productservice/mapper/ProductMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.naman.productservice.mapper;

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 java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ProductMapper {

public static GenericProductDto convertProductEntityToGenericProduct(Product product) {

GenericProductDto productDto = new GenericProductDto();
productDto.setId(product.getUuid().toString());
productDto.setTitle(product.getTitle());
productDto.setDescription(product.getDescription());
productDto.setImage(product.getImage());
productDto.setCategory(product.getCategory().getName());
productDto.setPrice(product.getPrice().getPrice());
return productDto;
}

public static Product convertProductDtoToProductEntity(GenericProductDto productDto, Optional<Category> categoryOptional) {
Product product = new Product();
product.setTitle(productDto.getTitle());
product.setDescription(productDto.getDescription());
product.setImage(productDto.getImage());

product.setCategory(getProductCategory(productDto.getCategory(), categoryOptional, product));
product.setPrice(new Price("INR", productDto.getPrice()));

return product;
}

public static Category getProductCategory(String categoryName, Optional<Category> categoryOptional, Product product) {
Category category = categoryOptional.isPresent()? categoryOptional.get():new Category(categoryName, new ArrayList<>());
List<Product> categoryProductList = category.getProducts();
categoryProductList.add(product);
category.setProducts(categoryProductList);

return category;
}
}
5 changes: 4 additions & 1 deletion src/main/java/dev/naman/productservice/models/BaseModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/dev/naman/productservice/models/Product.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface CategoryRepository

Optional<Category> findById(UUID uuid);

Optional<Category> findByName(String name);

@Override
List<Category> findAllById(Iterable<UUID> uuids);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package dev.naman.productservice.services;

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

import java.util.List;

public interface CategoryService {
Category getCategory(String uuid);

CategoryDto getCategory(String uuid) throws NotFoundException;
List<String> getProductTitles(List<String> categoryUUIDs);

List<CategoryDto> getAllCategories();

List<CategoryDto> getCategoryByName(String name) throws NotFoundException;
}
Loading