add.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 utimes.cc. All rights reserved.
  2. // Use of this source code is governed by utimes.cc.
  3. package action
  4. import (
  5. "gd_admin/apis"
  6. "gd_admin/errors"
  7. "gd_admin/impl/dbmodel"
  8. "encoding/json"
  9. "fmt"
  10. "gd_admin/common.in/jsonrpc2"
  11. "gd_admin/common.in/utils"
  12. "github.com/astaxie/beego/orm"
  13. "go.uber.org/zap"
  14. "golang.org/x/net/context"
  15. )
  16. func Add(ctx context.Context, req *apis.AddActionLogReq, reply *apis.AddActionLogReply) (err error) {
  17. // 捕获各个task中的异常并返回给调用者
  18. defer func() {
  19. if r := recover(); r != nil {
  20. err := fmt.Errorf("%+v", r)
  21. e := &jsonrpc2.Error{}
  22. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  23. l.Error("err",
  24. zap.String("system_err", err.Error()),
  25. zap.Stack("stacktrace"))
  26. }
  27. }
  28. }()
  29. if req.Uid <= 0 ||
  30. req.Modules == "" ||
  31. req.Func == "" ||
  32. req.ModifyTime == "" {
  33. return errors.ArgsError
  34. }
  35. p := dbmodel.Action{
  36. Uid: req.Uid,
  37. Name: req.Name,
  38. Modules: req.Modules,
  39. Func: req.Func,
  40. Before: req.Before,
  41. After: req.Content,
  42. CreatedAt: req.ModifyTime,
  43. }
  44. if _, err := p.Create(orm.NewOrm()); err != nil {
  45. l.Error("mysql",
  46. zap.String("sql", fmt.Sprintf("INSET %s", p.TableName())),
  47. zap.String("fields", utils.MarshalJsonString(p)),
  48. zap.String("error", err.Error()))
  49. return errors.DataBaseError
  50. }
  51. return nil
  52. }