From 1c31f55bc72cbf66a738f9bb8e06a2576a32ac49 Mon Sep 17 00:00:00 2001 From: FirdavsAX <137472686+FirdavsAX@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:35:42 +0500 Subject: [PATCH 1/2] ad --- WMS.Api/WMS.Api/appsettings.Development.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WMS.Api/WMS.Api/appsettings.Development.json b/WMS.Api/WMS.Api/appsettings.Development.json index 6bb8c8f..f683db4 100644 --- a/WMS.Api/WMS.Api/appsettings.Development.json +++ b/WMS.Api/WMS.Api/appsettings.Development.json @@ -6,6 +6,6 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Data Source=miraziz\\sqlexpress;Initial Catalog=WMS;Integrated Security=True;Pooling=False;Encrypt=True;Trust Server Certificate=True" + "DefaultConnection": "Data Source=DESKTOP-UVO0SDG;Initial Catalog=WMS_API_DB;Integrated Security=True;Pooling=False;Encrypt=True;Trust Server Certificate=True" } -} +} From fdd90d5ef942fb86d4dadfe54fef7ea6bc57650c Mon Sep 17 00:00:00 2001 From: FirdavsAX <137472686+FirdavsAX@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:54:12 +0500 Subject: [PATCH 2/2] Fix bugs. Added some Asnotracking for services. Added Items to SaleUpdateDto --- .../WMS.Api/Controllers/SalesController.cs | 4 +++ .../DTOs/Sale/SaleForUpdateDto.cs | 7 ++++- WMS.Api/WMS.Services/DashboardService.cs | 21 +++++++++---- WMS.Api/WMS.Services/SaleService.cs | 30 ++++++++++++++++++- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/WMS.Api/WMS.Api/Controllers/SalesController.cs b/WMS.Api/WMS.Api/Controllers/SalesController.cs index 71803b7..cf2d7aa 100644 --- a/WMS.Api/WMS.Api/Controllers/SalesController.cs +++ b/WMS.Api/WMS.Api/Controllers/SalesController.cs @@ -44,6 +44,10 @@ public ActionResult Post([FromBody] SaleForCreateDto sale) [HttpPut("{id:int}")] public ActionResult UpdateSale(int id, SaleForUpdateDto sale) { + if(id != sale.Id) + { + return BadRequest($"Id : {id} does not match with sale id : {sale.Id}"); + } _saleService.Update(sale); return NoContent(); diff --git a/WMS.Api/WMS.Services/DTOs/Sale/SaleForUpdateDto.cs b/WMS.Api/WMS.Services/DTOs/Sale/SaleForUpdateDto.cs index cde2272..3540565 100644 --- a/WMS.Api/WMS.Services/DTOs/Sale/SaleForUpdateDto.cs +++ b/WMS.Api/WMS.Services/DTOs/Sale/SaleForUpdateDto.cs @@ -1,4 +1,7 @@ -namespace WMS.Services.DTOs.Sale; +using System.ComponentModel.DataAnnotations; +using WMS.Services.DTOs.SaleItem; + +namespace WMS.Services.DTOs.Sale; public class SaleForUpdateDto { @@ -6,4 +9,6 @@ public class SaleForUpdateDto public DateTime Date { get; init; } public decimal TotalPaid { get; init; } public int CustomerId { get; init; } + [Required] + public IEnumerable SaleItems { get; set; } } diff --git a/WMS.Api/WMS.Services/DashboardService.cs b/WMS.Api/WMS.Services/DashboardService.cs index 534d2b6..b0f9557 100644 --- a/WMS.Api/WMS.Services/DashboardService.cs +++ b/WMS.Api/WMS.Services/DashboardService.cs @@ -34,9 +34,15 @@ public async Task GetDashboardAsync() private async Task GetSummaryAsync() { var summary = new SummaryDto(); - summary.Revenue = _context.Sales.Sum(x => x.TotalDue) - _context.Supplies.Sum(x => x.TotalDue); - summary.LowQuantityProducts = _context.Products.Count(x => x.LowQuantityAmount >= x.QuantityInStock); - summary.CustomersAmount = _context.Customers.Count(); + summary.Revenue = _context.Sales + .AsNoTracking() + .Sum(x => x.TotalDue) - _context.Supplies.AsNoTracking().Sum(x => x.TotalDue); + + summary.LowQuantityProducts = _context.Products + .AsNoTracking() + .Count(x => x.LowQuantityAmount >= x.QuantityInStock); + + summary.CustomersAmount = _context.Customers.AsNoTracking().Count(); return summary; } @@ -56,7 +62,7 @@ orderby category.Name SalesCount = groupedCategories.Count() }; - return await salesByCategory.ToListAsync(); + return await salesByCategory.AsSplitQuery().AsNoTracking().ToListAsync(); } private async Task> GetChartAsync() @@ -98,7 +104,8 @@ from expense in joinedExpense.DefaultIfEmpty() Income = income?.Income ?? 0, }; - return chartData.ToList(); + return chartData + .ToList(); } private async Task> GetLatestTransactionsAsync() @@ -113,6 +120,8 @@ private async Task> GetLatestTransactionsAsync() Date = x.Date, Type = "Sale" }) + .AsSplitQuery() + .AsNoTracking() .ToList(); var supplies = _context.Supplies .Where(x => x.Date.Day == DateTime.Now.Day) @@ -123,6 +132,8 @@ private async Task> GetLatestTransactionsAsync() Date = x.Date, Type = "Supply" }) + .AsSplitQuery() + .AsNoTracking() .ToList(); List transactions = [.. sales, .. supplies]; diff --git a/WMS.Api/WMS.Services/SaleService.cs b/WMS.Api/WMS.Services/SaleService.cs index 680fcb4..f90d644 100644 --- a/WMS.Api/WMS.Services/SaleService.cs +++ b/WMS.Api/WMS.Services/SaleService.cs @@ -105,7 +105,35 @@ public void Update(SaleForUpdateDto sale) } var entity = _mapper.Map(sale); - _context.Sales.Update(entity); + + entity.TotalDue = sale.SaleItems.Sum(x => x.Quantity * x.UnitPrice); + + foreach (var item in entity.SaleItems) + { + var product = _context.Products.FirstOrDefault(x => x.Id == item.ProductId); + + if (product is null) + { + throw new InvalidOperationException($"Cannot create sale for non-existing product: {item.ProductId}."); + } + + if (product?.QuantityInStock < item.Quantity) + { + throw new InvalidOperationException($"Not enough items in stock: {item.Quantity} for product {product.Name}."); + } + + product.QuantityInStock -= item.Quantity; + } + + var customer = _context.Customers.FirstOrDefault(x => x.Id == sale.CustomerId); + + if (customer is null) + { + throw new InvalidOperationException($"Cannot create sale for customer which does not exist. Customer id: {sale.CustomerId}"); + } + customer.Balance += entity.TotalPaid - entity.TotalDue; + + var createdSale = _context.Sales.Update(entity); _context.SaveChanges(); } }