SUBD_Labs/BeautySalonDatabaseImplement/Models/Visit.cs

71 lines
2.2 KiB
C#
Raw Normal View History

using BeautySalonContracts.BindingModels;
2023-05-13 16:10:51 +04:00
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]
2023-05-13 19:29:18 +04:00
public double Sum { get; private set; }
2023-05-13 23:36:15 +04:00
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,
2023-05-13 16:10:51 +04:00
ServiceId = model.ServiceId,
ServiceName = model.ServiceName,
Sum = model.Sum,
DateOfVisit = model.DateOfVisit
};
}
2023-05-13 16:10:51 +04:00
public void Update(VisitBindingModel? model)
{
if (model == null)
{
return;
}
2023-05-13 23:36:15 +04:00
2023-05-13 16:10:51 +04:00
Sum = model.Sum;
DateOfVisit = model.DateOfVisit;
}
2023-05-13 23:36:15 +04:00
public VisitViewModel GetViewModel
2023-05-13 16:10:51 +04:00
{
2023-05-13 23:36:15 +04:00
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
};
}
}
}
}