2024-06-20 22:32:22 +04:00
|
|
|
|
using Contracts.BindingModels;
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using Contracts.SearchModels;
|
2024-06-20 22:32:22 +04:00
|
|
|
|
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;
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
|
|
|
|
|
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();
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public static Purchase Create(Database context, PurchaseBindingModel model)
|
2024-06-23 20:39:28 +04:00
|
|
|
|
{
|
|
|
|
|
return new Purchase()
|
|
|
|
|
{
|
|
|
|
|
DatePurchase = DateTime.Now,
|
|
|
|
|
UserId = Guid.NewGuid(),
|
|
|
|
|
Status = PurchaseStatus.Unknown,
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-06-24 01:07:53 +04:00
|
|
|
|
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-24 01:07:53 +04:00
|
|
|
|
public PurchaseViewModel GetViewModel => new()
|
2024-06-23 16:10:45 +04:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public void Update(PurchaseBindingModel model)
|
2024-06-20 22:32:22 +04:00
|
|
|
|
{
|
|
|
|
|
if (model is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Update purchase: binding model is null");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 01:07:53 +04:00
|
|
|
|
DatePurchase = model.DatePurchase;
|
|
|
|
|
UserId = model.UserId;
|
|
|
|
|
PurchaseProducts = model.PurchaseProducts;
|
|
|
|
|
Status = model.Status;
|
2024-06-20 22:32:22 +04:00
|
|
|
|
}
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public void UpdateProducts(Database context, PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var purchaseProducts = context.PurchaseProducts.Where(rec =>
|
|
|
|
|
rec.Id == model.Id).ToList();
|
|
|
|
|
if (purchaseProducts != null && purchaseProducts.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.PurchaseProducts.RemoveRange(purchaseProducts.Where(rec
|
|
|
|
|
=> !model.PurchaseProducts.ContainsKey(rec.ProductId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateProduct in purchaseProducts)
|
|
|
|
|
{
|
|
|
|
|
updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2;
|
|
|
|
|
model.PurchaseProducts.Remove(updateProduct.ProductId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var purchase = context.Purchases.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.PurchaseProducts)
|
|
|
|
|
{
|
|
|
|
|
context.PurchaseProducts.Add(new PurchaseProducts
|
|
|
|
|
{
|
|
|
|
|
Purchase = purchase,
|
|
|
|
|
Product = context.Products.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_purchaseProducts = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-11 12:18:10 +04:00
|
|
|
|
}
|