forked from mtusi2026/mtusi2026lab2
init(app) moved app code from Lab1
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/storage"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func createUser(c fiber.Ctx) error {
|
||||
var req CreateUserRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
if msg := common.ValidateCreateUser(req.Username, req.DisplayName); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
}
|
||||
|
||||
var user User
|
||||
|
||||
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
|
||||
})
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(user)
|
||||
}
|
||||
|
||||
func getCurrentUser(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
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
|
||||
})
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.JSON(user)
|
||||
}
|
||||
Reference in New Issue
Block a user