96 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
namespace gehGassiApp.Domain.Base
{
/// <summary>
/// Basisklasse für eine Entität welche über Synchronisierungs-Eigenschaften verfügt
/// </summary>
public class SyncEntity : ObservableObject, ISyncEntity
{
/// <summary>
/// Guid als String - wird als Key verwendet.
/// Hier so gelöst, da nicht alle Datenbanken über Guids verfügen
/// </summary>
[Key]
[MaxLength(128)]
[JsonPropertyName("id")]
public string Id { get; set; }
/// <summary>
/// Eindeutiger Versions-Identifikator für diese Entität. Muss bei jeder Schreib-Operation gesetzt werden
/// </summary>
[Timestamp]
[JsonPropertyName("version")]
public byte[] Version { get; set; }
/// <summary>
/// Datum und Uhrzeit inkl. Zeitzone wann die letzte Aktualisierung stattgefunden hat.
/// </summary>
[JsonPropertyName("updatedAt")]
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
/// <summary>
/// Gibt an ob diese Entität gelöscht wurde
/// </summary>
[JsonPropertyName("deleted")]
public bool Deleted { get; set; }
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />.</returns>
public bool Equals(ISyncEntity other)
=> other != null
&& Id == other.Id
&& UpdatedAt == other.UpdatedAt
&& Deleted == other.Deleted
&& Version.SequenceEqual(other.Version);
}
/// <summary>
/// Basisklasse für eine Entität welche über Synchronisierungs-Eigenschaften verfügt für Views
/// </summary>
public class SyncEntityVw : ObservableObject, ISyncEntity
{
/// <summary>
/// Guid als String - wird als Key verwendet.
/// Hier so gelöst, da nicht alle Datenbanken über Guids verfügen
/// </summary>
public string Id { get; set; }
/// <summary>
/// Eindeutiger Versions-Identifikator für diese Entität. Muss bei jeder Schreib-Operation gesetzt werden
/// </summary>
public byte[] Version { get; set; }
/// <summary>
/// Datum und Uhrzeit inkl. Zeitzone wann die letzte Aktualisierung stattgefunden hat.
/// </summary>
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
/// <summary>
/// Gibt an ob diese Entität gelöscht wurde
/// </summary>
public bool Deleted { get; set; }
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />.</returns>
public bool Equals(ISyncEntity other)
=> other != null
&& Id == other.Id
&& UpdatedAt == other.UpdatedAt
&& Deleted == other.Deleted
&& Version.SequenceEqual(other.Version);
}
}