PIAPS_CW/DatabaseImplement/Models/Sell.cs

103 lines
3.0 KiB
C#
Raw Normal View History

2024-06-20 22:32:22 +04:00
using Contracts.BindingModels;
2024-06-23 20:39:28 +04:00
using Contracts.Converters;
2024-06-20 22:32:22 +04:00
using Contracts.ViewModels;
using DataModels.Models;
using System;
2024-06-11 12:18:10 +04:00
using System.Collections.Generic;
2024-06-24 15:06:42 +04:00
using System.ComponentModel.DataAnnotations.Schema;
2024-06-11 12:18:10 +04:00
using System.Linq;
2024-06-24 15:06:42 +04:00
using System.Runtime.Serialization;
2024-06-11 12:18:10 +04:00
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
2024-06-20 22:32:22 +04:00
public class Sell : ISell
2024-06-11 12:18:10 +04:00
{
2024-06-20 22:32:22 +04:00
public Guid Id { get; set; }
public DateTime DateSell { get; set; }
2024-06-24 15:06:42 +04:00
public Guid? UserId { get; set; }
private Dictionary<Guid, (IProduct, int)>? _sellProducts = null;
public virtual User? User { get; set; }
[DataMember]
[NotMapped]
public Dictionary<Guid, (IProduct, int)> 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<SellProducts> Products { get; set; } = new();
2024-06-23 20:39:28 +04:00
public static Sell Create(Database context, SellBindingModel model)
{
return new Sell()
{
Id = model.Id,
DateSell = model.DateSell,
2024-06-24 15:06:42 +04:00
UserId = model.UserId,
2024-06-23 20:39:28 +04:00
};
}
2024-06-20 22:32:22 +04:00
public SellBindingModel GetBindingModel => new()
2024-06-20 22:32:22 +04:00
{
Id = Id,
2024-06-23 20:39:28 +04:00
DateSell = DateSell,
2024-06-24 15:06:42 +04:00
UserId = UserId,
2024-06-20 22:32:22 +04:00
};
public SellViewModel GetViewModel => new()
2024-06-23 20:39:28 +04:00
{
Id = Id,
DateSell = DateSell,
2024-06-24 15:06:42 +04:00
UserId = UserId,
2024-06-23 20:39:28 +04:00
};
public static Sell ToSellFromView(SellViewModel model, Sell sell) => new()
2024-06-20 22:32:22 +04:00
{
Id = model.Id,
DateSell = model.DateSell
};
public static Sell ToSellFromBinding(SellBindingModel model, Sell sell) => new()
{
Id = model.Id,
DateSell = model.DateSell,
};
2024-06-23 20:39:28 +04:00
public void Update(SellBindingModel model)
2024-06-20 22:32:22 +04:00
{
if (model is null)
{
throw new ArgumentNullException("Update user: binding model is null");
}
2024-06-23 20:39:28 +04:00
DateSell = model.DateSell;
2024-06-24 15:06:42 +04:00
UserId = model.UserId;
2024-06-20 22:32:22 +04:00
}
public void UpdateProducts(Database context, SellBindingModel model)
{
var sellProducts = context.SellProducts.Where(rec =>
rec.Id == model.Id).ToList();
if (sellProducts != null && sellProducts.Count > 0)
{ // удалили те, которых нет в модели
context.SellProducts.RemoveRange(sellProducts.Where(rec
=> !model.SellProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateProduct in sellProducts)
{
updateProduct.Count = model.SellProducts[updateProduct.ProductId].Item2;
model.SellProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
}
2024-06-11 12:18:10 +04:00
}
}