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.

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