Coursework_ComputerStore_Li.../ComputerStoreDatabaseImplement/Models/Order.cs
2023-04-07 20:46:13 +04:00

93 lines
2.7 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; }
public int? ConsignmentID { get; private set; }
public int? RequestID { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Unknown;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
[Required]
public int SellerID { get; private set; }
public virtual Seller Seller { get; set; }
public virtual Consignment Consignment { get; private set; }
public virtual Request Request { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
Price = model.Price,
ConsignmentID = model.ConsignmentID,
RequestID = model.RequestID,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
SellerID = model.SellerID,
ID = model.ID,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel
{
get
{
var context = new ComputerStoreDatabase();
return new()
{
ID = ID,
ConsignmentID = ConsignmentID,
RequestID = RequestID,
DateCreate = DateCreate,
DateImplement = DateImplement,
Status = Status,
Price = Price,
SellerID = SellerID,
SellerUsername = context.Sellers.FirstOrDefault(x => x.ID == SellerID)?.Username ?? string.Empty
};
}
}
}
}