GetUser() works

This commit is contained in:
2022-11-18 23:12:37 +01:00
parent 5991631bfa
commit 55f53deadf
16 changed files with 411 additions and 38 deletions

View File

@@ -11,14 +11,14 @@ import (
"time"
)
//go:embed schema_1.0.ddl
var schema_1_0 string
//go:embed schema_1.ddl
var schema1 string
//go:embed schema_2.0.ddl
var schema_2_0 string
//go:embed schema_2.ddl
var schema2 string
//go:embed schema_3.0.ddl
var schema_3_0 string
//go:embed schema_3.ddl
var schema3 string
type Database struct {
db *sql.DB
@@ -40,7 +40,7 @@ func (db *Database) Migrate(ctx context.Context) error {
schema, err := db.ReadSchema(ctx)
if schema == 0 {
_, err = db.db.ExecContext(ctx, schema_3_0)
_, err = db.db.ExecContext(ctx, schema3)
if err != nil {
return err
}

View File

@@ -2,6 +2,7 @@ package db
import (
"blackforestbytes.com/simplecloudnotifier/api/models"
"database/sql"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"time"
)
@@ -110,3 +111,66 @@ func (db *Database) ClearProTokens(ctx TxContext, protoken string) error {
return nil
}
func (db *Database) GetUserByKey(ctx TxContext, key string) (*models.User, error) {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return nil, err
}
rows, err := tx.QueryContext(ctx, "SELECT * FROM users WHERE admin_key = ? OR send_key = ? OR read_key = ? LIMIT 1", key, key, key)
if err != nil {
return nil, err
}
user, err := models.DecodeUser(rows)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &user, nil
}
func (db *Database) GetChannelByKey(ctx TxContext, key string) (*models.Channel, error) {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return nil, err
}
rows, err := tx.QueryContext(ctx, "SELECT * FROM channels WHERE subscribe_key = ? OR send_key = ? LIMIT 1", key, key)
if err != nil {
return nil, err
}
channel, err := models.DecodeChannel(rows)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &channel, nil
}
func (db *Database) GetUser(ctx TxContext, userid int64) (models.User, error) {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return models.User{}, err
}
rows, err := tx.QueryContext(ctx, "SELECT * FROM users WHERE user_id = ? LIMIT 1", userid)
if err != nil {
return models.User{}, err
}
user, err := models.DecodeUser(rows)
if err != nil {
return models.User{}, err
}
return user, nil
}

View File

@@ -4,8 +4,8 @@ CREATE TABLE users
username TEXT NULL DEFAULT NULL,
read_key TEXT NOT NULL,
send_key TEXT NOT NULL,
read_key TEXT NOT NULL,
admin_key TEXT NOT NULL,
timestamp_created INTEGER NOT NULL,
@@ -51,21 +51,23 @@ CREATE TABLE channels
subscribe_key TEXT NOT NULL,
send_key TEXT NOT NULL,
messages_sent INTEGER NOT NULL DEFAULT '0',
timestamp_created INTEGER NOT NULL,
timestamp_lastread INTEGER NULL DEFAULT NULL,
timestamp_lastsent INTEGER NULL DEFAULT NULL
timestamp_lastsent INTEGER NULL DEFAULT NULL,
messages_sent INTEGER NOT NULL DEFAULT '0'
);
CREATE UNIQUE INDEX "idx_channels_identity" ON channels (owner_user_id, name);
CREATE TABLE subscriptions
(
subscription_id INTEGER PRIMARY KEY AUTOINCREMENT,
subscription_id INTEGER PRIMARY KEY AUTOINCREMENT,
subscriber_user_id INTEGER NOT NULL,
channel_owner_user_id INTEGER NOT NULL,
channel_name TEXT NOT NULL
subscriber_user_id INTEGER NOT NULL,
channel_owner_user_id INTEGER NOT NULL,
channel_name TEXT NOT NULL,
confirmed INTEGER CHECK(confirmed IN (0, 1)) NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX "idx_subscriptions_ref" ON subscriptions (subscriber_user_id, channel_owner_user_id, channel_name);