PIbd-22_Chernyshev_G.J._30_.../GarmentFactoryDatabaseImplement/Models/Order.cs
2024-05-05 18:46:39 +04:00

102 lines
3.0 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 GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryDataModels.Enums;
using GarmentFactoryDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GarmentFactoryDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; set; }
[Required]
public int ClientId { get; private set; }
[Required]
public int TextileId { get; private set; }
public int? ImplementerId { get; private set; }
[Required]
public int Count { get; 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? DateComplete { get; private set; }
// Для передачи названия изделия
public virtual Textile Textile { get; set; }
// Для передачи ФИО клиента
public virtual Client Client { get; set; }
public virtual Implementer? Implementer { get; set; } = new();
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
using var context = new GarmentFactoryDatabase();
return new Order()
{
Id = model.Id,
ClientId = model.ClientId,
TextileId = model.TextileId,
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateComplete = model.DateComplete,
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
using var context = new GarmentFactoryDatabase();
ImplementerId = model.ImplementerId;
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null;
Status = model.Status;
DateComplete = model.DateComplete;
context.SaveChanges();
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,
TextileId = TextileId,
ImplementerId = ImplementerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateComplete = DateComplete,
TextileName = Textile.TextileName,
ClientFIO = Client.ClientFIO,
ClientEmail = Client.Email,
ImplementerFIO = Implementer?.ImplementerFIO,
};
}
}