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

102 lines
3.1 KiB
C#
Raw Normal View History

2022-04-06 18:05:00 +08:00
using Microsoft.AspNetCore.Mvc;
using Yi.Framework.Common.Models;
2022-04-08 23:44:25 +08:00
using Yi.Framework.Interface;
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>
/// 6666
/// </summary>
/// <typeparam name="T"></typeparam>
2022-04-06 18:05:00 +08:00
[ApiController]
[Route("api/[controller]/[action]")]
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-08 23:44:25 +08:00
public readonly ILogger<T> _logger;
public IBaseService<T> _baseService;
public IRepository<T> _repository;
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]
2022-04-06 22:22:45 +08:00
public async Task<Result> Get(object id)
{
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]
2022-04-08 23:44:25 +08:00
public async Task<Result> Page(QueryPageCondition queryCondition)
2022-04-06 18:05:00 +08:00
{
2022-04-08 23:44:25 +08:00
return Result.Success().SetData(await _repository.CommonPage(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-08 23:44:25 +08:00
return Result.Success().SetData(await _repository.InsertReturnEntityAsync(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-08 23:44:25 +08:00
return Result.Success().SetStatus(await _repository.UpdateAsync(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-08 23:44:25 +08:00
public async Task<Result> DeleteList(List<Guid> ids)
2022-04-06 18:05:00 +08:00
{
2022-04-08 23:44:25 +08:00
return Result.Success().SetStatus(await _repository.DeleteByLogic(ids));
2022-04-06 18:05:00 +08:00
}
}
}