You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
5.4 KiB
161 lines
5.4 KiB
using Kean.Application.Utilities.Interfaces;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Kean.Presentation.Rest.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 通用服务
|
|
/// </summary>
|
|
[ApiController, Route("api")]
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
public class UtilitiesController : ControllerBase
|
|
{
|
|
private readonly ICrudService _crudService; // 通用增删改查服务
|
|
|
|
/// <summary>
|
|
/// 依赖注入
|
|
/// </summary>
|
|
public UtilitiesController(
|
|
ICrudService crudService)
|
|
{
|
|
_crudService = crudService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取资源列表
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
[HttpGet("{resource}")]
|
|
[ProducesResponseType(200)]
|
|
public async Task<IActionResult> Get(
|
|
[FromRoute] string resource,
|
|
[FromQuery] IDictionary<string, string> parameters)
|
|
{
|
|
string sort = null;
|
|
int? offset = null;
|
|
int? limit = null;
|
|
var parameter = parameters.FirstOrDefault(p => p.Key.Equals("sort", StringComparison.OrdinalIgnoreCase));
|
|
if (parameter.Key != null)
|
|
{
|
|
sort = parameter.Value;
|
|
parameters.Remove(parameter);
|
|
}
|
|
parameter = parameters.FirstOrDefault(p => p.Key.Equals("offset", StringComparison.OrdinalIgnoreCase));
|
|
if (parameter.Key != null)
|
|
{
|
|
if (int.TryParse(parameter.Value, out var i))
|
|
{
|
|
offset = i;
|
|
}
|
|
parameters.Remove(parameter);
|
|
}
|
|
parameter = parameters.FirstOrDefault(p => p.Key.Equals("limit", StringComparison.OrdinalIgnoreCase));
|
|
if (parameter.Key != null)
|
|
{
|
|
if (int.TryParse(parameter.Value, out var i))
|
|
{
|
|
limit = i;
|
|
}
|
|
parameters.Remove(parameter);
|
|
}
|
|
var items = await _crudService.GetList(resource, parameters, sort, offset, limit);
|
|
if (offset.HasValue || limit.HasValue)
|
|
{
|
|
var total = await _crudService.GetCount(resource, parameters);
|
|
return StatusCode(200, new { items, total });
|
|
}
|
|
else
|
|
{
|
|
return StatusCode(200, new { items, total = items.Count() });
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取资源项
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
[HttpGet("{resource}/{id}")]
|
|
[ProducesResponseType(200)]
|
|
public async Task<IActionResult> Get(
|
|
[FromRoute] string resource,
|
|
[FromRoute] string id)
|
|
{
|
|
return StatusCode(200, await _crudService.GetItem(resource, id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建资源
|
|
/// </summary>
|
|
/// <response code="201">成功</response>
|
|
[HttpPost("{resource}")]
|
|
[ProducesResponseType(201)]
|
|
public async Task<IActionResult> Create(
|
|
[FromRoute] string resource,
|
|
[FromBody] IDictionary<string, JsonElement> values)
|
|
{
|
|
return StatusCode(201, await _crudService.Create(resource, values.ToDictionary(
|
|
i => i.Key,
|
|
i => i.Value.ValueKind == JsonValueKind.Null ? null : i.Value.ToString()
|
|
)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改资源
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
[HttpPut("{resource}/{id}")]
|
|
[ProducesResponseType(200)]
|
|
public async Task<IActionResult> Modify(
|
|
[FromRoute] string resource,
|
|
[FromRoute] string id,
|
|
[FromBody] IDictionary<string, JsonElement> values)
|
|
{
|
|
await _crudService.Modify(resource, id, values.ToDictionary(
|
|
i => i.Key,
|
|
i => i.Value.ValueKind == JsonValueKind.Null ? null : i.Value.ToString()
|
|
));
|
|
return StatusCode(200);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除资源
|
|
/// </summary>
|
|
/// <response code="204">成功</response>
|
|
[HttpDelete("{resource}/{id}")]
|
|
[ProducesResponseType(204)]
|
|
public async Task<IActionResult> Delete(
|
|
[FromRoute] string resource,
|
|
[FromRoute] string id)
|
|
{
|
|
await _crudService.Delete(resource, new string[] { id });
|
|
return StatusCode(204);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量处理资源
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
/// <response code="405">方法不支持</response>
|
|
[HttpPost("{resource}/batch")]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(405)]
|
|
public async Task<IActionResult> Batch(
|
|
[FromRoute] string resource,
|
|
[FromBody] Batch<JsonElement> batch)
|
|
{
|
|
switch (batch.Method)
|
|
{
|
|
case BatchMethod.Delete:
|
|
await _crudService.Delete(resource, batch.Data.Select(i => i.ToString()));
|
|
return StatusCode(200);
|
|
default:
|
|
return StatusCode(405);
|
|
}
|
|
}
|
|
}
|
|
}
|