Coursework_ComputerStore_Li.../ComputerStoreDatabaseImplement/Models/Order.cs

120 lines
4.0 KiB
C#
Raw Normal View History

2023-04-07 20:46:13 +04:00
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerStoreDataModels.Enums;
using System.Runtime.ConstrainedExecution;
namespace ComputerStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int ID { get; private set; }
[Required]
public double Price { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Unknown;
2023-04-09 01:07:38 +04:00
[Required]
public int SellerID { get; private set; }
public virtual Seller Seller { get; set; }
2023-04-07 20:46:13 +04:00
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
2023-04-09 01:07:38 +04:00
[ForeignKey("OrderID")]
public virtual List<Consignment> _consignments { get; private set; } = new();
[ForeignKey("OrderID")]
public virtual List<Request> _requests { get; private set; } = new();
[NotMapped]
public List<IConsignmentModel> OrderConsignments
{
get
{
return _consignments.Select(x => (IConsignmentModel)x).ToList();
}
}
[NotMapped]
public List<IRequestModel> OrderRequests
{
get
{
return _requests.Select(x => (IRequestModel)x).ToList();
}
}
2023-04-07 20:46:13 +04:00
2023-04-09 01:07:38 +04:00
public static Order? Create(ComputerStoreDatabase context, OrderBindingModel? model)
2023-04-07 20:46:13 +04:00
{
if (model == null)
{
return null;
}
return new Order
{
2023-04-09 01:07:38 +04:00
ID = model.ID,
2023-04-07 20:46:13 +04:00
Price = model.Price,
Status = model.Status,
2023-04-09 01:07:38 +04:00
SellerID = model.SellerID,
2023-04-07 20:46:13 +04:00
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
2023-04-09 01:07:38 +04:00
_consignments = model.OrderConsignments.Select(x => context.Consignments.First(y => y.ID == x.ID)).ToList(),
_requests = model.OrderRequests.Select(x => context.Requests.First(y => y.ID == x.ID)).ToList(),
};
2023-04-07 20:46:13 +04:00
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2023-04-09 01:07:38 +04:00
Price = model.Price;
}
public void UpdateOrderRequests(ComputerStoreDatabase context, OrderBindingModel model)
{
if (model.OrderRequests == null)
{
return;
}
_requests = model.OrderRequests.Select(x => context.Requests.First(y => y.ID == x.ID)).ToList();
}
public void UpdateOrderConsignments(ComputerStoreDatabase context, OrderBindingModel model)
{
if (model.OrderRequests == null)
{
return;
}
_consignments = model.OrderConsignments.Select(x => context.Consignments.First(y => y.ID == x.ID)).ToList();
2023-04-07 20:46:13 +04:00
}
public OrderViewModel GetViewModel
{
get
{
var context = new ComputerStoreDatabase();
return new()
{
ID = ID,
2023-04-09 01:07:38 +04:00
Price = Price,
2023-04-07 20:46:13 +04:00
DateCreate = DateCreate,
DateImplement = DateImplement,
Status = Status,
SellerID = SellerID,
2023-04-09 01:07:38 +04:00
SellerUsername = context.Sellers.First(x => x.ID == SellerID)?.Username ?? string.Empty,
OrderConsignments = _consignments.Select(x => (IConsignmentModel)x.GetViewModel).ToList(),
OrderRequests = _requests.Select(x => (IRequestModel)x.GetViewModel).ToList()
2023-04-07 20:46:13 +04:00
};
}
}
}
}