From 508e109734decf770b3ab72ec7333edd23b9b11c Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 14 May 2022 23:25:50 +0200 Subject: [PATCH] implemented get all shares --- internal/client/client.go | 4 ++++ internal/db/db.go | 1 + internal/db/sqlite.go | 22 ++++++++++++++++++++++ internal/web/web.go | 10 ++++++++++ 4 files changed, 37 insertions(+) diff --git a/internal/client/client.go b/internal/client/client.go index 842d153..4a78afb 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -104,3 +104,7 @@ func (c *Client) GetObjectMetadata(ctx context.Context, key string) (*types.Meta return metadata, nil } + +func (c *Client) GetAllShares(ctx context.Context) ([]*types.Share, error) { + return c.db.GetAllShares(ctx) +} diff --git a/internal/db/db.go b/internal/db/db.go index 3d1c3db..374732b 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -10,5 +10,6 @@ type DB interface { GetShare(ctx context.Context, slug string) (*types.Share, error) CreateShare(ctx context.Context, share *types.Share) error DeleteShare(ctx context.Context, slug string) error + GetAllShares(ctx context.Context) ([]*types.Share, error) Close() error } diff --git a/internal/db/sqlite.go b/internal/db/sqlite.go index 62f92b4..881b957 100644 --- a/internal/db/sqlite.go +++ b/internal/db/sqlite.go @@ -78,6 +78,28 @@ func (db *sqlLiteDB) DeleteShare(ctx context.Context, slug string) error { return nil } +func (db *sqlLiteDB) GetAllShares(ctx context.Context) ([]*types.Share, error) { + res, err := db.db.QueryContext(ctx, "SELECT slug, objKey FROM shares") + if err != nil { + return nil, err + } + + var shares []*types.Share + + for res.Next() { + var share types.Share + + err = res.Scan(&share.Slug, &share.Key) + if err != nil { + return nil, err + } + + shares = append(shares, &share) + } + + return shares, nil +} + func (db *sqlLiteDB) Close() error { return db.db.Close() } diff --git a/internal/web/web.go b/internal/web/web.go index a227152..6e448a7 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -62,6 +62,16 @@ func StartWebserver(addr string, client client.Client, username, password string return } + shares, err := client.GetAllShares(r.Context()) + + if err != nil { + logrus.Error(err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(shares) }).Methods("GET") r.HandleFunc("/api/share", func(w http.ResponseWriter, r *http.Request) {