ISEbd-21_Khaliullov_R.A._Co.../Canteen/CanteenDatabaseImplement/Models/Order.cs

97 lines
2.5 KiB
C#
Raw Permalink Normal View History

2024-04-30 18:48:42 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.ViewModels;
using CanteenDataModels.Enums;
using CanteenDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace CanteenDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int ClientId { get; set; }
public int? CookId { get; set; }
public int DishId { get; private set; }
public int DishCount { get; private set; }
[Required]
public double OrderPrice { get; private set; }
[Required]
public OrderStatus OrderStatus { get; private set; }
[Required]
public PaymentStatus PaymentStatus { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
[Required]
public virtual Dish Dish { get; set; }
public virtual Client Client { get; set; }
public virtual Cook? Cook { get; set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
ClientId = model.ClientId,
CookId = model.CookId,
DishId = model.DishId,
DishCount = model.DishCount,
OrderPrice = model.OrderPrice,
OrderStatus = model.OrderStatus,
PaymentStatus = model.PaymentStatus,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
OrderStatus = model.OrderStatus;
PaymentStatus = model.PaymentStatus;
DateImplement = model.DateImplement;
CookId = model.CookId;
}
public OrderViewModel GetViewModel
{
get
{
using var context = new CanteenDatabase();
return new OrderViewModel
{
DishId = DishId,
CookId = CookId,
CookFIO = Cook?.CookFIO ?? string.Empty,
DishName = context.Dishes.FirstOrDefault(x => x.Id == DishId)?.DishName ?? string.Empty,
ClientId = ClientId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
DishCount = DishCount,
OrderPrice = OrderPrice,
OrderStatus = OrderStatus,
PaymentStatus = PaymentStatus,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
};
}
}
}
}