parent
224f16110f
commit
cc255de979
@ -0,0 +1,19 @@
|
||||
namespace Dto;
|
||||
|
||||
public class ActivityDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Type { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public TimeSpan Duration { get; set; }
|
||||
public float Distance { get; set; }
|
||||
public float Elevation { get; set; }
|
||||
public float AverageSpeed { get; set; }
|
||||
public int AverageHeartRate { get; set; }
|
||||
public int Calories { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string? Gpx { get; set; }
|
||||
public string? Image { get; set; }
|
||||
public int AthleteId { get; set; }
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
using Dto;
|
||||
using HeartTrackAPI.Request;
|
||||
using HeartTrackAPI.Responce;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Shared;
|
||||
using Model;
|
||||
/*
|
||||
namespace HeartTrackAPI.Controllers;
|
||||
[ApiController]
|
||||
[Route("api/activities")]
|
||||
public class ActivityController : Controller
|
||||
{
|
||||
private readonly IActivityService _activityService;
|
||||
private readonly ILogger<ActivityController> _logger;
|
||||
|
||||
public ActivityController(IActivityService activityService, ILogger<ActivityController> logger)
|
||||
{
|
||||
_activityService = activityService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(PageResponse<ActivityDto>), 200)]
|
||||
[ProducesResponseType(400)]
|
||||
[ProducesResponseType(500)]
|
||||
public async Task<ActionResult<PageResponse<ActivityDto>>> GetActivities([FromQuery] PageRequest pageRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var totalCount = await _activityService.GetNbItems();
|
||||
if (pageRequest.Count * pageRequest.Index >= totalCount)
|
||||
{
|
||||
_logger.LogError("To many object is asked the max is {totalCount} but the request is superior of ", totalCount);
|
||||
return BadRequest("To many object is asked the max is : " + totalCount);
|
||||
}
|
||||
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetActivities), pageRequest);
|
||||
// request.OrderingPropertyName
|
||||
var activities = await _activityService.GetActivities(pageRequest.Index, pageRequest.Count, ActivityOrderCriteria.None, pageRequest.Descending ?? false);
|
||||
var pageResponse = new PageResponse<UserDto>(pageRequest.Index, pageRequest.Count, totalCount, activities.Select(a => a.ToDto()));
|
||||
return Ok(pageResponse);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error while getting all activities");
|
||||
return StatusCode(500);
|
||||
}
|
||||
}
|
||||
/*
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<ActivityDto>> GetActivity(int id)
|
||||
{
|
||||
var activity = await _activityService.GetActivityByIdAsync(id);
|
||||
if (activity == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok(activity.ToDto());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ActivityDto>> PostActivity(ActivityDto activityDto)
|
||||
{
|
||||
var activity = activityDto.ToModel();
|
||||
var result = await _activityService.AddActivity(activity);
|
||||
if (result == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
return CreatedAtAction(nameof(GetActivity), new { id = result.Id }, result.ToDto());
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutActivity(int id, ActivityDto activityDto)
|
||||
{
|
||||
if (id != activityDto.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
var activity = activityDto.ToModel();
|
||||
var result = await _activityService.UpdateActivity(id, activity);
|
||||
if (result == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteActivity(int id)
|
||||
{
|
||||
var result = await _activityService.DeleteActivity(id);
|
||||
if (!result)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
}*/
|
@ -0,0 +1,68 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Model;
|
||||
public class Activity
|
||||
{
|
||||
public int IdActivity { get; private set; }
|
||||
public string Type { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime EndTime { get; set; }
|
||||
|
||||
private int _effort;
|
||||
[Range(0, 5)]
|
||||
public int Effort
|
||||
{
|
||||
get => _effort;
|
||||
set
|
||||
{
|
||||
if (value < 0 || value > 5)
|
||||
{
|
||||
throw new ArgumentException("Effort must be between 0 and 5.");
|
||||
}
|
||||
_effort = value;
|
||||
}
|
||||
}
|
||||
public float Variability { get; set; }
|
||||
public float Variance { get; set; }
|
||||
public float StandardDeviation { get; set; }
|
||||
public float Average { get; set; }
|
||||
public int Maximum { get; set; }
|
||||
public int Minimum { get; set; }
|
||||
public float AverageTemperature { get; set; }
|
||||
public bool HasAutoPause { get; set; }
|
||||
|
||||
public Activity(int idActivity ,string type, DateTime date, DateTime startTime, DateTime endTime,
|
||||
int effort, float variability, float variance, float standardDeviation,
|
||||
float average, int maximum, int minimum, float averageTemperature, bool hasAutoPause)
|
||||
{
|
||||
IdActivity = idActivity;
|
||||
Type = type;
|
||||
Date = date;
|
||||
StartTime = startTime;
|
||||
EndTime = endTime;
|
||||
Effort = effort;
|
||||
Variability = variability;
|
||||
Variance = variance;
|
||||
StandardDeviation = standardDeviation;
|
||||
Average = average;
|
||||
Maximum = maximum;
|
||||
Minimum = minimum;
|
||||
AverageTemperature = averageTemperature;
|
||||
HasAutoPause = hasAutoPause;
|
||||
}
|
||||
public Activity(){}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Activity #{IdActivity}: {Type} on {Date:d/M/yyyy} from {StartTime:HH:mm:ss} to {EndTime:HH:mm:ss}" +
|
||||
$" with an effort of {Effort}/5 and an average temperature of {AverageTemperature}°C" +
|
||||
$" and a heart rate variability of {Variability} bpm" +
|
||||
$" and a variance of {Variance} bpm" +
|
||||
$" and a standard deviation of {StandardDeviation} bpm" +
|
||||
$" and an average of {Average} bpm" +
|
||||
$" and a maximum of {Maximum} bpm" +
|
||||
$" and a minimum of {Minimum} bpm" +
|
||||
$" and auto pause is {(HasAutoPause ? "enabled" : "disabled")}.";
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using Shared;
|
||||
|
||||
namespace Model;
|
||||
|
||||
public interface IActivityService
|
||||
{
|
||||
public Task<IEnumerable<Activity>> GetActivities(int index, int count, ActivityOrderCriteria criteria, bool descending = false);
|
||||
public Task<Activity?> GetActivityByIdAsync(int id);
|
||||
public Task<Activity?> AddActivity(Activity activity);
|
||||
public Task<Activity?> UpdateActivity(int id, Activity activity);
|
||||
public Task<bool> DeleteActivity(int id);
|
||||
public Task<int> GetNbItems();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
namespace Shared;
|
||||
|
||||
public enum ActivityOrderCriteria
|
||||
{
|
||||
None,
|
||||
ByName,
|
||||
ByType,
|
||||
ByDate,
|
||||
ByDuration,
|
||||
ByDistance,
|
||||
ByElevation,
|
||||
ByAverageSpeed,
|
||||
ByAverageHeartRate,
|
||||
ByCalories,
|
||||
ByDescription
|
||||
}
|
Loading…
Reference in new issue