CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs

100 lines
3.1 KiB
C#
Raw Normal View History

2024-04-29 01:55:20 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using ElectronicsShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
2024-04-29 18:34:49 +04:00
using System.ComponentModel.DataAnnotations.Schema;
2024-04-29 01:55:20 +04:00
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Models
{
public class Order : IOrderModel
{
public int ID { get; set; }
[Required]
2024-04-29 17:59:45 +04:00
public double Sum { get; set; }
2024-04-30 21:59:22 +04:00
[ForeignKey("UserID")]
2024-04-29 19:34:14 +04:00
public int UserID { get; set; }
[Required]
2024-04-29 01:55:20 +04:00
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[Required]
public PaymeantOption PaymeantOption { get; set; } = PaymeantOption.Неизвестно;
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
2024-04-29 01:55:20 +04:00
[Required]
2024-04-29 18:34:49 +04:00
public DateTime? DateImplement { get; set; }
2024-04-29 01:55:20 +04:00
[Required]
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();
2024-04-29 18:34:49 +04:00
[NotMapped]
public Dictionary<int, (IProductModel, int)> _productList
{
get {
if (ProductList == null) {
ProductList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count));
}
return _productList;
}
}
public virtual List<OrderProducts> Products { get; set; } = new();
public static Order? Create(Database context ,OrderBindingModel? model)
2024-04-29 01:55:20 +04:00
{
if (model == null)
{
return null;
}
return new Order()
{
ID = model.ID,
2024-04-29 19:34:14 +04:00
UserID=model.UserID,
2024-04-29 01:55:20 +04:00
Sum = model.Sum,
Status = model.Status,
PaymeantOption = model.PaymeantOption,
DateCreate = model.DateCreate,
2024-04-29 18:34:49 +04:00
Products = model.ProductList.Select(x => new OrderProducts
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
2024-04-29 01:55:20 +04:00
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
ID = model.ID;
2024-04-29 19:34:14 +04:00
UserID = model.UserID;
2024-04-29 01:55:20 +04:00
Sum = model.Sum;
PaymeantOption = model.PaymeantOption;
Status = model.Status;
DateCreate = model.DateCreate;
2024-04-29 18:34:49 +04:00
if (model.DateImplement != null)
2024-04-29 01:55:20 +04:00
{
2024-04-29 18:34:49 +04:00
DateImplement = model.DateImplement;
2024-04-29 01:55:20 +04:00
}
//todo ProductList
}
public OrderViewModel GetViewModel => new()
{
ID = ID,
2024-04-29 19:34:14 +04:00
UserID = UserID,
2024-04-29 01:55:20 +04:00
Sum = Sum,
PaymeantOption = PaymeantOption,
Status = Status,
DateCreate = DateCreate,
2024-04-29 19:34:14 +04:00
DateImplement = DateImplement,
2024-04-29 01:55:20 +04:00
ProductList = ProductList
};
}
}