| | 1 | | using System.ComponentModel.DataAnnotations; |
| | 2 | | using System.ComponentModel.DataAnnotations.Schema; |
| | 3 | | using System.Text.Json.Serialization; |
| | 4 | | using Common.Core.Classes; |
| | 5 | | using Common.Core.Interfaces; |
| | 6 | |
|
| | 7 | | namespace Common.Core.Models; |
| | 8 | |
|
| | 9 | | /// <summary>This class contains details of a User.</summary> |
| | 10 | | public class User : ModelEdit, IUser |
| | 11 | | { |
| | 12 | | #region IUser Properties |
| | 13 | |
|
| | 14 | | /// <inheritdoc/> |
| 8 | 15 | | public override int Id { get; set; } |
| | 16 | |
|
| | 17 | | /// <inheritdoc/> |
| | 18 | | [MaxLength( 50 )] |
| 51 | 19 | | public string? Name { get; set; } |
| | 20 | |
|
| | 21 | | /// <inheritdoc/> |
| | 22 | | [Range( 0, 150 )] |
| | 23 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| 45 | 24 | | public int? Age { get; set; } |
| | 25 | |
|
| | 26 | | /// <inheritdoc/> |
| | 27 | | [Column( TypeName = "date" )] |
| | 28 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| 61 | 29 | | public DateOnly? BirthDate { get; set; } |
| | 30 | |
|
| | 31 | | /// <inheritdoc/> |
| | 32 | | [MaxLength( 50 )] |
| | 33 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| 49 | 34 | | public string? Email { get; set; } |
| | 35 | |
|
| | 36 | | /// <inheritdoc/> |
| | 37 | | [Required] |
| 39 | 38 | | public Genders Gender { get; set; } |
| | 39 | |
|
| | 40 | | #endregion |
| | 41 | |
|
| | 42 | | #region Public Methods |
| | 43 | |
|
| | 44 | | /// <inheritdoc/> |
| | 45 | | public override object Clone() |
| 1 | 46 | | { |
| 1 | 47 | | return MemberwiseClone(); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <inheritdoc/> |
| | 51 | | /// <param name="obj">An object implementing the IUser interface to compare with this object.</param> |
| | 52 | | public override bool Equals( object? obj ) |
| 11 | 53 | | { |
| 15 | 54 | | if( obj is null || obj is not IUser user ) { return false; } |
| | 55 | |
|
| 11 | 56 | | if( user.Name != Name ) { return false; } |
| 10 | 57 | | if( user.Email != Email ) { return false; } |
| 9 | 58 | | if( user.Age != Age ) { return false; } |
| 8 | 59 | | if( !user.BirthDate.Equals( BirthDate ) ) { return false; } |
| 7 | 60 | | if( user.Gender != Gender ) { return false; } |
| | 61 | |
|
| 4 | 62 | | return true; |
| 11 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc/> |
| | 66 | | /// <param name="obj">An object implementing the IUser interface with the changed values.</param> |
| | 67 | | public override void Update( object? obj ) |
| 3 | 68 | | { |
| 7 | 69 | | if( obj is null || obj is not IUser other ) { return; } |
| | 70 | |
|
| 5 | 71 | | if( other.Name != Name ) { Name = other.Name; OnPropertyChanged( nameof( Name ) ); } |
| 5 | 72 | | if( other.Age != Age ) { Age = other.Age; OnPropertyChanged( nameof( Age ) ); } |
| 5 | 73 | | if( other.Email != Email ) { Email = other.Email; OnPropertyChanged( nameof( Email ) ); } |
| 5 | 74 | | if( !other.BirthDate.Equals( BirthDate ) ) { BirthDate = other.BirthDate; OnPropertyChanged( nameof( BirthDate ) ); |
| 5 | 75 | | if( other.Gender != Gender ) { Gender = other.Gender; OnPropertyChanged( nameof( Gender ) ); } |
| 3 | 76 | | } |
| | 77 | |
|
| | 78 | | #endregion |
| | 79 | |
|
| | 80 | | #region Static Methods |
| | 81 | |
|
| | 82 | | /// <summary>Retrieves a list of users.</summary> |
| | 83 | | /// <returns>A list of users.</returns> |
| | 84 | | public static IList<User> GetUsers() |
| 4 | 85 | | { |
| 4 | 86 | | var items = new List<User>(); |
| | 87 | |
|
| 4 | 88 | | User wrk = new() { Name = "John Doe", Email = "john@doe-family.com", BirthDate = new DateOnly( 1981, 10, 14 ) }; |
| 4 | 89 | | wrk.Age = CalculateAge( wrk.BirthDate ); |
| 4 | 90 | | wrk.Gender = Genders.Male; |
| 4 | 91 | | items.Add( wrk ); |
| | 92 | |
|
| 4 | 93 | | wrk = new() { Name = "Jane Doe", Email = "jane@doe-family.com", BirthDate = new DateOnly( 1984, 7, 5 ) }; |
| 4 | 94 | | wrk.Age = CalculateAge( wrk.BirthDate ); |
| 4 | 95 | | wrk.Gender = Genders.Female; |
| 4 | 96 | | items.Add( wrk ); |
| | 97 | |
|
| 4 | 98 | | wrk = new() { Name = "Chris Doe", Email = "chris.doe@gmail.com", BirthDate = new DateOnly( 2002, 1, 19 ) }; |
| 4 | 99 | | wrk.Age = CalculateAge( wrk.BirthDate ); |
| 4 | 100 | | wrk.Gender = Genders.Neutral; |
| 4 | 101 | | items.Add( wrk ); |
| | 102 | |
|
| 4 | 103 | | wrk = new() { Name = "Donna Doe", Email = "donna.doe@gmail.com", BirthDate = new DateOnly( 2005, 12, 8 ) }; |
| 4 | 104 | | wrk.Age = CalculateAge( wrk.BirthDate ); |
| 4 | 105 | | wrk.Gender = Genders.Fluid; |
| 4 | 106 | | items.Add( wrk ); |
| | 107 | |
|
| 4 | 108 | | return items; |
| 4 | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary>Asynchronously retrieves a list of users.</summary> |
| | 112 | | /// <returns>A list of users.</returns> |
| | 113 | | public static async Task<IList<User>> GetUsersAsync() |
| 1 | 114 | | { |
| 1 | 115 | | IList<User> rtn = new List<User>(); |
| 4 | 116 | | await Task.Run( () => { rtn = GetUsers(); } ); |
| 1 | 117 | | return rtn; |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | #endregion |
| | 121 | | } |