change colors to work in uint32 and Rgba type
This commit is contained in:
+19
-14
@@ -29,10 +29,8 @@ func DbInit(ctx context.Context) {
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
pass_hash TEXT NOT NULL,
|
||||
pronouns TEXT DEFAULT NULL,
|
||||
color_red SMALLINT DEFAULT NULL,
|
||||
color_green SMALLINT DEFAULT NULL,
|
||||
color_blue SMALLINT DEFAULT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
@@ -69,10 +67,10 @@ func DbInit(ctx context.Context) {
|
||||
|
||||
func DbUserSave(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
INSERT INTO users (name, pass_hash, pronouns, rgba, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id
|
||||
`, user.Name, user.PasswordHash, user.Pronouns, user.Color[0], user.Color[1], user.Color[2], user.CreatedAt).
|
||||
`, user.Name, user.PasswordHash, user.Pronouns, RgbaToUint32(user.Color), user.CreatedAt).
|
||||
Scan(&user.Id)
|
||||
return err
|
||||
}
|
||||
@@ -85,23 +83,31 @@ func DbUserDelete(ctx context.Context, id uuid.UUID) error {
|
||||
}
|
||||
|
||||
func DbUserGetStandardInfoByName(ctx context.Context, user *User) error {
|
||||
var rgba int64
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT id, name, pass_hash, COALESCE(pronouns, ''), color_red, color_green, color_blue, created_at FROM users WHERE name = $1
|
||||
`, user.Name).Scan(&user.Id, &user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
SELECT id, name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE name = $1
|
||||
`, user.Name).Scan(&user.Id, &user.Name, &user.PasswordHash, &user.Pronouns, &rgba, &user.CreatedAt)
|
||||
if err == nil {
|
||||
user.Color = Uint32ToRgba(uint32(rgba))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserGetById(ctx context.Context, user *User) error {
|
||||
var rgba int64
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT name, pass_hash, COALESCE(pronouns, ''), color_red, color_green, color_blue, created_at FROM users WHERE id = $1
|
||||
`, user.Id).Scan(&user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
SELECT name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE id = $1
|
||||
`, user.Id).Scan(&user.Name, &user.PasswordHash, &user.Pronouns, &rgba, &user.CreatedAt)
|
||||
if err == nil {
|
||||
user.Color = Uint32ToRgba(uint32(rgba))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||
`, user.Color[0], user.Color[1], user.Color[2], user.Id)
|
||||
UPDATE users SET rgba = $1 WHERE id = $2
|
||||
`, RgbaToUint32(user.Color), user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -211,4 +217,3 @@ func DbConnectionGetMessagesBefore(ctx context.Context, before time.Time, connec
|
||||
}
|
||||
return messages, rows.Err()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user