PIAPS_CW/DatabaseImplement/Models/Supply.cs

90 lines
2.8 KiB
C#
Raw Normal View History

2024-06-22 12:18:43 +04:00
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Enums;
using DataModels.Models;
using System;
using System.Collections;
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 Supply : ISupply
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
2024-06-22 15:10:46 +04:00
public Guid SupplierId { get; set; }
[Required]
2024-06-22 12:18:43 +04:00
public DateTime Date { get; set; }
[Required]
public SupplyStatus Status { get; set; } = SupplyStatus.Pending;
private Dictionary<Guid, (IProduct, int)>? _supplyProducts = null;
[NotMapped]
public Dictionary<Guid, (IProduct, int)> SupplyProducts
{
get
{
if (_supplyProducts == null)
{
_supplyProducts = Products
.ToDictionary(recPC => recPC.Id, recPC =>
(recPC.Product as IProduct, recPC.Count));
}
return _supplyProducts;
}
}
[ForeignKey("SupplyId")]
public virtual List<SupplyProduct> Products { get; set; } = new();
2024-06-22 15:10:46 +04:00
public virtual Supplier Supplier { get; set; }
2024-06-22 12:18:43 +04:00
public static Supply Create(Database context, SupplyBindingModel model)
{
return new Supply()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
2024-06-22 15:10:46 +04:00
Date = model.Date,
SupplierId = model.SupplierId,
2024-06-22 12:18:43 +04:00
Products = model.SupplyProducts.Select(x => new
SupplyProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
2024-06-23 21:35:37 +04:00
}).ToList(),
Status = SupplyStatus.Pending,
2024-06-22 12:18:43 +04:00
};
}
public void Update(SupplyBindingModel model)
{
2024-06-23 22:18:51 +04:00
Status = model.Status;
2024-06-22 12:18:43 +04:00
}
2024-06-22 15:10:46 +04:00
public SupplyViewModel GetViewModel
2024-06-22 12:18:43 +04:00
{
2024-06-22 15:10:46 +04:00
get
{
var context = new Database();
return new()
{
Id = Id,
Name = Name,
Price = Price,
Products = SupplyProducts,
2024-06-23 22:18:51 +04:00
SupplierId = SupplierId,
2024-06-22 15:10:46 +04:00
Date = Date,
Status = Status,
2024-06-23 22:18:51 +04:00
SupplierName = Supplier.Name,
2024-06-22 15:10:46 +04:00
};
}
}
2024-06-22 12:18:43 +04:00
}
}