using Contracts.BindingModels; using Contracts.Converters; using Contracts.ViewModels; using DataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace DatabaseImplement.Models { public class Sell : ISell { public Guid Id { get; set; } public DateTime DateSell { get; set; } public Guid? UserId { get; set; } private Dictionary? _sellProducts = null; public virtual User? User { get; set; } [DataMember] [NotMapped] public Dictionary PurchaseProducts { get { if (_sellProducts == null) { _sellProducts = Products.ToDictionary(e => e.ProductId, e => (e.Product as IProduct, e.Count)); } return _sellProducts; } set { } } [ForeignKey("SellId")] public virtual List Products { get; set; } = new(); public static Sell Create(Database context, SellBindingModel model) { return new Sell() { Id = model.Id, DateSell = model.DateSell, UserId = model.UserId, }; } public SellBindingModel GetBindingModel => new() { Id = Id, DateSell = DateSell, UserId = UserId, }; public SellViewModel GetViewModel => new() { Id = Id, DateSell = DateSell, UserId = UserId, }; public static Sell ToSellFromView(SellViewModel model, Sell sell) => new() { Id = model.Id, DateSell = model.DateSell }; public static Sell ToSellFromBinding(SellBindingModel model, Sell sell) => new() { Id = model.Id, DateSell = model.DateSell, }; public void Update(SellBindingModel model) { if (model is null) { throw new ArgumentNullException("Update user: binding model is null"); } DateSell = model.DateSell; UserId = model.UserId; } } }