You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.8 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. "gorm.io/gorm"
  8. )
  9. // @title CreateApi
  10. // @description create base apis, 新增基础api
  11. // @auth (2020/04/05 20:22)
  12. // @param api model.SysApi
  13. // @return error
  14. func CreateApi(api model.SysApi) (err error) {
  15. findOne := global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).Find(&model.SysApi{}).Error
  16. if findOne == nil {
  17. return errors.New("存在相同api")
  18. } else {
  19. err = global.GVA_DB.Create(&api).Error
  20. }
  21. return err
  22. }
  23. // @title DeleteApi
  24. // @description delete a base api, 删除基础api
  25. // @param api model.SysApi
  26. // @auth (2020/04/05 20:22)
  27. // @return error
  28. func DeleteApi(api model.SysApi) (err error) {
  29. err = global.GVA_DB.Delete(api).Error
  30. ClearCasbin(1, api.Path, api.Method)
  31. return err
  32. }
  33. // @title GetInfoList
  34. // @description get apis by pagination, 分页获取数据
  35. // @auth (2020/04/05 20:22)
  36. // @param api model.SysApi
  37. // @param info request.PageInfo
  38. // @param order string
  39. // @param desc bool
  40. // @return err error
  41. // @return list interface{}
  42. // @return total int
  43. func GetAPIInfoList(api model.SysApi, info request.PageInfo, order string, desc bool) (err error, list interface{}, total int64) {
  44. limit := info.PageSize
  45. offset := info.PageSize * (info.Page - 1)
  46. db := global.GVA_DB.Model(&model.SysApi{})
  47. var apiList []model.SysApi
  48. if api.Path != "" {
  49. db = db.Where("path LIKE ?", "%"+api.Path+"%")
  50. }
  51. if api.Description != "" {
  52. db = db.Where("description LIKE ?", "%"+api.Description+"%")
  53. }
  54. if api.Method != "" {
  55. db = db.Where("method = ?", api.Method)
  56. }
  57. if api.ApiGroup != "" {
  58. db = db.Where("api_group = ?", api.ApiGroup)
  59. }
  60. err = db.Count(&total).Error
  61. if err != nil {
  62. return err, apiList, total
  63. } else {
  64. db = db.Limit(limit).Offset(offset)
  65. if order != "" {
  66. var OrderStr string
  67. if desc {
  68. OrderStr = order + " desc"
  69. } else {
  70. OrderStr = order
  71. }
  72. err = db.Order(OrderStr).Find(&apiList).Error
  73. } else {
  74. err = db.Order("api_group").Find(&apiList).Error
  75. }
  76. }
  77. return err, apiList, total
  78. }
  79. // @title GetAllApis
  80. // @description get all apis, 获取所有的api
  81. // @auth (2020/04/05 20:22)
  82. // @return err error
  83. // @return apis []SysApi
  84. func GetAllApis() (err error, apis []model.SysApi) {
  85. err = global.GVA_DB.Find(&apis).Error
  86. return
  87. }
  88. // @title GetApiById
  89. // @description 根据id获取api
  90. // @auth (2020/04/05 20:22)
  91. // @param api model.SysApi
  92. // @param id float64
  93. // @return error
  94. func GetApiById(id float64) (err error, api model.SysApi) {
  95. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  96. return
  97. }
  98. // @title UpdateApi
  99. // @description update a base api, update api
  100. // @auth (2020/04/05 20:22)
  101. // @param api model.SysApi
  102. // @return error
  103. func UpdateApi(api model.SysApi) (err error) {
  104. var oldA model.SysApi
  105. err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
  106. if oldA.Path != api.Path || oldA.Method != api.Method {
  107. flag := errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).Find(&model.SysApi{}).Error, gorm.ErrRecordNotFound)
  108. if !flag {
  109. return errors.New("存在相同api路径")
  110. }
  111. }
  112. if err != nil {
  113. return err
  114. } else {
  115. err = UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  116. if err != nil {
  117. return err
  118. } else {
  119. err = global.GVA_DB.Save(&api).Error
  120. }
  121. }
  122. return err
  123. }