2023-03-12 18:42:24 +04:00
|
|
|
|
using LawFirmContracts.BindingModels;
|
|
|
|
|
using LawFirmContracts.ViewModels;
|
|
|
|
|
using LawFirmDataModels.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;
|
2023-04-23 23:05:26 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2023-03-12 18:42:24 +04:00
|
|
|
|
|
|
|
|
|
namespace LawFirmDatabaseImplement.Models
|
|
|
|
|
{
|
2023-04-23 23:05:26 +04:00
|
|
|
|
[DataContract]
|
2023-03-12 18:42:24 +04:00
|
|
|
|
public class Shop : IShopModel
|
|
|
|
|
{
|
2023-04-23 23:05:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-12 18:42:24 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
2023-04-23 23:05:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-12 18:42:24 +04:00
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
2023-04-23 23:05:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-12 18:42:24 +04:00
|
|
|
|
public string Adress { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
2023-04-23 23:05:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-12 18:42:24 +04:00
|
|
|
|
public DateTime OpeningDate { get; set; }
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ShopId")]
|
|
|
|
|
public List<ShopDocument> Documents { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (IDocumentModel, int)>? _shopDocuments = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2023-04-23 23:05:26 +04:00
|
|
|
|
[DataMember]
|
2023-03-12 18:42:24 +04:00
|
|
|
|
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(LawFirmDatabase context, ShopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Shop()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Adress = model.Adress,
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Adress = model.Adress;
|
|
|
|
|
OpeningDate = model.OpeningDate;
|
|
|
|
|
MaxCountDocuments = model.MaxCountDocuments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Adress = Adress,
|
|
|
|
|
OpeningDate = OpeningDate,
|
|
|
|
|
ShopDocuments = ShopDocuments,
|
|
|
|
|
MaxCountDocuments = MaxCountDocuments
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateDocuments(LawFirmDatabase 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 updateDoc in ShopDocuments)
|
|
|
|
|
{
|
|
|
|
|
updateDoc.Count = model.ShopDocuments[updateDoc.DocumentId].Item2;
|
|
|
|
|
model.ShopDocuments.Remove(updateDoc.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|