rename database to postgressql
This commit is contained in:
@@ -10,7 +10,7 @@ func GetUserById(ctx context.Context, userId uuid.UUID) (*User, error) {
|
||||
user, err := CacheGetUserById(userId)
|
||||
if err != nil {
|
||||
user = &User{Id: userId}
|
||||
err = DbGetWholeUser(ctx, user)
|
||||
err = PgGetWholeUser(ctx, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ func HttpHandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
Event: message,
|
||||
})
|
||||
|
||||
err = DbConnectionMessageSave(ctx, message)
|
||||
err = PgConnectionMessageSave(ctx, message)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -133,7 +133,7 @@ func HttpHandleUserGetConnectionMessages(response http.ResponseWriter, request *
|
||||
if validBufCount > 0 {
|
||||
cutoff = buffer[0].CreatedAt
|
||||
}
|
||||
dbMessages, err := DbConnectionGetMessagesBefore(ctx, cutoff, connectionId, remaining)
|
||||
dbMessages, err := PgConnectionGetMessagesBefore(ctx, cutoff, connectionId, remaining)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -198,7 +198,7 @@ func HttpHandleUserNewConnection(response http.ResponseWriter, request *http.Req
|
||||
RecipientId: recipient.Id,
|
||||
State: ConnectionState.Stranger,
|
||||
}
|
||||
err = DbConnectionSave(ctx, connection)
|
||||
err = PgConnectionSave(ctx, connection)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -258,7 +258,7 @@ func HttpHandleUserDeleteConnection(response http.ResponseWriter, request *http.
|
||||
return
|
||||
}
|
||||
|
||||
err = DbConnectionDelete(ctx, conn)
|
||||
err = PgConnectionDelete(ctx, conn)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -309,7 +309,7 @@ func HttpHandleUserElevateConnection(response http.ResponseWriter, request *http
|
||||
return
|
||||
}
|
||||
|
||||
err = DbConnectionUpdateState(ctx, conn)
|
||||
err = PgConnectionUpdateState(ctx, conn)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
+6
-6
@@ -35,11 +35,11 @@ func HttpHandleUserNewToken(response http.ResponseWriter, request *http.Request)
|
||||
user, err = CacheGetUserByName(username)
|
||||
if err != nil {
|
||||
user = &User{Name: username}
|
||||
if err = DbUserGetStandardInfoByName(ctx, user); err != nil {
|
||||
if err = PgUserGetStandardInfoByName(ctx, user); err != nil {
|
||||
http.Error(response, "bad login", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if err = DbGetWholeUser(ctx, user); err != nil {
|
||||
if err = PgGetWholeUser(ctx, user); err != nil {
|
||||
http.Error(response, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func HttpHandleUserNew(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
err = DbUserSave(ctx, newUser)
|
||||
err = PgUserSave(ctx, newUser)
|
||||
if err != nil {
|
||||
http.Error(response, "name taken", http.StatusUnauthorized)
|
||||
return
|
||||
@@ -120,7 +120,7 @@ func HttpHandleUserDelete(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = DbUserDelete(ctx, userId)
|
||||
err = PgUserDelete(ctx, userId)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -149,7 +149,7 @@ func HttpHandleUserModifyAppearance(response http.ResponseWriter, request *http.
|
||||
return
|
||||
}
|
||||
user.Color = color
|
||||
err = DbUserSetColor(ctx, user)
|
||||
err = PgUserSetColor(ctx, user)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -177,7 +177,7 @@ func HttpHandleUserModifyAbout(response http.ResponseWriter, request *http.Reque
|
||||
}
|
||||
|
||||
user.Pronouns = pronouns
|
||||
err = DbUserSetPronouns(ctx, user)
|
||||
err = PgUserSetPronouns(ctx, user)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -15,7 +15,7 @@ func withCORS(h http.HandlerFunc) http.HandlerFunc {
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
DbInit(ctx)
|
||||
PgInit(ctx)
|
||||
|
||||
http.HandleFunc("/new/user", withCORS(HttpHandleUserNew))
|
||||
http.HandleFunc("/new/connection", withCORS(HttpHandleUserNewConnection))
|
||||
|
||||
+16
-16
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
var dbConn *pgxpool.Pool
|
||||
|
||||
func DbInit(ctx context.Context) {
|
||||
func PgInit(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 {
|
||||
@@ -65,7 +65,7 @@ func DbInit(ctx context.Context) {
|
||||
|
||||
}
|
||||
|
||||
func DbUserSave(ctx context.Context, user *User) error {
|
||||
func PgUserSave(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns, rgba, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
@@ -75,14 +75,14 @@ func DbUserSave(ctx context.Context, user *User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserDelete(ctx context.Context, id uuid.UUID) error {
|
||||
func PgUserDelete(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM users WHERE id = $1
|
||||
`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserGetStandardInfoByName(ctx context.Context, user *User) error {
|
||||
func PgUserGetStandardInfoByName(ctx context.Context, user *User) error {
|
||||
var rgba int64
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT id, name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE name = $1
|
||||
@@ -93,7 +93,7 @@ func DbUserGetStandardInfoByName(ctx context.Context, user *User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserGetById(ctx context.Context, user *User) error {
|
||||
func PgUserGetById(ctx context.Context, user *User) error {
|
||||
var rgba int64
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE id = $1
|
||||
@@ -104,25 +104,25 @@ func DbUserGetById(ctx context.Context, user *User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||
func PgUserSetColor(ctx context.Context, user *User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET rgba = $1 WHERE id = $2
|
||||
`, RgbaToUint32(user.Color), user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserSetPronouns(ctx context.Context, user *User) error {
|
||||
func PgUserSetPronouns(ctx context.Context, user *User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET pronouns = $1 WHERE id = $2
|
||||
`, user.Pronouns, user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetWholeUser(ctx context.Context, user *User) error {
|
||||
if err := DbUserGetById(ctx, user); err != nil {
|
||||
func PgGetWholeUser(ctx context.Context, user *User) error {
|
||||
if err := PgUserGetById(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := DbConnectionsGetBelongingToUser(ctx, user); err != nil {
|
||||
if err := PgConnectionsGetBelongingToUser(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -130,21 +130,21 @@ func DbGetWholeUser(ctx context.Context, user *User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DbConnectionSave(ctx context.Context, conn *Connection) error {
|
||||
func PgConnectionSave(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 DbConnectionDelete(ctx context.Context, conn *Connection) error {
|
||||
func PgConnectionDelete(ctx context.Context, conn *Connection) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM user_connections WHERE id = $1
|
||||
`, conn.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
|
||||
func PgConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT id, requestor_id, recipient_id, state, created_at
|
||||
FROM user_connections
|
||||
@@ -169,14 +169,14 @@ func DbConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbConnectionUpdateState(ctx context.Context, conn *Connection) error {
|
||||
func PgConnectionUpdateState(ctx context.Context, conn *Connection) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE user_connections SET state = $1 WHERE id = $2
|
||||
`, conn.State, conn.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbConnectionMessageSave(ctx context.Context, message *Message) error {
|
||||
func PgConnectionMessageSave(ctx context.Context, message *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)
|
||||
@@ -189,7 +189,7 @@ func DbConnectionMessageSave(ctx context.Context, message *Message) error {
|
||||
`, message.Sender, message.Receiver, message.CreatedAt, message.Content).Scan(&message.Id)
|
||||
}
|
||||
|
||||
func DbConnectionGetMessagesBefore(ctx context.Context, before time.Time, connection uuid.UUID, cap uint32) ([]*Message, error) {
|
||||
func PgConnectionGetMessagesBefore(ctx context.Context, before time.Time, connection uuid.UUID, cap uint32) ([]*Message, error) {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT id, sender_id, receiver_id, created_at, content
|
||||
FROM (
|
||||
Reference in New Issue
Block a user