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,6 +2,7 @@ package api
import ( import (
"errors" "errors"
"time"
"git.dubyatp.xyz/chat-api-server/db" "git.dubyatp.xyz/chat-api-server/db"
) )
@@ -39,7 +40,7 @@ func dbGetMessage(id string) (*Message, error) {
ID: message["ID"].(string), ID: message["ID"].(string),
UserID: int64(message["UserID"].(float64)), UserID: int64(message["UserID"].(float64)),
Body: message["Body"].(string), Body: message["Body"].(string),
Timestamp: int64(message["Timestamp"].(float64)), Timestamp: time.Time(time.Now()),
}, nil }, nil
} }
} }
@@ -61,7 +62,7 @@ func dbGetAllMessages() ([]*Message, error) {
ID: message["ID"].(string), ID: message["ID"].(string),
UserID: int64(message["UserID"].(float64)), UserID: int64(message["UserID"].(float64)),
Body: message["Body"].(string), Body: message["Body"].(string),
Timestamp: int64(message["Timestamp"].(float64)), Timestamp: time.Time(time.Now()),
}) })
} }
if len(result) == 0 { if len(result) == 0 {

View File

@@ -1,10 +1,12 @@
package api package api
import "time"
var messages = []*Message{ var messages = []*Message{
{ID: "1", UserID: 1, Body: "hello", Timestamp: 1234567890}, {ID: "1", UserID: 1, Body: "hello", Timestamp: time.Now()},
{ID: "2", UserID: 2, Body: "world", Timestamp: 1234567890}, {ID: "2", UserID: 2, Body: "world", Timestamp: time.Now()},
{ID: "3", UserID: 1, Body: "abababa", Timestamp: 1234567890}, {ID: "3", UserID: 1, Body: "abababa", Timestamp: time.Now()},
{ID: "4", UserID: 2, Body: "bitch", Timestamp: 1234567890}, {ID: "4", UserID: 2, Body: "bitch", Timestamp: time.Now()},
} }
var users = []*User{ var users = []*User{

View File

@@ -2,10 +2,13 @@ package api
import ( import (
"context" "context"
"encoding/json"
"net/http" "net/http"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/render" "github.com/go-chi/render"
"time"
) )
func MessageCtx(next http.Handler) http.Handler { func MessageCtx(next http.Handler) http.Handler {
@@ -60,7 +63,7 @@ type Message struct {
ID string `json:"id"` ID string `json:"id"`
UserID int64 `json:"user_id"` UserID int64 `json:"user_id"`
Body string `json:"body"` Body string `json:"body"`
Timestamp int64 `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
} }
type MessageRequest struct { type MessageRequest struct {
@@ -78,3 +81,25 @@ type MessageResponse struct {
Elapsed int64 `json:"elapsed"` 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)
}