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.

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