130 lines
3.2 KiB
C#
130 lines
3.2 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.ViewModels;
|
|
using DataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace DatabaseImplement.Models
|
|
{
|
|
public class Product : IProduct
|
|
{
|
|
[Required]
|
|
public Guid Id { get; set; }
|
|
public Guid? SaleId { 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; }
|
|
public string? Category { get; set; }
|
|
public string? Description { get; set; }
|
|
public virtual Sale? Sale { get; set; }
|
|
[Required]
|
|
public int Amount { get; set; }
|
|
[ForeignKey("ProductId")]
|
|
public virtual List<SellProducts> SellProducts { get; set; } = new();
|
|
public ProductBindingModel GetBindingModel() => new()
|
|
{
|
|
Id = Id,
|
|
Name = Name,
|
|
Price = Price,
|
|
Rate = Rate,
|
|
IsBeingSold = IsBeingSold,
|
|
Amount = Amount,
|
|
Category = Category,
|
|
Description = Description,
|
|
SaleId = SaleId
|
|
};
|
|
|
|
public static Product ToProductFromView(ProductViewModel model, Product product) => new()
|
|
{
|
|
Id = model.Id,
|
|
Name = model.Name,
|
|
Price = model.Price,
|
|
Rate = model.Rate,
|
|
IsBeingSold = model.IsBeingSold,
|
|
Amount = model.Amount,
|
|
Category = model.Category,
|
|
SaleId = model.SaleId
|
|
};
|
|
|
|
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,
|
|
Description = model.Description,
|
|
Category = model.Category,
|
|
SaleId = model.SaleId
|
|
};
|
|
|
|
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,
|
|
Description = model.Description,
|
|
Category = model.Category,
|
|
SaleId = model.SaleId
|
|
};
|
|
}
|
|
|
|
|
|
public void Update(ProductBindingModel model)
|
|
{
|
|
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;
|
|
Description = model.Description;
|
|
Category = model.Category;
|
|
SaleId = model.SaleId;
|
|
}
|
|
public ProductViewModel GetViewModel
|
|
{
|
|
get
|
|
{
|
|
double saleValue = 0;
|
|
if (Sale is not null)
|
|
saleValue = Sale.Value;
|
|
return new()
|
|
{
|
|
Id = Id,
|
|
Name = Name,
|
|
Price = Price,
|
|
IsBeingSold = IsBeingSold,
|
|
Rate = Rate,
|
|
Amount = Amount,
|
|
Description = Description,
|
|
Category = Category,
|
|
SaleId = SaleId,
|
|
ActualPrice = Price / 100 * (100 - saleValue)
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|