2024-06-20 22:32:22 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DataModels.Enums;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
using DataModels.Models;
|
2024-06-23 16:10:45 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-06-23 16:10:45 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
using System.Linq;
|
2024-06-23 16:10:45 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
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; }
|
2024-06-23 16:10:45 +04:00
|
|
|
|
[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()
|
2024-06-20 22:32:22 +04:00
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
2024-06-23 16:10:45 +04:00
|
|
|
|
DatePurchase = DatePurchase,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
PurchaseProducts = PurchaseProducts,
|
|
|
|
|
Status = Status
|
2024-06-20 22:32:22 +04:00
|
|
|
|
};
|
2024-06-11 12:18:10 +04:00
|
|
|
|
|
2024-06-23 16:10:45 +04:00
|
|
|
|
public PurchaseViewModel GetViewModel() => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
DatePurchase = DatePurchase,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
PurchaseProducts = PurchaseProducts,
|
|
|
|
|
Status = Status
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static Purchase ToPurchaseFromView(PurchaseViewModel model, Purchase purchase) => new()
|
2024-06-20 22:32:22 +04:00
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
2024-06-23 16:10:45 +04:00
|
|
|
|
DatePurchase = model.DatePurchase,
|
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
PurchaseProducts = model.PurchaseProducts,
|
|
|
|
|
Status = model.Status
|
|
|
|
|
};
|
2024-06-20 22:32:22 +04:00
|
|
|
|
|
|
|
|
|
public static Purchase ToPurchaseFromBinding(PurchaseBindingModel model, Purchase purchase) => new()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
DatePurchase = model.DatePurchase,
|
2024-06-23 16:10:45 +04:00
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
PurchaseProducts = model.PurchaseProducts,
|
|
|
|
|
Status = model.Status
|
|
|
|
|
};
|
2024-06-20 22:32:22 +04:00
|
|
|
|
|
|
|
|
|
public void Update(PurchaseBindingModel model, Purchase purchase)
|
|
|
|
|
{
|
|
|
|
|
if (model is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Update purchase: binding model is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DatePurchase = purchase.DatePurchase;
|
|
|
|
|
}
|
2024-06-11 12:18:10 +04:00
|
|
|
|
}
|
|
|
|
|
}
|