ISEbd-21_Melnikov_I.O._CarS.../CarService/CarServiceDatabase/Models/WorkInRequest.cs

63 lines
1.8 KiB
C#

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; }
/// <summary>
/// Оплаты за ремонт
/// </summary>
[ForeignKey("WorkInRequestId")]
public virtual List<WorkPayment> 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,
RepairRequest = context.RepairRequests.First(x => x.Id == 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,
WorkName = Work.Name ?? string.Empty
};
}
}