Creating models and some realization
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Enums;
|
||||
using ComputerShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
internal class EquipmentReceiving : IEquipmentReceivingModel
|
||||
{
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public int Id { get; set; }
|
||||
public EquipmentReceivingStatus Status { get; private set; } = EquipmentReceivingStatus.Неизвестен;
|
||||
|
||||
public static EquipmentReceiving? Create(EquipmentReceivingBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new EquipmentReceiving
|
||||
{
|
||||
Status = model.Status,
|
||||
DateImplement = model.DateImplement,
|
||||
Id = model.Id,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(EquipmentReceivingBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public EquipmentReceivingViewModel GetViewModel => new()
|
||||
{
|
||||
DateImplement = DateImplement,
|
||||
Id = Id,
|
||||
Status = Status,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Enums;
|
||||
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
|
||||
{
|
||||
internal class Supply : ISupplyModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public SupplyStatus Status { get; private set; } = SupplyStatus.Неизвестен;
|
||||
[Required]
|
||||
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public int OrderId { get; set; }
|
||||
|
||||
public int ReceivingId { get; set; }
|
||||
private Dictionary<int, (IOrderModel, int)>? _supplyOrders =
|
||||
null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IOrderModel, int)> SupplyOrders
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_supplyOrders == null)
|
||||
{
|
||||
_supplyOrders = Orders
|
||||
.ToDictionary(recPC => recPC.OrderId, recPC =>
|
||||
(recPC.Order as IOrderModel, recPC.Count));
|
||||
}
|
||||
return _supplyOrders;
|
||||
}
|
||||
}
|
||||
[ForeignKey("SupplyId")]
|
||||
public virtual List<SupplyOrder> Orders { get; set; } = new();
|
||||
[ForeignKey("SupplyId")]
|
||||
public virtual List<EquipmentReceiving> Receivings { get; set; } = new();
|
||||
|
||||
public static Supply Create(ComputerShopDatabase context, SupplyBindingModel model)
|
||||
{
|
||||
return new Supply
|
||||
{
|
||||
Id = model.Id,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
Orders = model.SupplyOrders.Select(x => new
|
||||
SupplyOrder
|
||||
{
|
||||
Order = context.Orders.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(SupplyBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
public SupplyViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
SupplyOrders = SupplyOrders
|
||||
};
|
||||
public void UpdateOrders(ComputerShopDatabase context, SupplyBindingModel model)
|
||||
{
|
||||
var SupplyOrders = context.SupplyOrders.Where(rec =>
|
||||
rec.Id == model.Id).ToList();
|
||||
if (SupplyOrders != null && SupplyOrders.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.SupplyOrders.RemoveRange(SupplyOrders.Where(rec
|
||||
=> !model.SupplyOrders.ContainsKey(rec.OrderId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateOrder in SupplyOrders)
|
||||
{
|
||||
updateOrder.Count = model.SupplyOrders[updateOrder.OrderId].Item2;
|
||||
model.SupplyOrders.Remove(updateOrder.OrderId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var supply = context.Supplies.First(x => x.Id == Id);
|
||||
foreach (var pc in model.SupplyOrders)
|
||||
{
|
||||
context.SupplyOrders.Add(new SupplyOrder
|
||||
{
|
||||
Supply = supply,
|
||||
Order = context.Orders.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_supplyOrders = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,10 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int SupplyId { get; set; }
|
||||
[Required]
|
||||
public int OrderId { get; set; }
|
||||
[Required]
|
||||
public int SupplyId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Order Order { get; set; } = new();
|
||||
public virtual Supply Supply { get; set; } = new();
|
||||
|
||||
Reference in New Issue
Block a user