DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/CrudController.cs
2021-10-25 17:38:52 +05:00

120 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// CRUD контроллер для админки.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TService"></typeparam>
[ApiController]
[Authorize]
public abstract class CrudController<T, TService> : ControllerBase
where T : IId
where TService : ICrudService<T>
{
protected readonly TService service;
public List<string> Roles { get; } = new List<string> { "Администратор" };
public CrudController(TService service)
{
this.service = service;
}
/// <summary>
/// Получить страницу с записями в PaginationContainer
/// </summary>
/// <param name="skip">пропустить skip записей</param>
/// <param name="take">получить take записей</param>
/// <param name="token">CancellationToken</param>
/// <returns>страница с записями в PaginationContainer</returns>
[HttpGet()]
public virtual async Task<ActionResult<PaginationContainer<T>>> GetPage(int skip = 0, int take = 32,
CancellationToken token = default)
{
if (!Roles.Any(role => User.IsInRole(role)))
return Forbid();
var result = await service.GetPageAsync(skip, take, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получить одну запись по Id
/// </summary>
/// <param name="id">id записи</param>
/// <param name="token"></param>
/// <returns>запись</returns>
[HttpGet("{id}")]
public virtual async Task<ActionResult<T>> Get(int id, CancellationToken token = default)
{
if (!Roles.Any(role => User.IsInRole(role)))
return Forbid();
var result = await service.GetAsync(id, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Добавить запись
/// </summary>
/// <param name="value">запись</param>
/// <param name="token"></param>
/// <returns>1 - добавлено, 0 - нет</returns>
[HttpPost]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public virtual async Task<IActionResult> Insert([FromBody] T value, CancellationToken token = default)
{
if (!Roles.Any(role => User.IsInRole(role)))
return Forbid();
var result = await service.InsertAsync(value, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Редактировать запись по id
/// </summary>
/// <param name="id">id записи</param>
/// <param name="value">запись</param>
/// <param name="token"></param>
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
[HttpPut("{id}")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public virtual async Task<IActionResult> Put(int id, [FromBody] T value, CancellationToken token = default)
{
if (!Roles.Any(role => User.IsInRole(role)))
return Forbid();
var result = await service.UpdateAsync(id, value, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Удалить запись по id
/// </summary>
/// <param name="id">id записи</param>
/// <param name="token"></param>
/// <returns>1 - успешно удалено, 0 - нет</returns>
[HttpDelete("{id}")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public virtual async Task<IActionResult> Delete(int id, CancellationToken token = default)
{
if (!Roles.Any(role => User.IsInRole(role)))
return Forbid();
var result = await service.DeleteAsync(id, token).ConfigureAwait(false);
return Ok(result);
}
}
}