DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/CrudController.cs

145 lines
5.2 KiB
C#
Raw Normal View History

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;
using Microsoft.AspNetCore.Mvc;
2022-04-11 18:00:34 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-08-10 14:36:35 +05:00
using System.Threading;
2021-09-10 11:28:57 +05:00
using System.Threading.Tasks;
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
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// CRUD контроллер для админки.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TService"></typeparam>
[ApiController]
[Route("api/[controller]")]
[Authorize]
public abstract class CrudController<T, TService> : ControllerBase
where T : IId
where TService : ICrudRepository<T>
2021-08-02 14:45:13 +05:00
{
2024-08-19 10:01:07 +05:00
protected readonly TService service;
public Func<T, CancellationToken, Task<bool>> InsertForbidAsync { get; protected set; } = null!;
public Func<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]
[Permission]
public virtual async Task<ActionResult<IEnumerable<T>>> GetAllAsync(CancellationToken token)
{
var result = await service.GetAllAsync(token).ConfigureAwait(false);
return Ok(result);
}
2023-05-19 16:51:41 +05:00
2021-09-10 11:28:57 +05:00
/// <summary>
2024-08-19 10:01:07 +05:00
/// Получить одну запись по Id
2021-09-10 11:28:57 +05:00
/// </summary>
2024-08-19 10:01:07 +05:00
/// <param name="id">id записи</param>
/// <param name="token"></param>
/// <returns>запись</returns>
[HttpGet("{id}")]
[Permission]
public virtual async Task<ActionResult<T?>> GetOrDefaultAsync(int id, CancellationToken token)
2021-08-02 14:45:13 +05:00
{
2024-08-19 10:01:07 +05:00
var result = await service.GetOrDefaultAsync(id, token).ConfigureAwait(false);
return Ok(result);
2021-08-02 14:45:13 +05:00
}
2023-05-19 16:51:41 +05:00
2024-08-19 10:01:07 +05:00
/// <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)
{
if (InsertForbidAsync is not null && await InsertForbidAsync(value, token))
return Forbid();
var result = await service.InsertAsync(value, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Добавить несколько записей<br/>
/// При невозможности добавить любую из записей, все не будут добавлены.
/// </summary>
/// <param name="values">записи</param>
/// <param name="token"></param>
/// <returns>id</returns>
[HttpPost("range")]
[Permission]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public virtual async Task<ActionResult<int>> InsertRangeAsync([FromBody] IEnumerable<T> values, CancellationToken token)
{
if (!values.Any())
return this.ValidationBadRequest(nameof(values), "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);
}
/// <summary>
/// Редактировать запись по id
/// </summary>
/// <param name="value">запись</param>
/// <param name="token"></param>
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
[HttpPut]
[Permission]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public virtual async Task<ActionResult<int>> 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 == ICrudRepository<T>.ErrorIdNotFound)
return this.ValidationBadRequest(nameof(value.Id), $"id:{value.Id} does not exist");
return Ok(result);
}
/// <summary>
/// Удалить запись по id
/// </summary>
/// <param name="id">id записи</param>
/// <param name="token"></param>
/// <returns>1 - успешно удалено, 0 - нет</returns>
[HttpDelete("{id}")]
[Permission]
public virtual async Task<ActionResult<int>> 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 == ICrudRepository<T>.ErrorIdNotFound)
return NoContent();
return Ok(result);
}
2021-08-02 14:45:13 +05:00
}