init(app) moved app code from Lab1

This commit is contained in:
imadev
2026-10-21 16:42:14 +03:00
parent 65003b6119
commit 1db3953226
22 changed files with 1297 additions and 0 deletions
+57
View File
@@ -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)
}