userResource.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package v2
  2. import (
  3. "context"
  4. v2pb "go-common/app/admin/live/live-admin/api/http/v2"
  5. "go-common/app/admin/live/live-admin/conf"
  6. v2rspb "go-common/app/service/live/resource/api/grpc/v2"
  7. "go-common/library/log"
  8. )
  9. // UserResourceService struct
  10. type UserResourceService struct {
  11. conf *conf.Config
  12. // optionally add other properties here, such as dao
  13. // dao *dao.Dao
  14. v2rsCli *v2rspb.Client
  15. }
  16. //NewUserResourceService init
  17. func NewUserResourceService(c *conf.Config) (s *UserResourceService) {
  18. s = &UserResourceService{
  19. conf: c,
  20. }
  21. var svc *v2rspb.Client
  22. var err error
  23. log.Info("ResourceServiceV2 Init: %+v", s.conf.ResourceClientV2)
  24. if svc, err = v2rspb.NewClient(s.conf.ResourceClientV2); err != nil {
  25. panic(err)
  26. }
  27. s.v2rsCli = svc
  28. return s
  29. }
  30. // Add implementation
  31. // Add 添加资源接口
  32. // `method:"POST" internal:"true" `
  33. func (s *UserResourceService) Add(ctx context.Context, req *v2pb.UserResourceAddReq) (resp *v2pb.UserResourceAddResp, err error) {
  34. respRPC, err := s.v2rsCli.Add(ctx, &v2rspb.AddReq{
  35. ResType: req.ResType,
  36. Title: req.Title,
  37. Url: req.Url,
  38. Weight: req.Weight,
  39. Creator: req.Creator,
  40. })
  41. if err == nil {
  42. resp = &v2pb.UserResourceAddResp{
  43. Id: respRPC.Id,
  44. CustomId: respRPC.CustomId,
  45. }
  46. }
  47. return
  48. }
  49. // Edit implementation
  50. // Edit 编辑现有资源
  51. // `method:"POST" internal:"true" `
  52. func (s *UserResourceService) Edit(ctx context.Context, req *v2pb.UserResourceEditReq) (resp *v2pb.UserResourceEditResp, err error) {
  53. resp = &v2pb.UserResourceEditResp{}
  54. _, err = s.v2rsCli.Edit(ctx, &v2rspb.EditReq{
  55. ResType: req.ResType,
  56. Title: req.Title,
  57. Url: req.Url,
  58. Weight: req.Weight,
  59. CustomId: req.CustomId,
  60. })
  61. return
  62. }
  63. // Get implementation
  64. // Get 获取资源列表
  65. // `method:"GET" internal:"true" `
  66. func (s *UserResourceService) Get(ctx context.Context, req *v2pb.UserResourceListReq) (resp *v2pb.UserResourceListResp, err error) {
  67. respRPC, err := s.v2rsCli.List(ctx, &v2rspb.ListReq{
  68. ResType: req.ResType,
  69. Page: req.Page,
  70. PageSize: req.PageSize,
  71. })
  72. if err == nil {
  73. resp = &v2pb.UserResourceListResp{
  74. CurrentPage: respRPC.CurrentPage,
  75. TotalCount: respRPC.TotalCount,
  76. List: convertRPCListRes(respRPC.List),
  77. }
  78. }
  79. return
  80. }
  81. // SetStatus implementation
  82. // SetStatus 更改资源状态
  83. // `method:"POST" internal:"true" `
  84. func (s *UserResourceService) SetStatus(ctx context.Context, req *v2pb.UserResourceSetStatusReq) (resp *v2pb.UserResourceSetStatusResp, err error) {
  85. resp = &v2pb.UserResourceSetStatusResp{}
  86. _, err = s.v2rsCli.SetStatus(ctx, &v2rspb.SetStatusReq{
  87. ResType: req.ResType,
  88. CustomId: req.CustomId,
  89. Status: req.Status,
  90. })
  91. return
  92. }
  93. // GetSingle implementation
  94. // Query 请求单个资源
  95. func (s *UserResourceService) GetSingle(ctx context.Context, req *v2pb.UserResourceGetSingleReq) (resp *v2pb.UserResourceGetSingleResp, err error) {
  96. respRPC, err := s.v2rsCli.Query(ctx, &v2rspb.QueryReq{
  97. CustomId: req.CustomId,
  98. ResType: req.ResType,
  99. })
  100. if err == nil {
  101. resp = &v2pb.UserResourceGetSingleResp{
  102. Id: respRPC.Id,
  103. ResType: respRPC.ResType,
  104. CustomId: respRPC.CustomId,
  105. Title: respRPC.Title,
  106. Url: respRPC.Url,
  107. Weight: respRPC.Weight,
  108. Creator: respRPC.Creator,
  109. Status: respRPC.Status,
  110. Ctime: respRPC.Ctime,
  111. Mtime: respRPC.Mtime,
  112. }
  113. }
  114. return
  115. }
  116. func convertRPCListRes(RPCList []*v2rspb.ListResp_List) (HTTPList []*v2pb.UserResourceListResp_List) {
  117. HTTPList = make([]*v2pb.UserResourceListResp_List, len(RPCList))
  118. for index, RPCListItem := range RPCList {
  119. HTTPList[index] = &v2pb.UserResourceListResp_List{
  120. Id: RPCListItem.Id,
  121. ResType: RPCListItem.ResType,
  122. CustomId: RPCListItem.CustomId,
  123. Title: RPCListItem.Title,
  124. Url: RPCListItem.Url,
  125. Weight: RPCListItem.Weight,
  126. Creator: RPCListItem.Creator,
  127. Status: RPCListItem.Status,
  128. Ctime: RPCListItem.Ctime,
  129. Mtime: RPCListItem.Mtime,
  130. }
  131. }
  132. return
  133. }