enable nullable TimezoneService

This commit is contained in:
ngfrolov 2023-04-18 16:16:11 +05:00
parent 66ac06c1fe
commit 6c694ff7bc
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
4 changed files with 17 additions and 14 deletions

View File

@ -15,7 +15,7 @@ namespace AsbCloudApp.Services
/// <param name="latitude"></param> /// <param name="latitude"></param>
/// <param name="longitude"></param> /// <param name="longitude"></param>
/// <returns></returns> /// <returns></returns>
SimpleTimezoneDto GetByCoordinates(double latitude, double longitude); SimpleTimezoneDto? GetOrDefaultByCoordinates(double latitude, double longitude);
/// <summary> /// <summary>
/// по координатам /// по координатам
@ -24,6 +24,6 @@ namespace AsbCloudApp.Services
/// <param name="longitude"></param> /// <param name="longitude"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<Data.SimpleTimezoneDto> GetByCoordinatesAsync(double latitude, double longitude, CancellationToken token); Task<Data.SimpleTimezoneDto?> GetOrDefaultByCoordinatesAsync(double latitude, double longitude, CancellationToken token);
} }
} }

View File

@ -11,26 +11,26 @@ namespace AsbCloudInfrastructure.Services
{ {
private class TimeZoneInfo private class TimeZoneInfo
{ {
public string Sunrise { get; set; } public string? Sunrise { get; set; }
public double Lng { get; set; } public double Lng { get; set; }
public double Lat { get; set; } public double Lat { get; set; }
public string CountryCode { get; set; } public string? CountryCode { get; set; }
public double GmtOffset { get; set; } public double GmtOffset { get; set; }
public double RawOffset { get; set; } public double RawOffset { get; set; }
public string Sunset { get; set; } public string? Sunset { get; set; }
public string TimezoneId { get; set; } public string? TimezoneId { get; set; }
public double DstOffset { get; set; } public double DstOffset { get; set; }
public string CountryName { get; set; } public string? CountryName { get; set; }
public string Time { get; set; } public string? Time { get; set; }
} }
private const string timezoneApiUrl = "http://api.geonames.org/timezoneJSON"; private const string timezoneApiUrl = "http://api.geonames.org/timezoneJSON";
private const string timezoneApiUserName = "asbautodrilling"; private const string timezoneApiUserName = "asbautodrilling";
public SimpleTimezoneDto GetByCoordinates(double latitude, double longitude) public SimpleTimezoneDto? GetOrDefaultByCoordinates(double latitude, double longitude)
=> GetByCoordinatesAsync(latitude, longitude, default).Result; => GetOrDefaultByCoordinatesAsync(latitude, longitude, default).Result;
public async Task<SimpleTimezoneDto> GetByCoordinatesAsync(double latitude, double longitude, CancellationToken token) public async Task<SimpleTimezoneDto?> GetOrDefaultByCoordinatesAsync(double latitude, double longitude, CancellationToken token)
{ {
var lat = latitude.ToString(System.Globalization.CultureInfo.InvariantCulture); var lat = latitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
var lng = longitude.ToString(System.Globalization.CultureInfo.InvariantCulture); var lng = longitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
@ -55,6 +55,9 @@ namespace AsbCloudInfrastructure.Services
}; };
var timezoneInfo = JsonSerializer.Deserialize<TimeZoneInfo>(responseJson, options); var timezoneInfo = JsonSerializer.Deserialize<TimeZoneInfo>(responseJson, options);
if(timezoneInfo is null)
return null;
return new SimpleTimezoneDto return new SimpleTimezoneDto
{ {
Hours = timezoneInfo.DstOffset, Hours = timezoneInfo.DstOffset,

View File

@ -326,7 +326,7 @@ namespace AsbCloudInfrastructure.Services
if (point.Latitude is not null & point.Longitude is not null) if (point.Latitude is not null & point.Longitude is not null)
{ {
var timezone = timezoneService.GetByCoordinates(point.Latitude!.Value, point.Longitude!.Value); var timezone = timezoneService.GetOrDefaultByCoordinates(point.Latitude!.Value, point.Longitude!.Value);
if (timezone is not null) if (timezone is not null)
{ {
return timezone; return timezone;

View File

@ -34,9 +34,9 @@ namespace AsbCloudWebApi.Tests.ServicesTests
telemetryTracker = new Mock<ITelemetryTracker>(); telemetryTracker = new Mock<ITelemetryTracker>();
timezoneService = new Mock<ITimezoneService>(); timezoneService = new Mock<ITimezoneService>();
timezoneService.Setup(s => s.GetByCoordinatesAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<CancellationToken>())) timezoneService.Setup(s => s.GetOrDefaultByCoordinatesAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(timezone)); .Returns(Task.FromResult(timezone));
timezoneService.Setup(s => s.GetByCoordinates(It.IsAny<double>(), It.IsAny<double>())) timezoneService.Setup(s => s.GetOrDefaultByCoordinates(It.IsAny<double>(), It.IsAny<double>()))
.Returns(timezone); .Returns(timezone);
context = TestHelpter.MakeRealTestContext(); context = TestHelpter.MakeRealTestContext();