Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions WMS.Api/WMS.Api/Controllers/SalesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public ActionResult<SaleDto> 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();
Expand Down
4 changes: 2 additions & 2 deletions WMS.Api/WMS.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
7 changes: 6 additions & 1 deletion WMS.Api/WMS.Services/DTOs/Sale/SaleForUpdateDto.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace WMS.Services.DTOs.Sale;
using System.ComponentModel.DataAnnotations;
using WMS.Services.DTOs.SaleItem;

namespace WMS.Services.DTOs.Sale;

public class SaleForUpdateDto
{
public int Id { get; set; }
public DateTime Date { get; init; }
public decimal TotalPaid { get; init; }
public int CustomerId { get; init; }
[Required]
public IEnumerable<SaleItemForCreateDto> SaleItems { get; set; }
}
21 changes: 16 additions & 5 deletions WMS.Api/WMS.Services/DashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ public async Task<DashboardDto> GetDashboardAsync()
private async Task<SummaryDto> 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;
}
Expand All @@ -56,7 +62,7 @@ orderby category.Name
SalesCount = groupedCategories.Count()
};

return await salesByCategory.ToListAsync();
return await salesByCategory.AsSplitQuery().AsNoTracking().ToListAsync();
}

private async Task<List<SplineChart>> GetChartAsync()
Expand Down Expand Up @@ -98,7 +104,8 @@ from expense in joinedExpense.DefaultIfEmpty()
Income = income?.Income ?? 0,
};

return chartData.ToList();
return chartData
.ToList();
}

private async Task<List<TransactionDto>> GetLatestTransactionsAsync()
Expand All @@ -113,6 +120,8 @@ private async Task<List<TransactionDto>> GetLatestTransactionsAsync()
Date = x.Date,
Type = "Sale"
})
.AsSplitQuery()
.AsNoTracking()
.ToList();
var supplies = _context.Supplies
.Where(x => x.Date.Day == DateTime.Now.Day)
Expand All @@ -123,6 +132,8 @@ private async Task<List<TransactionDto>> GetLatestTransactionsAsync()
Date = x.Date,
Type = "Supply"
})
.AsSplitQuery()
.AsNoTracking()
.ToList();

List<TransactionDto> transactions = [.. sales, .. supplies];
Expand Down
30 changes: 29 additions & 1 deletion WMS.Api/WMS.Services/SaleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,35 @@ public void Update(SaleForUpdateDto sale)
}

var entity = _mapper.Map<Sale>(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();
}
}