Skip to content

Commit

Permalink
Added first API endpoints for Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
stijuh committed Jun 4, 2023
1 parent 4134dbb commit 7ad90a1
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace GainsTracker.CoreAPI.Components.Friends.Controllers;
[ApiController]
[Authorize]
[Route("friend")]
public class FriendControllerBase : ExtendedControllerBase
public class FriendController : ExtendedControllerBase
{
private readonly IFriendService _friendService;

public FriendControllerBase(IFriendService friendService)
public FriendController(IFriendService friendService)
{
_friendService = friendService;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;
using GainsTracker.CoreAPI.Configurations.Controllers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Controllers;

[ApiController]
[Authorize]
[Route("gains/health-metric")]
public class HealthMetricController : ExtendedControllerBase
{
private readonly IHealthMetricService _metricService;

public HealthMetricController(IHealthMetricService metricService)
{
_metricService = metricService;
}

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

[HttpPost]
public void CreateMetric(MetricDto metricDto)
{
_metricService.AddMetricToGainsAccount(CurrentUsername, metricDto);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GainsTracker.CoreAPI.Configurations.Database;
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Configurations.Database;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Data;

Expand All @@ -7,4 +9,13 @@ public class BigBrainHealthMetric : BigBrain
public BigBrainHealthMetric(AppDbContext context) : base(context)
{
}

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

return gains.Metrics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Models;

[Table("Metric")]
[Table("metric")]
[JsonDerivedType(typeof(ProteinMetric))]
[JsonDerivedType(typeof(WeightMetric))]
public abstract class Metric
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public DateTime LoggingDate { get; init; } = DateTime.UtcNow;
protected bool _isInGoal { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using GainsTracker.Common.Models;
using GainsTracker.Common.Models.Generic;

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Models;

public class ProteinMetric : Metric
public class ProteinMetric : Metric, ITrackableGoal<ProteinMetric>
{
public ProteinMetric()
{
Date = DateTime.UtcNow;
}

public string Id { get; set; } = Guid.NewGuid().ToString();
public DateTime Date { get; init; }
public long TotalProteinIntake { get; set; }

public Goal<ProteinMetric> CreateAsGoal()
{
throw new NotImplementedException();
if (_isInGoal)
throw new ArgumentException("Already in a goal!");
_isInGoal = true;

return new Goal<ProteinMetric>(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ public void AddMetricToGainsAccount(string userHandle, MetricDto metricDto)

_bigBrain.SaveContext();
}

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

return data;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GainsTracker.CoreAPI.Components.Security.Controllers;

[Route("authentication")]
[Route("auth")]
[ApiController]
public class AuthenticationController : ControllerBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace GainsTracker.CoreAPI.Components.UserProfiles.Controllers;
[ApiController]
[Authorize]
[Route("/profile")]
public class UserProfileControllerBase : ExtendedControllerBase
public class UserProfileController : ExtendedControllerBase
{
private readonly IGainsService _gainsService;

public UserProfileControllerBase(IGainsService gainsService)
public UserProfileController(IGainsService gainsService)
{
_gainsService = gainsService;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace GainsTracker.CoreAPI.Components.Workouts.Controllers;
[ApiController]
[Authorize]
[Route("gains/workout")]
public class GainsControllerBase : ExtendedControllerBase
public class GainsController : ExtendedControllerBase
{
private readonly IGainsService _gainsService;

public GainsControllerBase(IGainsService service)
public GainsController(IGainsService service)
{
_gainsService = service;
}
Expand Down
4 changes: 4 additions & 0 deletions GainsTracker.CoreAPI/GainsTracker.CoreAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
<ItemGroup>
<ProjectReference Include="..\GainsTracker.Common\GainsTracker.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Components\Workouts\Controllers\Examples\" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion GainsTracker.CoreAPI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"commandLineArgs": "False"
"commandLineArgs": "False"
}
}
}

0 comments on commit 7ad90a1

Please sign in to comment.