PIAPS_CW/DatabaseImplement/Models/User.cs

88 lines
2.6 KiB
C#
Raw Normal View History

2024-06-04 21:16:50 +04:00
using Contracts.BindingModels;
using Contracts.SearchModels;
2024-06-04 21:16:50 +04:00
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
2024-06-22 21:15:11 +04:00
using System.ComponentModel;
2024-06-04 21:16:50 +04:00
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
2024-06-22 21:15:11 +04:00
public class User : IUser
{
public Guid Id { get; set; }
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
[Required]
public string FirstName { get; set; } = string.Empty;
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
[Required]
public string SecondName { get; set; } = string.Empty;
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
[Required]
public string PasswordHash { get; set; } = string.Empty;
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
[Required]
public string Email { get; set; } = string.Empty;
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
[Required]
public DateTime Birthday { get; set; }
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
public Role? Role { get; set; }
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
public bool OnlyImportantMails { get; set; } = false;
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
public UserBindingModel GetBindingModel() => new()
{
Id = Id,
FirstName = FirstName,
SecondName = SecondName,
Email = Email,
PasswordHash = PasswordHash,
Birthday = Birthday,
OnlyImportantMails = OnlyImportantMails,
Role = Role?.GetBindingModel() ?? new()
};
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
public static User ToUserFromView(UserViewModel model, Role role) => new()
{
Id = model.Id,
FirstName = model.FirstName,
SecondName = model.SecondName,
Email = model.Email,
Birthday = model.Birthday,
Role = role
};
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
public static User ToUserFromBinding(UserBindingModel model, Role role) => new()
{
Id = model.Id,
FirstName = model.FirstName,
SecondName = model.SecondName,
Email = model.Email,
PasswordHash = model.PasswordHash,
Birthday = model.Birthday,
OnlyImportantMails = model.OnlyImportantMails,
Role = role
};
2024-06-04 21:16:50 +04:00
2024-06-22 21:15:11 +04:00
public void Update(UserBindingModel model, Role role)
{
if (model is null)
{
throw new ArgumentNullException("Update user: binding model is null");
}
Email = model.Email ?? Email;
FirstName = model.FirstName ?? FirstName;
SecondName = model.SecondName ?? SecondName;
PasswordHash = model.PasswordHash ?? PasswordHash;
Birthday = model.Birthday;
OnlyImportantMails = model.OnlyImportantMails;
Role = role ?? Role;
}
}
2024-06-04 21:16:50 +04:00
}