Refactor DTOs to classes with enhanced properties

Changed `EnvelopeReceiverReadOnlyDto` and `EnvelopeReceiverReadOnlyUpdateDto` from records to classes, converting properties to standard get/set accessors and adding documentation comments. Introduced a default value for `ChangedWhen` in `EnvelopeReceiverReadOnlyUpdateDto`.

Modified `ReceiverCreateDto` to a class structure, updating properties to use init-only accessors and encapsulating SHA256 hash logic in a private field. Retained the `AddedWhen` property with its default value.
This commit is contained in:
Developer 02
2025-05-13 09:56:56 +02:00
parent 02aeaea8a9
commit cc11d70a27
3 changed files with 102 additions and 32 deletions

View File

@@ -1,18 +1,62 @@
using EnvelopeGenerator.Application.DTOs.Receiver; using EnvelopeGenerator.Application.DTOs.Receiver;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly namespace EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly;
/// <summary>
/// Represents a read-only Data Transfer Object (DTO) for an envelope receiver.
/// Contains information about the receiver, associated envelope, and audit details.
/// </summary>
[ApiExplorerSettings(IgnoreApi = true)]
public class EnvelopeReceiverReadOnlyDto
{ {
[ApiExplorerSettings(IgnoreApi = true)] /// <summary>
public record EnvelopeReceiverReadOnlyDto( /// Gets or inits the unique identifier of the envelope receiver.
long Id, /// </summary>
long EnvelopeId, public long Id { get; init; }
string ReceiverMail,
DateTime DateValid, /// <summary>
DateTime AddedWhen, /// Gets or inits the identifier of the associated envelope.
string AddedWho, /// </summary>
EnvelopeDto? Envelope = null, public long EnvelopeId { get; init; }
string? ChangedWho = null,
DateTime? ChangedWhen = null, /// <summary>
ReceiverReadDto? Receiver = null); /// Gets or inits the email address of the receiver.
/// </summary>
public string ReceiverMail { get; set; }
/// <summary>
/// Gets or inits the date until which the receiver is valid.
/// </summary>
public DateTime DateValid { get; set; }
/// <summary>
/// Gets or inits the date and time when the receiver was added.
/// </summary>
public DateTime AddedWhen { get; init; }
/// <summary>
/// Gets or inits the user who added the receiver.
/// </summary>
public string AddedWho { get; init; }
/// <summary>
/// Gets or inits the associated envelope details.
/// </summary>
public EnvelopeDto? Envelope { get; set; }
/// <summary>
/// Gets or inits the user who last changed the receiver, if any.
/// </summary>
public string? ChangedWho { get; set; }
/// <summary>
/// Gets or inits the date and time when the receiver was last changed, if any.
/// </summary>
public DateTime? ChangedWhen { get; set; }
/// <summary>
/// Gets or inits the associated receiver details.
/// </summary>
public ReceiverReadDto? Receiver { get; set; }
} }

View File

@@ -1,14 +1,31 @@
using DigitalData.Core.Abstractions; using DigitalData.Core.Abstractions;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly namespace EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly;
/// <summary>
/// Data Transfer Object for updating a read-only envelope receiver.
/// </summary>
[ApiExplorerSettings(IgnoreApi = true)]
public class EnvelopeReceiverReadOnlyUpdateDto : IUnique<long>
{ {
[ApiExplorerSettings(IgnoreApi = true)] /// <summary>
public record EnvelopeReceiverReadOnlyUpdateDto( /// Gets or sets the unique identifier of the envelope receiver.
long Id, /// </summary>
DateTime DateValid, public long Id { get; init; }
string ChangedWho) : IUnique<long>
{ /// <summary>
public DateTime ChangedWhen { get; } = DateTime.Now; /// Gets or sets the date when the envelope receiver becomes valid.
}; /// </summary>
public DateTime DateValid { get; set; }
/// <summary>
/// Gets or sets the name of the user who made the change.
/// </summary>
public string ChangedWho { get; set; }
/// <summary>
/// Gets or sets the date and time when the change was made.
/// </summary>
public DateTime ChangedWhen { get; set; } = DateTime.Now;
} }

View File

@@ -3,21 +3,30 @@ using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
namespace EnvelopeGenerator.Application.DTOs.Receiver namespace EnvelopeGenerator.Application.DTOs.Receiver;
{
[ApiExplorerSettings(IgnoreApi = true)]
public record ReceiverCreateDto([EmailAddress] string EmailAddress, string? TotpSecretkey = null)
{
public string Signature => sha256HexOfMail.Value;
private readonly Lazy<string> sha256HexOfMail = new(() => [ApiExplorerSettings(IgnoreApi = true)]
public record ReceiverCreateDto
{
public ReceiverCreateDto()
{
_sha256HexOfMail = new(() =>
{ {
var bytes_arr = Encoding.UTF8.GetBytes(EmailAddress.ToUpper()); var bytes_arr = Encoding.UTF8.GetBytes(EmailAddress.ToUpper());
var hash_arr = SHA256.HashData(bytes_arr); var hash_arr = SHA256.HashData(bytes_arr);
var hexa_str = BitConverter.ToString(hash_arr); var hexa_str = BitConverter.ToString(hash_arr);
return hexa_str.Replace("-", string.Empty); return hexa_str.Replace("-", string.Empty);
}); });
}
public DateTime AddedWhen { get; } = DateTime.Now; [EmailAddress]
}; public string EmailAddress { get; init; }
}
public string? TotpSecretkey { get; init; }
public string Signature => _sha256HexOfMail.Value;
private readonly Lazy<string> _sha256HexOfMail;
public DateTime AddedWhen { get; } = DateTime.Now;
};