120 lines
4.0 KiB
C#
120 lines
4.0 KiB
C#
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;
|
|
[Required]
|
|
public int SellerID { get; private set; }
|
|
public virtual Seller Seller { get; set; }
|
|
[Required]
|
|
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
|
public DateTime? DateImplement { get; private set; }
|
|
|
|
[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();
|
|
}
|
|
}
|
|
|
|
public static Order? Create(ComputerStoreDatabase context, OrderBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Order
|
|
{
|
|
ID = model.ID,
|
|
Price = model.Price,
|
|
Status = model.Status,
|
|
SellerID = model.SellerID,
|
|
DateCreate = model.DateCreate,
|
|
DateImplement = model.DateImplement,
|
|
_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(),
|
|
};
|
|
}
|
|
|
|
public void Update(OrderBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
Status = model.Status;
|
|
DateImplement = model.DateImplement;
|
|
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();
|
|
}
|
|
|
|
public OrderViewModel GetViewModel
|
|
{
|
|
get
|
|
{
|
|
var context = new ComputerStoreDatabase();
|
|
return new()
|
|
{
|
|
|
|
ID = ID,
|
|
Price = Price,
|
|
DateCreate = DateCreate,
|
|
DateImplement = DateImplement,
|
|
Status = Status,
|
|
SellerID = SellerID,
|
|
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()
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|