Skip to content

Commit

Permalink
Added Goals and abstract Trackable goals, that any metric class can e…
Browse files Browse the repository at this point in the history
…xtend. Refactored big parts to adhere to naming scheme of singular entities. Oh yea also added some tests
  • Loading branch information
stijuh committed May 31, 2023
1 parent 3ef4340 commit f1a5537
Show file tree
Hide file tree
Showing 57 changed files with 297 additions and 175 deletions.
11 changes: 11 additions & 0 deletions GainsTracker.Common/Models/Goal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GainsTracker.Common.Models;

public class Goal<T>
{
public Goal(T target)
{
Target = target;
}

public T Target { get; }
}
19 changes: 19 additions & 0 deletions GainsTracker.Common/Models/Interfaces/TrackableGoal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace GainsTracker.Common.Models.Interfaces;

public abstract class TrackableGoal<T>
{
private bool _isInGoal { get; set; }
protected T? Target { get; init; }

public Goal<T> CreateAsGoal()
{
if (_isInGoal)
throw new ArgumentException("Already in goal");

_isInGoal = true;

if (Target == null)
throw new ArgumentNullException();
return new Goal<T>(Target);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Security.Claims;
using GainsTracker.Common;
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friends.Services;
using GainsTracker.CoreAPI.Components.Friends.Services.Dto;
using GainsTracker.CoreAPI.Components.Friend.Services;
using GainsTracker.CoreAPI.Components.Friend.Services.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GainsTracker.CoreAPI.Components.Friends.Controllers;
namespace GainsTracker.CoreAPI.Components.Friend.Controllers;

[ApiController]
[Authorize]
Expand All @@ -25,7 +24,7 @@ public FriendController(IFriendService friendService)
[HttpGet]
public IActionResult GetFriends()
{
List<Friend> friends = _friendService.GetFriends(CurrentUsername);
List<Models.Friend> friends = _friendService.GetFriends(CurrentUsername);
return Ok(friends);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using GainsTracker.Common.Exceptions;
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Friend.Models;
using GainsTracker.CoreAPI.Components.Workout.Models;
using GainsTracker.CoreAPI.Configurations.Database;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Components.Friends.Data;
namespace GainsTracker.CoreAPI.Components.Friend.Data;

public class BigBrainFriends : BigBrain
public class BigBrainFriend : BigBrain
{
public BigBrainFriends(AppDbContext context) : base(context)
public BigBrainFriend(AppDbContext context) : base(context)
{
}

public List<Friend> GetFriendsByGainsId(string gainsId)
public List<Models.Friend> GetFriendsByGainsId(string gainsId)
{
List<Friend>? friendsByGainsId = Context.GainsAccounts
List<Models.Friend>? friendsByGainsId = Context.GainsAccounts
.Include(g => g.Friends)
.FirstOrDefault(g => g.Id == gainsId)?.Friends;
return friendsByGainsId ?? new List<Friend>();
return friendsByGainsId ?? new List<Models.Friend>();
}

public GainsAccount GetFriendInfoByGainsId(string gainsId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GainsTracker.CoreAPI.Components.Friends.Models.Exceptions;
namespace GainsTracker.CoreAPI.Components.Friend.Models.Exceptions;

public class AlreadyFriendsException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GainsTracker.CoreAPI.Components.Friends.Models.Exceptions;
namespace GainsTracker.CoreAPI.Components.Friend.Models.Exceptions;

public class FriendRequestAlreadySentException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Workout.Models;

namespace GainsTracker.CoreAPI.Components.Friends.Models;
namespace GainsTracker.CoreAPI.Components.Friend.Models;

public class Friend
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Workout.Models;

namespace GainsTracker.CoreAPI.Components.Friends.Models;
namespace GainsTracker.CoreAPI.Components.Friend.Models;

public class FriendRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GainsTracker.CoreAPI.Components.Friends.Models;
namespace GainsTracker.CoreAPI.Components.Friend.Models;

public enum FriendRequestStatus
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Workout.Models;

namespace GainsTracker.CoreAPI.Components.Friends.Services.Dto;
namespace GainsTracker.CoreAPI.Components.Friend.Services.Dto;

public class FriendDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friend.Models;

namespace GainsTracker.CoreAPI.Components.Friends.Services.Dto;
namespace GainsTracker.CoreAPI.Components.Friend.Services.Dto;

public class FriendRequestDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GainsTracker.CoreAPI.Components.Friends.Services.Dto;
namespace GainsTracker.CoreAPI.Components.Friend.Services.Dto;

public class FriendRequestOverviewDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using GainsTracker.CoreAPI.Components.Friends.Data;
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friends.Models.Exceptions;
using GainsTracker.CoreAPI.Components.Friends.Services.Dto;
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Gains.Services;
using GainsTracker.CoreAPI.Components.Friend.Data;
using GainsTracker.CoreAPI.Components.Friend.Models;
using GainsTracker.CoreAPI.Components.Friend.Models.Exceptions;
using GainsTracker.CoreAPI.Components.Friend.Services.Dto;
using GainsTracker.CoreAPI.Components.Workout.Models;
using GainsTracker.CoreAPI.Components.Workout.Services;

namespace GainsTracker.CoreAPI.Components.Friends.Services;
namespace GainsTracker.CoreAPI.Components.Friend.Services;

public class FriendService : IFriendService
{
private readonly BigBrainFriends _bigBrain;
private readonly BigBrainFriend _bigBrain;
private readonly IGainsService _gainsService;

public FriendService(BigBrainFriends bigBrain, IGainsService gainsService)
public FriendService(BigBrainFriend bigBrain, IGainsService gainsService)
{
_bigBrain = bigBrain;
_gainsService = gainsService;
}

public List<Friend> GetFriends(string userHandle)
public List<Models.Friend> GetFriends(string userHandle)
{
GainsAccount gainsAccount = _gainsService.GetGainsAccountFromUser(userHandle);
List<Friend> friends = _bigBrain.GetFriendsByGainsId(gainsAccount.Id);
List<Models.Friend> friends = _bigBrain.GetFriendsByGainsId(gainsAccount.Id);

return friends;
}
Expand Down Expand Up @@ -74,7 +74,7 @@ private void CheckFriendshipStatus(string userHandle, string friendHandle)

private bool AreFriends(string userHandle, string friendHandle)
{
List<Friend> userFriends = GetFriends(userHandle);
List<Models.Friend> userFriends = GetFriends(userHandle);
return userFriends.Any(friend =>
string.Equals(friend.FriendHandle, friendHandle, StringComparison.InvariantCultureIgnoreCase));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friends.Services.Dto;
using GainsTracker.CoreAPI.Components.Friend.Services.Dto;

namespace GainsTracker.CoreAPI.Components.Friends.Services;
namespace GainsTracker.CoreAPI.Components.Friend.Services;

public interface IFriendService
{
public void SendFriendRequest(string username, string friendName);
public void HandleFriendRequestState(string username, string requestId, bool accept = true);
public List<Friend> GetFriends(string username);
public List<Models.Friend> GetFriends(string username);
public FriendRequestOverviewDto GetFriendRequests(string username);
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GainsTracker.CoreAPI.Components.HealthMetric.Controllers;

[ApiController]
[Authorize]
[Route("gains/health-metric")]
public class HealthMetricController : ControllerBase
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GainsTracker.CoreAPI.Configurations.Database;

namespace GainsTracker.CoreAPI.Components.HealthMetric.Data;

public class BigBrainHealthMetric : BigBrain
{
public BigBrainHealthMetric(AppDbContext context) : base(context)
{
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using GainsTracker.Common.Models.Interfaces;

namespace GainsTracker.CoreAPI.Components.HealthMetric.Models;

public class ProteinMetric : TrackableGoal<ProteinMetric>
{
public DateTime Date { get; init; }
public long TotalProteinIntake { get; set; }

public ProteinMetric()
{
Date = DateTime.UtcNow;
Target = this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GainsTracker.CoreAPI.Components.HealthMetric.Services;

public class HealthMetricService
{

}
2 changes: 1 addition & 1 deletion GainsTracker.CoreAPI/Components/Security/Models/User.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Workout.Models;
using Microsoft.AspNetCore.Identity;

namespace GainsTracker.CoreAPI.Components.Security.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Security.Claims;
using System.Text;
using GainsTracker.Common.Exceptions;
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Security.Controllers.DTO;
using GainsTracker.CoreAPI.Components.Security.Models;
using GainsTracker.CoreAPI.Components.Workout.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Security.Claims;
using GainsTracker.Common;
using GainsTracker.CoreAPI.Components.Gains.Services;
using GainsTracker.CoreAPI.Components.Workout.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace GainsTracker.CoreAPI.Components.Workout.Controllers.Examples;

public class GainsExamplesController
{
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Security.Claims;
using GainsTracker.Common;
using GainsTracker.CoreAPI.Components.Gains.Models;
using GainsTracker.CoreAPI.Components.Gains.Services;
using GainsTracker.CoreAPI.Components.Gains.Services.Dto;
using GainsTracker.CoreAPI.Components.Workout.Models;
using GainsTracker.CoreAPI.Components.Workout.Services;
using GainsTracker.CoreAPI.Components.Workout.Services.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GainsTracker.CoreAPI.Components.Gains.Controllers;
namespace GainsTracker.CoreAPI.Components.Workout.Controllers;

[ApiController]
[Authorize]
[Route("gains")]
[Route("gains/workout")]
public class GainsController : ControllerBase
{
private readonly IGainsService _gainsService;
Expand All @@ -29,27 +29,27 @@ public IActionResult GetUserGains()
return Ok(account);
}

[HttpGet("workout")]
[HttpGet]
public async Task<IActionResult> GetUserWorkouts()
{
List<WorkoutDto> workouts = await _gainsService.GetWorkoutsByUsername(CurrentUsername);
return Ok(workouts);
}

[HttpPost("workout")]
[HttpPost]
public IActionResult AddWorkoutToAccount([FromBody] WorkoutDto workout)
{
_gainsService.AddWorkoutToGainsAccount(CurrentUsername, workout);
return NoContent();
}

[HttpGet("workout/{workoutId}/measurement")]
[HttpGet("{workoutId}/measurement")]
public IActionResult GetWorkoutWithMeasurements(string workoutId)
{
return Ok(_gainsService.GetWorkoutMeasurementsById(workoutId));
}

[HttpPost("workout/{workoutId}/measurement")]
[HttpPost("{workoutId}/measurement")]
public IActionResult AddMeasurementToWorkout(string workoutId, [FromBody] MeasurementDto measurementDto)
{
_gainsService.AddMeasurementToWorkout(workoutId, measurementDto);
Expand Down
Loading

0 comments on commit f1a5537

Please sign in to comment.