using CarServiceContracts.BindingModels; using CarServiceContracts.Models; using CarServiceContracts.ViewModels; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace CarServiceDatabase.Models { public class WorkInRequest : IWorkInRequestModel { public int Id { get; private set; } [Required] public int Count { get; private set; } [Required, Column(TypeName = "decimal (10,2)")] public decimal Cost { get; private set; } [Required] public int RepairRequestId { get; private set; } [Required] public int WorkId { get; private set; } /// /// Оплаты за ремонт /// [ForeignKey("WorkInRequestId")] public virtual List WorkPayments { get; set; } = new(); public virtual RepairRequest RepairRequest { get; set; } = new(); public virtual Work Work { get; set; } = new(); public static WorkInRequest? Create(CarServiceDbContext context, WorkInRequestBindingModel? model) { if (model == null) { return null; } return new() { Count = model.Count, Cost = context.Works.First(x => x.Id == model.WorkId).Price * model.Count, RepairRequestId = model.RepairRequestId, Work = context.Works.First(x => x.Id == model.WorkId) }; } public void Update(CarServiceDbContext context, WorkInRequestBindingModel? model) { if (model == null) { return; } Count = model.Count; Cost = context.Works.First(x => x.Id == model.WorkId).Price * model.Count; RepairRequestId = model.RepairRequestId; Work = context.Works.First(x => x.Id == model.WorkId); } public WorkInRequestViewModel GetViewModel => new() { Id = Id, Count = Count, Cost = Cost, RepairRequestId = RepairRequestId, WorkId = WorkId }; } }