-
Notifications
You must be signed in to change notification settings - Fork 0
Controller + metrics #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
635b32d
feat: updated product model
FCastillo-pixel 675523c
feat: added ProductDTO
FCastillo-pixel 77174ee
feat: added metrics to backend
FCastillo-pixel 66f3018
feat: added Controller
FCastillo-pixel fedb3d0
fix: added missing ;
FCastillo-pixel 773f52b
fix: corrected typo
FCastillo-pixel 1da6dba
fix: corrected typos
FCastillo-pixel 3307eff
fix: should be running now
FCastillo-pixel c388c03
feat: added a basic test
FCastillo-pixel d091ac7
feat: basic test + nonworking swagger
FCastillo-pixel 0931c96
feat: added services
FCastillo-pixel b82dac3
feat: added basic table
FCastillo-pixel cce5f29
feat: added filters
FCastillo-pixel 3176528
feat: added new endpoint to get categories
FCastillo-pixel 714bc41
feat: added pagination
FCastillo-pixel 7d5d07e
feat: added new endpoint to get all available pages
FCastillo-pixel 6a944c3
fix: updated pagination
FCastillo-pixel 0f7937f
feat: added creation and edit modal
FCastillo-pixel 6f9997f
fix: fixed tailwind config
FCastillo-pixel 2fc697d
fix: added an .env
FCastillo-pixel 6821239
fix: fixed warnings
FCastillo-pixel 2029024
fix
FCastillo-pixel db72b3d
fix: changed running port
FCastillo-pixel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
backend/src/main/java/com/inventory/backend/controller/ProductController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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
62
backend/src/main/java/com/inventory/backend/dto/ProductDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
30
backend/src/main/java/com/inventory/backend/dto/ProductPage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
backend/src/main/java/com/inventory/backend/model/InventoryMetrics.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.