PIbd-22_Petrushin_E.A._LawFirm/LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs
GokaPek c92e3a3ee5 2
2024-03-25 20:21:59 +04:00

98 lines
3.8 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 AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractLawFirmDataBaseImplement.Models;
namespace AbstractLawFirmDataBaseImplement.Models
{
public class Document : IDocumentModel
{
public int Id { get; set; }
[Required]
public string DocumentName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _productComponents =
null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> DocumentComponents
{
get
{
if (_productComponents == null)
{
_productComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _productComponents;
}
}
[ForeignKey("DocumentId")]
public virtual List<DocumentComponent> Components { get; set; } = new();
[ForeignKey("DocumentId")]
public virtual List<Order> Orders { get; set; } = new();
public static Document Create(AbstractLawFirmDataBase context, DocumentBindingModel model)
{
return new Document()
{
Id = model.Id,
DocumentName = model.DocumentName,
Price = model.Price,
Components = model.DocumentComponents.Select(x => new DocumentComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(DocumentBindingModel model)
{
DocumentName = model.DocumentName;
Price = model.Price;
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
DocumentName = DocumentName,
Price = Price,
DocumentComponents = DocumentComponents
};
public void UpdateComponents(AbstractLawFirmDataBase context, DocumentBindingModel model)
{
var documentComponents = context.DocumentComponents.Where(rec => rec.DocumentId == model.Id).ToList();
if (documentComponents != null && documentComponents.Count > 0)
{ // удалили те, которых нет в модели
context.DocumentComponents.RemoveRange(documentComponents.Where(rec => !model.DocumentComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in documentComponents)
{
updateComponent.Count = model.DocumentComponents[updateComponent.ComponentId].Item2;
model.DocumentComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var product = context.Documents.First(x => x.Id == Id);
foreach (var pc in model.DocumentComponents)
{
context.DocumentComponents.Add(new DocumentComponent
{
Document = product,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_productComponents = null;
}
}
}