Pibd-22_Presnyakova.V.V_Jew.../JewelryStoreDatabaseImplement/Models/Order.cs
2023-05-02 14:50:43 +04:00

115 lines
3.3 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 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;
using System.Runtime.Serialization;
namespace JewelryStoreDatabaseImplement.Models
{
[DataContract]
public class Order : IOrderModel
{
public int Id { get; private set; }
[DataMember]
public int JewelId { get; private set; }
[Required]
[DataMember]
public int ClientId { get; set; }
[DataMember]
public int? ImplementerId { get; private set; }
[DataMember]
public string JewelName { get; private set; } = string.Empty;
[Required]
[DataMember]
public int Count { get; private set; }
[Required]
[DataMember]
public double Sum { get; private set; }
[Required]
[DataMember]
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; }
public Implementer? Implementer { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
JewelId = model.JewelId,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
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;
ImplementerId = model.ImplementerId;
}
public OrderViewModel GetViewModel
{
get
{
using var context = new JewelryStoreDataBase();
return new OrderViewModel
{
Id = Id,
JewelId = JewelId,
ClientId = ClientId,
ImplementerId = ImplementerId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
JewelName = context.Jewels.FirstOrDefault(x => x.Id == JewelId)?.JewelName ?? string.Empty,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty
};
}
}
}
}