дуби дуби даба даба
This commit is contained in:
parent
1bde67b265
commit
e974da95b5
78
CarCenterDatabaseImplement/Models/Requirement.cs
Normal file
78
CarCenterDatabaseImplement/Models/Requirement.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using CarCenterContracts.BindingModels;
|
||||
using CarCenterContracts.Extensions;
|
||||
using CarCenterContracts.ViewModels;
|
||||
using CarCenterDataModels;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using CarCenterDataModels.ProxyModels;
|
||||
|
||||
namespace CarCenterDatabaseImplement.Models
|
||||
{
|
||||
public class Requirement : IRequirementModel
|
||||
{
|
||||
[Required]
|
||||
public int DirectorId { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string NameOfRequirement { get; private set; } = string.Empty;
|
||||
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public double Price { get; private set; }
|
||||
|
||||
private Dictionary<int, RequirementByCarModel>? _cachedCars;
|
||||
|
||||
public Dictionary<int, RequirementByCarModel> CarsModels =>
|
||||
_cachedCars ??= Cars.Select(x => (RequirementByCarModel)x).ToDictionary(x => x.CarId, x => x);
|
||||
|
||||
[Required]
|
||||
public Director? Director { get; private set; }
|
||||
|
||||
[Required]
|
||||
public List<RequirementByCar> Cars { get; private set; } = new();
|
||||
|
||||
public static Requirement Create(RequirementBindingModel model) => model.CastWithCommonProperties<Requirement, RequirementBindingModel>();
|
||||
|
||||
public static implicit operator RequirementViewModel(Requirement? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException("Возникла ошибка при попытки получить View-модель из null-объекта", nameof(model));
|
||||
}
|
||||
model._cachedCars = null;
|
||||
var result = model.CastWithCommonProperties<RequirementViewModel, Requirement>();
|
||||
result.DirectorLogin = model.Director?.Login ?? string.Empty;
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Update(RequirementBindingModel model)
|
||||
{
|
||||
Price = model.Price;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Привязка требований к дисциплиным
|
||||
/// </summary>
|
||||
public void UpdateCars(CarCenterDb context, RequirementBindingModel model)
|
||||
{
|
||||
var oldCars = context.RequirementByCars.Where(x => x.CarId == model.Id).ToDictionary(x => x.CarId, x => x);
|
||||
var newCars = model.CarsModels.ToDictionary(
|
||||
x => x.Key,
|
||||
x =>
|
||||
{
|
||||
var res = x.Value.CastWithCommonProperties<RequirementByCar, RequirementByCarModel>();
|
||||
res.RequirementId = model.Id;
|
||||
res.CarId = x.Key;
|
||||
return res;
|
||||
});
|
||||
context.RemoveRange(oldCars.Where(x => !newCars.ContainsKey(x.Key)).Select(x => x.Value));
|
||||
context.SaveChanges();
|
||||
context.AddRange (newCars.Where(x => !oldCars.ContainsKey(x.Key)).Select(x => x.Value));
|
||||
oldCars.Where(x => newCars.ContainsKey(x.Key))
|
||||
.Select(x => x.Value).ToList()
|
||||
.ForEach(x => x.Count = newCars[x.CarId].Count);
|
||||
context.SaveChanges();
|
||||
_cachedCars = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user