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
@@ -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<ProductDto> getCategory(@PathVariable("uuid") String uuid) {
List<Product> products = categoryServiceDB.getCategoryById(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);
}

return productDtos;
}

// get all categories
@GetMapping("/all")
public List<String> getAllCategories(){

return categoryServiceDB.getAllCategories();
}
}
Original file line number Diff line number Diff line change
@@ -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<GenericProductDto> 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<GenericProductDto> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<GenericProductDtoDB> 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<GenericProductDtoDB> 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));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

@Getter
@Setter
public class GetProductTitlesRequestDto {
private List<String> uuids;
public class CategoryDtoDB {
private String id;
private String name;
private List<GenericProductDtoDB> products;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;

@Getter
@Setter
public class ExceptionDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
11 changes: 5 additions & 6 deletions src/main/java/dev/naman/productservice/dtos/ProductDto.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Loading