rename postgr fucntions
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
var dbConn *pgxpool.Pool
|
||||
|
||||
func PgInit(ctx context.Context) {
|
||||
func Init(ctx context.Context) {
|
||||
var err error
|
||||
dbConn, err = pgxpool.New(ctx, "postgres://master:secret@localhost:5432") // TODO change to env in production
|
||||
if err != nil {
|
||||
@@ -91,7 +91,7 @@ func PgInit(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func PgUserSave(ctx context.Context, user *types.User) error {
|
||||
func UserSave(ctx context.Context, user *types.User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns, rgba, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
@@ -101,14 +101,14 @@ func PgUserSave(ctx context.Context, user *types.User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func PgUserDelete(ctx context.Context, id uuid.UUID) error {
|
||||
func UserDelete(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM users WHERE id = $1
|
||||
`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgUserGetStandardInfoByName(ctx context.Context, user *types.User) error {
|
||||
func UserGetStandardInfoByName(ctx context.Context, user *types.User) error {
|
||||
var rgba int64
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT id, name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE name = $1
|
||||
@@ -119,7 +119,7 @@ func PgUserGetStandardInfoByName(ctx context.Context, user *types.User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func PgUserGetById(ctx context.Context, user *types.User) error {
|
||||
func UserGetById(ctx context.Context, user *types.User) error {
|
||||
var rgba int64
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE id = $1
|
||||
@@ -130,25 +130,25 @@ func PgUserGetById(ctx context.Context, user *types.User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func PgUserSetColor(ctx context.Context, user *types.User) error {
|
||||
func UserSetColor(ctx context.Context, user *types.User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET rgba = $1 WHERE id = $2
|
||||
`, convertions.RgbaToUint32(user.Color), user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgUserSetPronouns(ctx context.Context, user *types.User) error {
|
||||
func UserSetPronouns(ctx context.Context, user *types.User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET pronouns = $1 WHERE id = $2
|
||||
`, user.Pronouns, user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgGetWholeUser(ctx context.Context, user *types.User) error {
|
||||
if err := PgUserGetById(ctx, user); err != nil {
|
||||
func GetWholeUser(ctx context.Context, user *types.User) error {
|
||||
if err := UserGetById(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := PgConnectionsGetBelongingToUser(ctx, user); err != nil {
|
||||
if err := ConnectionsGetBelongingToUser(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -156,21 +156,21 @@ func PgGetWholeUser(ctx context.Context, user *types.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func PgConnectionSave(ctx context.Context, conn *types.Connection) error {
|
||||
func ConnectionSave(ctx context.Context, conn *types.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 PgConnectionDelete(ctx context.Context, conn *types.Connection) error {
|
||||
func ConnectionDelete(ctx context.Context, conn *types.Connection) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM user_connections WHERE id = $1
|
||||
`, conn.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgConnectionsGetBelongingToUser(ctx context.Context, user *types.User) error {
|
||||
func ConnectionsGetBelongingToUser(ctx context.Context, user *types.User) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT id, requestor_id, recipient_id, state, created_at
|
||||
FROM user_connections
|
||||
@@ -195,14 +195,14 @@ func PgConnectionsGetBelongingToUser(ctx context.Context, user *types.User) erro
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func PgConnectionUpdateState(ctx context.Context, conn *types.Connection) error {
|
||||
func ConnectionUpdateState(ctx context.Context, conn *types.Connection) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE user_connections SET state = $1 WHERE id = $2
|
||||
`, conn.State, conn.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgConnectionMessageSave(ctx context.Context, message *types.Message) error {
|
||||
func ConnectionMessageSave(ctx context.Context, message *types.Message) error {
|
||||
if message.Id != (uuid.UUID{}) {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
INSERT INTO direct_messages (id, sender_id, receiver_id, created_at, content) VALUES ($1, $2, $3, $4, $5)
|
||||
@@ -215,7 +215,7 @@ func PgConnectionMessageSave(ctx context.Context, message *types.Message) error
|
||||
`, message.Sender, message.Receiver, message.CreatedAt, message.Content).Scan(&message.Id)
|
||||
}
|
||||
|
||||
func PgConnectionGetMessagesBefore(ctx context.Context, before time.Time, connection uuid.UUID, cap uint32) ([]*types.Message, error) {
|
||||
func ConnectionGetMessagesBefore(ctx context.Context, before time.Time, connection uuid.UUID, cap uint32) ([]*types.Message, error) {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT id, sender_id, receiver_id, created_at, content
|
||||
FROM (
|
||||
@@ -244,34 +244,34 @@ func PgConnectionGetMessagesBefore(ctx context.Context, before time.Time, connec
|
||||
return messages, rows.Err()
|
||||
}
|
||||
|
||||
func PgFileMetadataSave(ctx context.Context, metadata *types.FileMetadata) error {
|
||||
func FileMetadataSave(ctx context.Context, metadata *types.FileMetadata) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
INSERT INTO files_metadata (id, uploader_id, name, hash, created_at) VALUES ($1, $2, $3, $4, $5)
|
||||
`, metadata.Id, metadata.UploaderId, metadata.Name, metadata.Hash, metadata.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgFileMetadataGet(ctx context.Context, metadata *types.FileMetadata) error {
|
||||
func FileMetadataGet(ctx context.Context, metadata *types.FileMetadata) error {
|
||||
return dbConn.QueryRow(ctx, `
|
||||
SELECT uploader_id, name, hash, created_at FROM files_metadata WHERE id = $1
|
||||
`, metadata.Id).Scan(&metadata.UploaderId, &metadata.Name, &metadata.Hash, &metadata.CreatedAt)
|
||||
}
|
||||
|
||||
func PgFileMetadataDelete(ctx context.Context, metadataId *uuid.UUID) error {
|
||||
func FileMetadataDelete(ctx context.Context, metadataId *uuid.UUID) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM files_metadata WHERE id = $1
|
||||
`, metadataId)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgFileConnectionsAdd(ctx context.Context, metadataId *uuid.UUID, connectionId *uuid.UUID) error {
|
||||
func FileConnectionsAdd(ctx context.Context, metadataId *uuid.UUID, connectionId *uuid.UUID) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
INSERT INTO file_connections (file_id, connection_id) VALUES ($1, $2)
|
||||
`, metadataId, connectionId)
|
||||
return err
|
||||
}
|
||||
|
||||
func PgFileConnectionsGet(ctx context.Context, metadata *types.FileMetadata) error {
|
||||
func FileConnectionsGet(ctx context.Context, metadata *types.FileMetadata) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT connection_id FROM file_connections WHERE file_id = $1
|
||||
`, metadata.Id)
|
||||
@@ -290,7 +290,7 @@ func PgFileConnectionsGet(ctx context.Context, metadata *types.FileMetadata) err
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func PgFileMetadataHashExists(ctx context.Context, hash *types.Sha256Hash) (bool, error) {
|
||||
func FileMetadataHashExists(ctx context.Context, hash *types.Sha256Hash) (bool, error) {
|
||||
var exists bool
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT EXISTS (SELECT FROM files_metadata WHERE hash = $1)
|
||||
|
||||
Reference in New Issue
Block a user