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));
if (!withParams)
return;
if (model.Cost < 0)
if (model.Price < 0)
throw new InvalidOperationException();
if (model.EmployeeId < 0)
throw new InvalidOperationException();

View File

@ -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<int> CarIds { get; }

View File

@ -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<int> 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;

View File

@ -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;
}

View File

@ -36,7 +36,7 @@ namespace CarShowroomDatabaseStorage
modelBuilder.Entity<Car>().Property(c => c.IsSaled).HasDefaultValue(false);
modelBuilder.Entity<Service>().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
modelBuilder.Entity<Model>()
.HasOne(m => m.Make)

View File

@ -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;
}

View File

@ -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<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)
{
@ -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<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>
@Html.DisplayFor(modelItem =>
item.Cost)
item.Price)
</td>
<td>
@Html.DisplayFor(modelItem =>