sys_api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-admin-team/go-admin-core/sdk/runtime"
  6. "github.com/go-admin-team/go-admin-core/sdk/service"
  7. "gorm.io/gorm"
  8. "go-admin/app/admin/models"
  9. "go-admin/app/admin/service/dto"
  10. "go-admin/common/actions"
  11. cDto "go-admin/common/dto"
  12. )
  13. type SysApi struct {
  14. service.Service
  15. }
  16. // GetPage 获取SysApi列表
  17. func (e *SysApi) GetPage(c *dto.SysApiGetPageReq, p *actions.DataPermission, list *[]models.SysApi, count *int64) error {
  18. var err error
  19. var data models.SysApi
  20. err = e.Orm.Debug().Model(&data).
  21. Scopes(
  22. cDto.MakeCondition(c.GetNeedSearch()),
  23. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  24. actions.Permission(data.TableName(), p),
  25. ).
  26. Find(list).Limit(-1).Offset(-1).
  27. Count(count).Error
  28. if err != nil {
  29. e.Log.Errorf("Service GetSysApiPage error:%s", err)
  30. return err
  31. }
  32. return nil
  33. }
  34. // Get 获取SysApi对象with id
  35. func (e *SysApi) Get(d *dto.SysApiGetReq, p *actions.DataPermission, model *models.SysApi) *SysApi {
  36. var data models.SysApi
  37. err := e.Orm.Model(&data).
  38. Scopes(
  39. actions.Permission(data.TableName(), p),
  40. ).
  41. First(model, d.GetId()).Error
  42. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  43. err = errors.New("查看对象不存在或无权查看")
  44. e.Log.Errorf("Service GetSysApi error:%s", err)
  45. _ = e.AddError(err)
  46. return e
  47. }
  48. if err != nil {
  49. e.Log.Errorf("db error:%s", err)
  50. _ = e.AddError(err)
  51. return e
  52. }
  53. return e
  54. }
  55. // Update 修改SysApi对象
  56. func (e *SysApi) Update(c *dto.SysApiUpdateReq, p *actions.DataPermission) error {
  57. var model = models.SysApi{}
  58. db := e.Orm.Debug().First(&model, c.GetId())
  59. if db.RowsAffected == 0 {
  60. return errors.New("无权更新该数据")
  61. }
  62. c.Generate(&model)
  63. db = e.Orm.Save(&model)
  64. if err := db.Error; err != nil {
  65. e.Log.Errorf("Service UpdateSysApi error:%s", err)
  66. return err
  67. }
  68. return nil
  69. }
  70. // Remove 删除SysApi
  71. func (e *SysApi) Remove(d *dto.SysApiDeleteReq, p *actions.DataPermission) error {
  72. var data models.SysApi
  73. db := e.Orm.Model(&data).
  74. Scopes(
  75. actions.Permission(data.TableName(), p),
  76. ).Delete(&data, d.GetId())
  77. if err := db.Error; err != nil {
  78. e.Log.Errorf("Service RemoveSysApi error:%s", err)
  79. return err
  80. }
  81. if db.RowsAffected == 0 {
  82. return errors.New("无权删除该数据")
  83. }
  84. return nil
  85. }
  86. // CheckStorageSysApi 创建SysApi对象
  87. func (e *SysApi) CheckStorageSysApi(c *[]runtime.Router) error {
  88. for _, v := range *c {
  89. err := e.Orm.Debug().Where(models.SysApi{Path: v.RelativePath, Action: v.HttpMethod}).
  90. Attrs(models.SysApi{Handle: v.Handler}).
  91. FirstOrCreate(&models.SysApi{}).Error
  92. if err != nil {
  93. err := fmt.Errorf("Service CheckStorageSysApi error: %s \r\n ", err.Error())
  94. return err
  95. }
  96. }
  97. return nil
  98. }