BaseService.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证。
  2. //
  3. // 必须在法律法规允许的范围内正确使用,严禁将其用于非法、欺诈、恶意或侵犯他人合法权益的目的。
  4. namespace Admin.NET.Core;
  5. /// <summary>
  6. /// 实体操作基服务
  7. /// </summary>
  8. /// <typeparam name="TEntity"></typeparam>
  9. public class BaseService<TEntity> : IDynamicApiController where TEntity : class, new()
  10. {
  11. private readonly SqlSugarRepository<TEntity> _rep;
  12. public BaseService(SqlSugarRepository<TEntity> rep)
  13. {
  14. _rep = rep;
  15. }
  16. /// <summary>
  17. /// 获取详情 🔖
  18. /// </summary>
  19. /// <param name="id"></param>
  20. /// <returns></returns>
  21. [DisplayName("获取详情")]
  22. public virtual async Task<TEntity> GetDetail(long id)
  23. {
  24. return await _rep.GetByIdAsync(id);
  25. }
  26. /// <summary>
  27. /// 获取集合 🔖
  28. /// </summary>
  29. /// <returns></returns>
  30. [DisplayName("获取集合")]
  31. public virtual async Task<List<TEntity>> GetList()
  32. {
  33. return await _rep.GetListAsync();
  34. }
  35. ///// <summary>
  36. ///// 获取实体分页 🔖
  37. ///// </summary>
  38. ///// <param name="input"></param>
  39. ///// <returns></returns>
  40. //[ApiDescriptionSettings(Name = "Page")]
  41. //[DisplayName("获取实体分页")]
  42. //public async Task<SqlSugarPagedList<TEntity>> GetPage([FromQuery] BasePageInput input)
  43. //{
  44. // return await _rep.AsQueryable().ToPagedListAsync(input.Page, input.PageSize);
  45. //}
  46. /// <summary>
  47. /// 增加 🔖
  48. /// </summary>
  49. /// <param name="entity"></param>
  50. /// <returns></returns>
  51. [ApiDescriptionSettings(Name = "Add"), HttpPost]
  52. [DisplayName("增加")]
  53. public virtual async Task<bool> Add(TEntity entity)
  54. {
  55. return await _rep.InsertAsync(entity);
  56. }
  57. /// <summary>
  58. /// 更新 🔖
  59. /// </summary>
  60. /// <param name="entity"></param>
  61. /// <returns></returns>
  62. [ApiDescriptionSettings(Name = "Update"), HttpPost]
  63. [DisplayName("更新")]
  64. public virtual async Task<int> Update(TEntity entity)
  65. {
  66. return await _rep.AsUpdateable(entity).IgnoreColumns(true).ExecuteCommandAsync();
  67. }
  68. /// <summary>
  69. /// 删除 🔖
  70. /// </summary>
  71. /// <param name="id"></param>
  72. /// <returns></returns>
  73. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  74. [DisplayName("删除")]
  75. public virtual async Task<bool> Delete(long id)
  76. {
  77. return await _rep.DeleteByIdAsync(id);
  78. }
  79. }