Skip to content

Commit 23b2832

Browse files
committed
added endpoint count to collections and made it so that the db query returns endpoint counts
1 parent 2c438d9 commit 23b2832

5 files changed

Lines changed: 48 additions & 9 deletions

File tree

db/queries/collections.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ ORDER BY created_at DESC
77
LIMIT ? OFFSET ?;
88

99
-- name: GetCollections :many
10-
SELECT * FROM collections
11-
ORDER BY created_at DESC;
10+
SELECT
11+
c.id,
12+
c.name,
13+
c.created_at,
14+
c.updated_at,
15+
COUNT(e.id) AS endpoint_count
16+
FROM collections c
17+
LEFT JOIN endpoints e ON e.collection_id = c.id
18+
GROUP BY c.id, c.name, c.created_at, c.updated_at
19+
ORDER BY c.created_at DESC;
1220

1321
-- name: CountCollections :one
1422
SELECT COUNT(*) FROM collections;

internal/backend/collections/manager.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ func (c *CollectionsManager) List(ctx context.Context) ([]CollectionEntity, erro
100100
collections, err := c.DB.GetCollections(ctx)
101101
collectionsEntity := []CollectionEntity{}
102102
for _, collection := range collections {
103-
collectionsEntity = append(collectionsEntity, CollectionEntity{Collection: collection})
103+
collectionsEntity = append(collectionsEntity, CollectionEntity{Collection: database.Collection{
104+
ID: collection.ID,
105+
Name: collection.Name,
106+
CreatedAt: collection.CreatedAt,
107+
UpdatedAt: collection.UpdatedAt,
108+
},
109+
EndpointCount: int(collection.EndpointCount),
110+
})
104111
}
105112
if err != nil {
106113
return nil, err

internal/backend/collections/models.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
type CollectionEntity struct {
1111
database.Collection
12+
EndpointCount int
1213
}
1314

1415
func (c CollectionEntity) GetID() int64 {
@@ -19,6 +20,10 @@ func (c CollectionEntity) GetName() string {
1920
return c.Name
2021
}
2122

23+
func (c CollectionEntity) GetEnpointCount() int {
24+
return c.EndpointCount
25+
}
26+
2227
func (c CollectionEntity) GetCreatedAt() time.Time {
2328
return crud.ParseTimestamp(c.CreatedAt)
2429
}

internal/backend/database/collections.sql.go

Lines changed: 22 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/tui/views/collections-view.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package views
22

33
import (
4+
"fmt"
5+
46
"github.com/charmbracelet/bubbles/list"
57
tea "github.com/charmbracelet/bubbletea"
68
"github.com/maniac-en/req/internal/backend/collections"
@@ -63,7 +65,7 @@ func itemMapper(items []collections.CollectionEntity) []list.Item {
6365
for i, item := range items {
6466
newOpt := optionsProvider.Option{
6567
Name: item.GetName(),
66-
Subtext: "Sample",
68+
Subtext: fmt.Sprintf("%d endpoints", item.GetEnpointCount()),
6769
ID: item.GetID(),
6870
}
6971
opts[i] = newOpt

0 commit comments

Comments
 (0)