Skip to content

Commit 00dc5d7

Browse files
committed
fix: remove EndpointCount from CollectionEntity
- Remove EndpointCount field from core CollectionEntity - Use simple GetCollections query without JOIN - Add GetEndpointCountsByCollections query - Update itemMapper to fetch counts only for display
1 parent ce6a779 commit 00dc5d7

9 files changed

Lines changed: 105 additions & 68 deletions

File tree

db/queries/collections.sql

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

99
-- name: GetCollections :many
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;
10+
SELECT * FROM collections
11+
ORDER BY created_at DESC;
2012

2113
-- name: CountCollections :one
2214
SELECT COUNT(*) FROM collections;

db/queries/endpoints.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ LIMIT ? OFFSET ?;
3131
SELECT COUNT(*) FROM endpoints
3232
WHERE collection_id = ?;
3333

34+
-- name: GetEndpointCountsByCollections :many
35+
SELECT collection_id, COUNT(*) as count
36+
FROM endpoints
37+
GROUP BY collection_id;
38+
3439
-- name: UpdateEndpointName :one
3540
UPDATE endpoints
3641
SET

internal/backend/collections/manager.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,17 @@ func (c *CollectionsManager) Delete(ctx context.Context, id int64) error {
9696
}
9797

9898
func (c *CollectionsManager) List(ctx context.Context) ([]CollectionEntity, error) {
99-
log.Debug("listing all collections without pagination")
10099
collections, err := c.DB.GetCollections(ctx)
101-
collectionsEntity := []CollectionEntity{}
102-
for _, collection := range collections {
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-
})
111-
}
112100
if err != nil {
113101
return nil, err
114102
}
115-
return collectionsEntity, nil
103+
104+
entities := make([]CollectionEntity, len(collections))
105+
for i, collection := range collections {
106+
entities[i] = CollectionEntity{Collection: collection}
107+
}
108+
109+
return entities, nil
116110
}
117111

118112
func (c *CollectionsManager) ListPaginated(ctx context.Context, limit, offset int) (*PaginatedCollections, error) {

internal/backend/collections/models.go

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

1010
type CollectionEntity struct {
1111
database.Collection
12-
EndpointCount int
1312
}
1413

1514
func (c CollectionEntity) GetID() int64 {
@@ -20,9 +19,6 @@ func (c CollectionEntity) GetName() string {
2019
return c.Name
2120
}
2221

23-
func (c CollectionEntity) GetEnpointCount() int {
24-
return c.EndpointCount
25-
}
2622

2723
func (c CollectionEntity) GetCreatedAt() time.Time {
2824
return crud.ParseTimestamp(c.CreatedAt)

internal/backend/database/collections.sql.go

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

internal/backend/database/endpoints.sql.go

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

internal/backend/endpoints/manager.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,12 @@ func (e *EndpointsManager) UpdateEndpoint(ctx context.Context, id int64, data En
256256
log.Info("updated endpoint", "id", endpoint.ID, "name", endpoint.Name)
257257
return EndpointEntity{Endpoint: endpoint}, nil
258258
}
259+
260+
func (e *EndpointsManager) GetCountsByCollections(ctx context.Context) ([]database.GetEndpointCountsByCollectionsRow, error) {
261+
counts, err := e.DB.GetEndpointCountsByCollections(ctx)
262+
if err != nil {
263+
log.Error("failed to get endpoint counts", "error", err)
264+
return nil, err
265+
}
266+
return counts, nil
267+
}

internal/tui/app/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func NewAppModel(ctx *Context) AppModel {
149149
keys: appKeybinds,
150150
}
151151
model.Views = map[ViewName]views.ViewInterface{
152-
Collections: views.NewCollectionsView(model.ctx.Collections, 1),
152+
Collections: views.NewCollectionsView(model.ctx.Collections, model.ctx.Endpoints, 1),
153153
Endpoints: views.NewEndpointsView(model.ctx.Endpoints, 2),
154154
}
155155
return model

internal/tui/views/collections-view.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ import (
1010
"github.com/charmbracelet/bubbles/list"
1111
tea "github.com/charmbracelet/bubbletea"
1212
"github.com/maniac-en/req/internal/backend/collections"
13+
"github.com/maniac-en/req/internal/backend/endpoints"
1314
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
1415
"github.com/maniac-en/req/internal/tui/keybinds"
1516
"github.com/maniac-en/req/internal/tui/messages"
1617
)
1718

1819
type CollectionsView struct {
19-
width int
20-
height int
21-
list optionsProvider.OptionsProvider[collections.CollectionEntity, string]
22-
manager *collections.CollectionsManager
23-
help help.Model
24-
keys *keybinds.ListKeyMap
25-
order int
20+
width int
21+
height int
22+
list optionsProvider.OptionsProvider[collections.CollectionEntity, string]
23+
manager *collections.CollectionsManager
24+
endpointsManager *endpoints.EndpointsManager
25+
help help.Model
26+
keys *keybinds.ListKeyMap
27+
order int
2628
}
2729

2830
func (c CollectionsView) Init() tea.Cmd {
@@ -85,33 +87,55 @@ func (c CollectionsView) OnBlur() {
8587

8688
}
8789

88-
func itemMapper(items []collections.CollectionEntity) []list.Item {
90+
func itemMapper(items []collections.CollectionEntity, endpointsManager *endpoints.EndpointsManager) []list.Item {
8991
opts := make([]list.Item, len(items))
92+
93+
counts, err := endpointsManager.GetCountsByCollections(context.Background())
94+
if err != nil {
95+
for i, item := range items {
96+
opts[i] = optionsProvider.Option{
97+
Name: item.GetName(),
98+
Subtext: "0 endpoints",
99+
ID: item.GetID(),
100+
}
101+
}
102+
return opts
103+
}
104+
105+
countMap := make(map[int64]int)
106+
for _, count := range counts {
107+
countMap[count.CollectionID] = int(count.Count)
108+
}
109+
90110
for i, item := range items {
91-
newOpt := optionsProvider.Option{
111+
count := countMap[item.GetID()]
112+
opts[i] = optionsProvider.Option{
92113
Name: item.GetName(),
93-
Subtext: fmt.Sprintf("%d endpoints", item.GetEnpointCount()),
114+
Subtext: fmt.Sprintf("%d endpoints", count),
94115
ID: item.GetID(),
95116
}
96-
opts[i] = newOpt
97117
}
118+
98119
return opts
99120
}
100121

101-
func NewCollectionsView(collManager *collections.CollectionsManager, order int) *CollectionsView {
122+
func NewCollectionsView(collManager *collections.CollectionsManager, endpointsManager *endpoints.EndpointsManager, order int) *CollectionsView {
102123
keybinds := keybinds.NewListKeyMap()
103124
config := defaultListConfig[collections.CollectionEntity, string](keybinds)
104125

105126
config.GetItemsFunc = collManager.List
106-
config.ItemMapper = itemMapper
127+
config.ItemMapper = func(items []collections.CollectionEntity) []list.Item {
128+
return itemMapper(items, endpointsManager)
129+
}
107130
config.AdditionalKeymaps = keybinds
108131
config.Source = "collections"
109132

110133
return &CollectionsView{
111-
list: optionsProvider.NewOptionsProvider(config),
112-
manager: collManager,
113-
help: help.New(),
114-
keys: keybinds,
115-
order: order,
134+
list: optionsProvider.NewOptionsProvider(config),
135+
manager: collManager,
136+
endpointsManager: endpointsManager,
137+
help: help.New(),
138+
keys: keybinds,
139+
order: order,
116140
}
117141
}

0 commit comments

Comments
 (0)