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.

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