change saleStorage

This commit is contained in:
Zakharov_Rostislav 2024-05-13 15:21:18 +04:00
parent e4536d16c0
commit 3766e03974
8 changed files with 75 additions and 32 deletions

View File

@ -81,7 +81,7 @@ namespace CarShowroomBusinessLogic.BusinessLogic
throw new ArgumentNullException(nameof(model)); throw new ArgumentNullException(nameof(model));
if (!withParams) if (!withParams)
return; return;
if (model.Cost < 0) if (model.Price < 0)
throw new InvalidOperationException(); throw new InvalidOperationException();
if (model.EmployeeId < 0) if (model.EmployeeId < 0)
throw new InvalidOperationException(); throw new InvalidOperationException();

View File

@ -10,7 +10,7 @@ namespace CarShowroomContracts.AbstractModels
public interface ISale : IId public interface ISale : IId
{ {
DateTime SaleTime { get; } DateTime SaleTime { get; }
int Cost { get; } int Price { get; }
int? ClientId { get; } int? ClientId { get; }
int? EmployeeId { get; } int? EmployeeId { get; }
List<int> CarIds { get; } List<int> CarIds { get; }

View File

@ -11,7 +11,7 @@ namespace CarShowroomDataModels.Dtos
{ {
public int Id { get; set; } public int Id { get; set; }
public DateTime SaleTime { get; set; } public DateTime SaleTime { get; set; }
public int Cost { get; set; } public int Price { get; set; }
public int? ClientId { get; set; } public int? ClientId { get; set; }
public int? EmployeeId { get; set; } public int? EmployeeId { get; set; }
public List<int> CarIds { get; set; } = new(); public List<int> CarIds { get; set; } = new();
@ -20,7 +20,7 @@ namespace CarShowroomDataModels.Dtos
{ {
Id = model.Id; Id = model.Id;
SaleTime = model.SaleTime; SaleTime = model.SaleTime;
Cost = model.Cost; Price = model.Price;
ClientId = model.ClientId; ClientId = model.ClientId;
EmployeeId = model.EmployeeId; EmployeeId = model.EmployeeId;
CarIds = model.CarIds; CarIds = model.CarIds;

View File

@ -15,7 +15,7 @@ namespace CarShowroomDataModels.Views
[DisplayName("Время")] [DisplayName("Время")]
public DateTime SaleTime { get; set; } public DateTime SaleTime { get; set; }
[DisplayName("Сумма")] [DisplayName("Сумма")]
public int Cost { get; set; } public int Price { get; set; }
public int? ClientId { get; set; } public int? ClientId { get; set; }
[DisplayName("Клиент")] [DisplayName("Клиент")]
public string ClientName { get; set; } = string.Empty; public string ClientName { get; set; } = string.Empty;
@ -42,7 +42,7 @@ namespace CarShowroomDataModels.Views
{ {
Id = model.Id; Id = model.Id;
SaleTime = model.SaleTime; SaleTime = model.SaleTime;
Cost = model.Cost; Price = model.Price;
ClientId = model.ClientId; ClientId = model.ClientId;
EmployeeId = model.EmployeeId; EmployeeId = model.EmployeeId;
} }

View File

@ -36,7 +36,7 @@ namespace CarShowroomDatabaseStorage
modelBuilder.Entity<Car>().Property(c => c.IsSaled).HasDefaultValue(false); modelBuilder.Entity<Car>().Property(c => c.IsSaled).HasDefaultValue(false);
modelBuilder.Entity<Service>().Property(c => c.Price).HasDefaultValue(-1); modelBuilder.Entity<Service>().Property(c => c.Price).HasDefaultValue(-1);
modelBuilder.Entity<Model>().Property(c => c.Price).HasDefaultValue(-1); modelBuilder.Entity<Model>().Property(c => c.Price).HasDefaultValue(-1);
modelBuilder.Entity<Sale>().Property(c => c.Cost).HasDefaultValue(-1); modelBuilder.Entity<Sale>().Property(c => c.Price).HasDefaultValue(-1);
//on delete actions //on delete actions
modelBuilder.Entity<Model>() modelBuilder.Entity<Model>()
.HasOne(m => m.Make) .HasOne(m => m.Make)

View File

@ -20,7 +20,7 @@ namespace CarShowroomDatabaseStorage.Entities
public DateTime SaleTime { get; private set; } public DateTime SaleTime { get; private set; }
[Required] [Required]
[Column("sale_cost")] [Column("sale_cost")]
public int Cost { get; private set; } public int Price { get; private set; }
[Column("sale_client_id")] [Column("sale_client_id")]
public int? ClientId { get; private set; } public int? ClientId { get; private set; }
public virtual Client? Client { get; set; } public virtual Client? Client { get; set; }
@ -52,7 +52,7 @@ namespace CarShowroomDatabaseStorage.Entities
{ {
Id = sale.Id; Id = sale.Id;
SaleTime = sale.SaleTime; SaleTime = sale.SaleTime;
Cost = sale.Cost; Price = sale.Price;
ClientId = sale.ClientId; ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId; EmployeeId = sale.EmployeeId;
} }
@ -73,7 +73,7 @@ namespace CarShowroomDatabaseStorage.Entities
return; return;
} }
SaleTime = sale.SaleTime; SaleTime = sale.SaleTime;
Cost = sale.Cost; Price = sale.Price;
ClientId = sale.ClientId; ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId; EmployeeId = sale.EmployeeId;
} }

View File

@ -1,8 +1,10 @@
using CarShowroomContracts.StorageContracts; using CarShowroomContracts.AbstractModels;
using CarShowroomContracts.StorageContracts;
using CarShowroomDatabaseStorage.Entities; using CarShowroomDatabaseStorage.Entities;
using CarShowroomDataModels.Dtos; using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel; using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views; using CarShowroomDataModels.Views;
using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -47,17 +49,26 @@ namespace CarShowroomDatabaseStorage.Storages
} }
public SaleView? Insert(SaleDto model) public SaleView? Insert(SaleDto model)
{ {
var newSale = Sale.Create(model); using var context = new CarShowroomDatabase();
if (newSale == null) 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<int>("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) public SaleView? Update(SaleDto model)
{ {
@ -68,12 +79,12 @@ namespace CarShowroomDatabaseStorage.Storages
var sale = context.Sales var sale = context.Sales
.FirstOrDefault(rec => rec.Id == model.Id); .FirstOrDefault(rec => rec.Id == model.Id);
if (sale == null) if (sale == null)
return null; throw new InvalidOperationException("SaleId is incorrect");
sale.Update(model); sale.Update(model);
context.SaveChanges(); context.SaveChanges();
UpdateCarsAndServices(context, model, sale); SaleView result = UpdateCarsAndServices(context, model, sale.Id);
transaction.Commit(); transaction.Commit();
return sale.GetView(); return result;
} }
catch catch
{ {
@ -96,10 +107,10 @@ namespace CarShowroomDatabaseStorage.Storages
return null; return null;
} }
private void UpdateCarsAndServices(CarShowroomDatabase context, private SaleView UpdateCarsAndServices(CarShowroomDatabase context,
SaleDto model, Sale entity) 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 var saleCars = context.SaleCars
.Where(rec => rec.SaleId == model.Id) .Where(rec => rec.SaleId == model.Id)
.ToList(); .ToList();
@ -111,12 +122,12 @@ namespace CarShowroomDatabaseStorage.Storages
.Contains(rec.CarId))); .Contains(rec.CarId)));
context.SaveChanges(); context.SaveChanges();
} }
foreach (var sc in model.CarIds) foreach (var carId in model.CarIds)
{ {
context.SaleCars.Add(new SaleCar context.SaleCars.Add(new SaleCar
{ {
Sale = sale, Sale = sale,
Car = context.Cars.First(x => x.Id == sc) Car = context.Cars.First(x => x.Id == carId)
}); });
context.SaveChanges(); context.SaveChanges();
} }
@ -131,15 +142,47 @@ namespace CarShowroomDatabaseStorage.Storages
.Contains(rec.ServiceId))); .Contains(rec.ServiceId)));
context.SaveChanges(); context.SaveChanges();
} }
foreach (var sc in model.ServiceIds) foreach (var serviceId in model.ServiceIds)
{ {
context.SaleServices.Add(new SaleService context.SaleServices.Add(new SaleService
{ {
Sale = sale, Sale = sale,
Service = context.Services.First(x => x.Id == sc) Service = context.Services.First(x => x.Id == serviceId)
}); });
context.SaveChanges(); 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<Car> Cars = context.Cars
.Include(c => c.Model)
.Include(c => c.SaleCars)
.Where(c => c.SaleCars
.Select(sc => sc.SaleId)
.Contains(saleId))
.ToList();
List<Service> 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();
}
} }
} }

View File

@ -36,7 +36,7 @@
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => @Html.DisplayFor(modelItem =>
item.Cost) item.Price)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => @Html.DisplayFor(modelItem =>