Skip to content

Commit

Permalink
MetricDtos are now serializing all sub-properties to json
Browse files Browse the repository at this point in the history
  • Loading branch information
stijuh committed Jun 6, 2023
1 parent 7ad90a1 commit 84f036a
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 41 deletions.
16 changes: 16 additions & 0 deletions GainsTracker.Common/Models/Generic/GenericJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json;

namespace GainsTracker.Common.Models.Generic;

public static class GenericJsonSerializer
{
public static JsonDocument SerializeObjectToJson(object objectToSerialize)
{
JsonSerializerOptions options = new()
{
PropertyNameCaseInsensitive = true
};

return JsonSerializer.SerializeToDocument(objectToSerialize, objectToSerialize.GetType(), options);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Security.Claims;
using GainsTracker.Common;
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friends.Services;
using GainsTracker.CoreAPI.Components.Friends.Services.Dto;
using GainsTracker.CoreAPI.Configurations.Controllers;
Expand All @@ -20,7 +18,7 @@ public FriendController(IFriendService friendService)
{
_friendService = friendService;
}

[HttpGet]
public IActionResult GetFriends()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;
using GainsTracker.CoreAPI.Configurations.Controllers;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -20,14 +19,14 @@ public HealthMetricController(IHealthMetricService metricService)
}

[HttpGet]
public List<Metric> GetAllMetrics()
public List<MetricDto> GetAllMetrics()
{
return _metricService.GetAllMetricsByUsername(CurrentUsername);
}

[HttpPost]
public void CreateMetric(MetricDto metricDto)
public void CreateMetric(CreateMetricDto createMetricDto)
{
_metricService.AddMetricToGainsAccount(CurrentUsername, metricDto);
_metricService.AddMetricToGainsAccount(CurrentUsername, createMetricDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Components.Workouts.Models;
using GainsTracker.CoreAPI.Configurations.Database;
using Microsoft.EntityFrameworkCore;

Expand All @@ -12,8 +13,8 @@ public BigBrainHealthMetric(AppDbContext context) : base(context)

public List<Metric> GetAllMetricsByUsername(string username)
{
var gainsId = GetGainsIdByUsername(username);
var gains = Context.GainsAccounts.Include(g => g.Metrics)
string gainsId = GetGainsIdByUsername(username);
GainsAccount gains = Context.GainsAccounts.Include(g => g.Metrics)
.First(g => g.Id == gainsId);

return gains.Metrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace GainsTracker.CoreAPI.Components.HealthMetrics.Models;
[JsonDerivedType(typeof(WeightMetric))]
public abstract class Metric
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public DateTime LoggingDate { get; init; } = DateTime.UtcNow;
[JsonIgnore] public string Id { get; set; } = Guid.NewGuid().ToString();
[JsonIgnore] public MetricType Type { get; set; }
[JsonIgnore] public DateTime LoggingDate { get; init; } = DateTime.UtcNow;
protected bool _isInGoal { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace GainsTracker.CoreAPI.Components.HealthMetrics.Models;

public class ProteinMetric : Metric, ITrackableGoal<ProteinMetric>
{
public long TotalProteinIntake { get; set; }
public long ProteinIntake { get; set; }

public Goal<ProteinMetric> CreateAsGoal()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json;
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;

public class CreateMetricDto
{
public MetricType Type { get; set; }
public JsonDocument Data { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;

public class MetricDto
{
public string Id { get; set; }
public MetricType Type { get; set; }
public DateTime LoggingDate { get; set; }
public JsonDocument Data { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Data;
using GainsTracker.Common.Models.Generic;
using GainsTracker.CoreAPI.Components.HealthMetrics.Data;
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;
using GainsTracker.CoreAPI.Components.Workouts.Models;
Expand All @@ -14,19 +15,24 @@ public HealthMetricService(BigBrainHealthMetric bigBrain)
_bigBrain = bigBrain;
}

public void AddMetricToGainsAccount(string userHandle, MetricDto metricDto)
public void AddMetricToGainsAccount(string userHandle, CreateMetricDto createMetricDto)
{
Metric metric = MetricFactory.DeserializeMetricFromJson(metricDto.Type, metricDto.Data);
Metric metric = MetricFactory.DeserializeMetricFromJson(createMetricDto.Type, createMetricDto.Data);
GainsAccount gains = _bigBrain.GetGainsAccountByUsername(userHandle);
gains.AddMetric(metric);

_bigBrain.SaveContext();
}

public List<Metric> GetAllMetricsByUsername(string username)
public List<MetricDto> GetAllMetricsByUsername(string username)
{
List<Metric> data = _bigBrain.GetAllMetricsByUsername(username);

return data;
return data.Select(m => new MetricDto
{
Id = m.Id,
Type = m.Type,
LoggingDate = m.LoggingDate,
Data = GenericJsonSerializer.SerializeObjectToJson(m)
}).ToList();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Services;

public interface IHealthMetricService
{
void AddMetricToGainsAccount(string userHandle, MetricDto metricDto);
List<Metric> GetAllMetricsByUsername(string currentUsername);
void AddMetricToGainsAccount(string userHandle, CreateMetricDto createMetricDto);
List<MetricDto> GetAllMetricsByUsername(string currentUsername);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public UserProfileController(IGainsService gainsService)
{
_gainsService = gainsService;
}

//TODO: make this into a bigger updating profile thing with a dto, not little parts like this.
[HttpGet("displayname")]
public void UpdateDisplayName(string displayName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace GainsTracker.CoreAPI.Configurations.Controllers;

/// <summary>
/// Extended controller class to add commonly used controller functionality.
/// Extended controller class to add commonly used controller functionality.
/// </summary>
public abstract class ExtendedControllerBase : ControllerBase
{
Expand Down
23 changes: 12 additions & 11 deletions GainsTracker.CoreAPI/GainsTracker.CoreAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="7.0.2" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.15" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="AutoMapper" Version="12.0.1"/>
<PackageReference Include="EFCore.NamingConventions" Version="7.0.2"/>
<PackageReference Include="EntityFramework" Version="6.4.4"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.15"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.12" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.12"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GainsTracker.Common\GainsTracker.Common.csproj" />
<ProjectReference Include="..\GainsTracker.Common\GainsTracker.Common.csproj"/>
</ItemGroup>

<ItemGroup>
<Folder Include="Components\Workouts\Controllers\Examples\" />
<Folder Include="Components\Workouts\Controllers\Examples\"/>
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions GainsTracker.Testing.CoreAPI/GoalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public void GoalTargetContainsCorrectValues()
{
ProteinMetric protein = new()
{
TotalProteinIntake = 34
ProteinIntake = 34
};

Goal<ProteinMetric> proteinGoal = protein.CreateAsGoal();

Assert.Equal(34, proteinGoal.Target.TotalProteinIntake);
Assert.Equal(34, proteinGoal.Target.ProteinIntake);
}

[Fact(DisplayName = "Creating goals from targets that already are a goal results in an exception.")]
Expand Down
4 changes: 2 additions & 2 deletions GainsTracker.Testing.CoreAPI/MetricTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public void Test()
{
ProteinMetric protein = new()
{
TotalProteinIntake = 34
ProteinIntake = 34
};

Assert.Equal(34, protein.TotalProteinIntake);
Assert.Equal(34, protein.ProteinIntake);
}
}

0 comments on commit 84f036a

Please sign in to comment.