about summary refs log tree commit diff
path: root/src/rest-api/handler/db.go
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2024-05-27 21:30:54 +0200
committerBaitinq <manuelpalenzuelamerino@gmail.com>2024-05-27 23:52:44 +0200
commit81621c4f64f4a1c29906643f53314e2f71a014ae (patch)
tree503cc56b609dcad8f8eca6e64b6b9b15b02fd4fd /src/rest-api/handler/db.go
parentpayload-processor: fmt (diff)
downloadfs-tracer-backend-81621c4f64f4a1c29906643f53314e2f71a014ae.tar.gz
fs-tracer-backend-81621c4f64f4a1c29906643f53314e2f71a014ae.tar.bz2
fs-tracer-backend-81621c4f64f4a1c29906643f53314e2f71a014ae.zip
rest-api: connect to db and add /file/ GET endpoint
TODO: Only get files for your specific user
Diffstat (limited to 'src/rest-api/handler/db.go')
-rw-r--r--src/rest-api/handler/db.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/rest-api/handler/db.go b/src/rest-api/handler/db.go
new file mode 100644
index 0000000..0093c41
--- /dev/null
+++ b/src/rest-api/handler/db.go
@@ -0,0 +1,37 @@
+package handler
+
+import (
+	"context"
+
+	"github.com/Baitinq/fs-tracer-backend/lib"
+	"github.com/jmoiron/sqlx"
+)
+
+//go:generate mockgen -source=$GOFILE -package=$GOPACKAGE -destination=mock_$GOFILE
+type DB interface {
+	GetLatestFileByPath(ctx context.Context, path string) (*lib.File, error)
+}
+
+type DBImpl struct {
+	db *sqlx.DB
+}
+
+var _ DB = (*DBImpl)(nil)
+
+func NewDB(db *sqlx.DB) DB {
+	return &DBImpl{db: db}
+}
+
+func (db DBImpl) GetLatestFileByPath(ctx context.Context, path string) (*lib.File, error) {
+	var file lib.File
+	err := db.db.GetContext(ctx, &file, `
+		SELECT * FROM private.file
+		WHERE absolute_path = $1
+		ORDER BY timestamp DESC
+		LIMIT 1
+	`, path)
+	if err != nil {
+		return nil, err
+	}
+	return &file, nil
+}