BaseService.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core;
  7. /// <summary>
  8. /// 实体操作基服务
  9. /// </summary>
  10. /// <typeparam name="TEntity"></typeparam>
  11. public class BaseService<TEntity> : IDynamicApiController where TEntity : class, new()
  12. {
  13. private readonly SqlSugarRepository<TEntity> _rep;
  14. public BaseService(SqlSugarRepository<TEntity> rep)
  15. {
  16. _rep = rep;
  17. }
  18. /// <summary>
  19. /// 获取详情 🔖
  20. /// </summary>
  21. /// <param name="id"></param>
  22. /// <returns></returns>
  23. [DisplayName("获取详情")]
  24. public virtual async Task<TEntity> GetDetail(long id)
  25. {
  26. return await _rep.GetByIdAsync(id);
  27. }
  28. /// <summary>
  29. /// 获取集合 🔖
  30. /// </summary>
  31. /// <returns></returns>
  32. [DisplayName("获取集合")]
  33. public virtual async Task<List<TEntity>> GetList()
  34. {
  35. return await _rep.GetListAsync();
  36. }
  37. ///// <summary>
  38. ///// 获取实体分页 🔖
  39. ///// </summary>
  40. ///// <param name="input"></param>
  41. ///// <returns></returns>
  42. //[ApiDescriptionSettings(Name = "Page")]
  43. //[DisplayName("获取实体分页")]
  44. //public async Task<SqlSugarPagedList<TEntity>> GetPage([FromQuery] BasePageInput input)
  45. //{
  46. // return await _rep.AsQueryable().ToPagedListAsync(input.Page, input.PageSize);
  47. //}
  48. /// <summary>
  49. /// 增加 🔖
  50. /// </summary>
  51. /// <param name="entity"></param>
  52. /// <returns></returns>
  53. [ApiDescriptionSettings(Name = "Add"), HttpPost]
  54. [DisplayName("增加")]
  55. public virtual async Task<bool> Add(TEntity entity)
  56. {
  57. return await _rep.InsertAsync(entity);
  58. }
  59. /// <summary>
  60. /// 更新 🔖
  61. /// </summary>
  62. /// <param name="entity"></param>
  63. /// <returns></returns>
  64. [ApiDescriptionSettings(Name = "Update"), HttpPost]
  65. [DisplayName("更新")]
  66. public virtual async Task<int> Update(TEntity entity)
  67. {
  68. return await _rep.AsUpdateable(entity).IgnoreColumns(true).ExecuteCommandAsync();
  69. }
  70. /// <summary>
  71. /// 删除 🔖
  72. /// </summary>
  73. /// <param name="id"></param>
  74. /// <returns></returns>
  75. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  76. [DisplayName("删除")]
  77. public virtual async Task<bool> Delete(long id)
  78. {
  79. return await _rep.DeleteByIdAsync(id);
  80. }
  81. }