Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Centurybbx authored Aug 30, 2023
2 parents 8032a27 + ba66a9e commit f17049b
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 72 deletions.
2 changes: 1 addition & 1 deletion cmd/api/handler/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func PublishAction(ctx context.Context, c *app.RequestContext) {
return
}

videoFile, err := c.FormFile("data")
videoFile, err := c.Request.FormFile("data")
if err != nil {
logger.Errorf("Failed to get video file from request. %v", err)
ResponseError(c, http.StatusInternalServerError, response.PackPublishActionError("获取视频文件失败"))
Expand Down
119 changes: 98 additions & 21 deletions cmd/relation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"context"
"github.com/Tiktok-Lite/kotkit/internal/db"
"github.com/Tiktok-Lite/kotkit/kitex_gen/relation"
"github.com/Tiktok-Lite/kotkit/kitex_gen/user"
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant"
"github.com/Tiktok-Lite/kotkit/pkg/helper/converter"
"github.com/Tiktok-Lite/kotkit/pkg/log"
"github.com/pkg/errors"
"gorm.io/gorm"
)

// RelationServiceImpl implements the last service interface defined in the IDL.
type RelationServiceImpl struct{}

// RelationAction implements the RelationServiceImpl interface.
func (s *RelationServiceImpl) RelationAction(ctx context.Context, req *relation.RelationActionRequest) (resp *relation.RelationActionResponse, err error) {
// TODO: Your code here...
logger := log.Logger()
claims, err := Jwt.ParseToken(req.Token)
if err != nil {
Expand All @@ -41,15 +43,15 @@ func (s *RelationServiceImpl) RelationAction(ctx context.Context, req *relation.
return nil, err
}

if req.ActionType == 1 {
if req.ActionType == constant.FollowUserCode {
res := &relation.RelationActionResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "关注成功",
}
return res, nil
}

if req.ActionType == 2 {
if req.ActionType == constant.UnFollowUserCode {
res := &relation.RelationActionResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "取关成功",
Expand Down Expand Up @@ -94,22 +96,46 @@ func (s *RelationServiceImpl) RelationFollowList(ctx context.Context, req *relat
}
return res, nil
}
userListProto, err := converter.ConvertFollowingListModelToProto(followings)
if err != nil {
logger.Errorf(err.Error())

if followings == nil {
logger.Errorf("关注列表为空")
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取关注列表失败",
StatusCode: constant.StatusOKCode,
StatusMsg: "关注列表为空",
UserList: nil,
}
return res, nil
}

userProtoList := make([]*user.User, 0)
for _, following := range followings {
user, err := db.QueryUserByID(int64(following.UserID))
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
logger.Errorf(err.Error())
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "关注列表获取失败",
}
return res, nil
}
userProto, err := converter.ConvertUserModelToProto(user)
if err != nil {
logger.Errorf(err.Error())
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取关注列表失败",
UserList: nil,
}
return res, nil
}
userProtoList = append(userProtoList, userProto)
}

// 返回结果
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "success",
UserList: userListProto,
UserList: userProtoList,
}
return res, nil
}
Expand Down Expand Up @@ -147,22 +173,45 @@ func (s *RelationServiceImpl) RelationFollowerList(ctx context.Context, req *rel
}
return res, nil
}
userListProto, err := converter.ConvertFollowerListModelToProto(followers)
if err != nil {
logger.Errorf(err.Error())
if followers == nil {
logger.Errorf("粉丝列表为空")
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取粉丝列表失败",
StatusCode: constant.StatusOKCode,
StatusMsg: "粉丝列表为空",
UserList: nil,
}
return res, nil
}

userProtoList := make([]*user.User, 0)
for _, follower := range followers {
user, err := db.QueryUserByID(int64(follower.FollowerID))
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
logger.Errorf(err.Error())
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "粉丝列表获取失败",
}
return res, nil
}
userProto, err := converter.ConvertUserModelToProto(user)
if err != nil {
logger.Errorf(err.Error())
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取粉丝列表失败",
UserList: nil,
}
return res, nil
}
userProtoList = append(userProtoList, userProto)
}

// 返回结果
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "success",
UserList: userListProto,
UserList: userProtoList,
}
return res, nil
}
Expand Down Expand Up @@ -202,22 +251,50 @@ func (s *RelationServiceImpl) RelationFriendList(ctx context.Context, req *relat
}
return res, nil
}
userListProto, err := converter.ConvertFollowerListModelToProto(friends)
if err != nil {
logger.Errorf(err.Error())

if friends == nil {
logger.Errorf("好友列表为空")
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取朋友列表失败",
StatusCode: constant.StatusOKCode,
StatusMsg: "好友列表为空",
UserList: nil,
}
return res, nil
}

userProtoList := make([]*user.User, 0)
for _, friend := range friends {
user, err := db.QueryUserByID(int64(friend.FollowerID))
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
logger.Errorf(err.Error())
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "好友列表获取失败",
}
return res, nil
}
userProto, err := converter.ConvertUserModelToProto(user)
if err != nil {
logger.Errorf(err.Error())
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取好友列表失败",
UserList: nil,
}
return res, nil
}
userProtoList = append(userProtoList, userProto)
}

