cslms-api/internal/routers/auth.go

56 lines
1.5 KiB
Go
Raw Normal View History

2023-12-27 17:31:49 +09:00
package routers
import (
"learnsteam/cslms-api/internal/controllers"
2023-12-29 00:27:12 +09:00
"learnsteam/cslms-api/internal/middleware"
2023-12-27 17:31:49 +09:00
"learnsteam/cslms-api/internal/repositories"
"learnsteam/cslms-api/internal/services"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func InitAuthRouter(db *gorm.DB, router *gin.Engine) {
r := NewAuthRouter(db, router)
r.SetAuthRouter()
}
type AuthRouter interface {
SetRouter(db *gorm.DB, router *gin.Engine)
}
type authRouter struct {
db *gorm.DB
userRepository repositories.UserRepository
tokenRepository repositories.TokenRepository
service services.AuthService
tokenService services.TokenService
controller controllers.AuthController
router *gin.Engine
}
func NewAuthRouter(db *gorm.DB, router *gin.Engine) *authRouter {
userRepository := repositories.NewUserRepository(db)
tokenRepository := repositories.NewTokenRepository(db)
service := services.NewAuthService(userRepository, tokenRepository)
tokenService := services.NewTokenService(tokenRepository)
controller := controllers.NewAuthController(service, tokenService)
return &authRouter{
db: db,
userRepository: userRepository,
tokenRepository: tokenRepository,
service: service,
tokenService: tokenService,
controller: controller,
router: router,
}
}
func (r *authRouter) SetAuthRouter() {
group := r.router.Group("/auth")
group.POST("login", r.controller.Login)
group.POST("register", r.controller.Register)
2023-12-29 00:27:12 +09:00
group.POST("logout", middleware.Auth("member"), r.controller.Logout)
2023-12-27 17:31:49 +09:00
}