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

117 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
{
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]
2024-04-28 21:00:55 +04:00
public Dictionary<int, IOrderModel> ShipmentOrders
{
get
{
if (_shipmentOrders == null)
{
_shipmentOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));
}
return _shipmentOrders;
}
}
//!!!ПРОВЕРИТЬ, ЧТО ТУТ ПРАВИЛЬНО СДЕЛАНО (ЧТО НАЗВАНИЕ ПРАВИЛЬНОЕ У ВНЕШ. КЛЮЧА, ЧТО ПРАВИЛЬНЫЙ ТИП в <>)
[ForeignKey("ShipmentId")]
public virtual List<ShipmentOrder> Orders { get; set; } = new();
//!!!ПРОВЕРИТЬ, ЧТО ПРАВИЛЬНО ПРИСВАИВАЮ ЗНАЧЕНИЕ СПИСКУ ЗАКАЗОВ
public static Shipment Create(ComputerShopDatabase context, ShipmentBindingModel model)
{
return new Shipment()
{
Id = model.Id,
UserId = model.UserId,
2024-04-28 21:00:55 +04:00
User = context.Users.First(x => x.Id == 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)
{
2024-04-28 21:00:55 +04:00
if (model == null)
{
return;
}
if (!string.IsNullOrEmpty(model.ProviderName))
{
ProviderName = model.ProviderName;
}
DateShipment = model.DateShipment;
}
//!!!МБ ТУТ ЕЩЁ ЧТО-ТО ПРИСВАИВАТЬ
public ShipmentViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,
ProviderName = ProviderName,
DateShipment = DateShipment,
ShipmentOrders = ShipmentOrders
};
//!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ
//!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ
public void UpdateOrders(ComputerShopDatabase context, ShipmentBindingModel model)
{
var shipmentOrders = context.ShipmentOrders.Where(x => x.ShipmentId == model.Id).ToList();
//удаление тех заказов, которых нет в модели
if (shipmentOrders != null && shipmentOrders.Count > 0)
{
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;
}
}
}