delete.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package api_management
  2. import (
  3. "adm-management/errors"
  4. v1 "adm-management/pb/v1"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  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. )
  14. func DeleteAPI(ctx context.Context, req *v1.DeleteAPIRequest) (reply *v1.DeleteAPIReply, err error) {
  15. reply = &v1.DeleteAPIReply{}
  16. // 捕获各个task中的异常并返回给调用者
  17. defer func() {
  18. if r := recover(); r != nil {
  19. err = fmt.Errorf("%+v", r)
  20. e := &status.Status{}
  21. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  22. logger.Error("err",
  23. zap.String("system_err", err.Error()),
  24. zap.Stack("stacktrace"))
  25. }
  26. }
  27. }()
  28. db := database.DB()
  29. err = db.Exec("DELETE FROM t_adm_api_management WHERE id = ?", req.ApiId).Error
  30. if err != nil {
  31. if err == gorm.ErrRecordNotFound {
  32. return reply, nil
  33. }
  34. return reply, errors.SystemError
  35. }
  36. return reply, nil
  37. }
  38. func DeleteKey(ctx context.Context, req *v1.DeleteKeyRequest) (reply *v1.DeleteKeyReply, err error) {
  39. reply = &v1.DeleteKeyReply{}
  40. // 捕获各个task中的异常并返回给调用者
  41. defer func() {
  42. if r := recover(); r != nil {
  43. err = fmt.Errorf("%+v", r)
  44. e := &status.Status{}
  45. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  46. logger.Error("err",
  47. zap.String("system_err", err.Error()),
  48. zap.Stack("stacktrace"))
  49. }
  50. }
  51. }()
  52. db := database.DB()
  53. err = db.Exec("DELETE FROM t_adm_key_management WHERE `key` = ?", req.Key).Error
  54. if err != nil {
  55. if err == gorm.ErrRecordNotFound {
  56. return reply, nil
  57. }
  58. return reply, errors.SystemError
  59. }
  60. return reply, nil
  61. }