| | 1 | | using System.ComponentModel.DataAnnotations; |
| | 2 | | using System.ComponentModel.DataAnnotations.Schema; |
| | 3 | | using System.Data; |
| | 4 | | using System.Text.Json.Serialization; |
| | 5 | | using Common.Core.Classes; |
| | 6 | |
|
| | 7 | | namespace Common.Core.Models; |
| | 8 | |
|
| | 9 | | /// <summary>This class contains details of an Address.</summary> |
| | 10 | | public class Address : ModelBase, IEquatable<object> |
| | 11 | | { |
| | 12 | | #region Private Variables |
| | 13 | |
|
| | 14 | | private string? _street; |
| | 15 | | private string? _city; |
| | 16 | | private string? _province; |
| | 17 | | private string? _postcode; |
| | 18 | | private string? _country; |
| | 19 | |
|
| | 20 | | #endregion |
| | 21 | |
|
| | 22 | | #region Properties |
| | 23 | |
|
| | 24 | | /// <summary>Gets or sets the Street.</summary> |
| | 25 | | [MaxLength(50)] |
| | 26 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 27 | | public string? Street |
| | 28 | | { |
| 116 | 29 | | get => _street; |
| | 30 | | set |
| 75 | 31 | | { |
| 75 | 32 | | if( value != _street ) |
| 75 | 33 | | { |
| 75 | 34 | | _street = SetNullString( value ); |
| 75 | 35 | | OnPropertyChanged( nameof( Street ) ); |
| 75 | 36 | | OnPropertyChanged( nameof( FullAddress ) ); |
| 75 | 37 | | } |
| 75 | 38 | | } |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary>Gets or sets the City.</summary> |
| | 42 | | [MaxLength( 50 )] |
| | 43 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 44 | | public string? City |
| | 45 | | { |
| 112 | 46 | | get => _city; |
| | 47 | | set |
| 73 | 48 | | { |
| 73 | 49 | | if( value != _city ) |
| 73 | 50 | | { |
| 73 | 51 | | _city = SetNullString( value ); |
| 73 | 52 | | OnPropertyChanged( nameof( City ) ); |
| 73 | 53 | | OnPropertyChanged( nameof( FullAddress ) ); |
| 73 | 54 | | } |
| 73 | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary>Gets or sets the Province.</summary> |
| | 59 | | [MaxLength( 50 )] |
| | 60 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 61 | | public string? Province |
| | 62 | | { |
| 110 | 63 | | get => _province; |
| | 64 | | set |
| 73 | 65 | | { |
| 73 | 66 | | if( value != _province ) |
| 73 | 67 | | { |
| 73 | 68 | | _province = SetNullString( value ); |
| 73 | 69 | | OnPropertyChanged( nameof( Province ) ); |
| 73 | 70 | | OnPropertyChanged( nameof( FullAddress ) ); |
| 73 | 71 | | } |
| 73 | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary>Gets or sets the Postal Code.</summary> |
| | 76 | | [MaxLength( 20 )] |
| | 77 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 78 | | public string? Postcode |
| | 79 | | { |
| 106 | 80 | | get => _postcode; |
| | 81 | | set |
| 73 | 82 | | { |
| 73 | 83 | | if( value != _postcode ) |
| 73 | 84 | | { |
| 73 | 85 | | _postcode = SetNullString( value ); |
| 73 | 86 | | OnPropertyChanged( nameof( Postcode ) ); |
| 73 | 87 | | } |
| 73 | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary>Gets or sets the Country.</summary> |
| | 92 | | [MaxLength( 50 )] |
| | 93 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 94 | | public string? Country |
| | 95 | | { |
| 103 | 96 | | get => _country; |
| | 97 | | set |
| 72 | 98 | | { |
| 72 | 99 | | if( value != _country ) |
| 72 | 100 | | { |
| 72 | 101 | | _country = SetNullString( value ); |
| 72 | 102 | | OnPropertyChanged( nameof( Country ) ); |
| 72 | 103 | | } |
| 72 | 104 | | } |
| | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary>Gets the Full Address.</summary> |
| | 108 | | [NotMapped] |
| | 109 | | [JsonIgnore] |
| | 110 | | public string FullAddress |
| | 111 | | { |
| | 112 | | get |
| 2 | 113 | | { |
| 2 | 114 | | var values = new[] { Street?.Trim(), City?.Trim(), Province?.Trim() }; |
| 8 | 115 | | return string.Join( ", ", values.Where( s => !string.IsNullOrEmpty( s ) ) ); |
| 2 | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| | 119 | | #endregion |
| | 120 | |
|
| | 121 | | #region Public Methods |
| | 122 | |
|
| | 123 | | /// <inheritdoc/> |
| | 124 | | /// <param name="obj">An Address object to compare with this object.</param> |
| | 125 | | public new bool Equals( object? obj ) |
| 15 | 126 | | { |
| 19 | 127 | | if( obj is null || obj is not Address other ) { return false; } |
| | 128 | |
|
| 17 | 129 | | if( other.Street != Street ) { return false; } |
| 13 | 130 | | if( other.City != City ) { return false; } |
| 12 | 131 | | if( other.Province != Province ) { return false; } |
| 11 | 132 | | if( other.Postcode != Postcode ) { return false; } |
| 10 | 133 | | if( other.Country != Country ) { return false; } |
| | 134 | |
|
| 7 | 135 | | return true; |
| 15 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary>Builds the SQL script for any value changes.</summary> |
| | 139 | | /// <param name="obj">Address object containing the original values.</param> |
| | 140 | | /// <param name="mod">Address object containing the modified values.</param> |
| | 141 | | /// <param name="cur">Address object containing the current values.</param> |
| | 142 | | /// <param name="list">List of SQL script changes.</param> |
| | 143 | | /// <param name="prefix">Table column name prefix for Address fields (if required).</param> |
| | 144 | | /// <returns><see langword="true"/> if the current object is equal to the object containing the |
| | 145 | | /// original values; otherwise, <see langword="false"/>.</returns> |
| | 146 | | /// <remarks>This method assumes that the table column names are the same as the property names.</remarks> |
| | 147 | | public static bool UpdateAddress( Address obj, Address mod, Address cur, IList<string> list, |
| | 148 | | string prefix = "" ) |
| 7 | 149 | | { |
| 7 | 150 | | if( !cur.Equals( obj ) ) |
| 1 | 151 | | { |
| 1 | 152 | | mod.Street = cur.Street; |
| 1 | 153 | | mod.City = cur.City; |
| 1 | 154 | | mod.Province = cur.Province; |
| 1 | 155 | | mod.Postcode = cur.Postcode; |
| 1 | 156 | | mod.Country = cur.Country; |
| 1 | 157 | | return false; |
| | 158 | | } |
| | 159 | |
|
| 24 | 160 | | if( obj.Street != mod.Street ) { ModelData.SetSQLColumn( prefix + nameof( Street ), mod.Street, list ); } |
| 24 | 161 | | if( obj.City != mod.City ) { ModelData.SetSQLColumn( prefix + nameof( City ), mod.City, list ); } |
| 24 | 162 | | if( obj.Province != mod.Province ) { ModelData.SetSQLColumn( prefix + nameof( Province ), mod.Province, list ); } |
| 24 | 163 | | if( obj.Postcode != mod.Postcode ) { ModelData.SetSQLColumn( prefix + nameof( Postcode ), mod.Postcode, list ); } |
| 24 | 164 | | if( obj.Country != mod.Country ) { ModelData.SetSQLColumn( prefix + nameof( Country ), mod.Country, list ); } |
| 6 | 165 | | return true; |
| 7 | 166 | | } |
| | 167 | |
|
| | 168 | | #endregion |
| | 169 | |
|
| | 170 | | #region Internal Methods |
| | 171 | |
|
| | 172 | | internal static Address BuildAddress( DataRow row, string prefix = "" ) |
| 17 | 173 | | { |
| 17 | 174 | | return new Address() |
| 17 | 175 | | { |
| 17 | 176 | | Street = row.Field<string?>( prefix + nameof( Street ) ), |
| 17 | 177 | | City = row.Field<string?>( prefix + nameof( City ) ), |
| 17 | 178 | | Province = row.Field<string?>( prefix + nameof( Province ) ), |
| 17 | 179 | | Postcode = row.Field<string?>( prefix + nameof( Postcode ) ), |
| 17 | 180 | | Country = row.Field<string?>( prefix + nameof( Country ) ) |
| 17 | 181 | | }; |
| 17 | 182 | | } |
| | 183 | |
|
| | 184 | | #endregion |
| | 185 | | } |