using BlogContracts.BindingModels; using BlogContracts.ViewModels; using BlogDataModels; 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 Categories { get; set; } = new(); [ForeignKey("TopicId")] List Topics { get; set; } = new(); [ForeignKey("MessageId")] List 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, }; } }