PIbd-22-Ismailov_SUBD/BlogDataModels/BlogDatabaseImplement/Model/User.cs

83 lines
2.3 KiB
C#
Raw Normal View History

2023-09-06 21:16:28 +04:00
using ForumContracts.BindingModels;
using ForumContracts.ViewModels;
using ForumDataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace BlogDatabase.Models
{
public class User : IUserModel
{
[Required]
public string Username { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[Required]
public DateTime RegistrationDate { get; set; }
public int Id { get; set; }
[Required]
public int RoleId { get; set; }
[ForeignKey("CategoryId")]
List<Category> Categories { get; set; } = new();
[ForeignKey("TopicId")]
List<Topic> Topics { get; set; } = new();
[ForeignKey("MessageId")]
List<Message> Messages { get; set; } = new();
public virtual Role Role { get; set; }
public static User? Create(UserBindingModel model)
{
if (model == null)
{
return null;
}
return new User()
{
Id = model.Id,
Username = model.Username,
Email = model.Email,
Password = model.Password,
RegistrationDate = model.RegistrationDate,
RoleId = model.RoleId,
};
}
public void Update(UserBindingModel model)
{
if (model == null)
{
return;
}
Username = model.Username;
Email = model.Email;
Password = model.Password;
RegistrationDate = model.RegistrationDate;
}
public UserViewModel GetViewModel => new()
{
Id = Id,
Username = Username,
Email = Email,
Password = Password,
RegistrationDate = RegistrationDate,
RoleId = RoleId,
RoleName = Role == null ? string.Empty : Role.Name,
};
}
}