PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesDatabaseImplement/Models/Dress.cs

95 lines
3.5 KiB
C#
Raw Permalink Normal View History

2024-05-04 19:44:42 +04:00
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2024-06-22 21:11:56 +04:00
using System.Runtime.Serialization;
2024-05-04 19:44:42 +04:00
namespace SewingDressesDatabaseImplement.Models
{
2024-06-22 21:11:56 +04:00
[DataContract]
2024-05-04 19:44:42 +04:00
public class Dress : IDressModel
{
2024-06-22 21:11:56 +04:00
[DataMember]
2024-05-04 19:44:42 +04:00
public int Id { get; set; }
[Required]
2024-06-22 21:11:56 +04:00
[DataMember]
2024-05-04 19:44:42 +04:00
public string DressName { get; set; } = string.Empty;
[Required]
2024-06-22 21:11:56 +04:00
[DataMember]
2024-05-04 19:44:42 +04:00
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _dressComponents = null;
[NotMapped]
2024-06-22 21:11:56 +04:00
[DataMember]
2024-05-04 19:44:42 +04:00
public Dictionary<int, (IComponentModel, int)> DressComponents
{
get
{
if (_dressComponents == null)
{
_dressComponents = Components.ToDictionary(recDC => recDC.ComponentId, recDC => (recDC.Component as IComponentModel, recDC.Count));
}
return _dressComponents;
}
}
[ForeignKey("DressId")]
public virtual List<DressComponent> Components { get; set; } = new();
[ForeignKey("DressId")]
public virtual List<Order> Orders { get; set; } = new();
public static Dress Create(SewingDressesDatabase context, DressBindingModel model)
{
return new Dress()
{
Id = model.Id,
DressName = model.DressName,
Price = model.Price,
Components = model.DressComponents.Select(x => new DressComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(DressBindingModel model)
{
DressName = model.DressName;
Price = model.Price;
}
public DressViewModel GetViewModel => new()
{
Id = Id,
DressName = DressName,
Price = Price,
DressComponents = DressComponents
};
public void UpdateComponents(SewingDressesDatabase context, DressBindingModel model)
{
var dressComponents = context.DressComponents.Where(rec => rec.DressId == model.Id).ToList();
if (dressComponents != null && dressComponents.Count > 0)
{
context.DressComponents.RemoveRange(dressComponents.Where(rec => !model.DressComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in dressComponents)
{
updateComponent.Count = model.DressComponents[updateComponent.ComponentId].Item2;
model.DressComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var dress = context.Dresses.First(x => x.Id == Id);
foreach (var pc in model.DressComponents)
{
context.DressComponents.Add(new DressComponent
{
Dress = dress,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_dressComponents = null;
}
}
}