//-----------------------------------------------------------------------
//
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.Client.Main.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using AliasClientDb;
using AliasVault.Client.Main.Models.FormValidation;
///
/// Credential edit model.
///
public sealed class CredentialEdit
{
///
/// Gets or sets the Id of the login.
///
public Guid Id { get; set; }
///
/// Gets or sets notes field (free text input).
///
public string Notes { get; set; } = string.Empty;
///
/// Gets or sets username field.
///
public string Username { get; set; } = string.Empty;
///
/// Gets or sets the name of the service.
///
[Required]
[Display(Name = "Service Name")]
public string ServiceName { get; set; } = string.Empty;
///
/// Gets or sets the URL of the service.
///
public string? ServiceUrl { get; set; }
///
/// Gets or sets the logo of the service.
///
public byte[]? ServiceLogo { get; set; }
///
/// Gets or sets the Alias Identity object.
///
public Alias Alias { get; set; } = null!;
///
/// Gets or sets the Alias BirthDate. Can be empty string or a date in yyyy-MM-dd format.
///
[StringDateFormat("yyyy-MM-dd", AllowEmpty = true)]
public string AliasBirthDate { get; set; } = string.Empty;
///
/// Gets or sets the Alias Password object.
///
public Password Password { get; set; } = null!;
///
/// Gets or sets the Alias CreateDate.
///
public DateTime CreateDate { get; set; }
///
/// Gets or sets the Alias LastUpdate.
///
public DateTime LastUpdate { get; set; }
///
/// Gets or sets the Attachment list.
///
public List Attachments { get; set; } = [];
///
/// Gets or sets the TOTP codes list.
///
public List TotpCodes { get; set; } = [];
///
/// Creates a CredentialEdit instance from a Credential entity.
///
/// The credential entity to convert.
/// A new CredentialEdit instance.
public static CredentialEdit FromEntity(Credential credential)
{
// Create a deep copy of the credential object to prevent changes to the original object
var options = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve,
MaxDepth = 128,
};
// Create a deep copy of the credential object
var credentialJson = JsonSerializer.Serialize(credential, options);
var credentialCopy = JsonSerializer.Deserialize(credentialJson, options)!;
return new CredentialEdit
{
Id = credentialCopy.Id,
Notes = credentialCopy.Notes ?? string.Empty,
Username = credentialCopy.Username ?? string.Empty,
ServiceName = credentialCopy.Service.Name ?? string.Empty,
ServiceUrl = credentialCopy.Service.Url,
ServiceLogo = credentialCopy.Service.Logo,
Password = credentialCopy.Passwords.FirstOrDefault() ?? new Password
{
Value = string.Empty,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
},
Alias = credentialCopy.Alias,
AliasBirthDate = credentialCopy.Alias.BirthDate == DateTime.MinValue ? string.Empty : credentialCopy.Alias.BirthDate.ToString("yyyy-MM-dd"),
Attachments = credentialCopy.Attachments.ToList(),
TotpCodes = credentialCopy.TotpCodes.ToList(),
CreateDate = credentialCopy.CreatedAt,
LastUpdate = credentialCopy.UpdatedAt,
};
}
///
/// Converts this CredentialEdit instance to a Credential entity.
///
/// A new Credential entity.
public Credential ToEntity()
{
var credential = new Credential
{
Id = Id,
Notes = Notes,
Username = Username,
Service = new Service
{
Name = ServiceName,
Url = ServiceUrl,
Logo = ServiceLogo,
},
Passwords =
[
Password,
],
Alias = Alias,
Attachments = Attachments,
TotpCodes = TotpCodes,
};
if (string.IsNullOrWhiteSpace(AliasBirthDate))
{
credential.Alias.BirthDate = DateTime.MinValue;
}
else
{
credential.Alias.BirthDate = DateTime.Parse(AliasBirthDate, CultureInfo.InvariantCulture);
}
return credential;
}
}