package handler import ( "encoding/json" "time" "uuid" "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"` AttachmentIDs json.RawMessage `json:"attachment_ids"` } 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"` AttachmentIDs []string `json:"attachment_ids"` } type variantResponse struct { Name string `json:"name"` ContentType string `json:"content_type"` SizeBytes int64 `json:"size_bytes"` Width int `json:"width"` Height int `json:"height"` SHA256 string `json:"sha256"` } type attachmentResponse struct { ID string `json:"id"` ChatID string `json:"chat_id"` OwnerID string `json:"owner_id"` Filename string `json:"filename"` ContentType string `json:"content_type"` SizeBytes int64 `json:"size_bytes"` SHA256 string `json:"sha256"` Status string `json:"status"` Variants []variantResponse `json:"variants"` ErrorCode *string `json:"error_code"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_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), AttachmentIDs: toIDStrings(m.AttachmentIDs), } } func toAttachmentResponse(a domain.Attachment) attachmentResponse { return attachmentResponse{ ID: a.ID.String(), ChatID: a.ChatID.String(), OwnerID: a.OwnerID.String(), Filename: a.Filename, ContentType: a.ContentType, SizeBytes: a.SizeBytes, SHA256: a.SHA256, Status: a.Status, Variants: []variantResponse{}, ErrorCode: a.ErrorCode, CreatedAt: formatTime(a.CreatedAt), UpdatedAt: formatTime(a.UpdatedAt), } } func toIDStrings(ids []uuid.UUID) []string { result := make([]string, len(ids)) for i, id := range ids { result[i] = id.String() } return result } 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 }