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
Expand Up @@ -80,7 +80,7 @@ public void run(String... args) throws Exception {

productRepository.save(product);

productRepository.deleteById(UUID.fromString("95672ed6-3127-4248-ae33-97a261c0a6f4"));
//productRepository.deleteById(UUID.fromString("95672ed6-3127-4248-ae33-97a261c0a6f4"));

// Category category1 = categoryRepository.findById(UUID.fromString("5e6f679e-f326-44ae-b220-544b3822ab00")).get();
// System.out.println("Category name is: " + category1.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/products")
Expand Down Expand Up @@ -48,6 +49,11 @@ public GenericProductDto getProductById(@PathVariable("id") Long id) throws NotF
return productService.getProductById(id);
}

@GetMapping("/uuid/{uuid}")
public GenericProductDto getProductByUUID(@PathVariable("uuid")UUID uuid) throws NotFoundException{
return productService.getProductById(uuid);
}

@DeleteMapping("{id}")
public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") Long id) {
return new ResponseEntity<>(
Expand All @@ -56,6 +62,14 @@ public ResponseEntity<GenericProductDto> deleteProductById(@PathVariable("id") L
);
}

@DeleteMapping("/uuid/{uuid}")
public ResponseEntity<GenericProductDto> deleteProductByUUID(@PathVariable("uuid") UUID uuid){
return new ResponseEntity<GenericProductDto>(
productService.deleteProduct(uuid),
HttpStatus.OK
);
}

@PostMapping
public GenericProductDto createProduct(@RequestBody GenericProductDto product) {
// System.out.println(product.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Getter
@Setter
public class GenericProductDto {
private Long id;
private UUID uuid;
private String title;
private String description;
private String image;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/naman/productservice/models/BaseModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package dev.naman.productservice.repositories;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.models.Product;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.stereotype.Repository;

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

@Repository
public interface ProductRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

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

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

Expand Down Expand Up @@ -41,6 +42,11 @@ public GenericProductDto createProduct(GenericProductDto product) {
return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.createProduct(product));
}

@Override
public GenericProductDto getProductById(UUID uuid) throws NotFoundException {
return null;
}

@Override
public GenericProductDto getProductById(Long id) throws NotFoundException {
return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.getProductById(id));
Expand All @@ -56,6 +62,11 @@ public List<GenericProductDto> getAllProducts() {
return genericProductDtos;
}

@Override
public GenericProductDto deleteProduct(UUID uuid) {
return null;
}

@Override
public GenericProductDto deleteProduct(Long id) {
return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.deleteProduct(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import dev.naman.productservice.exceptions.NotFoundException;

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

public interface ProductService {

GenericProductDto createProduct(GenericProductDto product);

GenericProductDto getProductById(UUID uuid) throws NotFoundException;
GenericProductDto getProductById(Long id) throws NotFoundException;

List<GenericProductDto> getAllProducts();

GenericProductDto deleteProduct(Long id);
GenericProductDto deleteProduct(UUID uuid);
GenericProductDto deleteProduct(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,94 @@
package dev.naman.productservice.services;

import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
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.Repository;
import org.springframework.stereotype.Service;

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

@Service("selfProductServiceImpl")
@Primary
@Repository("selfProductServiceImpl")
public class SelfProductServiceImpl implements ProductService {

ProductRepository productRepository;
public SelfProductServiceImpl(ProductRepository productRepository){
this.productRepository = productRepository;
}
@Override
public GenericProductDto getProductById(UUID uuid) {
Product product = productRepository.findById(uuid).get();

return GetGenericProductDto(product);
}

@Override
public GenericProductDto getProductById(Long id) {
public GenericProductDto getProductById(Long id) throws NotFoundException {
return null;
}

@Override
public GenericProductDto createProduct(GenericProductDto product) {
return null;
Product product1 = new Product();


product1.setTitle(product.getTitle());
product1.setDescription(product.getDescription());
Category category = new Category();
category.setName(product.getCategory());
product1.setCategory(category);
Price price = new Price();
price.setPrice(product.getPrice());
product1.setPrice(price);
product1.setImage(product.getImage());

productRepository.save(product1);

return GetGenericProductDto(product1);
}

@Override
public List<GenericProductDto> getAllProducts() {
return null;
List<GenericProductDto> genericProductDtos = new ArrayList<>();

List<Product> products = productRepository.findAll();

for(Product product : products){
genericProductDtos.add(GetGenericProductDto(product));
}

return genericProductDtos;
}

@Override
public GenericProductDto deleteProduct(UUID uuid) {

productRepository.deleteById(uuid);
return new GenericProductDto();
}

@Override
public GenericProductDto deleteProduct(Long id) {
return null;
}

private GenericProductDto GetGenericProductDto(Product product){
GenericProductDto genericProductDto = new GenericProductDto();

genericProductDto.setUuid(product.getUuid());
genericProductDto.setTitle(product.getTitle());
genericProductDto.setDescription(product.getDescription());
genericProductDto.setCategory(product.getCategory().getName());
genericProductDto.setPrice(product.getPrice().getPrice());
genericProductDto.setImage(product.getImage());

return genericProductDto;
}
}