use uuid in connection, message. Add fetch connections function

This commit is contained in:
2026-04-07 20:06:06 +02:00
parent 9b57157769
commit 26fef0a777
4 changed files with 47 additions and 22 deletions
+37 -17
View File
@@ -2,8 +2,10 @@ package main
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
@@ -17,6 +19,8 @@ func DbInit(ctx context.Context) {
panic(err)
}
_, err = dbConn.Exec(ctx, `CREATE EXTENSION IF NOT EXISTS "gen_random_uuid";`)
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
@@ -35,11 +39,11 @@ func DbInit(ctx context.Context) {
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS user_connections (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
requestor_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
recipient_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
state TINYINT NOT NULL DEFAULT 0
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (requestor_id, recipient_id)
)
`)
if err != nil {
@@ -48,12 +52,16 @@ func DbInit(ctx context.Context) {
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sender_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
receiver_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
is_group_message BOOLEAN DEFAULT FALSE
)
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS chat_groups (
@@ -158,25 +166,37 @@ func DbGroupSetColor(ctx context.Context, group *Group) error {
return err
}
func DbConnectionSave(ctx context.Context, creationDate time.Time, requestorId uint32, recipientId uint32, isAccepted bool) error {
_, err := dbConn.Exec(ctx, `
INSERT INTO user_connections (created_at, requestor_id, recipient_id, is_accepted) VALUES ($1, $2, $3, $4)
`, creationDate, requestorId, recipientId, isAccepted)
return err
func DbConnectionSave(ctx context.Context, conn *Connection) error {
return dbConn.QueryRow(ctx, `
INSERT INTO user_connections (requestor_id, recipient_id, state, created_at) VALUES ($1, $2, $3, $4)
RETURNING id
`, conn.RequestorId, conn.RecipientId, conn.State, conn.CreatedAt).Scan(&conn.Id)
}
func DbConnectionAccept(ctx context.Context, requestorId uint32, recipientId uint32) error {
_, err := dbConn.Exec(ctx, `
UPDATE user_connections SET is_accepted = true WHERE requestor_id = $1 AND recipient_id = $2
`, requestorId, recipientId)
return err
}
func DbConnectionGetBelongingToUser(ctx context.Context, user *User) error {
rows, err := dbConn.Query(ctx, `
SELECT id, requestor_id, recipient_id, state, created_at
FROM connections
WHERE requestor_id = $1 OR recipient_id = $1
`, user.Id)
if err != nil {
return err
}
defer rows.Close()
func DbConnectionDelete(ctx context.Context, requestorId uint32, recipientId uint32) error {
_, err := dbConn.Exec(ctx, `
DELETE FROM user_connections WHERE requestor_id = $1 AND recipient_id = $2
`, requestorId, recipientId)
return err
if user.Connections == nil {
user.Connections = make(map[uuid.UUID]*Connection)
}
for rows.Next() {
conn := &Connection{}
if err = rows.Scan(&conn.Id, &conn.RequestorId, &conn.RecipientId, &conn.State, &conn.CreatedAt); err != nil {
return fmt.Errorf("scanning connection row: %w", err)
}
user.Connections[conn.Id] = conn
}
return rows.Err()
}
func DbGroupSave(ctx context.Context, group *Group) error {