forked from mtusi2026/mtusi2026lab2
feat(app) lab2 prepared for mr
This commit is contained in:
@@ -1,13 +1,28 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/storage"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/auth"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func createUser(c fiber.Ctx) error {
|
||||
type Handler struct {
|
||||
store *store.Store
|
||||
}
|
||||
|
||||
func NewHandler(st *store.Store) *Handler {
|
||||
return &Handler{store: st}
|
||||
}
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username" validate:"required"`
|
||||
DisplayName string `json:"display_name" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) CreateUser(c fiber.Ctx) error {
|
||||
var req CreateUserRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
@@ -16,18 +31,17 @@ func createUser(c fiber.Ctx) error {
|
||||
if msg := common.ValidateCreateUser(req.Username, req.DisplayName); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
}
|
||||
if msg := common.ValidatePassword(req.Password); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
}
|
||||
|
||||
var user User
|
||||
hash, err := auth.HashPassword(req.Password)
|
||||
if err != nil {
|
||||
return common.SendError(c, fiber.StatusInternalServerError,
|
||||
common.ErrorCodeInternal, "Internal server error")
|
||||
}
|
||||
|
||||
err := storage.GlobalStore.Update(func(tx storage.Tx) error {
|
||||
if tx.UsernameExists(req.Username) {
|
||||
return common.Conflict("username already taken")
|
||||
}
|
||||
|
||||
user = NewUser(req)
|
||||
tx.CreateUser(user)
|
||||
return nil
|
||||
})
|
||||
user, err := h.store.CreateUser(c.Context(), req.Username, req.DisplayName, hash)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
@@ -35,20 +49,13 @@ func createUser(c fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusCreated).JSON(user)
|
||||
}
|
||||
|
||||
func getCurrentUser(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
func (h *Handler) GetCurrentUser(c fiber.Ctx) error {
|
||||
userID, ok := c.Locals("userID").(uuid.UUID)
|
||||
if !ok {
|
||||
return common.SendChallenge(c, `Bearer realm="messenger"`, "authentication required")
|
||||
}
|
||||
|
||||
var user storage.UserEntity
|
||||
|
||||
err := storage.GlobalStore.View(func(tx storage.ReadOnlyTx) error {
|
||||
found, ok := tx.GetUser(userID)
|
||||
if !ok {
|
||||
return common.NotFound("no user with provided credentials exist")
|
||||
}
|
||||
|
||||
user = found
|
||||
return nil
|
||||
})
|
||||
user, err := h.store.GetUserByID(c.Context(), userID)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt common.RFCFormTime `json:"created_at"`
|
||||
}
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username" validate:"required"`
|
||||
DisplayName string `json:"display_name" validate:"required"`
|
||||
}
|
||||
|
||||
func NewUser(req CreateUserRequest) User {
|
||||
return User{
|
||||
ID: uuid.New(),
|
||||
Username: req.Username,
|
||||
DisplayName: req.DisplayName,
|
||||
CreatedAt: common.NowUTC(),
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) GetID() uuid.UUID {
|
||||
return u.ID
|
||||
}
|
||||
|
||||
func (u User) GetCreatedAt() time.Time {
|
||||
return time.Time(u.CreatedAt)
|
||||
}
|
||||
|
||||
func (u User) GetUsername() string {
|
||||
return u.Username
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
package users
|
||||
|
||||
import "github.com/gofiber/fiber/v3"
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func RegisterPublicRoutes(router fiber.Router) {
|
||||
router.Post("/users", createUser)
|
||||
func RegisterPublicRoutes(router fiber.Router, st *store.Store) {
|
||||
handler := NewHandler(st)
|
||||
router.Post("/users", handler.CreateUser)
|
||||
}
|
||||
|
||||
func RegisterRoutes(router fiber.Router) {
|
||||
router.Get("/users/me", getCurrentUser)
|
||||
func RegisterRoutes(router fiber.Router, st *store.Store) {
|
||||
handler := NewHandler(st)
|
||||
router.Get("/users/me", handler.GetCurrentUser)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user