PIbd-22_Petrushin_E.A._LawFirm/LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs
2024-04-20 11:43:03 +04:00

101 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 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;
using AbstractLawFirmDataBaseImplement;
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)>? _documentComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> DocumentComponents
{
get
{
if (_documentComponents == null)
{
_documentComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _documentComponents;
}
}
[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 document = context.Documents.First(x => x.Id == Id);
foreach (var pc in model.DocumentComponents)
{
context.DocumentComponents.Add(new DocumentComponent
{
Document = document,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_documentComponents = null;
}
}
}