68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.ViewModels;
|
|
using DataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DatabaseImplement.Models
|
|
{
|
|
public class Accounts : IAccountModel
|
|
{
|
|
[Required]
|
|
public string Login { get; set; } = string.Empty;
|
|
|
|
public List<DateTime> AuthorizationAttemptsHistory { get; set; } = new List<DateTime>();
|
|
|
|
public int ResidenceCityId { get; set; }
|
|
public virtual Cities ResidenceCity { get; set; } = new();
|
|
|
|
public DateTime AccountCreationDate { get; set; }
|
|
|
|
public int Id { get; private set; }
|
|
|
|
public static Accounts? Create(AccountsDatabase context, AccountBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Accounts()
|
|
{
|
|
Id = model.Id,
|
|
Login = model.Login,
|
|
ResidenceCityId = model.ResidenceCityId,
|
|
ResidenceCity = context.Cities.First(x => x.Id == model.ResidenceCityId),
|
|
AuthorizationAttemptsHistory = model.AuthorizationAttemptsHistory.Select(date => date.ToUniversalTime()).ToList(),
|
|
AccountCreationDate = model.AccountCreationDate.ToUniversalTime()
|
|
};
|
|
}
|
|
|
|
public void Update(AccountBindingModel? model, AccountsDatabase context)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
Login = model.Login;
|
|
ResidenceCityId = model.ResidenceCityId;
|
|
ResidenceCity = context.Cities.First(x => x.Id == model.ResidenceCityId);
|
|
AuthorizationAttemptsHistory = model.AuthorizationAttemptsHistory.Select(date => date.ToUniversalTime()).ToList();
|
|
AccountCreationDate = model.AccountCreationDate.ToUniversalTime();
|
|
}
|
|
|
|
public AccountViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
Login = Login,
|
|
ResidenceCityId = ResidenceCity.Id,
|
|
ResidenceCityName = ResidenceCity.Name,
|
|
AuthorizationAttemptsHistory = AuthorizationAttemptsHistory,
|
|
AccountCreationDate = AccountCreationDate,
|
|
};
|
|
}
|
|
}
|