using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
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
{
///
/// CRUD контроллер для админки.
///
///
///
[ApiController]
[Route("api/[controller]")]
[Authorize]
public abstract class CrudController : ControllerBase
where T : IId
where TService : ICrudService
{
protected readonly TService service;
public Func> InsertForbidAsync { get; protected set; } = null;
public Func> UpdateForbidAsync { get; protected set; } = null;
public Func> DeleteForbidAsync { get; protected set; } = null;
public CrudController(TService service)
{
this.service = service;
}
///
/// Получить все записи
///
/// CancellationToken
/// все записи
[HttpGet]
[Permission]
public virtual async Task>> GetAllAsync(CancellationToken token)
{
var result = await service.GetAllAsync(token).ConfigureAwait(false);
return Ok(result);
}
///
/// Получить одну запись по Id
///
/// id записи
///
/// запись
[HttpGet("{id}")]
[Permission]
public virtual async Task> GetAsync(int id, CancellationToken token)
{
var result = await service.GetAsync(id, token).ConfigureAwait(false);
return Ok(result);
}
///
/// Добавить запись
///
/// запись
///
/// id
[HttpPost]
[Permission]
public virtual async Task> InsertAsync([FromBody] T value, CancellationToken token)
{
if (InsertForbidAsync is not null && await InsertForbidAsync(value, token))
return Forbid();
var result = await service.InsertAsync(value, token).ConfigureAwait(false);
return Ok(result);
}
///
/// Добавить несколько записей
/// При невозможности добавить любую из записей, все не будут добавлены.
///
/// записи
///
/// id
[HttpPost("range")]
[Permission]
public virtual async Task> InsertRangeAsync([FromBody] IEnumerable values, CancellationToken token)
{
if (!values.Any())
return BadRequest("there is no values to add");
if (InsertForbidAsync is not null)
foreach (var value in values)
if(await InsertForbidAsync(value, token))
return Forbid();
var result = await service.InsertRangeAsync(values, token).ConfigureAwait(false);
return Ok(result);
}
///
/// Редактировать запись по id
///
/// запись
///
/// 1 - успешно отредактировано, 0 - нет
[HttpPut]
[Permission]
public virtual async Task> UpdateAsync([FromBody] T value, CancellationToken token)
{
if (UpdateForbidAsync is not null && await UpdateForbidAsync(value, token))
return Forbid();
var result = await service.UpdateAsync(value, token).ConfigureAwait(false);
if (result == ICrudService.ErrorIdNotFound)
return BadRequest($"id:{value.Id} does not exist in the db");
return Ok(result);
}
///
/// Удалить запись по id
///
/// id записи
///
/// 1 - успешно удалено, 0 - нет
[HttpDelete("{id}")]
public virtual async Task> DeleteAsync(int id, CancellationToken token)
{
if (DeleteForbidAsync is not null && await DeleteForbidAsync(id, token))
return Forbid();
var result = await service.DeleteAsync(id, token).ConfigureAwait(false);
if (result == ICrudService.ErrorIdNotFound)
return NoContent();
return Ok(result);
}
}
}