2024-06-20 22:32:22 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DataModels.Models;
|
2024-06-17 14:04:22 +04:00
|
|
|
|
using System;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
using System.Collections.Generic;
|
2024-06-17 14:04:22 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-06-23 17:04:11 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-06-22 18:13:07 +04:00
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
2024-06-11 12:18:10 +04:00
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Models
|
|
|
|
|
{
|
2024-06-17 14:04:22 +04:00
|
|
|
|
public class Product : IProduct
|
2024-06-11 12:18:10 +04:00
|
|
|
|
{
|
2024-06-17 14:04:22 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public Guid Id { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public double Rate { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public bool IsBeingSold { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int Amount { get; set; }
|
2024-06-23 17:04:11 +04:00
|
|
|
|
[ForeignKey("ProductId")]
|
|
|
|
|
public virtual List<PurchaseProducts> PurchaseProducts { get; set; } = new();
|
2024-06-20 22:32:22 +04:00
|
|
|
|
public ProductBindingModel GetBindingModel() => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Price = Price,
|
|
|
|
|
Rate = Rate,
|
|
|
|
|
IsBeingSold = IsBeingSold,
|
|
|
|
|
Amount = Amount
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static Product ToProductFromView(ProductViewModel model, Product product) => new()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Price = model.Price,
|
2024-06-23 17:04:26 +04:00
|
|
|
|
Rate = model.Rate,
|
2024-06-20 22:32:22 +04:00
|
|
|
|
IsBeingSold = model.IsBeingSold,
|
|
|
|
|
Amount = model.Amount
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static Product ToProductFromBinding(ProductBindingModel model, Product product) => new()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Rate = model.Rate,
|
|
|
|
|
IsBeingSold = model.IsBeingSold,
|
|
|
|
|
Amount = model.Amount
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-22 18:13:07 +04:00
|
|
|
|
public static Product Create(Database context, ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Product()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Rate = model.Rate,
|
|
|
|
|
IsBeingSold = model.IsBeingSold,
|
|
|
|
|
Amount = model.Amount
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Update(ProductBindingModel model)
|
2024-06-20 22:32:22 +04:00
|
|
|
|
{
|
|
|
|
|
if (model is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Update product: binding model is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
Rate = model.Rate;
|
|
|
|
|
IsBeingSold = model.IsBeingSold;
|
|
|
|
|
Amount = model.Amount;
|
|
|
|
|
}
|
2024-06-22 18:13:07 +04:00
|
|
|
|
public ProductViewModel GetViewModel
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
|
|
|
|
return new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Price = Price,
|
|
|
|
|
IsBeingSold = IsBeingSold,
|
2024-06-23 17:04:26 +04:00
|
|
|
|
Rate = Rate,
|
2024-06-22 18:13:07 +04:00
|
|
|
|
Amount = Amount
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-11 12:18:10 +04:00
|
|
|
|
}
|