fix(P1): Router Bug 修复 + Video 复合索引 + ListByAuthorID 加 LIMIT
This commit is contained in:
@@ -1,152 +1,152 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"feedsystem_video_go/internal/account"
|
"feedsystem_video_go/internal/account"
|
||||||
"feedsystem_video_go/internal/feed"
|
"feedsystem_video_go/internal/feed"
|
||||||
"feedsystem_video_go/internal/middleware/jwt"
|
"feedsystem_video_go/internal/middleware/jwt"
|
||||||
"feedsystem_video_go/internal/middleware/ratelimit"
|
"feedsystem_video_go/internal/middleware/ratelimit"
|
||||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||||
"feedsystem_video_go/internal/social"
|
"feedsystem_video_go/internal/social"
|
||||||
"feedsystem_video_go/internal/video"
|
"feedsystem_video_go/internal/video"
|
||||||
"feedsystem_video_go/internal/worker"
|
"feedsystem_video_go/internal/worker"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine {
|
func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
if err := r.SetTrustedProxies(nil); err != nil {
|
if err := r.SetTrustedProxies(nil); err != nil {
|
||||||
log.Printf("SetTrustedProxies failed: %v", err)
|
log.Printf("SetTrustedProxies failed: %v", err)
|
||||||
}
|
}
|
||||||
r.Static("/static", "./.run/uploads")
|
r.Static("/static", "./.run/uploads")
|
||||||
// rate_limit
|
// rate_limit
|
||||||
loginLimiter := ratelimit.Limit(cache, "account_login", 10, time.Minute, ratelimit.KeyByIP)
|
loginLimiter := ratelimit.Limit(cache, "account_login", 10, time.Minute, ratelimit.KeyByIP)
|
||||||
registerLimiter := ratelimit.Limit(cache, "account_register", 5, time.Hour, ratelimit.KeyByIP)
|
registerLimiter := ratelimit.Limit(cache, "account_register", 5, time.Hour, ratelimit.KeyByIP)
|
||||||
|
|
||||||
likeLimiter := ratelimit.Limit(cache, "like_write", 30, time.Minute, ratelimit.KeyByAccount)
|
likeLimiter := ratelimit.Limit(cache, "like_write", 30, time.Minute, ratelimit.KeyByAccount)
|
||||||
commentLimiter := ratelimit.Limit(cache, "comment_write", 10, time.Minute, ratelimit.KeyByAccount)
|
commentLimiter := ratelimit.Limit(cache, "comment_write", 10, time.Minute, ratelimit.KeyByAccount)
|
||||||
socialLimiter := ratelimit.Limit(cache, "social_write", 20, time.Minute, ratelimit.KeyByAccount)
|
socialLimiter := ratelimit.Limit(cache, "social_write", 20, time.Minute, ratelimit.KeyByAccount)
|
||||||
|
|
||||||
// account
|
// account
|
||||||
accountRepository := account.NewAccountRepository(db)
|
accountRepository := account.NewAccountRepository(db)
|
||||||
accountService := account.NewAccountService(accountRepository, cache)
|
accountService := account.NewAccountService(accountRepository, cache)
|
||||||
accountHandler := account.NewAccountHandler(accountService)
|
accountHandler := account.NewAccountHandler(accountService)
|
||||||
accountGroup := r.Group("/account")
|
accountGroup := r.Group("/account")
|
||||||
{
|
{
|
||||||
accountGroup.POST("/register", registerLimiter, accountHandler.CreateAccount)
|
accountGroup.POST("/register", registerLimiter, accountHandler.CreateAccount)
|
||||||
accountGroup.POST("/login", loginLimiter, accountHandler.Login)
|
accountGroup.POST("/login", loginLimiter, accountHandler.Login)
|
||||||
accountGroup.POST("/changePassword", accountHandler.ChangePassword)
|
accountGroup.POST("/changePassword", accountHandler.ChangePassword)
|
||||||
accountGroup.POST("/findByID", accountHandler.FindByID)
|
accountGroup.POST("/findByID", accountHandler.FindByID)
|
||||||
accountGroup.POST("/findByUsername", accountHandler.FindByUsername)
|
accountGroup.POST("/findByUsername", accountHandler.FindByUsername)
|
||||||
}
|
}
|
||||||
protectedAccountGroup := accountGroup.Group("")
|
protectedAccountGroup := accountGroup.Group("")
|
||||||
protectedAccountGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
protectedAccountGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
protectedAccountGroup.POST("/logout", accountHandler.Logout)
|
protectedAccountGroup.POST("/logout", accountHandler.Logout)
|
||||||
protectedAccountGroup.POST("/rename", accountHandler.Rename)
|
protectedAccountGroup.POST("/rename", accountHandler.Rename)
|
||||||
}
|
}
|
||||||
// video
|
// video
|
||||||
videoRepository := video.NewVideoRepository(db)
|
videoRepository := video.NewVideoRepository(db)
|
||||||
popularityMQ, err := rabbitmq.NewPopularityMQ(rmq)
|
popularityMQ, err := rabbitmq.NewPopularityMQ(rmq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("PopularityMQ init failed (mq disabled): %v", err)
|
log.Printf("PopularityMQ init failed (mq disabled): %v", err)
|
||||||
popularityMQ = nil
|
popularityMQ = nil
|
||||||
}
|
}
|
||||||
videoService := video.NewVideoService(videoRepository, cache, popularityMQ)
|
videoService := video.NewVideoService(videoRepository, cache, popularityMQ)
|
||||||
videoHandler := video.NewVideoHandler(videoService, accountService)
|
videoHandler := video.NewVideoHandler(videoService, accountService)
|
||||||
videoGroup := r.Group("/video")
|
videoGroup := r.Group("/video")
|
||||||
{
|
{
|
||||||
videoGroup.POST("/listByAuthorID", videoHandler.ListByAuthorID)
|
videoGroup.POST("/listByAuthorID", videoHandler.ListByAuthorID)
|
||||||
videoGroup.POST("/getDetail", videoHandler.GetDetail)
|
videoGroup.POST("/getDetail", videoHandler.GetDetail)
|
||||||
}
|
}
|
||||||
protectedVideoGroup := videoGroup.Group("")
|
protectedVideoGroup := videoGroup.Group("")
|
||||||
protectedVideoGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
protectedVideoGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo)
|
protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo)
|
||||||
protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover)
|
protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover)
|
||||||
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
|
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
|
||||||
}
|
}
|
||||||
// like
|
// like
|
||||||
likeMQ, err := rabbitmq.NewLikeMQ(rmq)
|
likeMQ, err := rabbitmq.NewLikeMQ(rmq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("LikeMQ init failed (mq disabled): %v", err)
|
log.Printf("LikeMQ init failed (mq disabled): %v", err)
|
||||||
likeMQ = nil
|
likeMQ = nil
|
||||||
}
|
}
|
||||||
likeRepository := video.NewLikeRepository(db)
|
likeRepository := video.NewLikeRepository(db)
|
||||||
likeService := video.NewLikeService(likeRepository, videoRepository, cache, likeMQ, popularityMQ)
|
likeService := video.NewLikeService(likeRepository, videoRepository, cache, likeMQ, popularityMQ)
|
||||||
likeHandler := video.NewLikeHandler(likeService)
|
likeHandler := video.NewLikeHandler(likeService)
|
||||||
likeGroup := r.Group("/like")
|
likeGroup := r.Group("/like")
|
||||||
protectedLikeGroup := likeGroup.Group("")
|
protectedLikeGroup := likeGroup.Group("")
|
||||||
protectedLikeGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
protectedLikeGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
protectedLikeGroup.POST("/like", likeLimiter, likeHandler.Like)
|
protectedLikeGroup.POST("/like", likeLimiter, likeHandler.Like)
|
||||||
protectedLikeGroup.POST("/unlike", likeLimiter, likeHandler.Unlike)
|
protectedLikeGroup.POST("/unlike", likeLimiter, likeHandler.Unlike)
|
||||||
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
|
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
|
||||||
protectedLikeGroup.POST("/listMyLikedVideos", likeHandler.ListMyLikedVideos)
|
protectedLikeGroup.POST("/listMyLikedVideos", likeHandler.ListMyLikedVideos)
|
||||||
}
|
}
|
||||||
// comment
|
// comment
|
||||||
commentRepository := video.NewCommentRepository(db)
|
commentRepository := video.NewCommentRepository(db)
|
||||||
commentMQ, err := rabbitmq.NewCommentMQ(rmq)
|
commentMQ, err := rabbitmq.NewCommentMQ(rmq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("CommentMQ init failed (mq disabled): %v", err)
|
log.Printf("CommentMQ init failed (mq disabled): %v", err)
|
||||||
commentMQ = nil
|
commentMQ = nil
|
||||||
}
|
}
|
||||||
commentService := video.NewCommentService(commentRepository, videoRepository, cache, commentMQ, popularityMQ)
|
commentService := video.NewCommentService(commentRepository, videoRepository, cache, commentMQ, popularityMQ)
|
||||||
commentHandler := video.NewCommentHandler(commentService, accountService)
|
commentHandler := video.NewCommentHandler(commentService, accountService)
|
||||||
commentGroup := r.Group("/comment")
|
commentGroup := r.Group("/comment")
|
||||||
{
|
{
|
||||||
commentGroup.POST("/listAll", commentHandler.GetAllComments)
|
commentGroup.POST("/listAll", commentHandler.GetAllComments)
|
||||||
}
|
}
|
||||||
protectedCommentGroup := commentGroup.Group("")
|
protectedCommentGroup := commentGroup.Group("")
|
||||||
protectedCommentGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
protectedCommentGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
protectedCommentGroup.POST("/publish", commentLimiter, commentHandler.PublishComment)
|
protectedCommentGroup.POST("/publish", commentLimiter, commentHandler.PublishComment)
|
||||||
protectedCommentGroup.POST("/delete", commentLimiter, commentHandler.DeleteComment)
|
protectedCommentGroup.POST("/delete", commentLimiter, commentHandler.DeleteComment)
|
||||||
}
|
}
|
||||||
// social
|
// social
|
||||||
socialMQ, err := rabbitmq.NewSocialMQ(rmq)
|
socialMQ, err := rabbitmq.NewSocialMQ(rmq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("SocialMQ init failed (mq disabled): %v", err)
|
log.Printf("SocialMQ init failed (mq disabled): %v", err)
|
||||||
socialMQ = nil
|
socialMQ = nil
|
||||||
}
|
}
|
||||||
socialRepository := social.NewSocialRepository(db)
|
socialRepository := social.NewSocialRepository(db)
|
||||||
socialService := social.NewSocialService(socialRepository, accountRepository, socialMQ)
|
socialService := social.NewSocialService(socialRepository, accountRepository, socialMQ)
|
||||||
socialHandler := social.NewSocialHandler(socialService)
|
socialHandler := social.NewSocialHandler(socialService)
|
||||||
socialGroup := r.Group("/social")
|
socialGroup := r.Group("/social")
|
||||||
protectedSocialGroup := socialGroup.Group("")
|
protectedSocialGroup := socialGroup.Group("")
|
||||||
protectedSocialGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
protectedSocialGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
protectedSocialGroup.POST("/follow", socialLimiter, socialHandler.Follow)
|
protectedSocialGroup.POST("/follow", socialLimiter, socialHandler.Follow)
|
||||||
protectedSocialGroup.POST("/unfollow", socialLimiter, socialHandler.Unfollow)
|
protectedSocialGroup.POST("/unfollow", socialLimiter, socialHandler.Unfollow)
|
||||||
protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers)
|
protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers)
|
||||||
protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers)
|
protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers)
|
||||||
}
|
}
|
||||||
// feed
|
// feed
|
||||||
feedRepository := feed.NewFeedRepository(db)
|
feedRepository := feed.NewFeedRepository(db)
|
||||||
feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
|
feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
|
||||||
feedHandler := feed.NewFeedHandler(feedService)
|
feedHandler := feed.NewFeedHandler(feedService)
|
||||||
feedGroup := r.Group("/feed")
|
feedGroup := r.Group("/feed")
|
||||||
feedGroup.Use(jwt.SoftJWTAuth(accountRepository, cache))
|
feedGroup.Use(jwt.SoftJWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
feedGroup.POST("/listLatest", feedHandler.ListLatest)
|
feedGroup.POST("/listLatest", feedHandler.ListLatest)
|
||||||
feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount)
|
feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount)
|
||||||
feedGroup.POST("/listByPopularity", feedHandler.ListByPopularity)
|
feedGroup.POST("/listByPopularity", feedHandler.ListByPopularity)
|
||||||
}
|
}
|
||||||
protectedFeedGroup := feedGroup.Group("")
|
protectedFeedGroup := feedGroup.Group("")
|
||||||
protectedFeedGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
protectedFeedGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing)
|
protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing)
|
||||||
}
|
}
|
||||||
//worker
|
//worker
|
||||||
timelineMQ, err := rabbitmq.NewTimelineMQ(rmq)
|
timelineMQ, err := rabbitmq.NewTimelineMQ(rmq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("timelineMQ init failed (mq disabled): %v", err)
|
log.Printf("timelineMQ init failed (mq disabled): %v", err)
|
||||||
socialMQ = nil
|
timelineMQ = nil
|
||||||
}
|
}
|
||||||
worker.StartOutboxPoller(db, timelineMQ)
|
worker.StartOutboxPoller(db, timelineMQ)
|
||||||
worker.StartConsumer(timelineMQ, "video.timeline.update.queue", cache)
|
worker.StartConsumer(timelineMQ, "video.timeline.update.queue", cache)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +1,49 @@
|
|||||||
package video
|
package video
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Video struct {
|
type Video struct {
|
||||||
ID uint `gorm:"primaryKey" json:"id"`
|
ID uint `gorm:"primaryKey" json:"id"`
|
||||||
AuthorID uint `gorm:"index;not null" json:"author_id"`
|
AuthorID uint `gorm:"index;not null" json:"author_id"`
|
||||||
Username string `gorm:"type:varchar(255);not null" json:"username"`
|
Username string `gorm:"type:varchar(255);not null" json:"username"`
|
||||||
Title string `gorm:"type:varchar(255);not null" json:"title"`
|
Title string `gorm:"type:varchar(255);not null" json:"title"`
|
||||||
Description string `gorm:"type:varchar(255);" json:"description,omitempty"`
|
Description string `gorm:"type:varchar(255);" json:"description,omitempty"`
|
||||||
PlayURL string `gorm:"type:varchar(255);not null" json:"play_url"`
|
PlayURL string `gorm:"type:varchar(255);not null" json:"play_url"`
|
||||||
CoverURL string `gorm:"type:varchar(255);not null" json:"cover_url"`
|
CoverURL string `gorm:"type:varchar(255);not null" json:"cover_url"`
|
||||||
CreateTime time.Time `gorm:"autoCreateTime" json:"create_time"`
|
CreateTime time.Time `gorm:"autoCreateTime;index:idx_videos_create_time,sort:desc;index:idx_videos_popularity_time_id,priority:2,sort:desc" json:"create_time"`
|
||||||
LikesCount int64 `gorm:"column:likes_count;not null;default:0" json:"likes_count"`
|
LikesCount int64 `gorm:"column:likes_count;not null;default:0;index:idx_videos_likes_count_id,priority:1,sort:desc" json:"likes_count"`
|
||||||
Popularity int64 `gorm:"column:popularity;not null;default:0" json:"popularity"`
|
Popularity int64 `gorm:"column:popularity;not null;default:0;index:idx_videos_popularity_time_id,priority:1,sort:desc" json:"popularity"`
|
||||||
}
|
}
|
||||||
|
}
|
||||||
type PublishVideoRequest struct {
|
|
||||||
Title string `json:"title"`
|
type PublishVideoRequest struct {
|
||||||
Description string `json:"description"`
|
Title string `json:"title"`
|
||||||
PlayURL string `json:"play_url"`
|
Description string `json:"description"`
|
||||||
CoverURL string `json:"cover_url"`
|
PlayURL string `json:"play_url"`
|
||||||
}
|
CoverURL string `json:"cover_url"`
|
||||||
|
}
|
||||||
type DeleteVideoRequest struct {
|
|
||||||
ID uint `json:"id"`
|
type DeleteVideoRequest struct {
|
||||||
}
|
ID uint `json:"id"`
|
||||||
|
}
|
||||||
type ListByAuthorIDRequest struct {
|
|
||||||
AuthorID uint `json:"author_id"`
|
type ListByAuthorIDRequest struct {
|
||||||
}
|
AuthorID uint `json:"author_id"`
|
||||||
|
}
|
||||||
type GetDetailRequest struct {
|
|
||||||
ID uint `json:"id"`
|
type GetDetailRequest struct {
|
||||||
}
|
ID uint `json:"id"`
|
||||||
|
}
|
||||||
type UpdateLikesCountRequest struct {
|
|
||||||
ID uint `json:"id"`
|
type UpdateLikesCountRequest struct {
|
||||||
LikesCount int64 `json:"likes_count"`
|
ID uint `json:"id"`
|
||||||
}
|
LikesCount int64 `json:"likes_count"`
|
||||||
|
}
|
||||||
type OutboxMsg struct {
|
|
||||||
ID uint `gorm:"primaryKey"`
|
type OutboxMsg struct {
|
||||||
VideoID uint `gorm:"index"`
|
ID uint `gorm:"primaryKey"`
|
||||||
EventType string `gorm:"type:varchar(50)"`
|
VideoID uint `gorm:"index"`
|
||||||
CreateTime time.Time `gorm:"autoCreateTime"`
|
EventType string `gorm:"type:varchar(50)"`
|
||||||
Status string `gorm:"type:varchar(50);index"`
|
CreateTime time.Time `gorm:"autoCreateTime"`
|
||||||
}
|
Status string `gorm:"type:varchar(50);index"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,104 +1,104 @@
|
|||||||
package video
|
package video
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VideoRepository struct {
|
type VideoRepository struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVideoRepository(db *gorm.DB) *VideoRepository {
|
func NewVideoRepository(db *gorm.DB) *VideoRepository {
|
||||||
return &VideoRepository{db: db}
|
return &VideoRepository{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) CreateVideo(ctx context.Context, video *Video) error {
|
func (vr *VideoRepository) CreateVideo(ctx context.Context, video *Video) error {
|
||||||
if err := vr.db.WithContext(ctx).Create(video).Error; err != nil {
|
if err := vr.db.WithContext(ctx).Create(video).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) CreateMsg(ctx context.Context, Msg *OutboxMsg) error {
|
func (vr *VideoRepository) CreateMsg(ctx context.Context, Msg *OutboxMsg) error {
|
||||||
if err := vr.db.WithContext(ctx).Create(Msg).Error; err != nil {
|
if err := vr.db.WithContext(ctx).Create(Msg).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) DeleteVideo(ctx context.Context, id uint) error {
|
func (vr *VideoRepository) DeleteVideo(ctx context.Context, id uint) error {
|
||||||
if err := vr.db.WithContext(ctx).Delete(&Video{}, id).Error; err != nil {
|
if err := vr.db.WithContext(ctx).Delete(&Video{}, id).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) ListByAuthorID(ctx context.Context, authorID int64) ([]Video, error) {
|
func (vr *VideoRepository) ListByAuthorID(ctx context.Context, authorID int64) ([]Video, error) {
|
||||||
var videos []Video
|
var videos []Video
|
||||||
if err := vr.db.WithContext(ctx).
|
if err := vr.db.WithContext(ctx).
|
||||||
Where("author_id = ?", authorID).
|
Where("author_id = ?", authorID).
|
||||||
Order("create_time desc").
|
Order("create_time desc").
|
||||||
Offset(0).
|
Limit(200).
|
||||||
Find(&videos).Error; err != nil {
|
Find(&videos).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return videos, nil
|
return videos, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) GetByID(ctx context.Context, id uint) (*Video, error) {
|
func (vr *VideoRepository) GetByID(ctx context.Context, id uint) (*Video, error) {
|
||||||
var video Video
|
var video Video
|
||||||
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
|
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
|
||||||
return (*Video)(nil), err
|
return (*Video)(nil), err
|
||||||
}
|
}
|
||||||
return &video, nil
|
return &video, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) UpdateLikesCount(ctx context.Context, id uint, likesCount int64) error {
|
func (vr *VideoRepository) UpdateLikesCount(ctx context.Context, id uint, likesCount int64) error {
|
||||||
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
Update("likes_count", likesCount).Error; err != nil {
|
Update("likes_count", likesCount).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) IsExist(ctx context.Context, id uint) (bool, error) {
|
func (vr *VideoRepository) IsExist(ctx context.Context, id uint) (bool, error) {
|
||||||
var video Video
|
var video Video
|
||||||
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
|
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) UpdatePopularity(ctx context.Context, id uint, change int64) error {
|
func (vr *VideoRepository) UpdatePopularity(ctx context.Context, id uint, change int64) error {
|
||||||
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
Update("popularity", gorm.Expr("popularity + ?", change)).Error; err != nil {
|
Update("popularity", gorm.Expr("popularity + ?", change)).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) ChangeLikesCount(ctx context.Context, id uint, change int64) error {
|
func (vr *VideoRepository) ChangeLikesCount(ctx context.Context, id uint, change int64) error {
|
||||||
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + ?, 0)", change)).Error; err != nil {
|
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + ?, 0)", change)).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) ChangePopularity(ctx context.Context, id uint, change int64) error {
|
func (vr *VideoRepository) ChangePopularity(ctx context.Context, id uint, change int64) error {
|
||||||
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
if err := vr.db.WithContext(ctx).Model(&Video{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", change)).Error; err != nil {
|
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", change)).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user