2023-03-20 17:51:46 +04:00
|
|
|
|
using SecuritySystemContracts.BindingModels;
|
|
|
|
|
using SecuritySystemContracts.ViewModels;
|
|
|
|
|
using SecuritySystemDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2023-05-16 01:43:26 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
|
2023-03-20 17:51:46 +04:00
|
|
|
|
namespace SecuritySystemDatabaseImplement.Models
|
|
|
|
|
{
|
2023-05-16 01:43:26 +04:00
|
|
|
|
[DataContract]
|
2023-03-20 17:51:46 +04:00
|
|
|
|
public class Secure : ISecureModel
|
|
|
|
|
{
|
2023-05-16 01:43:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-20 17:51:46 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
2023-05-16 01:43:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-20 17:51:46 +04:00
|
|
|
|
public string SecureName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
2023-05-16 01:43:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-20 17:51:46 +04:00
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _SecureComponents = null;
|
|
|
|
|
[NotMapped]
|
2023-05-16 01:43:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-20 17:51:46 +04:00
|
|
|
|
public Dictionary<int, (IComponentModel, int)> SecureComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_SecureComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_SecureComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
|
|
|
(recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _SecureComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("SecureId")]
|
|
|
|
|
public virtual List<SecureComponent> Components { get; set; } = new();
|
|
|
|
|
[ForeignKey("SecureId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
public static Secure Create(SecuritySystemDatabase context, SecureBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Secure()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
SecureName = model.SecureName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.SecureComponents.Select(x => new SecureComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(SecureBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
SecureName = model.SecureName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public SecureViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
SecureName = SecureName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
SecureComponents = SecureComponents
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(SecuritySystemDatabase context, SecureBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var SecureComponents = context.SecureComponents.Where(rec =>
|
|
|
|
|
rec.SecureId == model.Id).ToList();
|
|
|
|
|
if (SecureComponents != null && SecureComponents.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.SecureComponents.RemoveRange(SecureComponents.Where(rec
|
|
|
|
|
=> !model.SecureComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateComponent in SecureComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.SecureComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.SecureComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var Secure = context.Secures.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.SecureComponents)
|
|
|
|
|
{
|
|
|
|
|
context.SecureComponents.Add(new SecureComponent
|
|
|
|
|
{
|
|
|
|
|
Secure = Secure,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_SecureComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|