PIbd21.LyovushkinaA.A.LawFirm/LawFirm/AbstractLawFirmDatabaseImplement/Models/Shop.cs

115 lines
4.0 KiB
C#
Raw Normal View History

2024-03-24 22:31:39 +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;
namespace AbstractLawFirmDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime OpeningDate { get; set; }
[ForeignKey("ShopId")]
public List<ShopDocument> Documents { get; set; } = new();
private Dictionary<int, (IDocumentModel, int)>? _shopDocuments = null;
[NotMapped]
public Dictionary<int, (IDocumentModel, int)> ShopDocuments
{
get
{
if (_shopDocuments == null)
{
_shopDocuments = Documents.ToDictionary(recPC => recPC.DocumentId, recPC => (recPC.Document as IDocumentModel, recPC.Count));
}
return _shopDocuments;
}
}
[Required]
public int MaxCountDocuments { get; set; }
public static Shop Create(AbstractLawFirmDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
OpeningDate = model.OpeningDate,
Documents = model.ShopDocuments.Select(x => new ShopDocument
{
Document = context.Documents.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
MaxCountDocuments = model.MaxCountDocuments
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Address = model.Address;
OpeningDate = model.OpeningDate;
MaxCountDocuments = model.MaxCountDocuments;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
OpeningDate = OpeningDate,
ShopDocuments = ShopDocuments,
MaxCountDocuments = MaxCountDocuments
};
public void UpdateDocuments(AbstractLawFirmDatabase context, ShopBindingModel model)
{
var ShopDocuments = context.ShopDocuments.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopDocuments != null && ShopDocuments.Count > 0)
{
// удалили те, которых нет в модели
context.ShopDocuments.RemoveRange(ShopDocuments.Where(rec => !model.ShopDocuments.ContainsKey(rec.DocumentId)));
context.SaveChanges();
ShopDocuments = context.ShopDocuments.Where(rec => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей
foreach (var updateDocument in ShopDocuments)
{
updateDocument.Count = model.ShopDocuments[updateDocument.DocumentId].Item2;
model.ShopDocuments.Remove(updateDocument.DocumentId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var elem in model.ShopDocuments)
{
context.ShopDocuments.Add(new ShopDocument
{
Shop = shop,
Document = context.Documents.First(x => x.Id == elem.Key),
Count = elem.Value.Item2
});
context.SaveChanges();
}
_shopDocuments = null;
}
}
}