145 lines
4.4 KiB
C#
145 lines
4.4 KiB
C#
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; }
|
||
|
||
//!!!МБ ТУТ НЕ НАДО, НО НА ПОДОБИЕ С ЗАКАЗОМ И СВЯЗЬЮ С implmenter В ЛАБАХ НАДО
|
||
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)
|
||
{
|
||
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;
|
||
}
|
||
if (model.DateRequest != null)
|
||
{
|
||
DateRequest = model.DateRequest;
|
||
}
|
||
using var context = new ComputerShopDatabase();
|
||
//if (model.AssemblyId.HasValue && model.AssemblyId > 0)
|
||
//{
|
||
// AssemblyId = model.AssemblyId;
|
||
// Assembly = context.Assemblies.FirstOrDefault(x => x.Id == model.AssemblyId);
|
||
//}
|
||
if (!string.IsNullOrEmpty(model.ClientFIO))
|
||
{
|
||
ClientFIO = model.ClientFIO;
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
//!!!МБ ТУТ НАДО ПРИСВАИВАТЬ ЗНАЧЕНИЕ Assembly
|
||
public RequestViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
UserId = UserId,
|
||
AssemblyId = AssemblyId,
|
||
Assembly = Assembly,
|
||
DateRequest = DateRequest,
|
||
ClientFIO = ClientFIO,
|
||
RequestOrders = RequestOrders
|
||
};
|
||
|
||
|
||
//!!!МБ ЭТОТ МЕТОД БУДЕТ НЕ НУЖЕН, ПОТОМУ ЧТО ПО ЗАДАНИЮ НЕ НАПИСАНО, ЧТО МОЖНО ОБНОВЛЯТЬ
|
||
//!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ
|
||
//!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ
|
||
//!!!делаю как в Textile
|
||
public void UpdateOrders(ComputerShopDatabase context, RequestBindingModel model)
|
||
{
|
||
var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList();
|
||
//удаление тех заказов, которых нет в модели
|
||
if (requestOrders != null && requestOrders.Count > 0)
|
||
{
|
||
//!!!ТУТ МБ НЕ x.OrderId, а x.RequestId, но не факт (вряд ли)
|
||
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);
|
||
}
|
||
}
|
||
}
|