You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.6 KiB
Go

package admin
import (
"cls-server/internal/application/admin"
"cls-server/internal/infrastructure/middleware/auth"
"cls-server/internal/interfaces"
"cls-server/pkg/logger"
"github.com/gin-gonic/gin"
"net/http"
)
type AdminHandler struct {
service *admin.AdminService
authMiddleware *auth.AuthMiddleware
log logger.Logger
}
var _ interfaces.Handler = (*AdminHandler)(nil)
func NewAdminHandler(service *admin.AdminService,
authMiddleware *auth.AuthMiddleware,
log logger.New) *AdminHandler {
return &AdminHandler{service, authMiddleware, log("cls-server:handler:admin")}
}
func (a *AdminHandler) RegisterRouters(app gin.IRouter) {
adminApp := app.Group("/admin")
{
adminApp.POST("/register", a.register)
adminApp.GET("/profile", a.getAdminProfile)
adminApp.POST("/update-password", a.updatePassword)
adminApp.POST("/update-phone", a.updatePhone)
}
}
func (a *AdminHandler) register(c *gin.Context) {
dto := &admin.AdminDto{}
err := c.ShouldBindJSON(dto)
if err != nil {
a.log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = a.authMiddleware.VerifyPhoneValid(dto.Phone)
if err != nil {
a.log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = a.service.Register(dto)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.AbortWithStatus(http.StatusOK)
}
}
func (a *AdminHandler) updatePassword(c *gin.Context) {
dto := &admin.AdminDto{}
err := c.ShouldBindJSON(dto)
if err != nil {
a.log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = a.service.UpdatePassword(dto)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.AbortWithStatus(http.StatusOK)
}
}
func (a *AdminHandler) updatePhone(c *gin.Context) {
dto := &admin.AdminDto{}
err := c.ShouldBindJSON(dto)
if err != nil {
a.log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = a.authMiddleware.VerifyPhoneValid(dto.Phone)
if err != nil {
a.log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = a.service.UpdatePhone(dto)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.AbortWithStatus(http.StatusOK)
}
}
func (a *AdminHandler) getAdminProfile(c *gin.Context) {
dto := &admin.AdminDto{}
err := c.ShouldBindJSON(dto)
if err != nil {
a.log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = a.service.GetAdminProfile(dto)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.AbortWithStatusJSON(http.StatusOK, dto)
}
}