Skip to content

Commit

Permalink
fix: fix publish merge coding lose
Browse files Browse the repository at this point in the history
  • Loading branch information
Attack825 committed Aug 21, 2023
1 parent 1cb2714 commit f4d2f28
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/services/publish/handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"GuGoTik/src/constant/config"
"GuGoTik/src/constant/strings"
"GuGoTik/src/extra/tracing"
"GuGoTik/src/models"
"GuGoTik/src/rpc/feed"
"GuGoTik/src/rpc/publish"
"GuGoTik/src/storage/database"
"GuGoTik/src/storage/file"
grpc2 "GuGoTik/src/utils/grpc"
"GuGoTik/src/utils/logging"
"GuGoTik/src/utils/pathgen"
"GuGoTik/src/utils/rabbitmq"
Expand All @@ -27,13 +31,17 @@ var conn *amqp.Connection

var channel *amqp.Channel

var FeedClient feed.FeedServiceClient

func exitOnError(err error) {
if err != nil {
panic(err)
}
}

func init() {
FeedRpcConn := grpc2.Connect(config.FeedRpcServerName)
FeedClient = feed.NewFeedServiceClient(FeedRpcConn)
var err error

conn, err = amqp.Dial(rabbitmq.BuildMQConnAddr())
Expand Down Expand Up @@ -92,6 +100,85 @@ func init() {
exitOnError(err)
}

func (a PublishServiceImpl) ListVideo(ctx context.Context, req *publish.ListVideoRequest) (resp *publish.ListVideoResponse, err error) {
ctx, span := tracing.Tracer.Start(ctx, "ListVideoService")
defer span.End()
logger := logging.LogService("PublishServiceImpl.ListVideo").WithContext(ctx)

var videos []models.Video
err = database.Client.WithContext(ctx).
Where("user_id = ?", req.UserId).
Order("created_at DESC").
Find(&videos).Error
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("failed to query video")
logging.SetSpanError(span, err)
resp = &publish.ListVideoResponse{
StatusCode: strings.PublishServiceInnerErrorCode,
StatusMsg: strings.PublishServiceInnerError,
}
return
}
videoIds := make([]uint32, 0, len(videos))
for _, video := range videos {
videoIds = append(videoIds, video.ID)
}

queryVideoResp, err := FeedClient.QueryVideos(ctx, &feed.QueryVideosRequest{
ActorId: req.ActorId,
VideoIds: videoIds,
})
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("queryVideoResp failed to obtain")
logging.SetSpanError(span, err)
resp = &publish.ListVideoResponse{
StatusCode: strings.FeedServiceInnerErrorCode,
StatusMsg: strings.FeedServiceInnerError,
}
return
}

logger.WithFields(logrus.Fields{
"response": resp,
}).Debug("all process done, ready to launch response")
resp = &publish.ListVideoResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
VideoList: queryVideoResp.VideoList,
}
return
}

func (a PublishServiceImpl) CountVideo(ctx context.Context, req *publish.CountVideoRequest) (resp *publish.CountVideoResponse, err error) {
ctx, span := tracing.Tracer.Start(ctx, "CountVideoService")
defer span.End()
logger := logging.LogService("PublishServiceImpl.CountVideo").WithContext(ctx)
var count int64
err = database.Client.WithContext(ctx).Where("user_id = ?", req.UserId).Count(&count).Error
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("failed to count video")
resp = &publish.CountVideoResponse{
StatusCode: strings.PublishServiceInnerErrorCode,
StatusMsg: strings.PublishServiceInnerError,
}
logging.SetSpanError(span, err)
return
}

resp = &publish.CountVideoResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
Count: uint32(count),
}
return
}

func CloseMQConn() {
if err := conn.Close(); err != nil {
panic(err)
Expand Down

0 comments on commit f4d2f28

Please sign in to comment.