ListSilosForUser

This commit is contained in:
Abhishek Shroff
2024-07-31 21:01:16 -07:00
parent a342a599fa
commit 45d23dfe47
5 changed files with 57 additions and 9 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/api/auth"
"github.com/shroff/phylum/server/internal/app"
)
@@ -22,7 +23,7 @@ func SetupSiloRoutes(r *gin.RouterGroup, a *app.App) {
func createSiloListRouteHandler(a *app.App) func(c *gin.Context) {
return func(c *gin.Context) {
silos, err := a.ListSilos(context.Background())
silos, err := a.SilosForUser(context.Background(), auth.GetUsername(c))
if err != nil {
panic(err)
}
+14 -2
View File
@@ -28,8 +28,20 @@ func (a App) CreateSilo(ctx context.Context, id uuid.UUID, owner, storage, name
})
}
func (a App) ListSilos(ctx context.Context) ([]core.Silo, error) {
silos, err := a.Db.Queries().ListSilos(ctx)
func (a App) AllSilos(ctx context.Context) ([]core.Silo, error) {
silos, err := a.Db.Queries().ListAllSilos(ctx)
if err != nil {
return nil, err
}
results := make([]core.Silo, len(silos))
for i, s := range silos {
results[i] = core.NewSilo(a.Db, s.Name, s.Owner, s.ID, a.FindStorageBackend(s.Storage))
}
return results, nil
}
func (a App) SilosForUser(ctx context.Context, username string) ([]core.Silo, error) {
silos, err := a.Db.Queries().ListSilosForUser(ctx, username)
if err != nil {
return nil, err
}
+1 -1
View File
@@ -58,7 +58,7 @@ func setupSiloListCommand() *cobra.Command {
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
silos, err := app.Default.ListSilos(ctx)
silos, err := app.Default.AllSilos(ctx)
if err != nil {
logrus.Fatal(err)
}
+35 -3
View File
@@ -45,12 +45,44 @@ func (q *Queries) DeleteSilo(ctx context.Context, id uuid.UUID) error {
return err
}
const listSilos = `-- name: ListSilos :many
const listAllSilos = `-- name: ListAllSilos :many
SELECT id, created, modified, deleted, owner, name, storage from silos
`
func (q *Queries) ListSilos(ctx context.Context) ([]Silo, error) {
rows, err := q.db.Query(ctx, listSilos)
func (q *Queries) ListAllSilos(ctx context.Context) ([]Silo, error) {
rows, err := q.db.Query(ctx, listAllSilos)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Silo
for rows.Next() {
var i Silo
if err := rows.Scan(
&i.ID,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Owner,
&i.Name,
&i.Storage,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listSilosForUser = `-- name: ListSilosForUser :many
SELECT id, created, modified, deleted, owner, name, storage from silos where owner = $1::text
`
func (q *Queries) ListSilosForUser(ctx context.Context, username string) ([]Silo, error) {
rows, err := q.db.Query(ctx, listSilosForUser, username)
if err != nil {
return nil, err
}
+5 -2
View File
@@ -14,5 +14,8 @@ INSERT INTO silos(
$1, NOW(), NOW(), $2, $3, $4
);
-- name: ListSilos :many
SELECT * from silos;
-- name: ListAllSilos :many
SELECT * from silos;
-- name: ListSilosForUser :many
SELECT * from silos where owner = @username::text;