PIbd-23-Salin-O.A.-IceCream.../IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs

86 lines
2.8 KiB
C#
Raw Normal View History

2024-03-04 19:46:39 +04:00
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Enums;
using IceCreamShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IceCreamShopDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
[Required]
public int IceCreamId { get; private set; }
public virtual IceCream IceCream { get; private set; }
2024-04-01 20:04:44 +04:00
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; }
2024-04-15 15:31:58 +04:00
[Required]
public int? ImplementerId { get; private set; } = null;
public virtual Implementer? Implementer { get; private set; }
2024-04-15 15:25:51 +04:00
public static Order? Create(IceCreamShopDataBase context, OrderBindingModel model)
2024-03-04 19:46:39 +04:00
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
IceCreamId = model.IceCreamId,
2024-04-01 20:04:44 +04:00
IceCream = context.IceCreams.FirstOrDefault(x => x.Id == model.IceCreamId),
2024-04-02 11:13:02 +04:00
ClientId = model.ClientId,
Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId),
2024-04-15 15:25:51 +04:00
ImplementerId = model.ImplementerId,
Implementer = context.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId),
2024-04-02 11:13:02 +04:00
};
2024-03-04 19:46:39 +04:00
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
IceCreamId = IceCreamId,
2024-04-01 20:04:44 +04:00
ClientId = ClientId,
2024-04-15 15:25:51 +04:00
ImplementerId = ImplementerId,
2024-03-04 19:46:39 +04:00
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
IceCreamName = IceCream.IceCreamName,
2024-03-04 19:46:39 +04:00
Id = Id,
2024-04-15 15:25:51 +04:00
ClientFIO = Client.ClientFIO,
2024-04-15 15:31:58 +04:00
ImplementerFIO = (Implementer != null ? Implementer.ImplementerFIO : string.Empty)
2024-03-04 19:46:39 +04:00
};
}
}