PIbd-22_Smirnov_A.A._Securi.../SecuritySystem/SecuritySystemDatabaseImplement/Models/Secure.cs
2024-05-07 17:56:47 +04:00

119 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecuritySystemDatabaseImplement.Models
{
public class Secure : ISecureModel
{
public int Id { get; set; }
[Required]
public string SecureName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
public Dictionary<int, (ISensorModel, int)>? _secureSensors = null;
//это поле не будет "мапиться" в бд
[NotMapped]
public Dictionary<int, (ISensorModel, int)> SecureSensors
{
get
{
if (_secureSensors == null)
{
_secureSensors = Sensors
.ToDictionary(recPC => recPC.SensorId, recPC => (recPC.Sensor as ISensorModel, recPC.Count));
}
return _secureSensors;
}
}
//для реализации связи многие ко многим с заготовками
[ForeignKey("SecureId")]
public virtual List<SecureSensor> Sensors { 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,
Sensors = model.SecureSensors.Select(x => new SecureSensor
{
Sensor = context.Sensors.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,
SecureSensors = SecureSensors
};
Dictionary<int, (ISensorModel, int)> ISecureModel.SecureSensors => throw new NotImplementedException();
public void UpdateSensors(SecuritySystemDatabase context, SecureBindingModel model)
{
var secureSensors = context.SecureSensors.Where(rec => rec.SecureId == model.Id).ToList();
if (secureSensors != null && secureSensors.Count > 0)
{
// удалили те, которых нет в модели
context.SecureSensors.RemoveRange(secureSensors.Where(rec => !model.SecureSensors.ContainsKey(rec.SecureId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateSecure in secureSensors)
{
updateSecure.Count = model.SecureSensors[updateSecure.SecureId].Item2;
model.SecureSensors.Remove(updateSecure.SecureId);
}
context.SaveChanges();
}
var secure = context.Secures.First(x => x.Id == Id);
foreach (var pc in model.SecureSensors)
{
context.SecureSensors.Add(new SecureSensor
{
Secure = secure,
Sensor = context.Sensors.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_secureSensors = null;
}
}
}