91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.ViewModels;
|
|
using DataModels.Enums;
|
|
using DataModels.Models;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DatabaseImplement.Models
|
|
{
|
|
public class Purchase : IPurchase
|
|
{
|
|
public Guid Id { get; set; }
|
|
[Required]
|
|
public DateTime DatePurchase { get; set; }
|
|
[Required]
|
|
public Guid UserId { get; set; }
|
|
[Required]
|
|
public PurchaseStatus Status { get; private set; } = PurchaseStatus.Unknown;
|
|
private Dictionary<Guid, (IProduct, int)>? _purchaseProducts = null;
|
|
public virtual User? User { get; set; }
|
|
[DataMember]
|
|
[NotMapped]
|
|
public Dictionary<Guid, (IProduct, int)> PurchaseProducts
|
|
{
|
|
get
|
|
{
|
|
if (_purchaseProducts == null)
|
|
{
|
|
_purchaseProducts = Products.ToDictionary(e => e.ProductId, e => (e.Product as IProduct, e.Count));
|
|
}
|
|
return _purchaseProducts;
|
|
}
|
|
set { }
|
|
}
|
|
[ForeignKey("PurchaseId")]
|
|
public virtual List<PurchaseProducts> Products { get; set; } = new();
|
|
public PurchaseBindingModel GetBindingModel() => new()
|
|
{
|
|
Id = Id,
|
|
DatePurchase = DatePurchase,
|
|
UserId = UserId,
|
|
PurchaseProducts = PurchaseProducts,
|
|
Status = Status
|
|
};
|
|
|
|
public PurchaseViewModel GetViewModel() => new()
|
|
{
|
|
Id = Id,
|
|
DatePurchase = DatePurchase,
|
|
UserId = UserId,
|
|
PurchaseProducts = PurchaseProducts,
|
|
Status = Status
|
|
};
|
|
|
|
public static Purchase ToPurchaseFromView(PurchaseViewModel model, Purchase purchase) => new()
|
|
{
|
|
Id = model.Id,
|
|
DatePurchase = model.DatePurchase,
|
|
UserId = model.UserId,
|
|
PurchaseProducts = model.PurchaseProducts,
|
|
Status = model.Status
|
|
};
|
|
|
|
public static Purchase ToPurchaseFromBinding(PurchaseBindingModel model, Purchase purchase) => new()
|
|
{
|
|
Id = model.Id,
|
|
DatePurchase = model.DatePurchase,
|
|
UserId = model.UserId,
|
|
PurchaseProducts = model.PurchaseProducts,
|
|
Status = model.Status
|
|
};
|
|
|
|
public void Update(PurchaseBindingModel model, Purchase purchase)
|
|
{
|
|
if (model is null)
|
|
{
|
|
throw new ArgumentNullException("Update purchase: binding model is null");
|
|
}
|
|
|
|
DatePurchase = purchase.DatePurchase;
|
|
}
|
|
}
|
|
}
|