Pibd-22_Presnyakova.V.V_Jew.../JewelryStoreDatabaseImplement/Models/Order.cs

101 lines
2.9 KiB
C#
Raw Normal View History

2023-03-05 18:39:34 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JewelryStoreContracts.ViewModels;
using JewelryStoreContracts.BindingModels;
using JewelryStoreDataModels.Models;
using JewelryStoreDataModels.Enums;
using System.ComponentModel.DataAnnotations;
namespace JewelryStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int JewelId { get; private set; }
2023-04-01 21:09:59 +04:00
[Required]
public int ClientId { get; set; }
2023-03-05 18:39:34 +04:00
public string JewelName { get; private set; } = string.Empty;
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Jewel Jewel { get; set; }
public virtual Client Client { get; set; }
2023-03-05 18:39:34 +04:00
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
JewelId = model.JewelId,
2023-04-01 21:09:59 +04:00
ClientId = model.ClientId,
2023-03-05 18:39:34 +04:00
JewelName = model.JewelName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
JewelId = model.JewelId;
JewelName = model.JewelName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel
2023-03-05 18:39:34 +04:00
{
get
{
using var context = new JewelryStoreDataBase();
return new OrderViewModel
{
Id = Id,
JewelId = JewelId,
ClientId = ClientId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
2023-04-15 19:27:19 +04:00
JewelName = context.Jewels.FirstOrDefault(x => x.Id == JewelId)?.JewelName ?? string.Empty,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}
2023-03-05 18:39:34 +04:00
}
}