diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs index 468b617..e42888d 100644 --- a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs @@ -81,7 +81,7 @@ namespace CarShowroomBusinessLogic.BusinessLogic throw new ArgumentNullException(nameof(model)); if (!withParams) return; - if (model.Cost < 0) + if (model.Price < 0) throw new InvalidOperationException(); if (model.EmployeeId < 0) throw new InvalidOperationException(); diff --git a/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs b/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs index e96bc53..10a00bf 100644 --- a/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs +++ b/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs @@ -10,7 +10,7 @@ namespace CarShowroomContracts.AbstractModels public interface ISale : IId { DateTime SaleTime { get; } - int Cost { get; } + int Price { get; } int? ClientId { get; } int? EmployeeId { get; } List CarIds { get; } diff --git a/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs b/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs index 2e6347f..992c13b 100644 --- a/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs +++ b/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs @@ -11,7 +11,7 @@ namespace CarShowroomDataModels.Dtos { public int Id { get; set; } public DateTime SaleTime { get; set; } - public int Cost { get; set; } + public int Price { get; set; } public int? ClientId { get; set; } public int? EmployeeId { get; set; } public List CarIds { get; set; } = new(); @@ -20,7 +20,7 @@ namespace CarShowroomDataModels.Dtos { Id = model.Id; SaleTime = model.SaleTime; - Cost = model.Cost; + Price = model.Price; ClientId = model.ClientId; EmployeeId = model.EmployeeId; CarIds = model.CarIds; diff --git a/CarShowroom/CarShowroomDataModels/Views/SaleView.cs b/CarShowroom/CarShowroomDataModels/Views/SaleView.cs index 783060e..e5ba899 100644 --- a/CarShowroom/CarShowroomDataModels/Views/SaleView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/SaleView.cs @@ -15,7 +15,7 @@ namespace CarShowroomDataModels.Views [DisplayName("Время")] public DateTime SaleTime { get; set; } [DisplayName("Сумма")] - public int Cost { get; set; } + public int Price { get; set; } public int? ClientId { get; set; } [DisplayName("Клиент")] public string ClientName { get; set; } = string.Empty; @@ -42,7 +42,7 @@ namespace CarShowroomDataModels.Views { Id = model.Id; SaleTime = model.SaleTime; - Cost = model.Cost; + Price = model.Price; ClientId = model.ClientId; EmployeeId = model.EmployeeId; } diff --git a/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs b/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs index 9906eae..caf623f 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs @@ -36,7 +36,7 @@ namespace CarShowroomDatabaseStorage modelBuilder.Entity().Property(c => c.IsSaled).HasDefaultValue(false); modelBuilder.Entity().Property(c => c.Price).HasDefaultValue(-1); modelBuilder.Entity().Property(c => c.Price).HasDefaultValue(-1); - modelBuilder.Entity().Property(c => c.Cost).HasDefaultValue(-1); + modelBuilder.Entity().Property(c => c.Price).HasDefaultValue(-1); //on delete actions modelBuilder.Entity() .HasOne(m => m.Make) diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs index a1ae884..feee10f 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs @@ -20,7 +20,7 @@ namespace CarShowroomDatabaseStorage.Entities public DateTime SaleTime { get; private set; } [Required] [Column("sale_cost")] - public int Cost { get; private set; } + public int Price { get; private set; } [Column("sale_client_id")] public int? ClientId { get; private set; } public virtual Client? Client { get; set; } @@ -52,7 +52,7 @@ namespace CarShowroomDatabaseStorage.Entities { Id = sale.Id; SaleTime = sale.SaleTime; - Cost = sale.Cost; + Price = sale.Price; ClientId = sale.ClientId; EmployeeId = sale.EmployeeId; } @@ -73,7 +73,7 @@ namespace CarShowroomDatabaseStorage.Entities return; } SaleTime = sale.SaleTime; - Cost = sale.Cost; + Price = sale.Price; ClientId = sale.ClientId; EmployeeId = sale.EmployeeId; } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs index 70d2e69..ad92b77 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs @@ -1,8 +1,10 @@ -using CarShowroomContracts.StorageContracts; +using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.StorageContracts; using CarShowroomDatabaseStorage.Entities; using CarShowroomDataModels.Dtos; using CarShowroomDataModels.SearchModel; using CarShowroomDataModels.Views; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -47,17 +49,26 @@ namespace CarShowroomDatabaseStorage.Storages } public SaleView? Insert(SaleDto model) - { - var newSale = Sale.Create(model); - if (newSale == null) + { + using var context = new CarShowroomDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var newSale = Sale.Create(model); + if (newSale == null) + throw new InvalidOperationException("SaleId is incorrect"); + int saleId = context.Sales.Add(newSale).CurrentValues.GetValue("Id"); + context.SaveChanges(); + SaleView result = UpdateCarsAndServices(context, model, saleId); + transaction.Commit(); + return result; + } + catch { - return null; + transaction.Rollback(); + throw; } - using var context = new CarShowroomDatabase(); - context.Sales.Add(newSale); - context.SaveChanges(); - return newSale.GetView(); - } +} public SaleView? Update(SaleDto model) { @@ -68,12 +79,12 @@ namespace CarShowroomDatabaseStorage.Storages var sale = context.Sales .FirstOrDefault(rec => rec.Id == model.Id); if (sale == null) - return null; - sale.Update(model); + throw new InvalidOperationException("SaleId is incorrect"); + sale.Update(model); context.SaveChanges(); - UpdateCarsAndServices(context, model, sale); + SaleView result = UpdateCarsAndServices(context, model, sale.Id); transaction.Commit(); - return sale.GetView(); + return result; } catch { @@ -96,10 +107,10 @@ namespace CarShowroomDatabaseStorage.Storages return null; } - private void UpdateCarsAndServices(CarShowroomDatabase context, - SaleDto model, Sale entity) + private SaleView UpdateCarsAndServices(CarShowroomDatabase context, + SaleDto model, int saleId) { - var sale = context.Sales.First(x => x.Id == entity.Id); + var sale = context.Sales.First(x => x.Id == saleId); var saleCars = context.SaleCars .Where(rec => rec.SaleId == model.Id) .ToList(); @@ -111,12 +122,12 @@ namespace CarShowroomDatabaseStorage.Storages .Contains(rec.CarId))); context.SaveChanges(); } - foreach (var sc in model.CarIds) + foreach (var carId in model.CarIds) { context.SaleCars.Add(new SaleCar { Sale = sale, - Car = context.Cars.First(x => x.Id == sc) + Car = context.Cars.First(x => x.Id == carId) }); context.SaveChanges(); } @@ -131,15 +142,47 @@ namespace CarShowroomDatabaseStorage.Storages .Contains(rec.ServiceId))); context.SaveChanges(); } - foreach (var sc in model.ServiceIds) + foreach (var serviceId in model.ServiceIds) { context.SaleServices.Add(new SaleService { Sale = sale, - Service = context.Services.First(x => x.Id == sc) + Service = context.Services.First(x => x.Id == serviceId) }); context.SaveChanges(); } + return UpdatePrice(context, saleId); } + + private SaleView UpdatePrice(CarShowroomDatabase context, int saleId) + { + Sale? sale = context.Sales + .FirstOrDefault(rec => rec.Id == saleId); + if (sale == null) + throw new InvalidOperationException("SaleId is incorrect"); + List Cars = context.Cars + .Include(c => c.Model) + .Include(c => c.SaleCars) + .Where(c => c.SaleCars + .Select(sc => sc.SaleId) + .Contains(saleId)) + .ToList(); + List Services = context.Services + .Include(s => s.SaleServices) + .Where(s => s.SaleServices + .Select(ss => ss.SaleId) + .Contains(saleId)) + .ToList(); + int price = 0; + foreach(Car car in Cars) + price += car.Model?.Price ?? 0; + foreach (Service service in Services) + price += service.Price; + SaleDto model = new SaleDto(sale); + model.Price = price; + sale.Update(model); + context.SaveChanges(); + return sale.GetView(); + } } } diff --git a/CarShowroom/CarShowroomManagerApp/Views/Home/Index.cshtml b/CarShowroom/CarShowroomManagerApp/Views/Home/Index.cshtml index 0c8d03a..dbc7054 100644 --- a/CarShowroom/CarShowroomManagerApp/Views/Home/Index.cshtml +++ b/CarShowroom/CarShowroomManagerApp/Views/Home/Index.cshtml @@ -36,7 +36,7 @@ @Html.DisplayFor(modelItem => - item.Cost) + item.Price) @Html.DisplayFor(modelItem =>