PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopDatabaseImplement/Models/Order.cs
Arkadiy Radaev 44fa88599e res8
2024-05-17 13:28:57 +04:00

115 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, GiftShopDatabase context)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
GiftId = model.GiftId,
GiftName = model.GiftName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Gift = context.Gifts.FirstOrDefault(x => x.Id == model.GiftId),
ClientId = model.ClientId,
Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId),
ImplementerId = model.ImplementerId,
Implementer = (model.ImplementerId.HasValue ? context.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId)
: null),
};
}
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 = Client.ClientFIO,
GiftName = Gift.GiftName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ImplementerFIO = (Implementer != null ? Implementer.ImplementerFIO : string.Empty)
};
}
}
}
}