about summary refs log tree commit diff
path: root/src/rest-api/handler/db.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest-api/handler/db.go')
-rw-r--r--src/rest-api/handler/db.go29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/rest-api/handler/db.go b/src/rest-api/handler/db.go
index 0093c41..49d8e35 100644
--- a/src/rest-api/handler/db.go
+++ b/src/rest-api/handler/db.go
@@ -9,7 +9,8 @@ import (
 
 //go:generate mockgen -source=$GOFILE -package=$GOPACKAGE -destination=mock_$GOFILE
 type DB interface {
-	GetLatestFileByPath(ctx context.Context, path string) (*lib.File, error)
+	GetLatestFileByPath(ctx context.Context, path string, user_id string) (*lib.File, error)
+	GetUserIDByAPIKey(ctx context.Context, apiKey string) (string, error)
 }
 
 type DBImpl struct {
@@ -22,16 +23,36 @@ func NewDB(db *sqlx.DB) DB {
 	return &DBImpl{db: db}
 }
 
-func (db DBImpl) GetLatestFileByPath(ctx context.Context, path string) (*lib.File, error) {
+func (db DBImpl) GetLatestFileByPath(ctx context.Context, path string, user_id string) (*lib.File, error) {
 	var file lib.File
 	err := db.db.GetContext(ctx, &file, `
 		SELECT * FROM private.file
-		WHERE absolute_path = $1
+		WHERE
+			user_id = $1
+			AND absolute_path = $2
 		ORDER BY timestamp DESC
 		LIMIT 1
-	`, path)
+	`, user_id, path)
 	if err != nil {
 		return nil, err
 	}
 	return &file, nil
 }
+
+// TODO: Add test
+func (db DBImpl) GetUserIDByAPIKey(ctx context.Context, apiKey string) (string, error) {
+	if len(apiKey) != 44 {
+		return "", nil
+	}
+
+	var userID string
+	err := db.db.GetContext(ctx, &userID, `
+		SELECT id FROM private.api_keys
+		WHERE api_key = $1
+		LIMIT 1
+	`, apiKey)
+	if err != nil {
+		return "", err
+	}
+	return userID, nil
+}