Skip to content

Commit

Permalink
Renamed continent to region
Browse files Browse the repository at this point in the history
  • Loading branch information
nmakhmutov committed Jul 30, 2023
1 parent 815cf3f commit 480ac5a
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public async Task<Account> Handle(UpdateAccountCommand request, CancellationToke
var account = await _repository.GetAsync(request.Id, ct)
.ConfigureAwait(false) ?? throw AccountException.NotFound(request.Id);

var continent = await GetContinentAsync(request.Country, ct)
var region = await GetRegionAsync(request.Country, ct)
.ConfigureAwait(false);

account.Update(request.Nickname, request.FirstName, request.LastName, request.PreferNickname);
account.Update(request.DateFormat, request.TimeFormat, request.StartOfWeek);
account.Update(request.Language, continent, request.Country, request.TimeZone);
account.Update(request.Language, region, request.Country, request.TimeZone);

_repository.Update(account);

Expand All @@ -54,9 +54,9 @@ await _repository.UnitOfWork
return account;
}

private async Task<ContinentCode> GetContinentAsync(CountryCode country, CancellationToken ct)
private async Task<RegionCode> GetRegionAsync(CountryCode country, CancellationToken ct)
{
var result = await _worldClient.GetCountryAsync(country, ct);
return result is null ? ContinentCode.Empty : ContinentCode.Parse(result.Continent);
return result is null ? RegionCode.Empty : RegionCode.Parse(result.Region);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public async Task HandleAsync(AccountCreatedIntegrationEvent message)

if (ipInformation is not null)
{
_ = ContinentCode.TryParse(ipInformation.ContinentCode, out var continent);
_ = RegionCode.TryParse(ipInformation.ContinentCode, out var region);
_ = CountryCode.TryParse(ipInformation.CountryCode, out var country);
_ = TimeZone.TryParse(ipInformation.TimeZone, out var timeZone);

account.Update(account.Language, continent, country, timeZone);
account.Update(account.Language, region, country, timeZone);
}

var gravatar = await _gravatar.GetAsync(account.GetPrimaryEmail())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<AccountSummary> Handle(GetAccountSummaryQuery request, Cancell
a.prefer_nickname,
a.picture,
a.language,
a.continent_code,
a.region_code,
a.country_code,
a.time_zone,
a.date_format,
Expand All @@ -51,7 +51,7 @@ LIMIT 1
),
x.GetString(5),
Language.Parse(x.GetString(6)),
ContinentCode.Parse(x.GetString(7)),
RegionCode.Parse(x.GetString(7)),
CountryCode.Parse(x.GetString(8)),
TimeZone.Parse(x.GetString(9)),
DateFormat.Parse(x.GetString(10)),
Expand All @@ -70,7 +70,7 @@ internal sealed record AccountSummary(
Name Name,
string Picture,
Language Language,
ContinentCode ContinentCode,
RegionCode RegionCode,
CountryCode CountryCode,
TimeZone TimeZone,
DateFormat DateFormat,
Expand Down
6 changes: 3 additions & 3 deletions src/People.Api/Endpoints/Account/ModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static AccountSummaryModel ToModel(this AccountSummary result) =>
result.Name.FullName(),
result.Language.ToString(),
result.Picture,
result.ContinentCode.ToModel(),
result.RegionCode.ToModel(),
result.CountryCode.ToModel(),
result.TimeZone.ToString(),
result.DateFormat.ToString(),
Expand All @@ -36,7 +36,7 @@ internal static AccountDetailsModel ToModel(this Domain.Entities.Account account
account.Name.FullName(),
account.Language.ToString(),
account.Picture,
account.ContinentCode.ToModel(),
account.RegionCode.ToModel(),
account.CountryCode.ToModel(),
account.TimeZone.ToString(),
account.DateFormat.ToString(),
Expand All @@ -47,7 +47,7 @@ internal static AccountDetailsModel ToModel(this Domain.Entities.Account account
account.Externals.Select(x => x.ToModel())
);

private static string? ToModel(this ContinentCode code) =>
private static string? ToModel(this RegionCode code) =>
code.IsEmpty() ? null : code.ToString();

private static string? ToModel(this CountryCode code) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed record AccountDetailsModel(
string FullName,
string Language,
string Picture,
string? ContinentCode,
string? RegionCode,
string? CountryCode,
string TimeZone,
string DateFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed record AccountSummaryModel(
string FullName,
string Language,
string Picture,
string? ContinentCode,
string? RegionCode,
string? CountryCode,
string TimeZone,
string DateFormat,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace People.Api.Infrastructure.Providers.World;

internal sealed record CountryDetails(string Numeric, string Alpha2, string Alpha3, string Continent, string Region);
internal sealed record CountryDetails(string Numeric, string Alpha2, string Alpha3, string Region);
2 changes: 1 addition & 1 deletion src/People.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
.Enrich.WithProperty("ApplicationName", appName)
.Destructure.AsScalar<AccountId>()
.Destructure.AsScalar<Language>()
.Destructure.AsScalar<ContinentCode>()
.Destructure.AsScalar<RegionCode>()
.Destructure.AsScalar<CountryCode>()
.Destructure.AsScalar<TimeZone>()
.Destructure.AsScalar<DateFormat>()
Expand Down
8 changes: 4 additions & 4 deletions src/People.Domain/Entities/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Account(string nickname, Language language, IPAddress ip, IIpHasher hashe
{
Name = new Name(nickname);
Picture = DefaultPicture;
ContinentCode = ContinentCode.Empty;
RegionCode = RegionCode.Empty;
CountryCode = CountryCode.Empty;
Language = language;
TimeZone = TimeZone.Utc;
Expand All @@ -62,7 +62,7 @@ public Account(string nickname, Language language, IPAddress ip, IIpHasher hashe

public string Picture { get; private set; }

public ContinentCode ContinentCode { get; private set; }
public RegionCode RegionCode { get; private set; }

public CountryCode CountryCode { get; private set; }

Expand Down Expand Up @@ -218,9 +218,9 @@ public void Update(Uri? picture)
AddDomainEvent(new AccountUpdatedDomainEvent(this));
}

public void Update(Language language, ContinentCode continent, CountryCode country, TimeZone timeZone)
public void Update(Language language, RegionCode region, CountryCode country, TimeZone timeZone)
{
ContinentCode = continent;
RegionCode = region;
CountryCode = country;
Language = language;
TimeZone = timeZone;
Expand Down
100 changes: 0 additions & 100 deletions src/People.Domain/ValueObjects/ContinentCode.cs

This file was deleted.

113 changes: 113 additions & 0 deletions src/People.Domain/ValueObjects/RegionCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
namespace People.Domain.ValueObjects;

public readonly struct RegionCode :
IComparable,
IComparable<RegionCode>,
IEquatable<RegionCode>
{
private static readonly (string Code, string Name)[] Regions =
{
("AF", "Africa"),
("AN", "Antarctica"),
("AS", "Asia"),
("EU", "Europe"),
("NA", "North America"),
("OC", "Oceania"),
("SA", "South America")
};

public static RegionCode Empty =>
new("--");

private readonly string _value;

private RegionCode(string value) =>
_value = value.ToUpperInvariant();

public static RegionCode Parse(string? value)
{
if (TryParse(value, out var code))
return code;

throw new ArgumentException($"{nameof(RegionCode)} value must be two chars", nameof(value));
}

public static bool TryParse(string? value, out RegionCode regionCode)
{
regionCode = Empty;

if (string.IsNullOrWhiteSpace(value))
return false;

if (value.Length != 2)
return false;

regionCode = GetCodeOrDefault(value);

return !regionCode.IsEmpty();
}

private static RegionCode GetCodeOrDefault(string value)
{
if (value.Length == 2)
{
foreach (var (code, _) in Regions.AsSpan())
if (code.Equals(value, StringComparison.OrdinalIgnoreCase))
return new RegionCode(code);

return Empty;
}

foreach (var (code, name) in Regions.AsSpan())
if (name.Equals(value, StringComparison.OrdinalIgnoreCase))
return new RegionCode(code);

return Empty;
}

public bool IsEmpty() =>
this == Empty;

public bool Equals(RegionCode other) =>
_value == other._value;

public override bool Equals(object? obj) =>
obj is RegionCode other && Equals(other);

public override int GetHashCode() =>
_value.GetHashCode();

public override string ToString() =>
_value;

public int CompareTo(RegionCode other) =>
string.Compare(_value, other._value, StringComparison.Ordinal);

public int CompareTo(object? obj)
{
if (ReferenceEquals(null, obj))
return 1;

return obj is RegionCode other
? CompareTo(other)
: throw new ArgumentException($"Object must be of type {nameof(RegionCode)}");
}

public static bool operator <(RegionCode left, RegionCode right) =>
left.CompareTo(right) < 0;

public static bool operator >(RegionCode left, RegionCode right) =>
left.CompareTo(right) > 0;

public static bool operator <=(RegionCode left, RegionCode right) =>
left.CompareTo(right) <= 0;

public static bool operator >=(RegionCode left, RegionCode right) =>
left.CompareTo(right) >= 0;

public static bool operator ==(RegionCode left, RegionCode right) =>
left.Equals(right);

public static bool operator !=(RegionCode left, RegionCode right) =>
!left.Equals(right);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public void Configure(EntityTypeBuilder<Account> builder)
.HasMaxLength(2)
.IsRequired();

builder.Property(x => x.ContinentCode)
.HasColumnName("continent_code")
.HasConversion(x => x.ToString(), x => ContinentCode.Parse(x))
.HasDefaultValue(ContinentCode.Empty)
builder.Property(x => x.RegionCode)
.HasColumnName("region_code")
.HasConversion(x => x.ToString(), x => RegionCode.Parse(x))
.HasDefaultValue(RegionCode.Empty)
.HasMaxLength(2)
.IsRequired();

Expand Down
Loading

0 comments on commit 480ac5a

Please sign in to comment.