| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Linq; |
| using System.Text; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Threading.Tasks; |
| |
| |
| namespace Playnite.SDK.Models |
| namespace Playnite.SDK.Models |
| { |
| { |
| /// <summary> |
| /// <summary> |
| /// Represents base database object item. |
| /// Represents base database object item. |
| /// </summary> |
| /// </summary> |
| public class DatabaseObject : ObservableObject, IComparable, IIdentifiable |
| public class DatabaseObject : ObservableObject, IComparable, IIdentifiable |
| { |
| { |
| /// <summary> |
| /// <summary> |
| /// Gets or sets identifier of database object. |
| /// Gets or sets identifier of database object. |
| /// </summary> |
| /// </summary> |
| public Guid Id { get; set; } |
| public Guid Id { get; set; } |
| |
| |
| private string name; |
| private string name; |
| /// <summary> |
| /// <summary> |
| /// Gets or sets name. |
| /// Gets or sets name. |
| /// </summary> |
| /// </summary> |
| public string Name |
| public string Name |
| { |
| { |
| get |
| get |
| { |
| { |
| return name; |
| return name; |
| } |
| } |
| |
| |
| set |
| set |
| { |
| { |
| name = value; |
| name = value; |
| OnPropertyChanged(); |
| OnPropertyChanged(); |
| } |
| } |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Creates new instance of <see cref="DatabaseObject"/>. |
| /// Creates new instance of <see cref="DatabaseObject"/>. |
| /// </summary> |
| /// </summary> |
| public DatabaseObject() |
| public DatabaseObject() |
| { |
| { |
| Id = Guid.NewGuid(); |
| Id = Guid.NewGuid(); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Compares Names of database object. |
| /// Compares Names of database object. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
| public int CompareTo(object obj) |
| public int CompareTo(object obj) |
| { |
| { |
. | |
| // This is implemented for CollectionViewSource to do sorting properly. |
| |
| // That's also the reason why this is not virtual for inherited classes to do comparsion of other fields. |
| var objName = (obj as DatabaseObject).Name; |
| var objName = (obj as DatabaseObject).Name; |
| if (string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(objName)) |
| if (string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(objName)) |
| { |
| { |
| return 0; |
| return 0; |
| } |
| } |
| |
| |
| if (string.IsNullOrEmpty(Name)) |
| if (string.IsNullOrEmpty(Name)) |
| { |
| { |
| return 1; |
| return 1; |
| } |
| } |
| |
| |
| if (string.IsNullOrEmpty(objName)) |
| if (string.IsNullOrEmpty(objName)) |
| { |
| { |
| return -1; |
| return -1; |
| } |
| } |
| |
| |
| return string.Compare(Name, objName, true); |
| return string.Compare(Name, objName, true); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// DO NOT use for actual equality check, this only checks if db Ids are equal! |
| /// DO NOT use for actual equality check, this only checks if db Ids are equal! |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
| public override bool Equals(object obj) |
| public override bool Equals(object obj) |
| { |
| { |
. | |
| // This is implemented for CollectionViewSource to do grouping/sorting properly. |
| |
| // That's also the reason why this is not virtual for inherited classes to do comparsion of other fields. |
| if (obj is DatabaseObject dbObj) |
| if (obj is DatabaseObject dbObj) |
| { |
| { |
| return dbObj.Id == Id; |
| return dbObj.Id == Id; |
| } |
| } |
| else |
| else |
| { |
| { |
| return false; |
| return false; |
| } |
| } |
| } |
| } |
| |
| |
| /// <inheritdoc/> |
| /// <inheritdoc/> |
| public override int GetHashCode() |
| public override int GetHashCode() |
| { |
| { |
| if (Id == Guid.Empty) |
| if (Id == Guid.Empty) |
| { |
| { |
| return 0; |
| return 0; |
| } |
| } |
| else |
| else |
| { |
| { |
| return Id.GetHashCode(); |
| return Id.GetHashCode(); |
| } |
| } |
| } |
| } |
| |
| |
| /// <inheritdoc/> |
| /// <inheritdoc/> |
| public override string ToString() |
| public override string ToString() |
| { |
| { |
| return Name ?? string.Empty; |
| return Name ?? string.Empty; |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Copies differential properties to target object intance. |
| /// Copies differential properties to target object intance. |
| /// </summary> |
| /// </summary> |
| /// <param name="target">Target object instance to receive new data.</param> |
| /// <param name="target">Target object instance to receive new data.</param> |
| public virtual void CopyDiffTo(object target) |
| public virtual void CopyDiffTo(object target) |
| { |
| { |
| if (target == null) |
| if (target == null) |
| { |
| { |
| throw new ArgumentNullException("Cannot copy data to a null object."); |
| throw new ArgumentNullException("Cannot copy data to a null object."); |
| } |
| } |
| |
| |
| if (ReferenceEquals(this, target)) |
| if (ReferenceEquals(this, target)) |
| { |
| { |
| throw new ReferenceException("Cannot copy data to itself."); |
| throw new ReferenceException("Cannot copy data to itself."); |
| } |
| } |
| |
| |
| if (target is DatabaseObject dbo) |
| if (target is DatabaseObject dbo) |
| { |
| { |
| if (!string.Equals(Name, dbo.Name, StringComparison.Ordinal)) |
| if (!string.Equals(Name, dbo.Name, StringComparison.Ordinal)) |
| { |
| { |
| dbo.Name = Name; |
| dbo.Name = Name; |
| } |
| } |
| } |
| } |
| else |
| else |
| { |
| { |
| throw new TypeMismatchException($"Target object has to be of type {GetType().Name}"); |
| throw new TypeMismatchException($"Target object has to be of type {GetType().Name}"); |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |