1
0
Files
moniteur-baie/DataApi/Controllers/DataPacketsController.cs
2023-06-21 13:51:38 +02:00

60 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using MoniteurBaie.DataApi.Interfaces.Repositories;
using MoniteurBaie.DataModels;
namespace MoniteurBaie.DataApi.Controllers;
[ApiController]
[Route("packets")]
public class DataPacketsController : ControllerBase
{
private readonly ILogger<DataPacketsController> _logger;
private readonly IDataRepository _dataRepository;
public DataPacketsController(ILogger<DataPacketsController> logger, IDataRepository dataRepository)
{
_logger = logger;
_dataRepository = dataRepository;
}
[HttpGet("{id}", Name = "GetDataPacket")]
public async Task<IActionResult> GetAsync(uint id)
{
var packet = await _dataRepository.GetAsync(id);
return packet is null ? NotFound() : Ok(packet);
}
[HttpPost(Name = "CreateDataPacket")]
public async Task<IActionResult> CreateAsync(DataPacket packet)
{
var packetId = await _dataRepository.CreateAsync(packet);
return CreatedAtRoute("GetDataPacket", new { id = packetId }, packet with { Id = packetId });
}
[HttpDelete("{id}", Name = "DeleteDataPacket")]
public async Task<IActionResult> DeleteAsync(uint id)
{
var success = await _dataRepository.DeleteAsync(id);
return success ? NoContent() : NotFound();
}
[HttpGet("last", Name = "GetLastDataPackets")]
public IActionResult GetLastAsync(int count)
{
return Ok(_dataRepository.GetLastAsync(count));
}
[HttpGet("range", Name = "GetRangeDataPackets")]
public IActionResult GetRangeAsync(DateTime fromInstant, DateTime toInstant)
{
return Ok(_dataRepository.GetRangeAsync(fromInstant, toInstant));
}
[HttpPost("bulk", Name = "BulkCreateDataPackets")]
public async Task<IActionResult> BulkCreateAsync(IEnumerable<DataPacket> packets)
{
await _dataRepository.BulkCreateAsync(packets);
return Ok();
}
}