PIbd-22_Petrushin_E.A._LawFirm/LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs

101 lines
3.9 KiB
C#
Raw Normal View History

2024-03-25 20:21:59 +04:00
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;
2024-04-09 23:28:01 +04:00
using AbstractLawFirmDataBaseImplement;
2024-03-25 20:21:59 +04:00
2024-04-09 23:28:01 +04:00
namespace AbstractLawFirmDatabaseImplement.Models
2024-03-25 20:21:59 +04:00
{
public class Document : IDocumentModel
{
public int Id { get; set; }
[Required]
public string DocumentName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
2024-04-20 11:43:03 +04:00
private Dictionary<int, (IComponentModel, int)>? _documentComponents = null;
2024-03-25 20:21:59 +04:00
[NotMapped]
public Dictionary<int, (IComponentModel, int)> DocumentComponents
{
get
{
2024-04-09 23:28:01 +04:00
if (_documentComponents == null)
2024-03-25 20:21:59 +04:00
{
2024-04-09 23:28:01 +04:00
_documentComponents = Components
2024-03-25 20:21:59 +04:00
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
2024-04-09 23:28:01 +04:00
return _documentComponents;
2024-03-25 20:21:59 +04:00
}
}
[ForeignKey("DocumentId")]
public virtual List<DocumentComponent> Components { get; set; } = new();
[ForeignKey("DocumentId")]
public virtual List<Order> Orders { get; set; } = new();
2024-04-09 23:28:01 +04:00
public static Document Create(AbstractLawFirmDatabase context,
DocumentBindingModel model)
2024-03-25 20:21:59 +04:00
{
return new Document()
{
Id = model.Id,
DocumentName = model.DocumentName,
Price = model.Price,
2024-04-09 23:28:01 +04:00
Components = model.DocumentComponents.Select(x => new
DocumentComponent
2024-03-25 20:21:59 +04:00
{
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
};
2024-04-09 23:28:01 +04:00
public void UpdateComponents(AbstractLawFirmDatabase context,
DocumentBindingModel model)
2024-03-25 20:21:59 +04:00
{
var documentComponents = context.DocumentComponents.Where(rec => rec.DocumentId == model.Id).ToList();
if (documentComponents != null && documentComponents.Count > 0)
{ // удалили те, которых нет в модели
2024-04-09 23:28:01 +04:00
context.DocumentComponents.RemoveRange(documentComponents.Where(rec
=> !model.DocumentComponents.ContainsKey(rec.ComponentId)));
2024-03-25 20:21:59 +04:00
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in documentComponents)
{
updateComponent.Count = model.DocumentComponents[updateComponent.ComponentId].Item2;
model.DocumentComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
2024-04-09 23:28:01 +04:00
var document = context.Documents.First(x => x.Id == Id);
2024-03-25 20:21:59 +04:00
foreach (var pc in model.DocumentComponents)
{
context.DocumentComponents.Add(new DocumentComponent
{
2024-04-09 23:28:01 +04:00
Document = document,
2024-03-25 20:21:59 +04:00
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
2024-04-09 23:28:01 +04:00
_documentComponents = null;
2024-03-25 20:21:59 +04:00
}
}
2024-04-09 23:28:01 +04:00
}