Changes between Version 2 and Version 3 of AdvancedApplicationDevelopment


Ignore:
Timestamp:
02/24/26 06:06:08 (4 hours ago)
Author:
221181
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedApplicationDevelopment

    v2 v3  
    8080}
    8181}}}
     82
     83=== 2. Примање на нарачка (!PurchaseOrderService.cs) ===
     84
     85Зошто Transaction?
     86
     87Кога статусот на нарачката се менува во „Received“, залихите мора да се зголемат. Ако едната операција се изврши, а другата не, ќе настане неконзистентност во податоците.
     88
     89{{{
     90public async Task<bool> ReceivePurchaseOrderAsync(int poId)
     91{
     92    using var transaction = await _context.Database.BeginTransactionAsync();
     93    try
     94    {
     95        var po = await GetPurchaseOrderByIdAsync(poId);
     96        if (po == null || po.Status == "Received")
     97            return false;
     98
     99       
     100        foreach (var item in po.PurchaseOrderItems)
     101        {
     102            var stock = await _context.WarehouseStocks
     103                .FirstOrDefaultAsync(ws => ws.WarehouseId == po.WarehouseId && ws.ProductId == item.ProductId);
     104
     105            if (stock == null)
     106            {
     107                stock = new WarehouseStock
     108                {
     109                    WarehouseId = po.WarehouseId,
     110                    ProductId = item.ProductId,
     111                    QuantityOnHand = item.Quantity
     112                };
     113                _context.WarehouseStocks.Add(stock);
     114            }
     115            else
     116            {
     117                stock.QuantityOnHand += item.Quantity;
     118            }
     119
     120            stock.LastUpdated = DateTime.Now;
     121            item.ReceivedQuantity = item.Quantity;
     122        }
     123
     124        po.Status = "Received";
     125        po.ActualDeliveryDate = DateTime.Now.Date;
     126
     127        await _context.SaveChangesAsync();
     128        await transaction.CommitAsync();
     129        return true;
     130    }
     131    catch
     132    {
     133        await transaction.RollbackAsync();
     134        return false;
     135    }
     136}
     137}}}