Bug fixes and Counter integrated

This commit is contained in:
Sidharth Prabhu
2026-04-16 11:18:33 +05:30
parent 9af9f0599a
commit 09c1c3bb48
3 changed files with 61 additions and 4 deletions

View File

@@ -16,6 +16,9 @@ public class ProductController {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
@Autowired
private com.rit.canteen.sales.repository.StallRepository stallRepository;
@GetMapping @GetMapping
public org.springframework.data.domain.Page<Product> getAllProducts( public org.springframework.data.domain.Page<Product> getAllProducts(
@RequestParam(required = false) String search, @RequestParam(required = false) String search,
@@ -35,11 +38,15 @@ public class ProductController {
} }
@PostMapping @PostMapping
@org.springframework.transaction.annotation.Transactional
public Product createProduct(@Valid @RequestBody Product product) { public Product createProduct(@Valid @RequestBody Product product) {
return productRepository.save(product); Product savedProduct = productRepository.save(product);
updateStallAssociations(savedProduct, product.getStalls());
return productRepository.findById(savedProduct.getId()).orElse(savedProduct);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@org.springframework.transaction.annotation.Transactional
public ResponseEntity<Product> updateProduct(@PathVariable Long id, @Valid @RequestBody Product productDetails) { public ResponseEntity<Product> updateProduct(@PathVariable Long id, @Valid @RequestBody Product productDetails) {
return productRepository.findById(id) return productRepository.findById(id)
.map(product -> { .map(product -> {
@@ -70,11 +77,40 @@ public class ProductController {
product.setImageData(productDetails.getImageData()); product.setImageData(productDetails.getImageData());
product.setActive(productDetails.isActive()); product.setActive(productDetails.isActive());
product.setStock(productDetails.getStock()); product.setStock(productDetails.getStock());
return ResponseEntity.ok(productRepository.save(product));
Product updated = productRepository.save(product);
updateStallAssociations(updated, productDetails.getStalls());
return ResponseEntity.ok(productRepository.findById(updated.getId()).orElse(updated));
}) })
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
} }
private void updateStallAssociations(Product product, List<com.rit.canteen.sales.model.Stall> assignedStalls) {
// Remove product from all stalls currently associated
List<com.rit.canteen.sales.model.Stall> allStalls = stallRepository.findAll();
for (com.rit.canteen.sales.model.Stall stall : allStalls) {
if (stall.getProducts() != null && stall.getProducts().removeIf(p -> p.getId().equals(product.getId()))) {
stallRepository.save(stall);
}
}
// Add product to the newly assigned stalls
if (assignedStalls != null) {
for (com.rit.canteen.sales.model.Stall assignedStall : assignedStalls) {
stallRepository.findById(assignedStall.getId()).ifPresent(stall -> {
if (stall.getProducts() == null) {
stall.setProducts(new java.util.ArrayList<>());
}
if (stall.getProducts().stream().noneMatch(p -> p.getId().equals(product.getId()))) {
stall.getProducts().add(product);
stallRepository.save(stall);
}
});
}
}
}
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) { public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
return productRepository.findById(id) return productRepository.findById(id)

View File

@@ -459,8 +459,28 @@ const Products = () => {
</div> </div>
<div><label className="block text-[11px] uppercase font-bold text-[#64748b] mb-1.5">Barcode</label><input type="text" value={formData.barcode} onChange={(e) => setFormData({ ...formData, barcode: e.target.value })} className="w-full px-4 py-3 border border-[#e2e8f0] rounded-xl text-sm" /></div> <div><label className="block text-[11px] uppercase font-bold text-[#64748b] mb-1.5">Barcode</label><input type="text" value={formData.barcode} onChange={(e) => setFormData({ ...formData, barcode: e.target.value })} className="w-full px-4 py-3 border border-[#e2e8f0] rounded-xl text-sm" /></div>
<div><label className="block text-[11px] uppercase font-bold text-[#64748b] mb-1.5">Stock Quantity</label><input type="number" value={formData.stock} onChange={(e) => setFormData({ ...formData, stock: parseInt(e.target.value) || 0 })} className="w-full px-4 py-3 border border-[#e2e8f0] rounded-xl text-sm font-semibold" /></div> <div><label className="block text-[11px] uppercase font-bold text-[#64748b] mb-1.5">Stock Quantity</label><input type="number" value={formData.stock} onChange={(e) => setFormData({ ...formData, stock: parseInt(e.target.value) || 0 })} className="w-full px-4 py-3 border border-[#e2e8f0] rounded-xl text-sm font-semibold" /></div>
<div><label className="block text-[11px] uppercase font-bold text-[#64748b] mb-1.5">Counter</label> <div>
<select value={formData.counter} onChange={(e) => setFormData({ ...formData, counter: e.target.value })} className="w-full px-4 py-3 border border-[#e2e8f0] rounded-xl text-sm"><option value="">Select Counter</option><option value="Main Counter">Main Counter</option></select></div> <label className="block text-[11px] uppercase font-bold text-[#64748b] mb-1.5">Counter (Stall) *</label>
<select
required
value={formData.counter}
onChange={(e) => {
const stallName = e.target.value;
const selectedStall = allStalls.find(s => s.name === stallName);
setFormData({
...formData,
counter: stallName,
stalls: selectedStall ? [{ id: selectedStall.id, name: selectedStall.name }] : []
});
}}
className="w-full px-4 py-3 border border-[#e2e8f0] rounded-xl text-sm font-semibold focus:ring-4 focus:ring-[#231651]/5 focus:border-[#231651] transition-all outline-none"
>
<option value="">Select Stall</option>
{allStalls.map(stall => (
<option key={stall.id} value={stall.name}>{stall.name}</option>
))}
</select>
</div>
</div> </div>
<div className="space-y-4"> <div className="space-y-4">
<div className="group"> <div className="group">

View File

@@ -5,6 +5,7 @@ import react from '@vitejs/plugin-react'
export default defineConfig({ export default defineConfig({
plugins: [react()], plugins: [react()],
server: { server: {
port: 5175,
host: true, host: true,
proxy: { proxy: {
'/api': { '/api': {