Bug fixes and Counter integrated
This commit is contained in:
@@ -16,6 +16,9 @@ public class ProductController {
|
||||
@Autowired
|
||||
private ProductRepository productRepository;
|
||||
|
||||
@Autowired
|
||||
private com.rit.canteen.sales.repository.StallRepository stallRepository;
|
||||
|
||||
@GetMapping
|
||||
public org.springframework.data.domain.Page<Product> getAllProducts(
|
||||
@RequestParam(required = false) String search,
|
||||
@@ -35,11 +38,15 @@ public class ProductController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@org.springframework.transaction.annotation.Transactional
|
||||
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}")
|
||||
@org.springframework.transaction.annotation.Transactional
|
||||
public ResponseEntity<Product> updateProduct(@PathVariable Long id, @Valid @RequestBody Product productDetails) {
|
||||
return productRepository.findById(id)
|
||||
.map(product -> {
|
||||
@@ -70,11 +77,40 @@ public class ProductController {
|
||||
product.setImageData(productDetails.getImageData());
|
||||
product.setActive(productDetails.isActive());
|
||||
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());
|
||||
}
|
||||
|
||||
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}")
|
||||
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
|
||||
return productRepository.findById(id)
|
||||
|
||||
@@ -459,8 +459,28 @@ const Products = () => {
|
||||
</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">Counter</label>
|
||||
<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>
|
||||
<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 className="space-y-4">
|
||||
<div className="group">
|
||||
|
||||
@@ -5,6 +5,7 @@ import react from '@vitejs/plugin-react'
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 5175,
|
||||
host: true,
|
||||
proxy: {
|
||||
'/api': {
|
||||
|
||||
Reference in New Issue
Block a user