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.

163 lines
4.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. "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 AutoCreateApi
  31. // @description delete a base api by path and method, 删除基础api
  32. // @param api model.SysApi
  33. // @auth (2020/04/05 20:22)
  34. // @return error
  35. func AutoCreateApi(api model.SysApi) (err error) {
  36. err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  37. var fApi model.SysApi
  38. var txErr error
  39. fxErr := tx.Where("path = ? AND method = ?", api.Path, api.Method).First(&fApi).Error
  40. if errors.Is(fxErr, gorm.ErrRecordNotFound) {
  41. txErr = tx.Create(&api).Error
  42. if txErr != nil{
  43. return txErr
  44. }
  45. }
  46. return nil
  47. })
  48. return err
  49. }
  50. // @title GetInfoList
  51. // @description get apis by pagination, 分页获取数据
  52. // @auth (2020/04/05 20:22)
  53. // @param api model.SysApi
  54. // @param info request.PageInfo
  55. // @param order string
  56. // @param desc bool
  57. // @return err error
  58. // @return list interface{}
  59. // @return total int
  60. func GetAPIInfoList(api model.SysApi, info request.PageInfo, order string, desc bool) (err error, list interface{}, total int64) {
  61. limit := info.PageSize
  62. offset := info.PageSize * (info.Page - 1)
  63. db := global.GVA_DB.Model(&model.SysApi{})
  64. var apiList []model.SysApi
  65. if api.Path != "" {
  66. db = db.Where("path LIKE ?", "%"+api.Path+"%")
  67. }
  68. if api.Description != "" {
  69. db = db.Where("description LIKE ?", "%"+api.Description+"%")
  70. }
  71. if api.Method != "" {
  72. db = db.Where("method = ?", api.Method)
  73. }
  74. if api.ApiGroup != "" {
  75. db = db.Where("api_group = ?", api.ApiGroup)
  76. }
  77. err = db.Count(&total).Error
  78. if err != nil {
  79. return err, apiList, total
  80. } else {
  81. db = db.Limit(limit).Offset(offset)
  82. if order != "" {
  83. var OrderStr string
  84. if desc {
  85. OrderStr = order + " desc"
  86. } else {
  87. OrderStr = order
  88. }
  89. err = db.Order(OrderStr).Find(&apiList).Error
  90. } else {
  91. err = db.Order("api_group").Find(&apiList).Error
  92. }
  93. }
  94. return err, apiList, total
  95. }
  96. // @title GetAllApis
  97. // @description get all apis, 获取所有的api
  98. // @auth (2020/04/05 20:22)
  99. // @return err error
  100. // @return apis []SysApi
  101. func GetAllApis() (err error, apis []model.SysApi) {
  102. err = global.GVA_DB.Find(&apis).Error
  103. return
  104. }
  105. // @title GetApiById
  106. // @description 根据id获取api
  107. // @auth (2020/04/05 20:22)
  108. // @param api model.SysApi
  109. // @param id float64
  110. // @return error
  111. func GetApiById(id float64) (err error, api model.SysApi) {
  112. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  113. return
  114. }
  115. // @title UpdateApi
  116. // @description update a base api, update api
  117. // @auth (2020/04/05 20:22)
  118. // @param api model.SysApi
  119. // @return error
  120. func UpdateApi(api model.SysApi) (err error) {
  121. var oldA model.SysApi
  122. err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
  123. if oldA.Path != api.Path || oldA.Method != api.Method {
  124. if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&model.SysApi{}).Error, gorm.ErrRecordNotFound) {
  125. return errors.New("存在相同api路径")
  126. }
  127. }
  128. if err != nil {
  129. return err
  130. } else {
  131. err = UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  132. if err != nil {
  133. return err
  134. } else {
  135. err = global.GVA_DB.Save(&api).Error
  136. }
  137. }
  138. return err
  139. }