-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59cea8f
commit 0290919
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package persistence | ||
|
||
import "github.com/google/uuid" | ||
|
||
type ApiKey struct { | ||
Id uuid.UUID | ||
Key uuid.UUID | ||
ApiUser uuid.UUID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package repositories | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/KnoblauchPilze/user-service/pkg/db" | ||
"github.com/KnoblauchPilze/user-service/pkg/errors" | ||
"github.com/KnoblauchPilze/user-service/pkg/persistence" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type ApiKeyRepository interface { | ||
Create(ctx context.Context, apiKey persistence.ApiKey) error | ||
Get(ctx context.Context, id uuid.UUID) (persistence.ApiKey, error) | ||
List(ctx context.Context) ([]uuid.UUID, error) | ||
Delete(ctx context.Context, id uuid.UUID) error | ||
} | ||
|
||
type apiUserRepositoryImpl struct { | ||
conn db.Connection | ||
} | ||
|
||
func NewApiKeyRepository(conn db.Connection) ApiKeyRepository { | ||
return &apiUserRepositoryImpl{ | ||
conn: conn, | ||
} | ||
} | ||
|
||
const createApiKeySqlTemplate = "INSERT INTO api_key (id, key, api_user) VALUES($1, $2, $3)" | ||
|
||
func (r *apiUserRepositoryImpl) Create(ctx context.Context, apiKey persistence.ApiKey) error { | ||
_, err := r.conn.Exec(ctx, createApiKeySqlTemplate, apiKey.Id, apiKey.Key, apiKey.ApiUser) | ||
return err | ||
} | ||
|
||
const getApiKeySqlTemplate = "SELECT id, key, api_user FROM api_key WHERE id = $1" | ||
|
||
func (r *apiUserRepositoryImpl) Get(ctx context.Context, id uuid.UUID) (persistence.ApiKey, error) { | ||
res := r.conn.Query(ctx, getApiKeySqlTemplate, id) | ||
if err := res.Err(); err != nil { | ||
return persistence.ApiKey{}, err | ||
} | ||
|
||
var out persistence.ApiKey | ||
parser := func(rows db.Scannable) error { | ||
return rows.Scan(&out.Id, &out.Key, &out.ApiUser) | ||
} | ||
|
||
if err := res.GetSingleValue(parser); err != nil { | ||
return persistence.ApiKey{}, err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
const listApiKeySqlTemplate = "SELECT id FROM api_key" | ||
|
||
func (r *apiUserRepositoryImpl) List(ctx context.Context) ([]uuid.UUID, error) { | ||
res := r.conn.Query(ctx, listApiKeySqlTemplate) | ||
if err := res.Err(); err != nil { | ||
return []uuid.UUID{}, err | ||
} | ||
|
||
var out []uuid.UUID | ||
parser := func(rows db.Scannable) error { | ||
var id uuid.UUID | ||
err := rows.Scan(&id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out = append(out, id) | ||
return nil | ||
} | ||
|
||
if err := res.GetAll(parser); err != nil { | ||
return []uuid.UUID{}, err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
const deleteApiKeySqlTemplate = "DELETE FROM api_key WHERE id = $1" | ||
|
||
func (r *apiUserRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error { | ||
affected, err := r.conn.Exec(ctx, deleteApiKeySqlTemplate, id) | ||
if err != nil { | ||
return err | ||
} | ||
if affected != 1 { | ||
return errors.NewCode(db.NoMatchingSqlRows) | ||
} | ||
return nil | ||
} |