135 lines
5.0 KiB
C#
135 lines
5.0 KiB
C#
using CarCenterContracts.BindingModels;
|
|
using CarCenterContracts.ViewModels;
|
|
using CarCenterDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CarCenterDatabaseImplement.Models
|
|
{
|
|
public class Car : ICarModel
|
|
{
|
|
public int Id { get; set; }
|
|
[Required]
|
|
public string Name { get; set; }
|
|
private Dictionary<int, IPresaleModel>? _presaleCars = null;
|
|
[NotMapped]
|
|
public Dictionary<int, IPresaleModel> PresaleCars {
|
|
get
|
|
{
|
|
if (_presaleCars == null)
|
|
{
|
|
_presaleCars = Presales.ToDictionary(recDP => recDP.PresaleId, recDP => recDP.Presale as IPresaleModel);
|
|
}
|
|
return _presaleCars;
|
|
}
|
|
}
|
|
[ForeignKey("CarId")]
|
|
public virtual List<PresaleCar> Presales { get; set; } = new();
|
|
private Dictionary<int, IConfigurationModel>? _configurationCars = null;
|
|
[NotMapped]
|
|
public Dictionary<int, IConfigurationModel> ConfigurationCars
|
|
{
|
|
get
|
|
{
|
|
if (_configurationCars == null)
|
|
{
|
|
_configurationCars = Configurations.ToDictionary(recCP => recCP.ConfigurationId, recCP => recCP.Configuration as IConfigurationModel);
|
|
}
|
|
return _configurationCars;
|
|
}
|
|
}
|
|
public virtual List<ConfigurationCar> Configurations { get; set; } = new();
|
|
|
|
public int EmployeeId { get; set; }
|
|
public virtual Employee Employee { get; set; } = new();
|
|
|
|
public static Car? Create(CarCenterDatabase context, CarBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Car()
|
|
{
|
|
Id = model.Id,
|
|
Name=model.Name,
|
|
Configurations = model.ConfigurationCars.Select(x => new ConfigurationCar
|
|
{
|
|
Configuration = context.Configurations.First(y => y.Id == x.Key)
|
|
}).ToList(),
|
|
EmployeeId = model.EmployeeId,
|
|
Employee = context.Employees.First(x => x.Id == model.EmployeeId)
|
|
};
|
|
}
|
|
public void Update(CarBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
public CarViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
Name = Name,
|
|
PresaleCars = PresaleCars,
|
|
ConfigurationCars = ConfigurationCars,
|
|
EmployeeId = EmployeeId,
|
|
EmployeeName = Employee.Surname + " " + Employee.Name + " " + Employee.Patronymic,
|
|
};
|
|
public void UpdatePresales(CarCenterDatabase context, CarBindingModel model)
|
|
{
|
|
var presaleCars = context.PresaleCars.Where(rec => rec.CarId == model.Id).ToList();
|
|
if (presaleCars != null)
|
|
{
|
|
context.PresaleCars.RemoveRange(presaleCars.Where(rec => !model.PresaleCars.ContainsKey(rec.PresaleId)));
|
|
context.SaveChanges();
|
|
var car = context.Cars.First(x => x.Id == Id);
|
|
foreach (var updatePresale in presaleCars)
|
|
{
|
|
model.PresaleCars.Remove(updatePresale.PresaleId);
|
|
}
|
|
foreach (var pc in model.PresaleCars)
|
|
{
|
|
context.PresaleCars.Add(new PresaleCar
|
|
{
|
|
Car = car,
|
|
Presale = context.Presales.First(x => x.Id == pc.Key),
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_presaleCars = null;
|
|
}
|
|
}
|
|
public void UpdateConfigurations(CarCenterDatabase context, CarBindingModel model)
|
|
{
|
|
var configurationCars = context.ConfigurationCars.Where(rec => rec.CarId == model.Id).ToList();
|
|
if (configurationCars != null && configurationCars.Count > 0)
|
|
{
|
|
context.ConfigurationCars.RemoveRange(configurationCars.Where(rec => !model.ConfigurationCars.ContainsKey(rec.ConfigurationId)));
|
|
context.SaveChanges();
|
|
var car = context.Cars.First(x => x.Id == Id);
|
|
foreach (var updateConfiguration in configurationCars)
|
|
{
|
|
model.ConfigurationCars.Remove(updateConfiguration.ConfigurationId);
|
|
}
|
|
foreach (var cc in model.ConfigurationCars)
|
|
{
|
|
context.ConfigurationCars.Add(new ConfigurationCar
|
|
{
|
|
Car = car,
|
|
Configuration = context.Configurations.First(x => x.Id == cc.Key),
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_configurationCars = null;
|
|
}
|
|
}
|
|
}
|
|
}
|