ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallationDatabaseImplement/Models/Package.cs
Максим Куклев ed3e400b30 загрузил
2024-06-17 19:27:42 +04:00

108 lines
3.8 KiB
C#

using SoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using System.Runtime.Serialization;
namespace SoftwareInstallationDatabaseImplement.Models
{
public class Software : IPackageModel
{
[DataMember]
public int Id { get; set; }
[DataMember]
[Required]
public string RepairName { get; set; } = string.Empty;
[DataMember]
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
[DataMember]
[NotMapped]
public Dictionary<int, (IComponentModel, int)> SoftwareComponents
{
get
{
if (_SoftwareComponents == null)
{
_SoftwareComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _SoftwareComponents;
}
}
[ForeignKey("SoftwareId")]
public virtual List<PackageComponent> Components { get; set; } = new();
[ForeignKey("SoftwareId")]
public virtual List<Order> Orders { get; set; } = new();
public static Software Create(SoftwareInstallationDataBase context,
PackageBindingModel model)
{
return new Software()
{
Id = model.Id,
SoftwareName = model.SoftwareName,
Price = model.Price,
Components = model.SoftwareComponents.Select(x => new
PackageComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PackageBindingModel model)
{
SoftwareName = model.SoftwareName;
Price = model.Price;
}
public PackageViewModel GetViewModel => new()
{
Id = Id,
SoftwareName = SoftwareName,
Price = Price,
SoftwareComponents = SoftwareComponents
};
public void UpdateComponents(SoftwareInstallationDataBase context,
PackageBindingModel model)
{
var SoftwareComponents = context.SoftwareComponents.Where(rec =>
rec.SoftwareId == model.Id).ToList();
if (SoftwareComponents != null && SoftwareComponents.Count > 0)
{
context.SoftwareComponents.RemoveRange(SoftwareComponents.Where(rec
=> !model.SoftwareComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in SoftwareComponents)
{
updateComponent.Count =
model.SoftwareComponents[updateComponent.ComponentId].Item2;
model.SoftwareComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var Software = context.Softwares.First(x => x.Id == Id);
foreach (var pc in model.SoftwareComponents)
{
context.SoftwareComponents.Add(new PackageComponent
{
Software = Software,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_SoftwareComponents = null;
}
}
}