2025-02-24 16:48:20 +08:00
|
|
|
package authhttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
|
|
|
|
|
authsvc "github.com/ardeman/project-legalgo-go/internal/services/auth"
|
|
|
|
|
"github.com/ardeman/project-legalgo-go/internal/utilities/response"
|
|
|
|
|
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func RegisterUser(
|
|
|
|
|
router chi.Router,
|
|
|
|
|
validate *validator.Validate,
|
|
|
|
|
authSvc authsvc.AuthIntf,
|
|
|
|
|
) {
|
|
|
|
|
router.Post("/user/register", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
var spec authdomain.RegisterUserReq
|
|
|
|
|
|
|
|
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
"failed to unmarshal request",
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := validate.Struct(spec); err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.(validator.ValidationErrors).Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token, err := authSvc.RegisterUser(spec)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 11:07:17 +08:00
|
|
|
responsePayload := &authdomain.AuthResponse{
|
|
|
|
|
Token: token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.RespondJsonSuccess(ctx, w, responsePayload)
|
2025-02-24 16:48:20 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RegisterStaff(
|
|
|
|
|
router chi.Router,
|
|
|
|
|
validate *validator.Validate,
|
|
|
|
|
authSvc authsvc.AuthIntf,
|
|
|
|
|
) {
|
|
|
|
|
router.Post("/staff/register", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
var spec authdomain.RegisterStaffReq
|
|
|
|
|
|
|
|
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
"failed to unmarshal request",
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := validate.Struct(spec); err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.(validator.ValidationErrors).Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token, err := authSvc.RegisterStaff(spec)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-02-25 11:07:17 +08:00
|
|
|
responsePayload := &authdomain.AuthResponse{
|
|
|
|
|
Token: token,
|
|
|
|
|
}
|
|
|
|
|
response.RespondJsonSuccess(ctx, w, responsePayload)
|
2025-02-24 16:48:20 +08:00
|
|
|
})
|
|
|
|
|
}
|