forked from mtusi2026/mtusi2026lab2
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
package fiber
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab2/internal/model/dto"
|
|
httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab2/internal/model/http"
|
|
)
|
|
|
|
// HandleUsersCreate decodes registration input and returns the created user.
|
|
func (ctrl *Controller) HandleUsersCreate(ctx *fiber.Ctx) error {
|
|
var request httpmodel.UsersCreateRequest
|
|
if err := decode(ctx, &request, "username", "display_name", "password"); err != nil {
|
|
return err
|
|
}
|
|
user, err := ctrl.auth.Register(ctx.UserContext(), dto.UsersCreateRequestDTO{
|
|
Username: request.Username,
|
|
DisplayName: request.DisplayName,
|
|
}, request.Password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusCreated).JSON(userResponse(user))
|
|
}
|
|
|
|
// HandleUsersGetMe returns the account selected by the validated session.
|
|
func (ctrl *Controller) HandleUsersGetMe(ctx *fiber.Ctx) error {
|
|
user, err := ctrl.service.UsersGetMe(ctx.UserContext(), identity(ctx))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusOK).JSON(userResponse(user))
|
|
}
|
|
|
|
// HandleChatsCreate decodes the chat payload and returns the new chat.
|
|
func (ctrl *Controller) HandleChatsCreate(ctx *fiber.Ctx) error {
|
|
var request httpmodel.ChatsCreateRequest
|
|
if err := decode(ctx, &request, "title", "member_ids"); err != nil {
|
|
return err
|
|
}
|
|
chat, err := ctrl.service.ChatsCreate(ctx.UserContext(), dto.ChatsCreateRequestDTO{
|
|
UserID: identity(ctx),
|
|
Title: request.Title,
|
|
MemberIDs: request.MemberIDs,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusCreated).JSON(chatResponse(chat))
|
|
}
|
|
|
|
// HandleChatsGetByID delegates chat authorization and maps the accessible chat.
|
|
func (ctrl *Controller) HandleChatsGetByID(ctx *fiber.Ctx) error {
|
|
chat, err := ctrl.service.ChatsGetByID(ctx.UserContext(), identity(ctx), ctx.Params("chat_id"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusOK).JSON(chatResponse(chat))
|
|
}
|
|
|
|
// HandleChatsGetPage returns the service's page of visible chats.
|
|
func (ctrl *Controller) HandleChatsGetPage(ctx *fiber.Ctx) error {
|
|
page, err := ctrl.service.ChatsGetPage(ctx.UserContext(), pageRequest(ctx))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusOK).JSON(chatPageResponse(page))
|
|
}
|
|
|
|
// HandleMessagesCreate decodes text; the service derives and authorizes the sender.
|
|
func (ctrl *Controller) HandleMessagesCreate(ctx *fiber.Ctx) error {
|
|
var request httpmodel.MessagesCreateRequest
|
|
if err := decode(ctx, &request, "text"); err != nil {
|
|
return err
|
|
}
|
|
message, err := ctrl.service.MessagesCreate(ctx.UserContext(), dto.MessagesCreateRequestDTO{
|
|
UserID: identity(ctx),
|
|
ChatID: ctx.Params("chat_id"),
|
|
Text: request.Text,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusCreated).JSON(messageResponse(message))
|
|
}
|
|
|
|
// HandleMessagesGetPage returns the service's ordered history page.
|
|
func (ctrl *Controller) HandleMessagesGetPage(ctx *fiber.Ctx) error {
|
|
page, err := ctrl.service.MessagesGetPage(ctx.UserContext(), pageRequest(ctx))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Status(http.StatusOK).JSON(messagePageResponse(page))
|
|
}
|