PIbd-33_Abazov_A.A._KOP_29/AbazovApp/AccountsDataBaseImplement/Models/Account.cs
2023-11-29 23:40:28 +04:00

69 lines
2.0 KiB
C#

using AccountsContracts.BindingModels;
using AccountsContracts.ViewModels;
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement.Models
{
public class Account : IAccountModel
{
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
public int Id { get; private set; }
public int InterestId { get; set; }
public virtual Interest Interest { get; set; } = new();
public static Account? Create(AccountsDatabase context, AccountBindingModel? model)
{
if (model == null)
{
return null;
}
return new Account()
{
Id = model.Id,
Login = model.Login,
Password = model.Password,
Email = model.Email,
InterestId = model.InterestId,
Interest = context.Interests.First(x => x.Id == model.InterestId),
};
}
public void Update(AccountBindingModel? model, AccountsDatabase context)
{
if (model == null)
{
return;
}
Login = model.Login;
Password = model.Password;
InterestId = model.InterestId;
Interest = context.Interests.First(x => x.Id == model.InterestId);
}
public AccountViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Password = Password,
Email = Email,
InterestId = InterestId,
InterestName = Interest.Name,
};
}
}