implement proper timestamps

This commit is contained in:
2025-03-24 11:45:54 -04:00
parent d1e77ad4e2
commit 8948640311
3 changed files with 38 additions and 10 deletions

View File

@@ -2,10 +2,13 @@ package api
import (
"context"
"encoding/json"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"time"
)
func MessageCtx(next http.Handler) http.Handler {
@@ -57,10 +60,10 @@ func ListMessages(w http.ResponseWriter, r *http.Request) {
type messageKey struct{}
type Message struct {
ID string `json:"id"`
UserID int64 `json:"user_id"`
Body string `json:"body"`
Timestamp int64 `json:"timestamp"`
ID string `json:"id"`
UserID int64 `json:"user_id"`
Body string `json:"body"`
Timestamp time.Time `json:"timestamp"`
}
type MessageRequest struct {
@@ -78,3 +81,25 @@ type MessageResponse struct {
Elapsed int64 `json:"elapsed"`
}
func (m MessageResponse) MarshalJSON() ([]byte, error) {
type OrderedMessageResponse struct {
ID string `json:"id"`
UserID int64 `json:"user_id"`
Body string `json:"body"`
Timestamp string `json:"timestamp"`
User *UserPayload `json:"user,omitempty"`
Elapsed int64 `json:"elapsed"`
}
ordered := OrderedMessageResponse{
ID: m.Message.ID,
UserID: m.Message.UserID,
Body: m.Message.Body,
Timestamp: m.Message.Timestamp.Format(time.RFC3339),
User: m.User,
Elapsed: m.Elapsed,
}
return json.Marshal(ordered)
}