change user type from int to uuid string

This commit is contained in:
2025-03-27 20:33:48 -04:00
parent 732fbacc61
commit 9d7ad260f2
5 changed files with 35 additions and 29 deletions

View File

@@ -8,7 +8,7 @@ import (
"git.dubyatp.xyz/chat-api-server/db"
)
func dbGetUser(id int64) (*User, error) {
func dbGetUser(id string) (*User, error) {
data := db.ExecDB("users")
if data == nil {
return nil, errors.New("failed to load users database")
@@ -17,9 +17,9 @@ func dbGetUser(id int64) (*User, error) {
users := data["users"].([]interface{})
for _, u := range users {
user := u.(map[string]interface{})
if int64(user["ID"].(float64)) == id {
if user["ID"].(string) == id {
return &User{
ID: int64(user["ID"].(float64)),
ID: user["ID"].(string),
Name: user["Name"].(string),
}, nil
}
@@ -52,7 +52,7 @@ func dbGetMessage(id string) (*Message, error) {
}
return &Message{
ID: message["ID"].(string),
UserID: int64(message["UserID"].(float64)),
UserID: message["UserID"].(string),
Body: message["Body"].(string),
Timestamp: timestamp,
Edited: edited,
@@ -88,7 +88,7 @@ func dbGetAllMessages() ([]*Message, error) {
}
result = append(result, &Message{
ID: message["ID"].(string),
UserID: int64(message["UserID"].(float64)),
UserID: message["UserID"].(string),
Body: message["Body"].(string),
Timestamp: timestamp,
Edited: edited,

View File

@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
@@ -110,15 +109,14 @@ func newMessageID() string {
}
func NewMessage(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20)
err := r.ParseMultipartForm(64 << 10)
if err != nil {
http.Error(w, "Unable to parse form", http.StatusBadRequest)
return
}
userIDStr := r.FormValue("user_id")
userID, err := strconv.ParseInt(userIDStr, 10, 64)
if err != nil {
userID := r.FormValue("user_id")
if userID == "" {
http.Error(w, "Invalid user ID", http.StatusBadRequest)
return
}
@@ -150,7 +148,7 @@ type messageKey struct{}
type Message struct {
ID string `json:"id"`
UserID int64 `json:"user_id"`
UserID string `json:"user_id"`
Body string `json:"body"`
Timestamp time.Time `json:"timestamp"`
Edited time.Time `json:"edited"`
@@ -175,7 +173,7 @@ type MessageResponse struct {
func (m MessageResponse) MarshalJSON() ([]byte, error) {
type OrderedMessageResponse struct {
ID string `json:"id"`
UserID int64 `json:"user_id"`
UserID string `json:"user_id"`
Body string `json:"body"`
Timestamp string `json:"timestamp"`
Edited *string `json:"edited,omitempty"` // Use a pointer to allow null values

View File

@@ -1,7 +1,15 @@
package api
import (
"github.com/google/uuid"
)
func newUserID() string {
return "user_" + uuid.New().String()
}
type User struct {
ID int64 `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
}