From f14386eceb19acfc142d58aa80be9a42c171fe7a Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 28 Mar 2021 21:08:45 +0200 Subject: [PATCH] add key and user repositories. --- database/postgres/db.go | 4 +- database/postgres/keys.go | 82 +++++++++++++++++++ .../20210328163342_create_table_key.sql | 14 ++++ .../20210328163346_create_table_user.sql | 13 +++ database/postgres/queries/keys.sql | 15 ++++ database/postgres/queries/user.sql | 11 +++ database/postgres/user.go | 56 +++++++++++++ 7 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 database/postgres/keys.go create mode 100644 database/postgres/migrations/20210328163342_create_table_key.sql create mode 100644 database/postgres/migrations/20210328163346_create_table_user.sql create mode 100644 database/postgres/queries/keys.sql create mode 100644 database/postgres/queries/user.sql create mode 100644 database/postgres/user.go diff --git a/database/postgres/db.go b/database/postgres/db.go index 6096c42..0d0e73e 100644 --- a/database/postgres/db.go +++ b/database/postgres/db.go @@ -89,11 +89,11 @@ func (d *DB) Comments() repositories.CommentRepository { } func (d *DB) Keys() repositories.KeyRepository { - panic("implement me") + return &keyRepository{insertWithIDs: d.insertWithIDs, db: d.db} } func (d *DB) Users() repositories.UserRepository { - panic("implement me") + return &userRepository{db: d.db} } func (d *DB) Files() repositories.FileRepository { diff --git a/database/postgres/keys.go b/database/postgres/keys.go new file mode 100644 index 0000000..e552b76 --- /dev/null +++ b/database/postgres/keys.go @@ -0,0 +1,82 @@ +package postgres + +import ( + "context" + "database/sql" + "git.aiterp.net/rpdata/api/database/postgres/psqlcore" + "git.aiterp.net/rpdata/api/internal/generate" + "git.aiterp.net/rpdata/api/models" +) + +type keyRepository struct { + db *sql.DB + insertWithIDs bool +} + +func (r *keyRepository) Find(ctx context.Context, id string) (*models.Key, error) { + key, err := psqlcore.New(r.db).SelectKey(ctx, id) + if err != nil { + return nil, err + } + + return &models.Key{ + ID: key.ID, + Name: key.Name, + User: key.User, + Secret: key.Secret, + }, nil +} + +func (r *keyRepository) List(ctx context.Context, filter models.KeyFilter) ([]*models.Key, error) { + var keys []psqlcore.Key + + if filter.UserID != nil { + keys2, err := psqlcore.New(r.db).SelectKeysByUser(ctx, *filter.UserID) + if err != nil { + return nil, err + } + + keys = keys2 + } else { + keys2, err := psqlcore.New(r.db).SelectKeys(ctx) + if err != nil { + return nil, err + } + + keys = keys2 + } + + results := make([]*models.Key, 0, len(keys)) + for _, key := range keys { + results = append(results, &models.Key{ + ID: key.ID, + Name: key.Name, + User: key.User, + Secret: key.Secret, + }) + } + + return results, nil +} + +func (r *keyRepository) Insert(ctx context.Context, key models.Key) (*models.Key, error) { + if !r.insertWithIDs || len(key.ID) < 4 { + key.ID = generate.KeyID() + } + + err := psqlcore.New(r.db).InsertKey(ctx, psqlcore.InsertKeyParams{ + ID: key.ID, + Name: key.Name, + UserID: key.User, + Secret: key.Secret, + }) + if err != nil { + return nil, err + } + + return &key, nil +} + +func (r *keyRepository) Delete(ctx context.Context, key models.Key) error { + return psqlcore.New(r.db).DeleteKey(ctx, key.ID) +} diff --git a/database/postgres/migrations/20210328163342_create_table_key.sql b/database/postgres/migrations/20210328163342_create_table_key.sql new file mode 100644 index 0000000..95a80ed --- /dev/null +++ b/database/postgres/migrations/20210328163342_create_table_key.sql @@ -0,0 +1,14 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE key ( + id TEXT NOT NULL PRIMARY KEY, + name TEXT NOT NULL, + "user" TEXT NOT NULL, + secret TEXT NOT NULL +); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE key; +-- +goose StatementEnd diff --git a/database/postgres/migrations/20210328163346_create_table_user.sql b/database/postgres/migrations/20210328163346_create_table_user.sql new file mode 100644 index 0000000..f7e7fc7 --- /dev/null +++ b/database/postgres/migrations/20210328163346_create_table_user.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE "user" ( + id TEXT NOT NULL PRIMARY KEY, + nick TEXT NOT NULL, + permissions TEXT[] NOT NULL +); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE "user"; +-- +goose StatementEnd diff --git a/database/postgres/queries/keys.sql b/database/postgres/queries/keys.sql new file mode 100644 index 0000000..32786bb --- /dev/null +++ b/database/postgres/queries/keys.sql @@ -0,0 +1,15 @@ +-- name: SelectKey :one +SELECT * FROM "key" WHERE id=$1 LIMIT 1; + +-- name: SelectKeys :many +SELECT * FROM "key"; + +-- name: SelectKeysByUser :many +SELECT * FROM "key" WHERE "user" = $1; + +-- name: InsertKey :exec +INSERT INTO key (id, name, "user", secret) +VALUES (@id, @name, @user_id, @secret); + +-- name: DeleteKey :exec +DELETE FROM "key" WHERE id = $1; \ No newline at end of file diff --git a/database/postgres/queries/user.sql b/database/postgres/queries/user.sql new file mode 100644 index 0000000..2bc92a2 --- /dev/null +++ b/database/postgres/queries/user.sql @@ -0,0 +1,11 @@ +-- name: SelectUser :one +SELECT * FROM "user" WHERE id=$1 LIMIT 1; + +-- name: SelectUsers :many +SELECT * FROM "user"; + +-- name: InsertUser :exec +INSERT INTO "user" (id, nick, permissions) +VALUES ( + @id, @nick, @permissions +); diff --git a/database/postgres/user.go b/database/postgres/user.go new file mode 100644 index 0000000..9e68350 --- /dev/null +++ b/database/postgres/user.go @@ -0,0 +1,56 @@ +package postgres + +import ( + "context" + "database/sql" + "git.aiterp.net/rpdata/api/database/postgres/psqlcore" + "git.aiterp.net/rpdata/api/models" +) + +type userRepository struct { + db *sql.DB +} + +func (r *userRepository) Find(ctx context.Context, id string) (*models.User, error) { + user, err := psqlcore.New(r.db).SelectUser(ctx, id) + if err != nil { + return nil, err + } + + return &models.User{ + ID: user.ID, + Nick: user.Nick, + Permissions: user.Permissions, + }, nil +} + +func (r *userRepository) List(ctx context.Context) ([]*models.User, error) { + users, err := psqlcore.New(r.db).SelectUsers(ctx) + if err != nil { + return nil, err + } + + results := make([]*models.User, 0, len(users)) + for _, user := range users { + results = append(results, &models.User{ + ID: user.ID, + Nick: user.Nick, + Permissions: user.Permissions, + }) + } + + return results, nil +} + +func (r *userRepository) Insert(ctx context.Context, user models.User) (*models.User, error) { + err := psqlcore.New(r.db).InsertUser(ctx, psqlcore.InsertUserParams{ + ID: user.ID, + Nick: user.Nick, + Permissions: user.Permissions, + }) + if err != nil { + return nil, err + } + + return &user, nil +}