PIbd-22_Chernyshev_Shabunov.../ComputerShopDatabaseImplement/Models/Request.cs

126 lines
3.6 KiB
C#
Raw Normal View History

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<int, IOrderModel>? _requestOrders = null;
[NotMapped]
public Dictionary<int, IOrderModel> RequestOrders
{
get
{
if (_requestOrders == null)
{
_requestOrders = Orders.ToDictionary(x => x.OrderId, x => x.Order as IOrderModel);
}
return _requestOrders;
}
}
[ForeignKey("RequestId")]
public virtual List<RequestOrder> 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;
}
2024-04-28 21:00:55 +04:00
if (!string.IsNullOrEmpty(model.ClientFIO))
{
ClientFIO = model.ClientFIO;
}
//DateMake не обновляю, потому что странно менять дату оформления заявки после её создания
}
public RequestViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,
AssemblyId = AssemblyId,
2024-04-28 21:00:55 +04:00
Assembly = Assembly,
DateRequest = DateRequest,
2024-04-28 21:00:55 +04:00
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;
}
2024-04-28 21:00:55 +04:00
//Связывание заявки со сборкой
2024-04-28 21:00:55 +04:00
public void ConnectAssembly(ComputerShopDatabase context, int assemblyId)
{
AssemblyId = assemblyId;
Assembly = context.Assemblies.First(x => x.Id == assemblyId);
}
}
}