PIAPS_CW/DatabaseImplement/Models/Purchase.cs
Аришина) d402b05d5f 1
2024-11-24 19:51:40 +04:00

111 lines
3.3 KiB
C#

using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DataModels.Enums;
using DataModels.Models;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace DatabaseImplement.Models
{
public class Purchase : IPurchase
{
public Guid Id { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public DateTime? DateClosed { get; set; }
[Required]
public Guid UserId { get; set; }
public double Cost { get; set; }
public int ProductCount { get; set; }
[Required]
public PurchaseStatus Status { get; private set; } = PurchaseStatus.Unknown;
[Required]
public bool IsPaid { get; set; }
public virtual User? User { get; set; }
[NotMapped]
public List<Product>? Products { get; set; }
public static Purchase Create(Database context, PurchaseBindingModel model)
{
return new Purchase()
{
Id = model.Id,
DateCreated = model.DateCreated,
UserId = model.UserId,
Status = model.Status,
Cost = model.Cost,
ProductCount = model.ProductCount,
IsPaid = model.IsPaid,
};
}
public PurchaseBindingModel GetBindingModel => new()
{
Id = Id,
DateCreated = DateCreated,
UserId = UserId,
Status = Status,
Cost = Cost,
ProductCount = ProductCount,
IsPaid = IsPaid,
};
public PurchaseViewModel GetViewModel => new()
{
Id = Id,
DateCreated = DateCreated,
DateClosed = DateClosed,
UserId = UserId,
Status = Status,
ProductCount = ProductCount,
Cost = Cost,
IsPaid = IsPaid,
};
public static Purchase ToPurchaseFromView(PurchaseViewModel model, Purchase purchase) => new()
{
Id = model.Id,
DateCreated = model.DateCreated,
DateClosed = model.DateClosed,
UserId = model.UserId,
Status = model.Status,
ProductCount = model.ProductCount,
Cost = model.Cost,
IsPaid = model.IsPaid,
};
public static Purchase ToPurchaseFromBinding(PurchaseBindingModel model, Purchase purchase) => new()
{
Id = model.Id,
DateCreated = model.DateCreated,
DateClosed = model.DateClosed,
UserId = model.UserId,
Status = model.Status,
ProductCount = model.ProductCount,
Cost = model.Cost,
IsPaid = model.IsPaid,
};
public void Update(PurchaseBindingModel model)
{
if (model is null)
{
throw new ArgumentNullException("Update purchase: binding model is null");
}
DateClosed = model.DateClosed;
Status = model.Status;
ProductCount = model.ProductCount;
Cost = model.Cost;
IsPaid = model.IsPaid;
}
}
}