forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/auth"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
||||
@@ -25,38 +27,34 @@ type CreateUserRequest struct {
|
||||
func (h *Handler) CreateUser(c fiber.Ctx) error {
|
||||
var req CreateUserRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if msg := common.ValidateCreateUser(req.Username, req.DisplayName); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
if err := common.ValidateCreateUser(req.Username, req.DisplayName); err != nil {
|
||||
return err
|
||||
}
|
||||
if msg := common.ValidatePassword(req.Password); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
if err := common.ValidatePassword(req.Password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hash, err := auth.HashPassword(req.Password)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, common.Internal())
|
||||
slog.Error("hash password", "err", err)
|
||||
return common.Internal()
|
||||
}
|
||||
|
||||
user, err := h.store.CreateUser(c.Context(), req.Username, req.DisplayName, hash)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(user)
|
||||
}
|
||||
|
||||
func (h *Handler) GetCurrentUser(c fiber.Ctx) error {
|
||||
userID, ok := common.UserID(c)
|
||||
if !ok {
|
||||
return common.SendChallenge(c, `Bearer realm="messenger"`, "authentication required")
|
||||
}
|
||||
|
||||
user, err := h.store.GetUserByID(c.Context(), userID)
|
||||
user, err := h.store.GetUserByID(c.Context(), common.MustUserID(c))
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(user)
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func RegisterPublicRoutes(router fiber.Router, st *store.Store) {
|
||||
handler := NewHandler(st)
|
||||
router.Post("/users", handler.CreateUser)
|
||||
}
|
||||
|
||||
func RegisterProtectedRoutes(router fiber.Router, st *store.Store) {
|
||||
handler := NewHandler(st)
|
||||
router.Get("/users/me", handler.GetCurrentUser)
|
||||
}
|
||||
Reference in New Issue
Block a user