2023-04-08 07:28:40 +04:00

55 lines
1.7 KiB
C#

using LawFirmContracts.BindingModels;
using LawFirmContracts.Models;
using LawFirmContracts.ViewModels;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using LawFirmContracts;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LawFirmDatabase.Models
{
public class Item : IItemModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required, Column(TypeName = "decimal (10,2)")]
public decimal Price { get; private set; }
[Required]
public int PaymentId { get; private set; }
[ForeignKey("ItemId")]
public virtual List<Service> Services { get; set; } = new();
public virtual Payment Payments { get; set; } = new();
public static Item? Create(LawFirmDBContext context, ItemBindingModel? model)
{
if (model == null)
{
return null;
}
return new()
{
Name = model.Name,
Price = model.Price,
Payments = context.Payments.First(x => x.Id == model.PaymentId)
};
}
public void Update(LawFirmDBContext context, ItemBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Price = model.Price;
Payments = context.Payments.First(x => x.Id == model.PaymentId);
}
public ItemViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
PaymentId = PaymentId,
};
}
}