PIbd-32_Chubykina_P.P._COP_11/COP/ClientDataBaseImplement/Models/Client.cs

69 lines
1.9 KiB
C#

using ClientsDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using ClientsContracts.BindingModels;
using ClientsContracts.ViewModels;
namespace ClientDataBaseImplement.Models
{
public class Client : IClientModel
{
[Required]
public string Fio { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
public int Id { get; private set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; } = new();
[Required]
public string Photo { get; set; } = string.Empty;
public static Client? Create(ClientsDatabase context, ClientBindingModel? model)
{
if (model == null)
{
return null;
}
return new Client()
{
Id = model.Id,
Fio = model.Fio,
Email = model.Email,
CategoryId = model.CategoryId,
Category = context.Categories.First(x => x.Id == model.CategoryId),
Photo = model.Photo,
};
}
public void Update(ClientBindingModel? model, ClientsDatabase context)
{
if (model == null)
{
return;
}
Fio = model.Fio;
CategoryId = model.CategoryId;
Category = context.Categories.First(x => x.Id == model.CategoryId);
Photo = model.Photo;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
Fio = Fio,
Email = Email,
CategoryId = CategoryId,
CategoryName = Category.Name,
Photo = Photo,
};
}
}