checkAPI.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package api_management
  2. import (
  3. "adm-management/errors"
  4. "adm-management/model"
  5. v1 "adm-management/pb/v1"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "git.getensh.com/common/gopkgsv2/database"
  10. "git.getensh.com/common/gopkgsv2/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. )
  15. func CheckAPI(ctx context.Context, req *v1.CheckAPIRequest) (reply *v1.CheckAPIReply, err error) {
  16. defer func() {
  17. if r := recover(); r != nil {
  18. err = fmt.Errorf("%+v", r)
  19. e := &status.Status{}
  20. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  21. logger.Error("err",
  22. zap.String("system_err", err.Error()),
  23. zap.Stack("stacktrace"))
  24. }
  25. }
  26. }()
  27. reply = &v1.CheckAPIReply{}
  28. db := database.DB()
  29. api := model.AdmAPIList{}
  30. err = db.Raw("SELECT api_no, `desc`, request, response,related_table_ids FROM t_adm_api_management where id = ?", req.ApiId).Find(&api).Error
  31. if err != nil {
  32. if err == gorm.ErrRecordNotFound {
  33. return reply, nil
  34. }
  35. return reply, errors.SystemError
  36. }
  37. related := []model.AdmRelatedTableList{}
  38. str := "select table_name, `desc` from t_adm_data_management where id in (" + api.RelatedTableIds + ")"
  39. err = db.Raw(str).Find(&related).Error
  40. if err != nil {
  41. if err == gorm.ErrRecordNotFound {
  42. return reply, nil
  43. }
  44. reply.ApiNo = api.ApiNo
  45. reply.Desc = api.Desc
  46. reply.Request = api.Request
  47. reply.Response = api.Response
  48. reply.List = make([]*v1.RelatedTables, 0, 0)
  49. return reply, nil
  50. }
  51. reply.ApiNo = api.ApiNo
  52. reply.Desc = api.Desc
  53. reply.Request = api.Request
  54. reply.Response = api.Response
  55. reply.List = make([]*v1.RelatedTables, 0, len(related))
  56. for _, v := range related {
  57. reply.List = append(reply.List, &v1.RelatedTables{
  58. TableName: v.TableName,
  59. Desc: v.Desc,
  60. })
  61. }
  62. return reply, nil
  63. }