package http import ( "feedsystem_video_go/internal/account" "feedsystem_video_go/internal/feed" "feedsystem_video_go/internal/middleware" rediscache "feedsystem_video_go/internal/redis" "feedsystem_video_go/internal/social" "feedsystem_video_go/internal/video" "github.com/gin-gonic/gin" "gorm.io/gorm" ) func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine { r := gin.Default() // account accountRepository := account.NewAccountRepository(db) accountService := account.NewAccountService(accountRepository, cache) accountHandler := account.NewAccountHandler(accountService) accountGroup := r.Group("/account") { accountGroup.POST("/register", accountHandler.CreateAccount) accountGroup.POST("/login", accountHandler.Login) accountGroup.POST("/changePassword", accountHandler.ChangePassword) accountGroup.POST("/findByID", accountHandler.FindByID) accountGroup.POST("/findByUsername", accountHandler.FindByUsername) } protectedAccountGroup := accountGroup.Group("") protectedAccountGroup.Use(middleware.JWTAuth(accountRepository, cache)) { protectedAccountGroup.POST("/logout", accountHandler.Logout) protectedAccountGroup.POST("/rename", accountHandler.Rename) } // video videoRepository := video.NewVideoRepository(db) videoService := video.NewVideoService(videoRepository, cache) videoHandler := video.NewVideoHandler(videoService, accountService) videoGroup := r.Group("/video") { videoGroup.POST("/listByAuthorID", videoHandler.ListByAuthorID) videoGroup.POST("/getDetail", videoHandler.GetDetail) } protectedVideoGroup := videoGroup.Group("") protectedVideoGroup.Use(middleware.JWTAuth(accountRepository, cache)) { protectedVideoGroup.POST("/publish", videoHandler.PublishVideo) } // like likeRepository := video.NewLikeRepository(db) likeService := video.NewLikeService(likeRepository, videoRepository) likeHandler := video.NewLikeHandler(likeService) likeGroup := r.Group("/like") protectedLikeGroup := likeGroup.Group("") protectedLikeGroup.Use(middleware.JWTAuth(accountRepository, cache)) { protectedLikeGroup.POST("/like", likeHandler.Like) protectedLikeGroup.POST("/unlike", likeHandler.Unlike) protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked) } // comment commentRepository := video.NewCommentRepository(db) commentService := video.NewCommentService(commentRepository, videoRepository) commentHandler := video.NewCommentHandler(commentService, accountService) commentGroup := r.Group("/comment") { commentGroup.POST("/listAll", commentHandler.GetAllComments) } protectedCommentGroup := commentGroup.Group("") protectedCommentGroup.Use(middleware.JWTAuth(accountRepository, cache)) { protectedCommentGroup.POST("/publish", commentHandler.PublishComment) protectedCommentGroup.POST("/delete", commentHandler.DeleteComment) } // social socialRepository := social.NewSocialRepository(db) socialService := social.NewSocialService(socialRepository, accountRepository) socialHandler := social.NewSocialHandler(socialService) socialGroup := r.Group("/social") protectedSocialGroup := socialGroup.Group("") protectedSocialGroup.Use(middleware.JWTAuth(accountRepository, cache)) { protectedSocialGroup.POST("/follow", socialHandler.Follow) protectedSocialGroup.POST("/unfollow", socialHandler.Unfollow) protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers) protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers) } // feed feedRepository := feed.NewFeedRepository(db) feedService := feed.NewFeedService(feedRepository, likeRepository, cache) feedHandler := feed.NewFeedHandler(feedService) feedGroup := r.Group("/feed") feedGroup.Use(middleware.SoftJWTAuth(accountRepository, cache)) { feedGroup.POST("/listLatest", feedHandler.ListLatest) feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount) } protectedFeedGroup := feedGroup.Group("") protectedFeedGroup.Use(middleware.JWTAuth(accountRepository, cache)) { protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing) } return r }