194 lines
5.0 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace gehGassiApp.Domain.Common
{
/// <summary>
/// Repräsentiert eine Adresse
/// </summary>
public class Address : ObservableObject
{
private string _addressLine1;
private string _addressLine2;
private string _city;
private string _zip;
private string _state;
private string _countryCode;
/// <summary>
/// Optionale Adresszeile 1
/// </summary>
[MaxLength(100)]
[JsonPropertyName("addressLine1")]
public string AddressLine1
{
get => _addressLine1;
set
{
_addressLine1 = value;
OnPropertyChanged(AddressLine1);
OnPropertyChanged(nameof(Preview));
}
}
/// <summary>
/// Optionale Adresszeile 2
/// </summary>
[MaxLength(100)]
[JsonPropertyName("addressLine2")]
public string AddressLine2
{
get => _addressLine2;
set
{
_addressLine2 = value;
OnPropertyChanged(AddressLine2);
OnPropertyChanged(nameof(Preview));
}
}
/// <summary>
/// Optional: Stadt / Ort
/// </summary>
[MaxLength(100)]
[JsonPropertyName("city")]
public string City
{
get => _city;
set
{
_city = value;
OnPropertyChanged(City);
OnPropertyChanged(nameof(Preview));
}
}
/// <summary>
/// Optinale PLZ
/// </summary>
[MaxLength(20)]
[JsonPropertyName("zip")]
public string Zip
{
get => _zip;
set
{
_zip = value;
OnPropertyChanged(nameof(Zip));
OnPropertyChanged(nameof(Preview));
}
}
/// <summary>
/// Bundesland / Staat
/// </summary>
[MaxLength(100)]
[JsonPropertyName("state")]
public string State
{
get => _state;
set
{
_state = value;
OnPropertyChanged(nameof(State));
OnPropertyChanged(nameof(Preview));
}
}
/// <summary>
/// ISO2 Ländercode
/// </summary>
[Required]
[MaxLength(2)]
[JsonPropertyName("countryCode")]
public string CountryCode
{
get => _countryCode;
set
{
_countryCode = value;
OnPropertyChanged(nameof(CountryCode));
OnPropertyChanged(nameof(Preview));
}
}
/// <summary>
/// Anzeige der Adresse als ein String
/// </summary>
[NotMapped]
[JsonIgnore]
public string Preview
{
get
{
var line = string.Empty;
if (!string.IsNullOrWhiteSpace(AddressLine1))
{
line += $"{AddressLine1}";
}
if (!string.IsNullOrWhiteSpace(AddressLine2))
{
if (!string.IsNullOrWhiteSpace(line))
line += ", ";
line += $"{AddressLine2}";
}
if (!string.IsNullOrWhiteSpace(Zip))
{
if (!string.IsNullOrWhiteSpace(line))
line += ", ";
line += $"{Zip}";
}
if (!string.IsNullOrWhiteSpace(City))
{
if (!string.IsNullOrWhiteSpace(line))
line += ", ";
line += $"{City}";
}
return line;
}
}
/// <summary>
/// Anzeige Ort mit Komma für die Detailansicht.
/// Wenn ort, dann Ort, sonst leer
/// </summary>
[NotMapped]
[JsonIgnore]
public string CityWithComma
{
get
{
if(!string.IsNullOrWhiteSpace(City))
return $"{City}, ";
return string.Empty;
}
}
/// <summary>
/// Klonen der Adresse
/// </summary>
/// <returns>Addresse</returns>
public Address Clone()
{
var clonedAddress = new Address()
{
AddressLine1 = AddressLine1,
AddressLine2 = AddressLine2,
Zip = Zip,
City = City,
State = State,
CountryCode = CountryCode
};
return clonedAddress;
}
}
}