You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
6.5 KiB
188 lines
6.5 KiB
using APIMappers;
|
|
using Dto;
|
|
using HeartTrackAPI.Request;
|
|
using HeartTrackAPI.Responce;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
using Shared;
|
|
using Model.Manager;
|
|
using Model.Repository;
|
|
|
|
namespace HeartTrackAPI.Controllers;
|
|
[ApiController]
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
public class ActivityController : Controller
|
|
{
|
|
private readonly IActivityRepository _activityService;
|
|
private readonly ILogger<ActivityController> _logger;
|
|
|
|
public ActivityController(IDataManager dataManager, ILogger<ActivityController> logger)
|
|
{
|
|
_activityService = dataManager.ActivityRepo;
|
|
_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);
|
|
var activities = await _activityService.GetActivities(pageRequest.Index, pageRequest.Count, ActivityOrderCriteria.None, pageRequest.Descending ?? false);
|
|
if(activities == null)
|
|
{
|
|
return NotFound("No activities found");
|
|
}
|
|
var pageResponse = new PageResponse<ActivityDto>(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);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> 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());
|
|
}
|
|
|
|
/*
|
|
public async Task<IActionResult> PostFitFile( Stream file, string contentType) // [FromForm]
|
|
{
|
|
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
|
|
{
|
|
ModelState.AddModelError("File",
|
|
$"The request couldn't be processed (Error 1).");
|
|
// Log error
|
|
|
|
return BadRequest(ModelState);
|
|
}
|
|
if (file == null)
|
|
{
|
|
return BadRequest("No file was provided");
|
|
}
|
|
//var fileUploadSummary = await _fileService.UploadFileAsync(HttpContext.Request.Body, Request.ContentType);
|
|
// var activity = await _activityManager.AddActivityFromFitFile(file);
|
|
var activity = new Activity
|
|
{
|
|
Id = 1,
|
|
Type = "Running",
|
|
Date = new DateTime(2021, 10, 10),
|
|
StartTime = new DateTime(2021, 10, 10, 10, 0, 0),
|
|
EndTime = new DateTime(2021, 10, 10, 11, 0, 0),
|
|
Effort = 3,
|
|
Variability = 0.5f,
|
|
Variance = 0.5f,
|
|
StandardDeviation = 0.5f,
|
|
Average = 5.0f,
|
|
Maximum = 10,
|
|
Minimum = 0,
|
|
AverageTemperature = 20.0f,
|
|
HasAutoPause = false,
|
|
Users =
|
|
{
|
|
new User
|
|
{
|
|
Id = 3, Username = "Athlete3",
|
|
ProfilePicture =
|
|
"https://plus.unsplash.com/premium_photo-1705091981693-6006f8a20479?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
|
|
FirstName = "First3", LastName = "Last3",
|
|
Sexe = "M", Lenght = 190, Weight = 80, DateOfBirth = new DateTime(1994, 3, 3), Email = "ath@ex.fr",
|
|
Role = new Athlete()
|
|
}
|
|
}
|
|
};
|
|
if (activity == null)
|
|
{
|
|
return BadRequest("The file provided is not a valid fit file");
|
|
}
|
|
return CreatedAtAction(nameof(GetActivity), new { id = activity.Id }, activity.ToDto());
|
|
}*/
|
|
|
|
[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());
|
|
}
|
|
|
|
[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();
|
|
}
|
|
|
|
/*
|
|
public async Task<FileUploadSummary> UploadFileAsync(Stream fileStream, string contentType)
|
|
{
|
|
var fileCount = 0;
|
|
long totalSizeInBytes = 0;
|
|
var boundary = GetBoundary(MediaTypeHeaderValue.Parse(contentType));
|
|
var multipartReader = new MultipartReader(boundary, fileStream);
|
|
var section = await multipartReader.ReadNextSectionAsync();
|
|
var filePaths = new List<string>();
|
|
var notUploadedFiles = new List<string>();
|
|
|
|
while (section != null)
|
|
{
|
|
var fileSection = section.AsFileSection();
|
|
if (fileSection != null)
|
|
{
|
|
totalSizeInBytes += await SaveFileAsync(fileSection, filePaths, notUploadedFiles);
|
|
fileCount++;
|
|
}
|
|
section = await multipartReader.ReadNextSectionAsync();
|
|
}
|
|
return new FileUploadSummary
|
|
{
|
|
TotalFilesUploaded = fileCount,
|
|
TotalSizeUploaded = ConvertSizeToString(totalSizeInBytes),
|
|
FilePaths = filePaths,
|
|
NotUploadedFiles = notUploadedFiles
|
|
};
|
|
}*/
|
|
} |