52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
|
package routers
|
||
|
|
||
|
import (
|
||
|
"learnsteam/cslms-api/internal/controllers"
|
||
|
"learnsteam/cslms-api/internal/middleware"
|
||
|
"learnsteam/cslms-api/internal/repositories"
|
||
|
"learnsteam/cslms-api/internal/services"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
func InitUserRouter(db *gorm.DB, router *gin.Engine) {
|
||
|
r := NewUserRouter(db, router)
|
||
|
r.SetUserRouter()
|
||
|
}
|
||
|
|
||
|
type UserRouter interface {
|
||
|
SetRouter(db *gorm.DB, router *gin.Engine)
|
||
|
}
|
||
|
|
||
|
type userRouter struct {
|
||
|
db *gorm.DB
|
||
|
repository repositories.UserRepository
|
||
|
service services.UserService
|
||
|
controller controllers.UserController
|
||
|
router *gin.Engine
|
||
|
}
|
||
|
|
||
|
func NewUserRouter(db *gorm.DB, router *gin.Engine) *userRouter {
|
||
|
repository := repositories.NewUserRepository(db)
|
||
|
tokenRepository := repositories.NewTokenRepository(db)
|
||
|
service := services.NewUserService(repository, tokenRepository)
|
||
|
|
||
|
tokenService := services.NewTokenService(tokenRepository)
|
||
|
controller := controllers.NewUserController(service, tokenService)
|
||
|
|
||
|
return &userRouter{
|
||
|
db: db,
|
||
|
repository: repository,
|
||
|
service: service,
|
||
|
controller: controller,
|
||
|
router: router,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *userRouter) SetUserRouter() {
|
||
|
group := r.router.Group("/user")
|
||
|
group.GET("", middleware.Auth("admin"), r.controller.List)
|
||
|
group.GET("/:id", middleware.Auth("admin"), r.controller.Find)
|
||
|
}
|