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 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, }; } }