PIAPS_CW/DatabaseImplement/Models/Purchase.cs

91 lines
2.6 KiB
C#
Raw Normal View History

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;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
2024-06-11 12:18:10 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2024-06-11 12:18:10 +04:00
using System.Linq;
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; }
[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,
DatePurchase = DatePurchase,
UserId = UserId,
PurchaseProducts = PurchaseProducts,
Status = Status
2024-06-20 22:32:22 +04:00
};
2024-06-11 12:18:10 +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,
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,
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
}
}