Skip to content

Commit

Permalink
Merge pull request #18 from brenordv/feature/dotnet8-migration
Browse files Browse the repository at this point in the history
Fixed serialization problem.
  • Loading branch information
brenordv authored Feb 11, 2024
2 parents e82695e + 0520d36 commit be658c0
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Raccoon.Ninja.AzFn.DataApi/DataApiFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<IActionResult> RunAsync(

return latestReading is null
? new NoContentResult()
: new OkObjectResult(JsonConvert.SerializeObject(response));
: new OkObjectResult(response);
}
catch (Exception e)
{
Expand Down
5 changes: 2 additions & 3 deletions Raccoon.Ninja.AzFn.DataApi/DataLatestHbA1cFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Models;
using Microsoft.Azure.Functions.Worker;
using Newtonsoft.Json;

namespace Raccoon.Ninja.AzFn.DataApi;

Expand Down Expand Up @@ -59,11 +58,11 @@ public async Task<IActionResult> RunAsync(
if (latestSuccessful is null && latestPartialSuccessful is null)
return new NoContentResult();

return new OkObjectResult(JsonConvert.SerializeObject(new DataLatestHbA1CFuncResponse
return new OkObjectResult(new DataLatestHbA1CFuncResponse
{
LatestSuccessful = latestSuccessful,
LatestPartialSuccessful = latestPartialSuccessful
}));
});
}
catch (Exception e)
{
Expand Down
3 changes: 1 addition & 2 deletions Raccoon.Ninja.AzFn.DataApi/DataSeriesApiFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Models;
using Microsoft.Azure.Functions.Worker;
using Newtonsoft.Json;

namespace Raccoon.Ninja.AzFn.DataApi;

Expand Down Expand Up @@ -73,7 +72,7 @@ public async Task<IActionResult> RunAsync(

return response.Count == 0
? new NoContentResult()
: new OkObjectResult(JsonConvert.SerializeObject(response));
: new OkObjectResult(response);
}

private static int GetDataSeriesMaxRecords()
Expand Down
8 changes: 3 additions & 5 deletions Raccoon.Ninja.AzFn.ScheduledTasks/DataTransferFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DataTransferFunc(ILogger<DataTransferFunc> logger)
"%CosmosContainerName%",
Connection = "CosmosConnectionString",
CreateIfNotExists = false)]
public string Run(
public IEnumerable<GlucoseReading> Run(
[TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timer,
[CosmosDBInput(
databaseName: "%CosmosDatabaseName%",
Expand All @@ -54,16 +54,14 @@ public string Run(
{
_logger.LogWarning("No documents to transfer");

return null;
return new List<GlucoseReading>();
}

var glucoseReadings = documents.ToGlucoseReadings(previousReading);

_logger.LogInformation("Converted {Count} documents to CosmosDb", documents.Count);

var result = JsonConvert.SerializeObject(glucoseReadings);

return result;
return glucoseReadings;
}
catch (Exception e)
{
Expand Down
7 changes: 2 additions & 5 deletions Raccoon.Ninja.AzFn.ScheduledTasks/HbA1cCalcFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;

Expand All @@ -24,7 +23,7 @@ public HbA1CCalcFunc(ILogger<HbA1CCalcFunc> logger)
"%CosmosAggregateContainerName%",
Connection = "CosmosConnectionString",
CreateIfNotExists = false)]
public string Run(
public HbA1CCalculation Run(
[TimerTrigger("0 0 0 * * *"
#if DEBUG
, RunOnStartup = true
Expand Down Expand Up @@ -56,9 +55,7 @@ public string Run(
if (previousCalculation is not null)
hbA1C = hbA1C with { Delta = hbA1C.Value - previousCalculation.Value };

var result = JsonConvert.SerializeObject(hbA1C);

return result;
return hbA1C;
}
catch (Exception e)
{
Expand Down
8 changes: 4 additions & 4 deletions Raccoon.Ninja.Domain.Core/Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Raccoon.Ninja.Domain.Core.Entities;

public record BaseEntity
{
[JsonProperty("id")]
[JsonPropertyName("id")]
public string Id { get; init; } = Guid.NewGuid().ToString();

[JsonProperty("value")]
[JsonPropertyName("value")]
public float Value { get; init; }

[JsonProperty("delta", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("delta")]
public float? Delta { get; init; }
};
6 changes: 3 additions & 3 deletions Raccoon.Ninja.Domain.Core/Entities/GlucoseReading.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;

namespace Raccoon.Ninja.Domain.Core.Entities;

public record GlucoseReading: BaseEntity
{
[JsonProperty("trend")]
[JsonPropertyName("trend")]
public Trend Trend { get; init; }

[JsonProperty("readAt")]
[JsonPropertyName("readAt")]
public long ReadTimestampUtc { get; init; }

[JsonIgnore] public DateTime ReadTimestampUtcAsDateTime => ReadTimestampUtc.ToUtcDateTime();
Expand Down
12 changes: 6 additions & 6 deletions Raccoon.Ninja.Domain.Core/Entities/HbA1CCalculation.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;

namespace Raccoon.Ninja.Domain.Core.Entities;

public record HbA1CCalculation: BaseEntity
{
[JsonProperty("docType")] public AggregateType DocType { get; init; } = AggregateType.HbA1CCalculation;
[JsonProperty("referenceDate")] public DateOnly ReferenceDate { get; init; }
[JsonProperty("createdAt")] public long CreatedAtUtc { get; init; } = DateTime.UtcNow.ToUnixTimestamp();
[JsonProperty("status")] public HbA1CCalculationStatus Status { get; init; } = HbA1CCalculationStatus.NotCalculated;
[JsonProperty("error", NullValueHandling = NullValueHandling.Ignore)] public string Error { get; init; }
[JsonPropertyName("docType")] public AggregateType DocType { get; init; } = AggregateType.HbA1CCalculation;
[JsonPropertyName("referenceDate")] public DateOnly ReferenceDate { get; init; }
[JsonPropertyName("createdAt")] public long CreatedAtUtc { get; init; } = DateTime.UtcNow.ToUnixTimestamp();
[JsonPropertyName("status")] public HbA1CCalculationStatus Status { get; init; } = HbA1CCalculationStatus.NotCalculated;
[JsonPropertyName("error")] public string Error { get; init; }

/// <summary>
/// Creates an instance marked as error.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace Raccoon.Ninja.Domain.Core.Models;

[ExcludeFromCodeCoverage]
public record DataLatestHbA1CFuncResponse
{
[JsonProperty("latestSuccessful")]
[JsonPropertyName("latestSuccessful")]
public HbA1CCalculationResponse LatestSuccessful { get; init; }

[JsonProperty("latestPartialSuccessful")]
[JsonPropertyName("latestPartialSuccessful")]
public HbA1CCalculationResponse LatestPartialSuccessful { get; init; }
};
18 changes: 10 additions & 8 deletions Raccoon.Ninja.Domain.Core/Models/GlucoseReadingResponse.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Raccoon.Ninja.Domain.Core.Converters;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;

namespace Raccoon.Ninja.Domain.Core.Models;

[ExcludeFromCodeCoverage]
public record GlucoseReadingResponse
{
[JsonProperty("id")] public string Id { get; init; }
[JsonPropertyName("id")] public string Id { get; init; }

[JsonProperty("trend")]
[JsonPropertyName("trend")]
public Trend Trend { get; init; }

[JsonProperty("value")]
[JsonPropertyName("value")]
public float Value { get; init; }

[JsonProperty("delta", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("delta")]
public float? Delta { get; init; }

[JsonProperty("readAt")]
[JsonPropertyName("readAt")]
public long ReadTimestampUtc { get; init; }

[JsonProperty("readAtDateTimeUtc")] public DateTime ReadTimestampUtcAsDateTime => ReadTimestampUtc.ToUtcDateTime();
[JsonPropertyName("readAtDateTimeUtc")] public DateTime ReadTimestampUtcAsDateTime => ReadTimestampUtc.ToUtcDateTime();

[JsonProperty("trendLabel")] public string TrendLabel => Converter.ToTrendString(Trend);
[JsonPropertyName("trendLabel")] public string TrendLabel => Converter.ToTrendString(Trend);

public static implicit operator GlucoseReadingResponse(GlucoseReading reading) => new()
{
Expand Down
21 changes: 11 additions & 10 deletions Raccoon.Ninja.Domain.Core/Models/HbA1cCalculationResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;
Expand All @@ -7,25 +8,25 @@ namespace Raccoon.Ninja.Domain.Core.Models;

public record HbA1CCalculationResponse
{
[JsonProperty("id")] public string Id { get; init; }
[JsonPropertyName("id")] public string Id { get; init; }

[JsonProperty("value")] public float Value { get; init; }
[JsonPropertyName("value")] public float Value { get; init; }

[JsonProperty("delta", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("delta")]
public float? Delta { get; init; }

[JsonProperty("docType")] public AggregateType DocType { get; init; }
[JsonProperty("referenceDate")] public DateOnly ReferenceDate { get; init; }
[JsonProperty("createdAt")] public long CreatedAtUtc { get; init; }
[JsonProperty("status")] public HbA1CCalculationStatus Status { get; init; }
[JsonPropertyName("docType")] public AggregateType DocType { get; init; }
[JsonPropertyName("referenceDate")] public DateOnly ReferenceDate { get; init; }
[JsonPropertyName("createdAt")] public long CreatedAtUtc { get; init; }
[JsonPropertyName("status")] public HbA1CCalculationStatus Status { get; init; }

[JsonProperty("error", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("error")]
public string Error { get; init; }

/// <summary>
/// A calculation is considered stale if it's more than a day old.
/// </summary>
[JsonProperty("isStale")] public bool IsStale => DateTime.UtcNow.Date.AddDays(-1) > CreatedAtUtc.ToUtcDateTime().Date;
[JsonPropertyName("isStale")] public bool IsStale => DateTime.UtcNow.Date.AddDays(-1) > CreatedAtUtc.ToUtcDateTime().Date;

public static implicit operator HbA1CCalculationResponse(HbA1CCalculation calculation)
{
Expand Down

0 comments on commit be658c0

Please sign in to comment.