niki/delivery/http_server/admin/admin/register.go

45 lines
1.2 KiB
Go

package adminhandler
import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
// Register godoc
// @Summary Register an admin by super-admin
// @Tags Admin
// @Accept json
// @Produce json
// @Param Request body adminserviceparam.RegisterRequest true "Admin Register Request Body"
// @Success 200 {object} adminserviceparam.RegisterResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
// @Router /admins/register [post].
func (h Handler) Register(c echo.Context) error {
var req adminserviceparam.RegisterRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.adminVld.ValidateRegisterRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.adminSvc.Register(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}