PIbd-23_Sergunov_M.A._GiftShop/GiftShop/GiftShopDatabaseImplement/Models/Order.cs

111 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Enums;
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
namespace GiftShopDatabaseImplement.Models
{
[DataContract]
public class Order : IOrderModel
{
[DataMember]
public int Id { get; private set; }
[DataMember]
public int GiftId { get; private set; }
[Required]
[DataMember]
public int ClientId { get; set; }
[DataMember]
public int? ImplementerId { get; private set; }
[DataMember]
public string GiftName { get; private set; } = string.Empty;
[DataMember]
[Required]
public int Count { get; private set; }
[DataMember]
[Required]
public double Sum { get; private set; }
[DataMember]
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[DataMember]
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
[DataMember]
public DateTime? DateImplement { get; private set; }
public virtual Gift Gift { get; set; }
public Client Client { get; set; }
public Implementer? Implementer { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
GiftId = model.GiftId,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
GiftName = model.GiftName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
ImplementerId = model.ImplementerId;
}
public OrderViewModel GetViewModel
{
get
{
using var context = new GiftShopDatabase();
return new OrderViewModel
{
Id = Id,
GiftId = GiftId,
ClientId = ClientId,
ImplementerId = ImplementerId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
GiftName = context.Gifts.FirstOrDefault(x => x.Id == GiftId)?.GiftName ?? string.Empty,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty
};
}
}
}
}