forked from ddrilling/AsbCloudServer
118 lines
4.3 KiB
C#
118 lines
4.3 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
// 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 Func<T, CancellationToken, Task<bool>> InsertForbidAsync { get; protected set; } = null;
|
|
public Func<int, T, CancellationToken, Task<bool>> UpdateForbidAsync { get; protected set; } = null;
|
|
public Func<int, CancellationToken, Task<bool>> DeleteForbidAsync { get; protected set; } = null;
|
|
|
|
public CrudController(TService service)
|
|
{
|
|
this.service = service;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить все записи
|
|
/// </summary>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>все записи</returns>
|
|
[HttpGet("all")]
|
|
[Permission]
|
|
public virtual async Task<ActionResult<IEnumerable<T>>> GetAllAsync(CancellationToken token = default)
|
|
{
|
|
var result = await service.GetAllAsync(token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить одну запись по Id
|
|
/// </summary>
|
|
/// <param name="id">id записи</param>
|
|
/// <param name="token"></param>
|
|
/// <returns>запись</returns>
|
|
[HttpGet("{id}")]
|
|
[Permission]
|
|
public virtual async Task<ActionResult<T>> GetAsync(int id, CancellationToken token = default)
|
|
{
|
|
var result = await service.GetAsync(id, token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавить запись
|
|
/// </summary>
|
|
/// <param name="value">запись</param>
|
|
/// <param name="token"></param>
|
|
/// <returns>id</returns>
|
|
[HttpPost]
|
|
[Permission]
|
|
public virtual async Task<ActionResult<int>> InsertAsync([FromBody] T value, CancellationToken token = default)
|
|
{
|
|
if (InsertForbidAsync is not null && await InsertForbidAsync(value, token))
|
|
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}")]
|
|
[Permission]
|
|
public virtual async Task<ActionResult<int>> UpdateAsync(int id, [FromBody] T value, CancellationToken token = default)
|
|
{
|
|
if (UpdateForbidAsync is not null && await UpdateForbidAsync(id, value, token))
|
|
Forbid();
|
|
|
|
var result = await service.UpdateAsync(id, value, token).ConfigureAwait(false);
|
|
if (result == 0)
|
|
return BadRequest($"id:{id} does not exist in the db");
|
|
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<ActionResult<int>> DeleteAsync(int id, CancellationToken token = default)
|
|
{
|
|
if (DeleteForbidAsync is not null && await DeleteForbidAsync(id, token))
|
|
Forbid();
|
|
|
|
var result = await service.DeleteAsync(id, token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
}
|
|
|
|
|
|
}
|