Draft and New Arrivals pages added

This commit is contained in:
Sidharth Prabhu
2026-04-20 10:44:08 +05:30
parent 592abf03a1
commit e58baba908
7 changed files with 520 additions and 0 deletions

View File

@@ -33,6 +33,11 @@ public class ProductController {
return productRepository.findAllWithStalls(org.springframework.data.domain.PageRequest.of(page, size));
}
@GetMapping("/drafts")
public List<Product> getDraftProducts() {
return productRepository.findByIsDraftTrue();
}
@GetMapping("/category/{categoryName}")
public List<Product> getProductsByCategory(@PathVariable String categoryName) {
return productRepository.findByCategory(categoryName);
@@ -172,4 +177,43 @@ public class ProductController {
}
return ResponseEntity.ok().build();
}
@PostMapping("/{id}/publish")
@org.springframework.transaction.annotation.Transactional
public ResponseEntity<Product> publishProduct(@PathVariable Long id, @RequestBody Product productDetails) {
return productRepository.findById(id)
.map(product -> {
// Generate new PRD ID
String newProductId = "PRD-" + String.format("%04d", new java.util.Random().nextInt(10000));
// Update all details first
product.setName(productDetails.getName());
product.setProductId(newProductId);
product.setCategory(productDetails.getCategory());
product.setDescription(productDetails.getDescription());
product.setBasePrice(productDetails.getBasePrice());
product.setPrice(productDetails.getPrice());
product.setOfferPrice(productDetails.getOfferPrice());
product.setCounter(productDetails.getCounter());
product.setTag(productDetails.getTag());
product.setBarcode(productDetails.getBarcode());
product.setImageData(productDetails.getImageData());
product.setStock(productDetails.getStock());
product.getSessions().clear();
if (productDetails.getSessions() != null) {
product.getSessions().addAll(productDetails.getSessions());
}
// Finalize
product.setDraft(false);
product.setActive(true);
Product updated = productRepository.save(product);
updateStallAssociations(updated, productDetails.getStalls());
return ResponseEntity.ok(productRepository.findById(updated.getId()).orElse(updated));
})
.orElse(ResponseEntity.notFound().build());
}
}

View File

@@ -63,6 +63,8 @@ public class Product {
private Integer stock = 0;
private Boolean isDraft = false;
@ManyToMany(mappedBy = "products", fetch = FetchType.EAGER)
@JsonIgnoreProperties({"products", "baseItems", "sessions", "imageData"})
private List<Stall> stalls = new ArrayList<>();
@@ -141,4 +143,7 @@ public class Product {
public List<Stall> getStalls() { return stalls; }
public void setStalls(List<Stall> stalls) { this.stalls = stalls; }
public boolean isDraft() { return isDraft != null && isDraft; }
public void setDraft(Boolean draft) { isDraft = draft; }
}

View File

@@ -22,6 +22,8 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
@Query("SELECT DISTINCT p.category FROM Product p WHERE p.category IS NOT NULL")
List<String> findDistinctCategories();
List<Product> findByIsDraftTrue();
boolean existsByNameAndCategory(String name, String category);
@org.springframework.data.jpa.repository.Modifying

View File

@@ -1,9 +1,12 @@
package com.rit.canteen.sales.service;
import com.rit.canteen.sales.model.Product;
import com.rit.canteen.sales.model.PurchaseOrder;
import com.rit.canteen.sales.model.PurchaseOrderItem;
import com.rit.canteen.sales.model.Vendor;
import com.rit.canteen.sales.repository.PurchaseOrderRepository;
import com.rit.canteen.sales.repository.VendorRepository;
import com.rit.canteen.sales.repository.ProductRepository;
import com.rit.canteen.sales.repository.PurchaseOrderHistoryRepository;
import com.rit.canteen.sales.model.PurchaseOrderHistory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +33,9 @@ public class PurchaseService {
@Autowired
private PurchaseOrderHistoryRepository historyRepository;
@Autowired
private ProductRepository productRepository;
public List<PurchaseOrder> getAllOrders() {
return purchaseOrderRepository.findAll();
}
@@ -60,6 +66,11 @@ public class PurchaseService {
PurchaseOrder savedOrder = purchaseOrderRepository.save(order);
// Handle Product Import if status is RECEIVED
if ("RECEIVED".equals(savedOrder.getStatus())) {
importToNewArrivals(savedOrder);
}
// Update vendor statistics (only for NEW orders to avoid double counting,
// OR we should adjust the difference for updates - but user mostly wants new orders tracked)
if (order.getId() == null && savedOrder.getVendor() != null && savedOrder.getVendor().getId() != null) {
@@ -136,4 +147,22 @@ public class PurchaseService {
return summary;
}
private void importToNewArrivals(PurchaseOrder order) {
if (order.getItems() == null) return;
for (PurchaseOrderItem item : order.getItems()) {
Product draftProduct = new Product();
draftProduct.setName(item.getProductName());
draftProduct.setStock(item.getQuantity() != null ? item.getQuantity().intValue() : 0);
draftProduct.setBasePrice(item.getRate());
draftProduct.setDraft(true);
draftProduct.setActive(false); // Not live yet
// Set a unique product ID if possible
draftProduct.setProductId("DRAFT-" + String.format("%04d", new java.util.Random().nextInt(10000)) + "-" + item.getId());
productRepository.save(draftProduct);
}
}
}