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
|
|
|
|
|
{
|
|
|
|
|
//!!! МОЖНО ДЕЛАТЬ ПО АНАЛОГИИ С Product(Textile) ИЛИ С Component. Делаю по аналогии с Textile
|
|
|
|
|
public class Shipment : IShipmentModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; private set; }
|
|
|
|
|
|
|
|
|
|
//!!!мб не нужен (для передачи логина пользователя)
|
|
|
|
|
public virtual User User { get; set; }
|
|
|
|
|
|
|
|
|
|
//!!!МБ ТУТ НЕ НУЖЕН string.Empty
|
|
|
|
|
[Required]
|
|
|
|
|
public string ProviderName { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
//!!!МБ ТУТ НЕ НУЖЕН DateTime.Now
|
|
|
|
|
[Required]
|
|
|
|
|
public DateTime DateShipment { get; set; } = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
//!!!МБ ТУТ КАК-ТО ПО-ДРУГОМУ
|
|
|
|
|
private Dictionary<int, IOrderModel>? _shipmentOrders = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IOrderModel>? ShipmentOrders
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_shipmentOrders == null)
|
|
|
|
|
{
|
|
|
|
|
//!!!ТУТ ПРОПИСАТЬ ЛОГИКУ (НЕ ЗНАЮ ПОКА, КАК)
|
2024-04-25 17:44:09 +04:00
|
|
|
|
_shipmentOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
return _shipmentOrders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//!!!ПРОВЕРИТЬ, ЧТО ТУТ ПРАВИЛЬНО СДЕЛАНО (ЧТО НАЗВАНИЕ ПРАВИЛЬНОЕ У ВНЕШ. КЛЮЧА, ЧТО ПРАВИЛЬНЫЙ ТИП в <>)
|
2024-04-25 17:44:09 +04:00
|
|
|
|
[ForeignKey("ShipmentId")]
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public virtual List<ShipmentOrder> Orders { get; set; } = new();
|
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
//!!!ПРОВЕРИТЬ, ЧТО ПРАВИЛЬНО ПРИСВАИВАЮ ЗНАЧЕНИЕ СПИСКУ ЗАКАЗОВ
|
|
|
|
|
public static Shipment Create(ComputerShopDatabase context, ShipmentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Shipment()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
ProviderName = model.ProviderName,
|
|
|
|
|
DateShipment = model.DateShipment,
|
|
|
|
|
Orders = model.ShipmentOrders.Select(x => new ShipmentOrder
|
|
|
|
|
{
|
|
|
|
|
Order = context.Orders.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
//!!!МБ ТУТ КАКИЕ-ТО ДРУГИЕ ПОЛЯ НАДО БУДЕТ ОБНОВЛЯТЬ
|
|
|
|
|
public void Update(ShipmentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
ProviderName = model.ProviderName;
|
|
|
|
|
DateShipment = model.DateShipment;
|
|
|
|
|
}
|
|
|
|
|
//!!!МБ ТУТ ЕЩЁ ЧТО-ТО ПРИСВАИВАТЬ
|
|
|
|
|
public ShipmentViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
ProviderName = ProviderName,
|
|
|
|
|
DateShipment = DateShipment,
|
|
|
|
|
ShipmentOrders = ShipmentOrders
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//!!!МБ ЭТОТ МЕТОД БУДЕТ НЕ НУЖЕН, ПОТОМУ ЧТО ПО ЗАДАНИЮ НЕ НАПИСАНО, ЧТО МОЖНО ОБНОВЛЯТЬ
|
|
|
|
|
//!!!МБ НАЗВАТЬ КАК-ТО ПО-ДРУГОМУ (мб метод вынести в Implement)
|
|
|
|
|
//!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ
|
|
|
|
|
//!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ
|
|
|
|
|
//!!!делаю как в Textile
|
|
|
|
|
public void UpdateOrders(ComputerShopDatabase context, ShipmentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var shipmentOrders = context.ShipmentOrders.Where(x => x.ShipmentId == model.Id).ToList();
|
|
|
|
|
//удаление тех заказов, которых нет в модели
|
|
|
|
|
if (shipmentOrders != null && shipmentOrders.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
//!!!ТУТ МБ НЕ x.OrderId, а x.ShipmentId, но не факт (вряд ли)
|
|
|
|
|
context.ShipmentOrders.RemoveRange(shipmentOrders.Where(x => !model.ShipmentOrders.ContainsKey(x.OrderId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
//добавление новых заказов
|
|
|
|
|
var currentShipment = context.Shipments.First(x => x.Id == Id);
|
|
|
|
|
foreach (var shipment_order in model.ShipmentOrders)
|
|
|
|
|
{
|
|
|
|
|
context.ShipmentOrders.Add(new ShipmentOrder
|
|
|
|
|
{
|
|
|
|
|
Shipment = currentShipment,
|
|
|
|
|
Order = context.Orders.First(x => x.Id == shipment_order.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_shipmentOrders = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|