PIbd-22_Isaeva_A.I._FishFac.../FishFactoryDatabaseImplement/Models/Order.cs
2024-06-21 21:03:33 +04:00

81 lines
2.7 KiB
C#
Raw Permalink 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.ComponentModel.DataAnnotations;
using FishFactoryDataModel.Enums;
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModel.Models;
namespace FishFactoryDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int CannedId { get; private set; }
public virtual Canned Canned { get; set; }
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; set; }
public int? ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; set; } = new();
[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 static Order Create (FishFactoryDatabase context, OrderBindingModel model)
{
return new Order()
{
Id = model.Id,
CannedId = model.CannedId,
Canned = context.Canneds.First(x => x.Id == model.CannedId),
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId == null ? null : context.Implementers.First(i => i.Id == model.ImplementerId),
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
using var context = new FishFactoryDatabase();
ImplementerId = model.ImplementerId;
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
CannedId = CannedId,
CannedName = Canned.CannedName,
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
ClientEmail = Client.Email,
ImplementerId = ImplementerId,
ImplementerFIO = Implementer == null ? string.Empty : Implementer.ImplementerFIO,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}