using AccountContracts.BindingModels; using AccountContracts.ViewModels; using AccountsDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AccountDatabaseImplement.Models { public class Account : IAccountModel { public int Id { get; private set; } [Required] public string Login { get; set; } public string? Warnings { get; set; } = null; public float? Rating { get; set; } = null; [Required] public int RoleId { get; set; } public virtual Role Role { get; set; } = new(); public static Account? Create(AccountBindingModel? model, AccountDatabase context) { if (model == null) { return null; } return new Account() { Id = model.Id, Login = model.Login, Warnings = model.Warnings, Rating = model.Rating, RoleId = model.RoleId, Role = context.Roles.First(x => x.Id == model.RoleId), }; } public void Update(AccountBindingModel? model) { if (model == null) { return; } Login = model.Login; Warnings = model.Warnings; Rating = model.Rating; } public AccountViewModel GetViewModel => new() { Id = Id, Login = Login, Warnings = Warnings, Rating = Rating, RoleId = RoleId, RoleName = Role.RoleName, }; } }