PIbd-23_Starostin_I.K._Cour.../STODataBaseImplement/Car.cs

129 lines
4.4 KiB
C#
Raw Normal View History

2024-05-01 14:32:42 +04:00
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement
{
public class Car : ICarModel
{
public int Id { get ; private set; }
[Required]
public string CarNumber { get; private set; } = string.Empty;
public string CarBrand { get; private set; } = string.Empty;
public string CarModel { get; private set; } = string.Empty;
public DateTime Year { get; private set; } = DateTime.MinValue;
[ForeignKey("CarId")]
public virtual List<Service> Services { get; private set; } = new();
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; set; } = new();
public Dictionary<int, (ITechnicalWorkModel, int)> _CarTechnicalWorks = null;
[ForeignKey("CarId")]
public virtual List<CarTechnicalWork> TechnicalWorks { get; set; } = new();
[NotMapped]
public Dictionary<int, (ITechnicalWorkModel, int)> CarTechnicalWorks
{
get
{
if (_CarTechnicalWorks == null)
{
_CarTechnicalWorks = TechnicalWorks
.ToDictionary(recPC => recPC.Id, recPC =>
(recPC.TechnicalWork as ITechnicalWorkModel, recPC.Count));
}
return _CarTechnicalWorks;
}
}
public static Car Create(STODatabase context, CarBindingModel model)
{
if (model == null)
{
return null;
}
return new Car()
{
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
Id = model.Id,
CarNumber = model.CarNumber,
CarBrand = model.CarBrand,
CarModel = model.CarModel,
TechnicalWorks = model.CarTechnicalWorks.Select(x => new
CarTechnicalWork
{
TechnicalWork = context.TechnicalWorks.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(CarBindingModel model)
{
if (model == null)
{
return;
}
CarNumber = model.CarNumber;
CarBrand = model.CarBrand;
CarModel = model.CarModel;
Year = model.Year;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
CarNumber = CarNumber,
CarBrand = CarBrand,
CarModel = CarModel,
Year = Year
};
public void UpdateTechnicalWorks(STODatabase context, CarBindingModel model)
{
var technicalWorks = context.CarTechnicalWork.Where(rec =>
rec.CarId == model.Id).ToList();
if (technicalWorks != null && CarTechnicalWorks.Count > 0)
{
context.CarTechnicalWork.RemoveRange(technicalWorks.Where(rec
=> !model.CarTechnicalWorks.ContainsKey(rec.TechnicalWorkId)));
context.SaveChanges();
foreach (var updateComponent in technicalWorks)
{
updateComponent.Count =
model.CarTechnicalWorks[updateComponent.TechnicalWorkId].Item2;
model.CarTechnicalWorks.Remove(updateComponent.TechnicalWorkId);
}
context.SaveChanges();
}
var manufacture = context.Cars.First(x => x.Id == Id);
foreach (var pc in model.CarTechnicalWorks)
{
context.CarTechnicalWork.Add(new CarTechnicalWork
{
Car = manufacture,
TechnicalWork = context.TechnicalWorks.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_CarTechnicalWorks = null;
}
}
}