ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallationDatabaseImplement/Models/Package.cs

108 lines
3.8 KiB
C#
Raw Normal View History

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