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

74 lines
1.7 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 Work : IWorkModel
{
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, Column(TypeName = "decimal (3,1)")]
public decimal Duration { get; private set; }
[Required]
public int WorkerId { get; private set; }
/// <summary>
/// Работы в заявке
/// </summary>
[ForeignKey("WorkId")]
public virtual List<WorkInRequest> WorksInRequest { get; set; } = new();
public virtual Worker Worker { get; set; } = new();
public static Work? Create(WorkBindingModel? model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Duration = model.Duration,
WorkerId = model.WorkerId
};
}
public static Work Create(WorkViewModel model)
{
return new()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Duration = model.Duration,
WorkerId = model.WorkerId
};
}
public void Update(WorkBindingModel? model)
{
if (model == null)
{
return;
}
Id = model.Id;
Name = model.Name;
Price = model.Price;
Duration = model.Duration;
WorkerId = model.WorkerId;
}
public WorkViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
Duration = Duration,
WorkerId = WorkerId
};
}
}