persistence/Persistence.API/Controllers/ChangeLogController.cs

176 lines
6.3 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
using Persistence.Models.Requests;
using Persistence.Repositories;
2024-11-29 10:03:52 +05:00
using System.Net;
namespace Persistence.API.Controllers;
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class ChangeLogController : ControllerBase, IChangeLogApi
{
private IChangeLogRepository repository;
public ChangeLogController(IChangeLogRepository repository)
{
this.repository = repository;
}
2024-11-26 10:32:44 +05:00
[HttpPost("{idDiscriminator}")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
2024-11-29 10:03:52 +05:00
public async Task<IActionResult> Add(
[FromRoute] Guid idDiscriminator,
2024-11-27 17:59:37 +05:00
[FromBody] DataWithWellDepthAndSectionDto dto,
CancellationToken token)
{
2024-11-26 10:32:44 +05:00
var userId = User.GetUserId<Guid>();
var result = await repository.AddRange(userId, idDiscriminator, [dto], token);
2024-12-03 17:05:46 +05:00
return CreatedAtAction(nameof(Add), result);
}
[HttpPost("range/{idDiscriminator}")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
2024-11-29 10:03:52 +05:00
public async Task<IActionResult> AddRange(
[FromRoute] Guid idDiscriminator,
[FromBody] IEnumerable<DataWithWellDepthAndSectionDto> dtos,
2024-12-03 17:05:46 +05:00
CancellationToken token = default)
{
2024-11-27 17:59:37 +05:00
var userId = User.GetUserId<Guid>();
var result = await repository.AddRange(userId, idDiscriminator, dtos, token);
2024-11-27 17:59:37 +05:00
2024-12-03 17:05:46 +05:00
return CreatedAtAction(nameof(AddRange), result);
}
2024-11-26 10:32:44 +05:00
[HttpDelete]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
2024-12-03 17:05:46 +05:00
public async Task<IActionResult> Delete(Guid id, CancellationToken token = default)
{
2024-11-27 17:59:37 +05:00
var userId = User.GetUserId<Guid>();
var result = await repository.MarkAsDeleted(userId, [id], token);
return Ok(result);
}
2024-11-26 10:32:44 +05:00
[HttpDelete("range")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
2024-12-03 17:05:46 +05:00
public async Task<IActionResult> DeleteRange(IEnumerable<Guid> ids, CancellationToken token = default)
{
2024-11-27 17:59:37 +05:00
var userId = User.GetUserId<Guid>();
var result = await repository.MarkAsDeleted(userId, ids, token);
return Ok(result);
}
[HttpPost("replace/{idDiscriminator}")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> ClearAndAddRange(
[FromRoute] Guid idDiscriminator,
[FromBody] IEnumerable<DataWithWellDepthAndSectionDto> dtos,
2024-12-03 17:05:46 +05:00
CancellationToken token = default)
{
var userId = User.GetUserId<Guid>();
var result = await repository.ClearAndAddRange(userId, idDiscriminator, dtos, token);
return Ok(result);
}
[HttpPut]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Update(
DataWithWellDepthAndSectionDto dto,
2024-12-03 17:05:46 +05:00
CancellationToken token = default)
{
var userId = User.GetUserId<Guid>();
var result = await repository.UpdateRange(userId, [dto], token);
return Ok(result);
}
[HttpPut("range")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> UpdateRange(
IEnumerable<DataWithWellDepthAndSectionDto> dtos,
2024-12-03 17:05:46 +05:00
CancellationToken token = default)
{
var userId = User.GetUserId<Guid>();
var result = await repository.UpdateRange(userId, dtos, token);
return Ok(result);
}
[HttpGet("{idDiscriminator}")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(PaginationContainer<DataWithWellDepthAndSectionDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCurrent(
[FromRoute] Guid idDiscriminator,
[FromQuery] SectionPartRequest filterRequest,
[FromQuery] PaginationRequest paginationRequest,
2024-12-03 17:05:46 +05:00
CancellationToken token = default)
2024-11-27 17:59:37 +05:00
{
var moment = new DateTimeOffset(3000, 1, 1, 0, 0, 0, TimeSpan.Zero);
var result = await repository.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest, token);
2024-11-27 17:59:37 +05:00
return Ok(result);
}
[HttpGet("moment/{idDiscriminator}")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(PaginationContainer<DataWithWellDepthAndSectionDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetByDate(
[FromRoute] Guid idDiscriminator,
2024-11-27 17:59:37 +05:00
DateTimeOffset moment,
[FromQuery] SectionPartRequest filterRequest,
[FromQuery] PaginationRequest paginationRequest,
2024-12-03 17:05:46 +05:00
CancellationToken token = default)
{
var result = await repository.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest, token);
2024-11-27 17:59:37 +05:00
return Ok(result);
}
[HttpGet("history/{idDiscriminator}")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(IEnumerable<ChangeLogDto>), (int)HttpStatusCode.OK)]
2024-12-03 17:05:46 +05:00
public async Task<IActionResult> GetChangeLogForDate(
[FromRoute] Guid idDiscriminator,
DateTimeOffset dateBegin,
2024-12-03 17:05:46 +05:00
DateTimeOffset dateEnd,
CancellationToken token = default)
{
var result = await repository.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, token);
2024-11-27 17:59:37 +05:00
return Ok(result);
}
[HttpGet("datesChange/{idDiscriminator}")]
2024-11-29 10:03:52 +05:00
[ProducesResponseType(typeof(IEnumerable<DateOnly>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetDatesChange([FromRoute] Guid idDiscriminator, CancellationToken token = default)
{
var result = await repository.GetDatesChange(idDiscriminator, token);
2024-11-27 17:59:37 +05:00
return Ok(result);
}
[HttpGet("part/{idDiscriminator}")]
[ProducesResponseType(typeof(IEnumerable<DataWithWellDepthAndSectionDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetPart([FromRoute] Guid idDiscriminator, DateTimeOffset dateBegin, int take = 86400, CancellationToken token = default)
{
var result = await repository.GetGtDate(idDiscriminator, dateBegin, token);
2024-11-27 17:59:37 +05:00
return Ok(result);
}
[HttpGet("datesRange/{idDiscriminator}")]
[ProducesResponseType(typeof(DatesRangeDto), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
public async Task<IActionResult> GetDatesRangeAsync([FromRoute] Guid idDiscriminator, CancellationToken token = default)
{
var result = await repository.GetDatesRange(idDiscriminator, token);
2024-11-27 17:59:37 +05:00
if (result is null)
2024-12-03 17:05:46 +05:00
return NoContent();
return Ok(result);
}
}