Browse Source

add key and user repositories.

master
Gisle Aune 4 years ago
parent
commit
f14386eceb
  1. 4
      database/postgres/db.go
  2. 82
      database/postgres/keys.go
  3. 14
      database/postgres/migrations/20210328163342_create_table_key.sql
  4. 13
      database/postgres/migrations/20210328163346_create_table_user.sql
  5. 15
      database/postgres/queries/keys.sql
  6. 11
      database/postgres/queries/user.sql
  7. 56
      database/postgres/user.go

4
database/postgres/db.go

@ -89,11 +89,11 @@ func (d *DB) Comments() repositories.CommentRepository {
} }
func (d *DB) Keys() repositories.KeyRepository { func (d *DB) Keys() repositories.KeyRepository {
panic("implement me")
return &keyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
} }
func (d *DB) Users() repositories.UserRepository { func (d *DB) Users() repositories.UserRepository {
panic("implement me")
return &userRepository{db: d.db}
} }
func (d *DB) Files() repositories.FileRepository { func (d *DB) Files() repositories.FileRepository {

82
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)
}

14
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

13
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

15
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;

11
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
);

56
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
}
Loading…
Cancel
Save