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)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package users
|
||||
|
||||
import "github.com/gofiber/fiber/v3"
|
||||
|
||||
func RegisterPublicRoutes(router fiber.Router) {
|
||||
router.Post("/users", createUser)
|
||||
}
|
||||
|
||||
func RegisterRoutes(router fiber.Router) {
|
||||
router.Get("/users/me", getCurrentUser)
|
||||
}
|
||||
Reference in New Issue
Block a user