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