forked from ddrilling/AsbCloudServer
116 lines
4.3 KiB
C#
116 lines
4.3 KiB
C#
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<IActionResult> 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<IActionResult> 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]
|
||
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}")]
|
||
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}")]
|
||
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);
|
||
}
|
||
}
|
||
}
|