forked from mtusi2026/mtusi2026lab4
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
package fiber
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto"
|
|
httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http"
|
|
)
|
|
|
|
// userResponse converts a user DTO into its HTTP representation.
|
|
func userResponse(user dto.UserDTO) httpmodel.UserResponse {
|
|
return httpmodel.UserResponse{
|
|
ID: user.ID.String(),
|
|
Username: user.Username,
|
|
DisplayName: user.DisplayName,
|
|
CreatedAt: user.CreatedAt,
|
|
}
|
|
}
|
|
|
|
// chatResponse converts a chat DTO and its member identifiers into an HTTP response.
|
|
func chatResponse(chat dto.ChatDTO) httpmodel.ChatResponse {
|
|
members := make([]string, len(chat.MemberIDs))
|
|
for index, id := range chat.MemberIDs {
|
|
members[index] = id.String()
|
|
}
|
|
return httpmodel.ChatResponse{
|
|
ID: chat.ID.String(),
|
|
Title: chat.Title,
|
|
MemberIDs: members,
|
|
CreatedAt: chat.CreatedAt,
|
|
}
|
|
}
|
|
|
|
// messageResponse converts a message DTO into its HTTP representation.
|
|
func messageResponse(message dto.MessageDTO) httpmodel.MessageResponse {
|
|
ids := append([]string{}, message.AttachmentIDs...)
|
|
return httpmodel.MessageResponse{
|
|
AttachmentIDs: ids,
|
|
ID: message.ID.String(),
|
|
ChatID: message.ChatID.String(),
|
|
SenderID: message.SenderID.String(),
|
|
Text: message.Text,
|
|
CreatedAt: message.CreatedAt,
|
|
}
|
|
}
|
|
|
|
// pageRequest extracts identity, resource and pagination parameters from the request.
|
|
func pageRequest(ctx *fiber.Ctx) dto.PageRequestDTO {
|
|
return dto.PageRequestDTO{
|
|
UserID: identity(ctx),
|
|
ChatID: ctx.Params("chat_id"),
|
|
Limit: ctx.Query("limit"),
|
|
Cursor: ctx.Query("cursor"),
|
|
}
|
|
}
|
|
|
|
// chatPageResponse converts a page of chat DTOs into an HTTP response.
|
|
func chatPageResponse(page dto.ChatPageDTO) httpmodel.ChatPageResponse {
|
|
response := httpmodel.ChatPageResponse{
|
|
Items: make([]httpmodel.ChatResponse, len(page.Items)),
|
|
NextCursor: page.NextCursor,
|
|
}
|
|
for index, chat := range page.Items {
|
|
response.Items[index] = chatResponse(chat)
|
|
}
|
|
return response
|
|
}
|
|
|
|
// messagePageResponse converts a page of message DTOs into an HTTP response.
|
|
func messagePageResponse(page dto.MessagePageDTO) httpmodel.MessagePageResponse {
|
|
response := httpmodel.MessagePageResponse{
|
|
Items: make([]httpmodel.MessageResponse, len(page.Items)),
|
|
NextCursor: page.NextCursor,
|
|
}
|
|
for index, message := range page.Items {
|
|
response.Items[index] = messageResponse(message)
|
|
}
|
|
return response
|
|
}
|