persistence/Persistence.API/Controllers/TechMessagesController.cs

129 lines
4.3 KiB
C#
Raw Normal View History

2024-12-10 10:43:12 +05:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
2024-12-05 11:30:07 +05:00
using Persistence.Models.Requests;
using Persistence.Repositories;
2024-12-10 10:43:12 +05:00
using System.Net;
namespace Persistence.API.Controllers;
/// <summary>
/// Работа с состояниями систем автобурения (АБ)
/// </summary>
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class TechMessagesController : ControllerBase
{
2024-12-10 10:43:12 +05:00
private readonly ITechMessagesRepository techMessagesRepository;
private static readonly Dictionary<int, string> categories = new()
{
{ 0, "System" },
{ 1, "Авария" },
{ 2, "Предупреждение" },
{ 3, "Инфо" },
{ 4, "Прочее" }
};
public TechMessagesController(ITechMessagesRepository techMessagesRepository)
{
this.techMessagesRepository = techMessagesRepository;
}
/// <summary>
/// Получить список технологических сообщений в виде страницы
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult<PaginationContainer<TechMessageDto>>> GetPage([FromQuery] PaginationRequest request, CancellationToken token)
{
var result = await techMessagesRepository.GetPage(request, token);
return Ok(result);
}
/// <summary>
/// Получить статистику по системам
/// </summary>
/// <param name="autoDrillingSystem"></param>
/// <param name="categoryIds"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("statistics")]
public async Task<ActionResult<IEnumerable<MessagesStatisticDto>>> GetStatistics([FromQuery] IEnumerable<string> autoDrillingSystem, [FromQuery] IEnumerable<int> categoryIds, CancellationToken token)
{
var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token);
return Ok(result);
}
/// <summary>
/// Получить список всех систем
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("systems")]
public async Task<ActionResult<Dictionary<string, int>>> GetSystems(CancellationToken token)
{
var result = await techMessagesRepository.GetSystems(token);
return Ok(result);
}
/// <summary>
/// Получить диапазон дат, для которых есть данные в репозитории
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("range")]
public async Task<ActionResult<DatesRangeDto>> GetDatesRangeAsync(CancellationToken token)
{
var result = await techMessagesRepository.GetDatesRangeAsync(token);
return Ok(result);
}
/// <summary>
/// Получить порцию записей, начиная с заданной даты
/// </summary>
/// <param name="dateBegin"></param>
/// <param name="take"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("part")]
public async Task<ActionResult<IEnumerable<SetpointLogDto>>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token)
{
var result = await techMessagesRepository.GetPart(dateBegin, take, token);
return Ok(result);
}
/// <summary>
/// Добавить новые технологические сообщения
/// </summary>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
public async Task<IActionResult> AddRange([FromBody] IEnumerable<TechMessageDto> dtos, CancellationToken token)
{
var userId = User.GetUserId<Guid>();
var result = await techMessagesRepository.AddRange(dtos, userId, token);
2024-12-10 10:43:12 +05:00
return CreatedAtAction(nameof(AddRange), result);
}
/// <summary>
/// Получить словарь категорий
/// </summary>
/// <returns></returns>
[HttpGet("categories")]
public ActionResult<Dictionary<int, string>> GetImportantCategories()
{
return Ok(categories);
}
}