2024-04-25 01:10:50 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
//!!!ДОБАВИТЬ МЕТОД ПО ПРИВЯЗКЕ СБОРКИ
|
2024-04-25 17:44:09 +04:00
|
|
|
|
//!!!МБ У Id сделать private set у ВСЕХ МОИХ МОДЕЛЕЙ
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public class Request : IRequestModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual User User { get; set; }
|
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
public int? AssemblyId { get; set; }
|
|
|
|
|
|
2024-04-27 15:55:58 +04:00
|
|
|
|
//!!!МБ ТУТ НЕ НАДО, НО НА ПОДОБИЕ С ЗАКАЗОМ И СВЯЗЬЮ С implmenter В ЛАБАХ НАДО
|
|
|
|
|
public virtual Assembly? Assembly { get; set; } = new();
|
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
[Required]
|
2024-04-25 23:01:53 +04:00
|
|
|
|
public DateTime DateRequest { get; set; }
|
2024-04-25 17:44:09 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string ClientFIO { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IOrderModel>? _requestOrders = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IOrderModel> RequestOrders
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_requestOrders == null)
|
|
|
|
|
{
|
|
|
|
|
//!!!ТУТ ПРОПИСАТЬ ЛОГИКУ (НЕ ЗНАЮ ПОКА, КАК)
|
2024-04-25 17:44:09 +04:00
|
|
|
|
if (_requestOrders == null)
|
|
|
|
|
{
|
|
|
|
|
_requestOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));
|
|
|
|
|
}
|
2024-04-25 01:10:50 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return _requestOrders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
[ForeignKey("RequestId")]
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public virtual List<RequestOrder> Orders { get; set; } = new();
|
|
|
|
|
|
2024-04-27 15:55:58 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public static Request Create(ComputerShopDatabase context, RequestBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Request() {
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
UserId = model.UserId,
|
2024-04-27 15:55:58 +04:00
|
|
|
|
User = context.Users.First(x => x.Id == model.UserId),
|
2024-04-25 01:10:50 +04:00
|
|
|
|
AssemblyId = model.AssemblyId,
|
2024-04-27 15:55:58 +04:00
|
|
|
|
Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : null,
|
2024-04-25 23:01:53 +04:00
|
|
|
|
DateRequest = model.DateRequest,
|
2024-04-25 01:10:50 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-04-27 15:55:58 +04:00
|
|
|
|
if (model == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
2024-04-25 23:01:53 +04:00
|
|
|
|
DateRequest = model.DateRequest;
|
2024-04-27 15:55:58 +04:00
|
|
|
|
AssemblyId = model.AssemblyId.HasValue ? model.AssemblyId : AssemblyId;
|
|
|
|
|
Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : Assembly;
|
2024-04-25 01:10:50 +04:00
|
|
|
|
ClientFIO = model.ClientFIO;
|
2024-04-27 15:55:58 +04:00
|
|
|
|
context.SaveChanges();
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
//!!!МБ ТУТ НЕ ВСЁ НАДО ПРИСВАИВАТЬ
|
|
|
|
|
public RequestViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
AssemblyId = AssemblyId,
|
2024-04-25 23:01:53 +04:00
|
|
|
|
DateRequest = DateRequest,
|
2024-04-25 01:10:50 +04:00
|
|
|
|
ClientFIO = ClientFIO
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-27 15:55:58 +04:00
|
|
|
|
|
2024-04-25 23:01:53 +04:00
|
|
|
|
//!!!МБ ЭТОТ МЕТОД БУДЕТ НЕ НУЖЕН, ПОТОМУ ЧТО ПО ЗАДАНИЮ НЕ НАПИСАНО, ЧТО МОЖНО ОБНОВЛЯТЬ
|
|
|
|
|
//!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ
|
|
|
|
|
//!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ
|
|
|
|
|
//!!!делаю как в 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;
|
|
|
|
|
}
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
}
|