Files
mmorales.photo/back/DataModels/User.cs
2025-08-25 18:52:59 +02:00

86 lines
2.7 KiB
C#

using back.DTO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Transactional.Abstractions.Interfaces;
namespace back.DataModels;
[Table("Users")]
public class User : IEntity<User>
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; } = null!;
[Required, EmailAddress]
public string Email { get; set; } = null!;
[Required, MinLength(8)]
public string Password { get; set; } = null!;
[Required]
public string Salt { get; set; } = null!;
public string CreatedAt { get; set; } = null!;
public virtual Person IdNavigation { get; set; } = null!;
public virtual ICollection<Gallery> Galleries { get; set; } = [];
public virtual ICollection<Gallery> GalleriesNavigation { get; set; } = [];
public virtual ICollection<Photo> Photos { get; set; } = [];
public virtual ICollection<Role> Roles { get; set; } = [];
public User() { }
public User(string id, string email, string password, DateTimeOffset createdAt)
{
Id = id;
Email = email;
Password = password;
CreatedAt = createdAt.ToString("dd-MM-yyyy HH:mm:ss zz");
}
public UserDto ToDto() => new()
{
Id = Id,
Roles = Roles
};
public bool IsAdmin() => Roles.Any(r => r.IsAdmin());
public bool IsContentManager() => Roles.Any(r => r.IsContentManager());
public bool IsUser() => Roles.Any(r => r.IsUser());
public override int GetHashCode() => HashCode.Combine(Id, Email);
public override bool Equals(object? obj) => obj is User otherEvent && Equals(otherEvent);
public bool Equals(User? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id && Email == other.Email;
}
public bool IsNull => this is null;
public object Clone() => (User)MemberwiseClone();
public int CompareTo(object? obj)
{
if (obj is null) return 1;
if (obj is not User other) throw new ArgumentException("Object is not a Person");
return CompareTo(other);
}
public int CompareTo(User? other)
{
if (other is null) return 1;
if (ReferenceEquals(this, other)) return 0;
return string.Compare(Id, other.Id, StringComparison.OrdinalIgnoreCase);
}
public const string SystemUserId = "00000000-0000-0000-0000-000000000001";
public static readonly User SystemUser = new(
id: SystemUserId,
email: "sys@t.em",
password: "",
createdAt: DateTime.UtcNow
)
{
Roles = [Role.AdminRole, Role.ContentManagerRole, Role.UserRole]
};
}