for _, friend := range userProtoList {
friend.IsFollow = true
}

// 返回结果
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "success",
UserList: userListProto,
UserList: userProtoList,
}
return res, nil
}
36 changes: 23 additions & 13 deletions cmd/video/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ func (s *VideoServiceImpl) PublishAction(ctx context.Context, req *video.Publish
return res, nil
}

// 默认文件不得大于50MB
if len(req.Data) > 50*1024*1024 {
logger.Errorf("Video file is too large %v.", len(req.Data))
res := &video.PublicActionResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "视频文件太大",
}
return res, nil
}

userID := claims.Id
videoTitle, coverTitle := fmt.Sprintf("%d_%s_%d.mp4", userID, req.Title, time.Now().Unix()), fmt.Sprintf("%d_%s_%d.jpg", userID, req.Title, time.Now().Unix())

Expand All @@ -242,19 +252,7 @@ func (s *VideoServiceImpl) PublishAction(ctx context.Context, req *video.Publish
Title: req.Title,
}

err = oss.PublishVideo(req.Data, videoTitle, coverTitle)
if err != nil {
logger.Errorf("Error occurs when publishing video. %v", err)
res := &video.PublicActionResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "上传视频到minio失败",
}
return res, nil
}

// TODO: 在上传数据库和OSS中间加入事务
// 注意:先把东西上传到oss后写入数据库,目的是防止上传失败后数据库中有记录但是oss中没有
// 保证数据的原子性
// 注意保证数据的原子性
err = db.CreateVideo(video_)
if err != nil {
logger.Errorf("Error occurs when creating video to database. %v", err)
Expand All @@ -265,6 +263,18 @@ func (s *VideoServiceImpl) PublishAction(ctx context.Context, req *video.Publish
return res, nil
}

go func() {
err = oss.PublishVideo(req.Data, videoTitle, coverTitle)
if err != nil {
logger.Errorf("Error occurs when publishing video to minio. %v", err)
// minio发布失败则删除数据库中的记录
e := db.DeleteVideoById(video_.ID, uint(userID))
if e != nil {
logger.Errorf("Error occurs when deleting video and records. %v", e)
}
}
}()

res := &video.PublicActionResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "创建视频成功",
Expand Down
2 changes: 0 additions & 2 deletions internal/db/relation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
"fmt"
"github.com/Tiktok-Lite/kotkit/internal/model"
"github.com/Tiktok-Lite/kotkit/kitex_gen/relation"
"github.com/pkg/errors"
Expand Down Expand Up @@ -71,7 +70,6 @@ func DelRelation(userID uint, toUserID uint) error {

func GetFollowerListByUserID(UserID uint) ([]*FollowRelation, error) {
var RelationList []*FollowRelation
fmt.Println(UserID)
if err := DB().Where("user_id = ?", UserID).Find(&RelationList).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
Expand Down
44 changes: 43 additions & 1 deletion internal/db/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,28 @@ func QueryVideoListByUserID(userID int64) ([]*model.Video, error) {
}

func CreateVideo(video *model.Video) error {
if err := DB().Debug().Create(video).Error; err != nil {
tx := DB().Begin()

if err := tx.Debug().Create(video).Error; err != nil {
tx.Rollback()
return err
}

if err := AddWorkCountById(video.UserID, tx); err != nil {
return err
}

tx.Commit()
return nil
}

func AddWorkCountById(uid uint, tx *gorm.DB) error {
var user model.User
if err := tx.Debug().Model(&user).Where("id = ?", uid).UpdateColumn("work_count", gorm.Expr("work_count + ?", 1)).Error; err != nil {
tx.Rollback()
return err
}
return nil
}

func QueryVideoLikeRelation(vid, uid int64) (bool, error) {
Expand Down Expand Up @@ -105,3 +122,28 @@ func QueryVideosByVideoIds(videoIds []int64) ([]*model.Video, error) {
}
return videos, nil
}

func DeleteVideoById(vid, uid uint) error {
tx := DB().Begin()

if err := tx.Debug().Where("id = ?", vid).Delete(&model.Video{}).Error; err != nil {
tx.Rollback()
return err
}
if err := DeleteWorkCountById(uid, tx); err != nil {
return err
}

tx.Commit()
return nil
}

func DeleteWorkCountById(uid uint, tx *gorm.DB) error {
var user model.User
if err := tx.Debug().Model(&user).Where("id = ?", uid).UpdateColumn("work_count", gorm.Expr("work_count - ?", 1)).Error; err != nil {
tx.Rollback()
return err
}

return nil
}
5 changes: 5 additions & 0 deletions pkg/helper/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ const (
FavoriteCode = 1
UnFavoriteCode = 2
)

const (
PostCommentCode = 1
DeleteCommentCode = 2
)

const (
PostMessageCode = 1

const (

Check failure on line 36 in pkg/helper/constant/const.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected const, expected name
FollowUserCode = 1

Check failure on line 37 in pkg/helper/constant/const.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected =, expected )
UnFollowUserCode = 2
)
Loading

0 comments on commit f17049b

Please sign in to comment.