Gisle Aune
4 years ago
7 changed files with 193 additions and 2 deletions
-
4database/postgres/db.go
-
82database/postgres/keys.go
-
14database/postgres/migrations/20210328163342_create_table_key.sql
-
13database/postgres/migrations/20210328163346_create_table_user.sql
-
15database/postgres/queries/keys.sql
-
11database/postgres/queries/user.sql
-
56database/postgres/user.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) |
|||
} |
@ -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 |
@ -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 |
@ -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; |
@ -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 |
|||
); |
@ -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 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue