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
{
///
/// 通用服务
///
[ApiController, Route("api")]
[ApiExplorerSettings(IgnoreApi = true)]
public class UtilitiesController : ControllerBase
{
private readonly ICrudService _crudService; // 通用增删改查服务
///
/// 依赖注入
///
public UtilitiesController(
ICrudService crudService)
{
_crudService = crudService;
}
///
/// 获取资源列表
///
/// 成功
[HttpGet("{resource}")]
[ProducesResponseType(200)]
public async Task Get(
[FromRoute] string resource,
[FromQuery] IDictionary 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() });
}
}
///
/// 获取资源项
///
/// 成功
[HttpGet("{resource}/{id}")]
[ProducesResponseType(200)]
public async Task Get(
[FromRoute] string resource,
[FromRoute] string id)
{
return StatusCode(200, await _crudService.GetItem(resource, id));
}
///
/// 创建资源
///
/// 成功
[HttpPost("{resource}")]
[ProducesResponseType(201)]
public async Task Create(
[FromRoute] string resource,
[FromBody] IDictionary values)
{
return StatusCode(201, await _crudService.Create(resource, values.ToDictionary(
i => i.Key,
i => i.Value.ValueKind == JsonValueKind.Null ? null : i.Value.ToString()
)));
}
///
/// 修改资源
///
/// 成功
[HttpPut("{resource}/{id}")]
[ProducesResponseType(200)]
public async Task Modify(
[FromRoute] string resource,
[FromRoute] string id,
[FromBody] IDictionary values)
{
await _crudService.Modify(resource, id, values.ToDictionary(
i => i.Key,
i => i.Value.ValueKind == JsonValueKind.Null ? null : i.Value.ToString()
));
return StatusCode(200);
}
///
/// 删除资源
///
/// 成功
[HttpDelete("{resource}/{id}")]
[ProducesResponseType(204)]
public async Task Delete(
[FromRoute] string resource,
[FromRoute] string id)
{
await _crudService.Delete(resource, new string[] { id });
return StatusCode(204);
}
///
/// 批量处理资源
///
/// 成功
/// 方法不支持
[HttpPost("{resource}/batch")]
[ProducesResponseType(200)]
[ProducesResponseType(405)]
public async Task Batch(
[FromRoute] string resource,
[FromBody] Batch batch)
{
switch (batch.Method)
{
case BatchMethod.Delete:
await _crudService.Delete(resource, batch.Data.Select(i => i.ToString()));
return StatusCode(200);
default:
return StatusCode(405);
}
}
}
}