Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
10 changes: 9 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -46,6 +45,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.inventory.backend.controller;

import com.inventory.backend.dto.ProductDTO;
import com.inventory.backend.dto.ProductPage;
import com.inventory.backend.model.InventoryMetrics;
import com.inventory.backend.model.Product;
import com.inventory.backend.service.ProductService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/products")
@CrossOrigin(origins = "http://localhost:8080")
public class ProductController {
private final ProductService service;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a best practice, Service and DAO classes should depend on interfaces rather than concrete implementations to promote low coupling and flexibility.


public ProductController(ProductService service) {
this.service = service;
}

// GET /products
@GetMapping
public ProductPage getProducts(
@RequestParam Optional<String> name,
@RequestParam Optional<List<String>> category,
@RequestParam Optional<Boolean> availability,
@RequestParam Optional<String> sortBy,
@RequestParam Optional<String> sortBy2,
@RequestParam(defaultValue = "true") boolean asc,
@RequestParam(defaultValue = "true") boolean asc2,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size
){
Set<String> categorySet = category.map(HashSet::new).orElse(null);
return service.getFilteredProductsPage(
name,
Optional.ofNullable(categorySet),
availability,
sortBy,
sortBy2,
asc,
asc2,
page,
size
);
}

// POST /products
@PostMapping
public Product createProduct(@RequestBody @Valid ProductDTO dto) {
return service.createProduct(dto);
}

// PUT /products/{id}
@PutMapping("/{id}")
public Product updateProduct(@PathVariable UUID id, @RequestBody @Valid ProductDTO dto) {
return service.updateProduct(id,dto);
}

// DELETE /products/{id}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable UUID id) {
service.deleteProduct(id);
}

// POST /products/{id}/outofstock
@PostMapping("/{id}/outofstock")
public void markOutOfStock(@PathVariable UUID id) {
service.markOutOfStock(id);
}

// PUT /products/{id}/instock?defaultQuantity=10
@PutMapping("/{id}/instock")
public void markInStock(@PathVariable UUID id, @RequestParam(defaultValue = "10") int defaultQuantity) {
service.markInStock(id, defaultQuantity);
}

// GET /products/metrics
@GetMapping("/metrics")
public InventoryMetrics getMetrics() {
return service.getMetrics();
}

@GetMapping("/categories")
public Set<String> getCategories(){
return service.getAllCategories();
}
}
62 changes: 62 additions & 0 deletions backend/src/main/java/com/inventory/backend/dto/ProductDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.inventory.backend.dto;

import jakarta.validation.constraints.*;
import java.time.LocalDate;

public class ProductDTO {
@NotBlank(message = "Name is required")
@Size(max = 120, message = "Name must not exceed 120 characters")
private String name;

@NotBlank(message = "Category is required")
private String category;

@NotNull(message = "Unit price is required")
@PositiveOrZero(message = "Unit price must be 0 or more")
private Double unitPrice;

@PositiveOrZero(message = "Stock must be 0 or more")
private Integer quantityInStock;

private LocalDate expirationDate;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public Double getUnitPrice() {
return unitPrice;
}

public void setUnitPrice(Double unitPrice) {
this.unitPrice = unitPrice;
}

public Integer getQuantityInStock() {
return quantityInStock;
}

public void setQuantityInStock(Integer quantityInStock) {
this.quantityInStock = quantityInStock;
}

public LocalDate getExpirationDate() {
return expirationDate;
}

public void setExpirationDate(LocalDate expirationDate) {
this.expirationDate = expirationDate;
}
}
30 changes: 30 additions & 0 deletions backend/src/main/java/com/inventory/backend/dto/ProductPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.inventory.backend.dto;

import com.inventory.backend.model.Product;
import java.util.List;

public class ProductPage {
private List<Product> items;
private long total;

public ProductPage(List<Product> items, long total) {
this.items = items;
this.total = total;
}

public List<Product> getItems(){
return items;
}

public void setItems(List<Product> items) {
this.items = items;
}

public long getTotal() {
return total;
}

public void setTotal(long total) {
this.total = total;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.inventory.backend.model;

import java.util.*;
import java.util.stream.Collectors;

public class InventoryMetrics {
private int totalInStock;
private double totalValue;
private double averagePrice;

private Map<String, CategoryMetrics> byCategory;

public InventoryMetrics() {}

public InventoryMetrics(int totalInStock, double totalValue, double averagePrice, Map<String, CategoryMetrics> byCategory) {
this.totalInStock = totalInStock;
this.totalValue = totalValue;
this.averagePrice = averagePrice;
this.byCategory = byCategory;
}

public static InventoryMetrics fromProductList(List<Product> products) {
int totalStock = products.stream().mapToInt(Product::getQuantityInStock).sum();

double totalValue = products.stream()
.mapToDouble(Product::getTotalValue)
.sum();

List<Product> inStock = products.stream()
.filter(p -> p.getQuantityInStock() > 0)
.collect(Collectors.toList());

double averagePrice = inStock.isEmpty() ? 0:
inStock.stream().mapToDouble(Product::getUnitPrice).average().orElse(0);

Map<String, CategoryMetrics> categoryMetrics = products.stream()
.collect(Collectors.groupingBy(
Product::getCategory,
Collectors.collectingAndThen(Collectors.toList(), CategoryMetrics::fromProductList)
));
return new InventoryMetrics(totalStock, totalValue, averagePrice, categoryMetrics);
}

public int getTotalInStock() {
return totalInStock;
}

public void setTotalInStock(int totalInStock) {
this.totalInStock = totalInStock;
}

public double getTotalValue() {
return totalValue;
}

public void setTotalValue(double totalValue) {
this.totalValue = totalValue;
}

public double getAveragePrice() {
return averagePrice;
}

public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}

public Map<String, CategoryMetrics> getByCategory() {
return byCategory;
}

public void setByCategory(Map<String, CategoryMetrics> byCategory) {
this.byCategory = byCategory;
}

public static class CategoryMetrics {
private int inStock;
private double totalValue;
private double averagePrice;

public static CategoryMetrics fromProductList(List<Product> products) {
int stock = products.stream().mapToInt(Product::getQuantityInStock).sum();
double value = products.stream().mapToDouble(Product::getTotalValue).sum();

List<Product> inStock = products.stream().filter(p -> p.getQuantityInStock() > 0).toList();
double avg = inStock.isEmpty() ? 0 :
inStock.stream().mapToDouble(Product::getUnitPrice).average().orElse(0);

return new CategoryMetrics(stock, value, avg);
}

public CategoryMetrics() {}

public CategoryMetrics(int inStock, double totalValue, double averagePrice) {
this.inStock = inStock;
this.totalValue = totalValue;
this.averagePrice = averagePrice;
}

public double getTotalValue() {
return totalValue;
}

public void setTotalValue(double totalValue) {
this.totalValue = totalValue;
}

public double getAveragePrice() {
return averagePrice;
}

public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}

public int getInStock() {
return inStock;
}

public void setInStock(int inStock) {
this.inStock = inStock;
}
}
}
Loading
Loading