forked from mtusi2026/mtusi2026lab4
129 lines
2.8 KiB
Go
129 lines
2.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"time"
|
|
|
|
"messenger/internal/domain"
|
|
)
|
|
|
|
type createUserRequest struct {
|
|
Username *string `json:"username"`
|
|
DisplayName *string `json:"display_name"`
|
|
Password *string `json:"password"`
|
|
}
|
|
|
|
type createChatRequest struct {
|
|
Title *string `json:"title"`
|
|
MemberIDs *[]string `json:"member_ids"`
|
|
}
|
|
|
|
type createMessageRequest struct {
|
|
Text *string `json:"text"`
|
|
}
|
|
|
|
type userResponse struct {
|
|
ID string `json:"id"`
|
|
Username string `json:"username"`
|
|
DisplayName string `json:"display_name"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
type chatResponse struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
MemberIDs []string `json:"member_ids"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
type messageResponse struct {
|
|
ID string `json:"id"`
|
|
ChatID string `json:"chat_id"`
|
|
SenderID string `json:"sender_id"`
|
|
Text string `json:"text"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
type pageResponse[T any] struct {
|
|
Items []T `json:"items"`
|
|
NextCursor *string `json:"next_cursor"`
|
|
}
|
|
|
|
type sessionResponse struct {
|
|
Token string `json:"token"`
|
|
TokenType string `json:"token_type"`
|
|
ExpiresAt string `json:"expires_at"`
|
|
User userResponse `json:"user"`
|
|
}
|
|
|
|
type healthResponse struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
type errorBody struct {
|
|
Error errorDetails `json:"error"`
|
|
}
|
|
|
|
type errorDetails struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
RequestID string `json:"request_id"`
|
|
}
|
|
|
|
const timeLayout = "2006-01-02T15:04:05.000000Z"
|
|
|
|
func formatTime(t time.Time) string { return t.UTC().Format(timeLayout) }
|
|
|
|
func toUserResponse(u domain.User) userResponse {
|
|
return userResponse{
|
|
ID: u.ID.String(),
|
|
Username: u.Username,
|
|
DisplayName: u.DisplayName,
|
|
CreatedAt: formatTime(u.CreatedAt),
|
|
}
|
|
}
|
|
|
|
func toSessionResponse(s domain.Session) sessionResponse {
|
|
return sessionResponse{
|
|
Token: s.Token,
|
|
TokenType: "Bearer",
|
|
ExpiresAt: formatTime(s.ExpiresAt),
|
|
User: toUserResponse(s.User),
|
|
}
|
|
}
|
|
|
|
func toChatResponse(c domain.Chat) chatResponse {
|
|
members := make([]string, len(c.MemberIDs))
|
|
for i, id := range c.MemberIDs {
|
|
members[i] = id.String()
|
|
}
|
|
return chatResponse{
|
|
ID: c.ID.String(),
|
|
Title: c.Title,
|
|
MemberIDs: members,
|
|
CreatedAt: formatTime(c.CreatedAt),
|
|
}
|
|
}
|
|
|
|
func toMessageResponse(m domain.Message) messageResponse {
|
|
return messageResponse{
|
|
ID: m.ID.String(),
|
|
ChatID: m.ChatID.String(),
|
|
SenderID: m.SenderID.String(),
|
|
Text: m.Text,
|
|
CreatedAt: formatTime(m.CreatedAt),
|
|
}
|
|
}
|
|
|
|
func toPageResponse[D, R any](p domain.Page[D], conv func(D) R) pageResponse[R] {
|
|
items := make([]R, len(p.Items))
|
|
for i, it := range p.Items {
|
|
items[i] = conv(it)
|
|
}
|
|
resp := pageResponse[R]{Items: items}
|
|
if p.Next != nil {
|
|
s := encodeCursor(*p.Next)
|
|
resp.NextCursor = &s
|
|
}
|
|
return resp
|
|
}
|