server.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package rpc
  2. import (
  3. "go-common/app/service/main/favorite/conf"
  4. "go-common/app/service/main/favorite/model"
  5. "go-common/app/service/main/favorite/service"
  6. "go-common/library/net/rpc"
  7. "go-common/library/net/rpc/context"
  8. )
  9. // RPC favorite rpc.
  10. type RPC struct {
  11. c *conf.Config
  12. s *service.Service
  13. }
  14. // New init rpc.
  15. func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
  16. r := &RPC{
  17. c: c,
  18. s: s,
  19. }
  20. svr = rpc.NewServer(c.RPCServer)
  21. if err := svr.Register(r); err != nil {
  22. panic(err)
  23. }
  24. return
  25. }
  26. // Ping check connection success.
  27. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  28. return
  29. }
  30. // Folder return folder by mid.
  31. func (r *RPC) Folder(c context.Context, a *model.ArgFolder, res *model.Folder) (err error) {
  32. var fl *model.Folder
  33. if fl, err = r.s.Folder(c, a.Type, a.Mid, a.Vmid, a.Fid); err == nil {
  34. *res = *fl
  35. }
  36. return
  37. }
  38. // Folders return folder by mid.
  39. func (r *RPC) Folders(c context.Context, a *model.ArgFolders, res *[]*model.Folder) (err error) {
  40. *res, err = r.s.Folders(c, a.Type, a.Mid, a.FVmids)
  41. return
  42. }
  43. // AllFolders return users folders.
  44. func (r *RPC) AllFolders(c context.Context, a *model.ArgAllFolders, res *[]*model.Folder) (err error) {
  45. *res, err = r.s.UserFolders(c, a.Type, a.Mid, a.Vmid, a.Oid, a.Type)
  46. return
  47. }
  48. // AddFolder add a Folder.
  49. func (r *RPC) AddFolder(c context.Context, a *model.ArgAddFolder, res *int64) (err error) {
  50. var fid int64
  51. if fid, err = r.s.AddFolder(c, a.Type, a.Mid, a.Name, a.Description, a.Cover, int32(a.Public), a.Cookie, a.AccessKey); err == nil {
  52. *res = fid
  53. }
  54. return
  55. }
  56. // UpdateFolder update a Folder.
  57. func (r *RPC) UpdateFolder(c context.Context, a *model.ArgUpdateFolder, res *struct{}) (err error) {
  58. err = r.s.UpdateFolder(c, a.Type, a.Fid, a.Mid, a.Name, a.Description, a.Cover, int32(a.Public), nil, nil)
  59. return
  60. }
  61. // DelFolder del a folder.
  62. func (r *RPC) DelFolder(c context.Context, a *model.ArgDelFolder, res *struct{}) (err error) {
  63. err = r.s.DelFolder(c, a.Type, a.Mid, a.Fid)
  64. return
  65. }
  66. // Favorites return favorites by mid.
  67. func (r *RPC) Favorites(c context.Context, a *model.ArgFavs, res *model.Favorites) (err error) {
  68. var fs *model.Favorites
  69. if fs, err = r.s.Favorites(c, a.Type, a.Mid, a.Vmid, a.Fid, a.Tid, a.Tv, a.Pn, a.Ps, a.Keyword, a.Order); err == nil {
  70. *res = *fs
  71. }
  72. return
  73. }
  74. // Add add a favorite relation.
  75. func (r *RPC) Add(c context.Context, a *model.ArgAdd, res *struct{}) (err error) {
  76. err = r.s.AddFav(c, a.Type, a.Mid, a.Fid, a.Oid, a.Type)
  77. return
  78. }
  79. // Del del a favorite relation.
  80. func (r *RPC) Del(c context.Context, a *model.ArgDel, res *struct{}) (err error) {
  81. err = r.s.DelFav(c, a.Type, a.Mid, a.Fid, a.Oid, a.Type)
  82. return
  83. }
  84. // Adds add a resource to folders.
  85. func (r *RPC) Adds(c context.Context, a *model.ArgAdds, res *struct{}) (err error) {
  86. for _, fid := range a.Fids {
  87. err = r.s.AddFav(c, a.Type, a.Mid, fid, a.Oid, a.Type)
  88. }
  89. return
  90. }
  91. // Dels del a resource in fodlers.
  92. func (r *RPC) Dels(c context.Context, a *model.ArgDels, res *struct{}) (err error) {
  93. for _, fid := range a.Fids {
  94. err = r.s.DelFav(c, a.Type, a.Mid, fid, a.Oid, a.Type)
  95. }
  96. return
  97. }
  98. // MultiAdd multi add favorite relations.
  99. func (r *RPC) MultiAdd(c context.Context, a *model.ArgMultiAdd, res *struct{}) (err error) {
  100. err = r.s.MultiAddFavs(c, a.Type, a.Mid, a.Fid, a.Oids)
  101. return
  102. }
  103. // MultiDel multi del favorite relations.
  104. func (r *RPC) MultiDel(c context.Context, a *model.ArgMultiDel, res *struct{}) (err error) {
  105. err = r.s.MultiDelFavs(c, a.Type, a.Mid, a.Fid, a.Oids)
  106. return
  107. }
  108. // IsFav check favorited relation.
  109. func (r *RPC) IsFav(c context.Context, a *model.ArgIsFav, faved *bool) (err error) {
  110. *faved, err = r.s.IsFavored(c, a.Type, a.Mid, a.Oid)
  111. return
  112. }
  113. // IsFavs return favored relation map.
  114. func (r *RPC) IsFavs(c context.Context, a *model.ArgIsFavs, res *map[int64]bool) (err error) {
  115. *res, err = r.s.IsFavoreds(c, a.Type, a.Mid, a.Oids)
  116. return
  117. }
  118. // InDefault return favored in default folder.
  119. func (r *RPC) InDefault(c context.Context, a *model.ArgInDefaultFolder, in *bool) (err error) {
  120. *in, err = r.s.InDefaultFolder(c, a.Type, a.Mid, a.Oid)
  121. return
  122. }
  123. // IsFavedByFid check the oid and fid relation.
  124. func (r *RPC) IsFavedByFid(c context.Context, a *model.ArgIsFavedByFid, faved *bool) (err error) {
  125. *faved, err = r.s.IsFavedByFid(c, a.Type, a.Mid, a.Oid, a.Fid)
  126. return
  127. }
  128. // CntUserFolders count user's folders.
  129. func (r *RPC) CntUserFolders(c context.Context, a *model.ArgCntUserFolders, count *int) (err error) {
  130. *count, err = r.s.CntUserFolders(c, a.Type, a.Mid, a.Vmid)
  131. return
  132. }
  133. // Users return favored users by mid.
  134. func (r *RPC) Users(c context.Context, a *model.ArgUsers, res *model.UserList) (err error) {
  135. var us *model.UserList
  136. if us, err = r.s.UserList(c, a.Type, a.Oid, a.Pn, a.Ps); err == nil {
  137. *res = *us
  138. }
  139. return
  140. }
  141. // under v2 ===
  142. // AddVideo add a favorite video.
  143. func (r *RPC) AddVideo(c context.Context, a *model.ArgAddVideo, res *struct{}) (err error) {
  144. for _, fid := range a.Fids {
  145. err = r.s.AddFav(c, model.TypeVideo, a.Mid, fid, a.Aid, model.TypeVideo)
  146. }
  147. return
  148. }