learnsteam-quiz-api/internal/routers/user.go

52 lines
1.4 KiB
Go
Raw Normal View History

2023-10-19 22:47:12 +09:00
package routers
import (
2023-11-17 01:38:01 +09:00
"learnsteam/learsteam-quiz-api/internal/controllers"
"learnsteam/learsteam-quiz-api/internal/middleware"
"learnsteam/learsteam-quiz-api/internal/repositories"
"learnsteam/learsteam-quiz-api/internal/services"
2023-10-19 22:47:12 +09:00
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
2023-11-17 01:38:01 +09:00
func InitUserRouter(db *gorm.DB, router *gin.Engine) {
r := NewUserRouter(db, router)
r.SetUserRouter(db, router)
}
2023-10-19 22:47:12 +09:00
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(db *gorm.DB, router *gin.Engine) {
group := router.Group("/user")
2023-11-17 01:38:01 +09:00
group.GET("", middleware.Auth(), r.controller.List)
2023-10-19 22:47:12 +09:00
group.GET("/:id", middleware.Auth(), r.controller.Find)
}