SUBD_Labs/BeautySalonDatabaseImplement/Models/Visit.cs

71 lines
2.2 KiB
C#

using BeautySalonContracts.BindingModels;
using BeautySalonContracts.ViewModels;
using BeautySalonDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace BeautySalonDatabaseImplement.Models
{
public class Visit : IVisitModel
{
public int Id { get; private set; }
[Required]
public DateTime DateOfVisit { get; private set; }
public int ClientId { get; private set; }
public int ServiceId { get; private set; }
public string ClientFIO { get; set; } = string.Empty;
public string ServiceName { get; set; } = string.Empty;
[Required]
public double Sum { get; private set; }
public Client Client { get; set; }
public virtual Service Service { get; set; }
public static Visit? Create(VisitBindingModel? model)
{
if (model == null)
{
return null;
}
return new Visit()
{
Id = model.Id,
ClientId = model.ClientId,
ClientFIO = model.ClientFIO,
ServiceId = model.ServiceId,
ServiceName = model.ServiceName,
Sum = model.Sum,
DateOfVisit = model.DateOfVisit
};
}
public void Update(VisitBindingModel? model)
{
if (model == null)
{
return;
}
Sum = model.Sum;
DateOfVisit = model.DateOfVisit;
}
public VisitViewModel GetViewModel
{
get
{
using var context = new BeautySalonDatabase();
return new VisitViewModel
{
Id = Id,
ClientId = ClientId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
ServiceId = ServiceId,
ServiceName = context.Services.FirstOrDefault(x => x.Id == ServiceId)?.ServiceName ?? string.Empty,
Sum = Sum,
DateOfVisit = DateOfVisit
};
}
}
}
}