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

75 lines
2.1 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();
[Required]
public string Avatar { get; set; } = string.Empty;
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),
Avatar = model.Avatar,
};
}
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);
Avatar = model.Avatar;
}
public AccountViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Password = Password,
Email = Email,
InterestId = InterestId,
InterestName = Interest.Name,
Avatar = Avatar,
};
}
}