Files
mtusi2026lab6/backend/internal/httpapi/handler/user.go
T

44 lines
973 B
Go

package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"high-loud-apps-labs/internal/httpapi/dto"
"high-loud-apps-labs/internal/httpapi/middleware"
"high-loud-apps-labs/internal/service"
)
type UserHandler struct {
users *service.UserService
}
func NewUserHandler(users *service.UserService) *UserHandler {
return &UserHandler{users: users}
}
func (h *UserHandler) Create(c *gin.Context) {
var req dto.CreateUserRequest
if err := bindStrictJSON(c, &req); err != nil {
respondError(c, err)
return
}
u, err := h.users.Create(c.Request.Context(), req.Username, req.DisplayName, req.Password)
if err != nil {
respondError(c, err)
return
}
c.JSON(http.StatusCreated, dto.UserFromDomain(u))
}
func (h *UserHandler) Current(c *gin.Context) {
u := middleware.CurrentUser(c)
if u == nil {
respondError(c, &badRequestError{msg: "нет текущего пользователя"})
return
}
c.JSON(http.StatusOK, dto.UserFromDomain(u))
}