using ComputerShopContracts.BindingModels; using ComputerShopContracts.ViewModels; using ComputerShopDataModels.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 ComputerShopDatabaseImplement.Models { //!!!МБ У Id сделать private set у ВСЕХ МОИХ МОДЕЛЕЙ public class Request : IRequestModel { public int Id { get; set; } [Required] public int UserId { get; set; } public virtual User User { get; set; } public int? AssemblyId { get; set; } public virtual Assembly? Assembly { get; set; } = new(); [Required] public DateTime DateRequest { get; set; } [Required] public string ClientFIO { get; set; } = string.Empty; private Dictionary? _requestOrders = null; [NotMapped] public Dictionary RequestOrders { get { if (_requestOrders == null) { _requestOrders = Orders.ToDictionary(x => x.OrderId, x => x.Order as IOrderModel); } return _requestOrders; } } [ForeignKey("RequestId")] public virtual List Orders { get; set; } = new(); public static Request Create(ComputerShopDatabase context, RequestBindingModel model) { return new Request() { Id = model.Id, UserId = model.UserId, User = context.Users.First(x => x.Id == model.UserId), AssemblyId = model.AssemblyId, Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : null, DateRequest = model.DateRequest, ClientFIO = model.ClientFIO, Orders = model.RequestOrders.Select(x => new RequestOrder { Order = context.Orders.First(y => y.Id == x.Key) }).ToList() }; } public void Update(RequestBindingModel model) { if (model == null) { return; } if (!string.IsNullOrEmpty(model.ClientFIO)) { ClientFIO = model.ClientFIO; } //DateMake не обновляю, потому что странно менять дату оформления заявки после её создания } public RequestViewModel GetViewModel => new() { Id = Id, UserId = UserId, AssemblyId = AssemblyId, Assembly = Assembly, DateRequest = DateRequest, ClientFIO = ClientFIO, RequestOrders = RequestOrders }; //!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ //!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ public void UpdateOrders(ComputerShopDatabase context, RequestBindingModel model) { var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList(); //удаление тех заказов, которых нет в модели if (requestOrders != null && requestOrders.Count > 0) { context.RequestOrders.RemoveRange(requestOrders.Where(x => !model.RequestOrders.ContainsKey(x.OrderId))); context.SaveChanges(); } //добавление новых заказов var currentRequest = context.Requests.First(x => x.Id == Id); foreach (var request_order in model.RequestOrders) { context.RequestOrders.Add(new RequestOrder { Request = currentRequest, Order = context.Orders.First(x => x.Id == request_order.Key) }); context.SaveChanges(); } _requestOrders = null; } //Связывание заявки со сборкой public void ConnectAssembly(ComputerShopDatabase context, int assemblyId) { AssemblyId = assemblyId; Assembly = context.Assemblies.First(x => x.Id == assemblyId); } } }