Skip to content

Commit bffb049

Browse files
committed
changes:
- new sqlc func for get all collections - some bug fixes in the list config - changed existing list collections func to not use pagination - added a generic to list config for crud - defined the crud struct - added a crud struct to the collections view which is supplied to the list component
1 parent 03c2674 commit bffb049

7 files changed

Lines changed: 75 additions & 15 deletions

File tree

db/queries/collections.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ SELECT * FROM collections
66
ORDER BY created_at DESC
77
LIMIT ? OFFSET ?;
88

9+
-- name: GetCollections :many
10+
SELECT * FROM collections
11+
ORDER BY created_at DESC;
12+
913
-- name: CountCollections :one
1014
SELECT COUNT(*) FROM collections;
1115

internal/backend/collections/manager.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,16 @@ 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 with default pagination")
100-
paginated, err := c.ListPaginated(ctx, 50, 0)
99+
log.Debug("listing all collections without pagination")
100+
collections, err := c.DB.GetCollections(ctx)
101+
collectionsEntity := []CollectionEntity{}
102+
for _, collection := range collections {
103+
collectionsEntity = append(collectionsEntity, CollectionEntity{Collection: collection})
104+
}
101105
if err != nil {
102106
return nil, err
103107
}
104-
return paginated.Collections, nil
108+
return collectionsEntity, nil
105109
}
106110

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

internal/backend/database/collections.sql.go

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

internal/tui/components/OptionsProvider/component.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package optionsProvider
33
import (
44
"github.com/charmbracelet/bubbles/list"
55
tea "github.com/charmbracelet/bubbletea"
6-
"github.com/charmbracelet/lipgloss"
76
)
87

98
type OptionsProvider struct {
@@ -33,12 +32,13 @@ func (o OptionsProvider) Update(msg tea.Msg) (OptionsProvider, tea.Cmd) {
3332
case tea.WindowSizeMsg:
3433
o.height = msg.Height
3534
o.width = msg.Width
35+
o.list.SetSize(o.list.Width(), o.height)
3636
}
3737
return o, nil
3838
}
3939

4040
func (o OptionsProvider) View() string {
41-
return lipgloss.NewStyle().Height(o.height).Width(o.width).Align(lipgloss.Center, lipgloss.Center).Render("Hello world from select")
41+
return o.list.View()
4242
}
4343

4444
func (o OptionsProvider) OnFocus() {
@@ -49,14 +49,15 @@ func (o OptionsProvider) OnBlur() {
4949

5050
}
5151

52-
func initList[T any](config *ListConfig[T]) list.Model {
52+
func initList[T, C any](config *ListConfig[T, C]) list.Model {
5353

5454
// items := config.ItemMapper(config.Items)
5555

56-
list := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
56+
list := list.New([]list.Item{}, list.NewDefaultDelegate(), 30, 0)
5757

5858
// list configuration
5959
list.SetFilteringEnabled(config.FilteringEnabled)
60+
list.SetShowStatusBar(config.ShowStatusBar)
6061
list.SetShowPagination(config.ShowPagination)
6162
list.SetShowHelp(config.ShowHelp)
6263
list.SetShowTitle(config.ShowTitle)
@@ -66,7 +67,7 @@ func initList[T any](config *ListConfig[T]) list.Model {
6667
return list
6768
}
6869

69-
func NewOptionsProvider[T any](config *ListConfig[T]) OptionsProvider {
70+
func NewOptionsProvider[T, C any](config *ListConfig[T, C]) OptionsProvider {
7071
return OptionsProvider{
7172
list: initList(config),
7273
// onSelectAction: config.OnSelectAction,

internal/tui/components/OptionsProvider/config.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package optionsProvider
22

33
import (
4+
"context"
5+
46
"github.com/charmbracelet/bubbles/list"
57
tea "github.com/charmbracelet/bubbletea"
68
)
79

8-
type ListConfig[T any] struct {
9-
Items []T
10+
type ListConfig[T, C any] struct {
1011
OnSelectAction tea.Msg
1112

1213
ShowPagination bool
14+
ShowStatusBar bool
1315
ShowHelp bool
1416
ShowTitle bool
1517
Width, Height int
@@ -21,6 +23,14 @@ type ListConfig[T any] struct {
2123

2224
ItemMapper func([]T) []list.Item
2325

24-
// CrudOps Crud
26+
CrudOps Crud[C]
2527
// Style lipgloss.Style
2628
}
29+
30+
type Crud[T any] struct {
31+
Create func(context.Context, string) (T, error)
32+
Read func(context.Context, int64) (T, error)
33+
Update func(context.Context, int64, string) (T, error)
34+
Delete func(context.Context, int64) error
35+
List func(context.Context) ([]T, error)
36+
}

internal/tui/views/collections-view-helper.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
55
)
66

7-
func defaultListConfig[T any]() *optionsProvider.ListConfig[T] {
8-
config := optionsProvider.ListConfig[T]{
7+
func defaultListConfig[T, C any]() *optionsProvider.ListConfig[T, C] {
8+
config := optionsProvider.ListConfig[T, C]{
99
ShowPagination: false,
10+
ShowStatusBar: false,
1011
ShowHelp: false,
1112
ShowTitle: false,
12-
FilteringEnabled: false,
13+
FilteringEnabled: true,
1314
}
1415
return &config
1516
}

internal/tui/views/collections-view.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ func (c CollectionsView) OnBlur() {
5454
}
5555

5656
func NewCollectionsView(collManager *collections.CollectionsManager) *CollectionsView {
57-
config := defaultListConfig[string]()
57+
config := defaultListConfig[string, collections.CollectionEntity]()
58+
config.CrudOps = optionsProvider.Crud[collections.CollectionEntity]{
59+
Create: collManager.Create,
60+
Read: collManager.Read,
61+
Update: collManager.Update,
62+
Delete: collManager.Delete,
63+
List: collManager.List,
64+
}
5865
return &CollectionsView{
5966
list: optionsProvider.NewOptionsProvider(config),
6067
}

0 commit comments

Comments
 (0)