CourseWork_CompShop/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Purchase.cs

88 lines
2.8 KiB
C#
Raw Normal View History

2023-04-05 19:08:51 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Enums;
using ComputerShopDataModels.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Models
{
internal class Purchase : IPurchaseModel
{
public string ComponentName { get; private set; } = String.Empty;
public int Id { get; private set; }
[Required]
public int ComponentId { get; private set; }
[Required]
public int ClientId { get; private set; }
[Required]
2023-04-05 19:08:51 +04:00
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public PurchaseStatus Status { get; private set; } = PurchaseStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Component Component { get; set; }
public virtual Client Client { get; set; }
2023-04-05 19:08:51 +04:00
public static Purchase? Create(PurchaseBindingModel? model)
{
if (model == null)
{
return null;
}
return new Purchase
{
ComponentId = model.ComponentId,
ComponentName = model.ComponentName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Id = model.Id,
ClientId = model.ClientId
2023-04-05 19:08:51 +04:00
};
}
public void Update(PurchaseBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public PurchaseViewModel GetViewModel
2023-04-05 19:08:51 +04:00
{
get
{
var context = new ComputerShopDatabase();
return new()
{
ComponentId = ComponentId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
ComponentName = context.Components.FirstOrDefault(x => x.Id == ComponentId)?.ComponentName ?? string.Empty,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty
};
}
}
2023-04-05 19:08:51 +04:00
}
}