handler_resource.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "go-common/app/job/main/aegis/model"
  8. )
  9. //ERROR
  10. var (
  11. ErrMangaNoIndex = errors.New("漫画无图")
  12. )
  13. type baseResourceAddHandler struct {
  14. *Service
  15. }
  16. type mangaResourceAddHandler struct {
  17. baseResourceAddHandler
  18. }
  19. type baseResourceUpdateHandler struct {
  20. *Service
  21. }
  22. type baseResourceCancelHandler struct {
  23. *Service
  24. }
  25. func (h baseResourceAddHandler) CheckMessage(msg json.RawMessage) (addObj interface{}, err error) {
  26. return h.checkRscAddMsg(msg)
  27. }
  28. func (h baseResourceAddHandler) HandleMessage(c context.Context, addObj interface{}) error {
  29. return h.writeRscAdd(c, addObj.(*model.AddOption))
  30. }
  31. //漫画的,校验是否有图
  32. func (h mangaResourceAddHandler) CheckMessage(msg json.RawMessage) (addObj interface{}, err error) {
  33. if addObj, err = h.baseResourceAddHandler.CheckMessage(msg); err != nil {
  34. return
  35. }
  36. addopt := addObj.(*model.AddOption)
  37. metas := make(map[string]interface{})
  38. if err = json.Unmarshal([]byte(addopt.MetaData), &metas); err != nil {
  39. return
  40. }
  41. if index, ok := metas["index"]; !ok || len(fmt.Sprint(index)) == 0 {
  42. return nil, ErrMangaNoIndex
  43. }
  44. return
  45. }
  46. func (h mangaResourceAddHandler) HandleMessage(c context.Context, addObj interface{}) error {
  47. return h.baseResourceAddHandler.HandleMessage(c, addObj.(*model.AddOption))
  48. }
  49. func (h baseResourceUpdateHandler) CheckMessage(msg json.RawMessage) (updateObj interface{}, err error) {
  50. return h.checkRscUpdateMsg(msg)
  51. }
  52. func (h baseResourceUpdateHandler) HandleMessage(c context.Context, updateObj interface{}) error {
  53. return h.writeRscUpdate(c, updateObj.(*model.UpdateOption))
  54. }
  55. func (h baseResourceCancelHandler) CheckMessage(msg json.RawMessage) (cancelObj interface{}, err error) {
  56. return h.checkRscCancelMsg(msg)
  57. }
  58. func (h baseResourceCancelHandler) HandleMessage(c context.Context, cancelObj interface{}) error {
  59. return h.writeRscCancel(c, cancelObj.(*model.CancelOption))
  60. }