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

66 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LawFirmContracts;
using LawFirmContracts.BindingModels;
using LawFirmContracts.Models;
using LawFirmContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LawFirmDatabase.Models
{
public class Service : IServiceModel
{
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 ItemId { get; private set; }
public int CaseId { get; private set; }
public virtual Item Items { get; set; } = new();
public virtual Case Cases { get; set; } = new();
public static Service? Create(LawFirmDBContext context, IServiceModel? model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Items = context.Items.First(x => x.Id == model.ItemId),
Cases = context.Cases.First(x => x.Id == model.CaseId)
};
}
public void Update(LawFirmDBContext context, IServiceModel? model)
{
if (model == null)
{
return;
}
Id = model.Id;
Name = model.Name;
Price = model.Price;
Items = context.Items.First(x => x.Id == model.ItemId);
Cases = context.Cases.First(x => x.Id == model.CaseId);
}
public ServiceViewModel? GetViewModel() => new()
{
Id = Id,
Name = Name,
Price = Price,
ItemId = ItemId,
};
}
}