Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseCrudController.cs

102 lines
3.2 KiB
C#
Raw Normal View History

2022-04-06 18:05:00 +08:00
using Microsoft.AspNetCore.Mvc;
2022-04-09 16:16:32 +08:00
using Microsoft.Extensions.Localization;
2022-04-06 18:05:00 +08:00
using Yi.Framework.Common.Models;
2022-04-08 23:44:25 +08:00
using Yi.Framework.Interface;
2022-04-09 16:16:32 +08:00
using Yi.Framework.Language;
2022-04-08 23:44:25 +08:00
using Yi.Framework.Model.Models;
2022-04-06 18:05:00 +08:00
using Yi.Framework.Model.Query;
using Yi.Framework.Repository;
2022-04-06 22:22:45 +08:00
using Yi.Framework.WebCore.AttributeExtend;
2022-04-06 18:05:00 +08:00
namespace Yi.Framework.ApiMicroservice.Controllers
{
2022-04-07 22:48:10 +08:00
/// <summary>
/// Json To Sql 类比模式,通用模型
2022-04-07 22:48:10 +08:00
/// </summary>
/// <typeparam name="T"></typeparam>
2022-04-06 18:05:00 +08:00
[ApiController]
2022-04-08 23:44:25 +08:00
public class BaseCrudController<T> : ControllerBase where T : BaseModelEntity,new()
2022-04-06 18:05:00 +08:00
{
2022-04-16 00:01:00 +08:00
private readonly ILogger<T> _logger;
private IBaseService<T> _baseService;
private IRepository<T> _repository;
2022-04-08 23:44:25 +08:00
public BaseCrudController(ILogger<T> logger, IBaseService<T> iBaseService)
2022-04-06 18:05:00 +08:00
{
_logger = logger;
2022-04-08 23:44:25 +08:00
_baseService = iBaseService;
_repository = iBaseService._repository;
2022-04-06 18:05:00 +08:00
}
2022-04-07 22:48:10 +08:00
/// <summary>
/// 主键查询
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:get:one")]
2022-04-06 18:05:00 +08:00
[HttpGet]
public async Task<Result> GetById(long id)
2022-04-06 22:22:45 +08:00
{
2022-04-08 23:44:25 +08:00
return Result.Success().SetData(await _repository.GetByIdAsync(id));
2022-04-06 22:22:45 +08:00
}
2022-04-07 22:48:10 +08:00
/// <summary>
/// 列表查询
/// </summary>
/// <returns></returns>
[Permission($"{nameof(T)}:get:list")]
2022-04-08 23:44:25 +08:00
[HttpPost]
public async Task<Result> GetList(QueryCondition queryCondition)
2022-04-06 18:05:00 +08:00
{
2022-04-08 23:44:25 +08:00
return Result.Success().SetData(await _repository.GetListAsync(queryCondition));
2022-04-06 18:05:00 +08:00
}
2022-04-07 22:48:10 +08:00
/// <summary>
/// 条件分页查询
/// </summary>
/// <param name="queryCondition"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:get:page")]
2022-04-06 18:05:00 +08:00
[HttpPost]
public async Task<Result> PageList(QueryPageCondition queryCondition)
2022-04-06 18:05:00 +08:00
{
2022-04-10 22:56:45 +08:00
return Result.Success().SetData(await _repository.CommonPageAsync(queryCondition));
2022-04-06 18:05:00 +08:00
}
2022-04-07 22:48:10 +08:00
/// <summary>
/// 添加
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:add")]
2022-04-06 18:05:00 +08:00
[HttpPost]
public async Task<Result> Add(T entity)
{
2022-04-09 16:16:32 +08:00
return Result.Success().SetData(await _repository.InsertReturnSnowflakeIdAsync(entity));
2022-04-06 18:05:00 +08:00
}
2022-04-07 22:48:10 +08:00
/// <summary>
/// 修改
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:update")]
2022-04-06 18:05:00 +08:00
[HttpPut]
public async Task<Result> Update(T entity)
{
2022-04-10 22:56:45 +08:00
return Result.Success().SetStatus(await _repository.UpdateIgnoreNullAsync(entity));
2022-04-06 18:05:00 +08:00
}
2022-04-07 22:48:10 +08:00
/// <summary>
/// 列表删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:delete:list")]
2022-04-06 18:05:00 +08:00
[HttpDelete]
2022-04-09 16:16:32 +08:00
public async Task<Result> DeleteList(List<long> ids)
2022-04-06 18:05:00 +08:00
{
2022-04-10 22:56:45 +08:00
return Result.Success().SetStatus(await _repository.DeleteByLogicAsync(ids));
2022-04-06 18:05:00 +08:00
}
}
}