SharpRSS/SharpRSS.API/Models/DbUser.cs
2024-06-16 13:43:30 +02:00

55 lines
1.8 KiB
C#
Executable File

using System;
using Newtonsoft.Json;
using SharpRSS.API.Contracts.DTOs.Users;
using SharpRSS.API.Cryptography;
using ToolQit.Extensions;
namespace SharpRSS.API.Models
{
// Database model
internal class DbUser
{
public DbUser() { }
public DbUser(InsertUser user)
{
Uid = user.Uid;
DisplayName = user.DisplayName;
Email = user.Email;
Gid = user.GroupId;
Active = user.Active;
PswHash = Hasher.HashPassword(user.Password, out byte[] salt);
Salt = salt;
}
public string Uid { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Gid { get; set; } = string.Empty;
public bool Active { get; set; }
public DateTime DateCreated { get; set; } = DateTime.Now;
public byte[] PswHash { get; set; }
public byte[] Salt { get; set; }
public User ToDto()
{
string json = JsonConvert.SerializeObject(this);
return JsonConvert.DeserializeObject<User>(json) ?? new User();
}
public void Update(InsertUser user)
{
if (user == null) return;
if (!user.DisplayName.IsNullEmptyWhiteSpace())
DisplayName = user.DisplayName;
if (!user.Email.IsNullEmptyWhiteSpace())
Email = user.Email;
if (!user.GroupId.IsNullEmptyWhiteSpace())
Gid = user.GroupId;
Active = user.Active;
if (!user.Password.IsNullEmptyWhiteSpace())
{
PswHash = Hasher.HashPassword(user.Password, out byte[] salt);
Salt = salt;
}
}
}
}