Add count to pagination for talkgroups

This commit is contained in:
Daniel Ponte 2024-11-25 19:06:56 -05:00
parent 201a7d755e
commit 1ca2e884f7
6 changed files with 176 additions and 6 deletions

View file

@ -1244,6 +1244,63 @@ func (_c *Store_GetTalkgroupsWithLearnedBySystem_Call) RunAndReturn(run func(con
return _c return _c
} }
// GetTalkgroupsWithLearnedBySystemCount provides a mock function with given fields: ctx, system
func (_m *Store) GetTalkgroupsWithLearnedBySystemCount(ctx context.Context, system int32) (int64, error) {
ret := _m.Called(ctx, system)
if len(ret) == 0 {
panic("no return value specified for GetTalkgroupsWithLearnedBySystemCount")
}
var r0 int64
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, int32) (int64, error)); ok {
return rf(ctx, system)
}
if rf, ok := ret.Get(0).(func(context.Context, int32) int64); ok {
r0 = rf(ctx, system)
} else {
r0 = ret.Get(0).(int64)
}
if rf, ok := ret.Get(1).(func(context.Context, int32) error); ok {
r1 = rf(ctx, system)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Store_GetTalkgroupsWithLearnedBySystemCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTalkgroupsWithLearnedBySystemCount'
type Store_GetTalkgroupsWithLearnedBySystemCount_Call struct {
*mock.Call
}
// GetTalkgroupsWithLearnedBySystemCount is a helper method to define mock.On call
// - ctx context.Context
// - system int32
func (_e *Store_Expecter) GetTalkgroupsWithLearnedBySystemCount(ctx interface{}, system interface{}) *Store_GetTalkgroupsWithLearnedBySystemCount_Call {
return &Store_GetTalkgroupsWithLearnedBySystemCount_Call{Call: _e.mock.On("GetTalkgroupsWithLearnedBySystemCount", ctx, system)}
}
func (_c *Store_GetTalkgroupsWithLearnedBySystemCount_Call) Run(run func(ctx context.Context, system int32)) *Store_GetTalkgroupsWithLearnedBySystemCount_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(int32))
})
return _c
}
func (_c *Store_GetTalkgroupsWithLearnedBySystemCount_Call) Return(_a0 int64, _a1 error) *Store_GetTalkgroupsWithLearnedBySystemCount_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *Store_GetTalkgroupsWithLearnedBySystemCount_Call) RunAndReturn(run func(context.Context, int32) (int64, error)) *Store_GetTalkgroupsWithLearnedBySystemCount_Call {
_c.Call.Return(run)
return _c
}
// GetTalkgroupsWithLearnedBySystemP provides a mock function with given fields: ctx, system, offset, perPage // GetTalkgroupsWithLearnedBySystemP provides a mock function with given fields: ctx, system, offset, perPage
func (_m *Store) GetTalkgroupsWithLearnedBySystemP(ctx context.Context, system int32, offset int32, perPage int32) ([]database.GetTalkgroupsWithLearnedBySystemPRow, error) { func (_m *Store) GetTalkgroupsWithLearnedBySystemP(ctx context.Context, system int32, offset int32, perPage int32) ([]database.GetTalkgroupsWithLearnedBySystemPRow, error) {
ret := _m.Called(ctx, system, offset, perPage) ret := _m.Called(ctx, system, offset, perPage)
@ -1365,6 +1422,62 @@ func (_c *Store_GetTalkgroupsWithLearnedP_Call) RunAndReturn(run func(context.Co
return _c return _c
} }
// GetTalkgroupsWithLearnedPCount provides a mock function with given fields: ctx
func (_m *Store) GetTalkgroupsWithLearnedPCount(ctx context.Context) (int64, error) {
ret := _m.Called(ctx)
if len(ret) == 0 {
panic("no return value specified for GetTalkgroupsWithLearnedPCount")
}
var r0 int64
var r1 error
if rf, ok := ret.Get(0).(func(context.Context) (int64, error)); ok {
return rf(ctx)
}
if rf, ok := ret.Get(0).(func(context.Context) int64); ok {
r0 = rf(ctx)
} else {
r0 = ret.Get(0).(int64)
}
if rf, ok := ret.Get(1).(func(context.Context) error); ok {
r1 = rf(ctx)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Store_GetTalkgroupsWithLearnedPCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTalkgroupsWithLearnedPCount'
type Store_GetTalkgroupsWithLearnedPCount_Call struct {
*mock.Call
}
// GetTalkgroupsWithLearnedPCount is a helper method to define mock.On call
// - ctx context.Context
func (_e *Store_Expecter) GetTalkgroupsWithLearnedPCount(ctx interface{}) *Store_GetTalkgroupsWithLearnedPCount_Call {
return &Store_GetTalkgroupsWithLearnedPCount_Call{Call: _e.mock.On("GetTalkgroupsWithLearnedPCount", ctx)}
}
func (_c *Store_GetTalkgroupsWithLearnedPCount_Call) Run(run func(ctx context.Context)) *Store_GetTalkgroupsWithLearnedPCount_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context))
})
return _c
}
func (_c *Store_GetTalkgroupsWithLearnedPCount_Call) Return(_a0 int64, _a1 error) *Store_GetTalkgroupsWithLearnedPCount_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *Store_GetTalkgroupsWithLearnedPCount_Call) RunAndReturn(run func(context.Context) (int64, error)) *Store_GetTalkgroupsWithLearnedPCount_Call {
_c.Call.Return(run)
return _c
}
// GetUserByID provides a mock function with given fields: ctx, id // GetUserByID provides a mock function with given fields: ctx, id
func (_m *Store) GetUserByID(ctx context.Context, id int) (database.User, error) { func (_m *Store) GetUserByID(ctx context.Context, id int) (database.User, error) {
ret := _m.Called(ctx, id) ret := _m.Called(ctx, id)

View file

@ -30,8 +30,10 @@ type Querier interface {
GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error)
GetTalkgroupsWithLearned(ctx context.Context) ([]GetTalkgroupsWithLearnedRow, error) GetTalkgroupsWithLearned(ctx context.Context) ([]GetTalkgroupsWithLearnedRow, error)
GetTalkgroupsWithLearnedBySystem(ctx context.Context, system int32) ([]GetTalkgroupsWithLearnedBySystemRow, error) GetTalkgroupsWithLearnedBySystem(ctx context.Context, system int32) ([]GetTalkgroupsWithLearnedBySystemRow, error)
GetTalkgroupsWithLearnedBySystemCount(ctx context.Context, system int32) (int64, error)
GetTalkgroupsWithLearnedBySystemP(ctx context.Context, system int32, offset int32, perPage int32) ([]GetTalkgroupsWithLearnedBySystemPRow, error) GetTalkgroupsWithLearnedBySystemP(ctx context.Context, system int32, offset int32, perPage int32) ([]GetTalkgroupsWithLearnedBySystemPRow, error)
GetTalkgroupsWithLearnedP(ctx context.Context, offset int32, perPage int32) ([]GetTalkgroupsWithLearnedPRow, error) GetTalkgroupsWithLearnedP(ctx context.Context, offset int32, perPage int32) ([]GetTalkgroupsWithLearnedPRow, error)
GetTalkgroupsWithLearnedPCount(ctx context.Context) (int64, error)
GetUserByID(ctx context.Context, id int) (User, error) GetUserByID(ctx context.Context, id int) (User, error)
GetUserByUID(ctx context.Context, id int) (User, error) GetUserByUID(ctx context.Context, id int) (User, error)
GetUserByUsername(ctx context.Context, username string) (User, error) GetUserByUsername(ctx context.Context, username string) (User, error)

View file

@ -377,6 +377,18 @@ func (q *Queries) GetTalkgroupsWithLearnedBySystem(ctx context.Context, system i
return items, nil return items, nil
} }
const getTalkgroupsWithLearnedBySystemCount = `-- name: GetTalkgroupsWithLearnedBySystemCount :one
SELECT COUNT(*) FROM talkgroups tg
WHERE tg.system_id = $1
`
func (q *Queries) GetTalkgroupsWithLearnedBySystemCount(ctx context.Context, system int32) (int64, error) {
row := q.db.QueryRow(ctx, getTalkgroupsWithLearnedBySystemCount, system)
var count int64
err := row.Scan(&count)
return count, err
}
const getTalkgroupsWithLearnedBySystemP = `-- name: GetTalkgroupsWithLearnedBySystemP :many const getTalkgroupsWithLearnedBySystemP = `-- name: GetTalkgroupsWithLearnedBySystemP :many
SELECT SELECT
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, tg.learned, tg.ignored, sys.id, sys.name tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, tg.learned, tg.ignored, sys.id, sys.name
@ -483,6 +495,18 @@ func (q *Queries) GetTalkgroupsWithLearnedP(ctx context.Context, offset int32, p
return items, nil return items, nil
} }
const getTalkgroupsWithLearnedPCount = `-- name: GetTalkgroupsWithLearnedPCount :one
SELECT COUNT(*) FROM talkgroups tg
WHERE ignored IS NOT TRUE
`
func (q *Queries) GetTalkgroupsWithLearnedPCount(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, getTalkgroupsWithLearnedPCount)
var count int64
err := row.Scan(&count)
return count, err
}
const restoreTalkgroupVersion = `-- name: RestoreTalkgroupVersion :one const restoreTalkgroupVersion = `-- name: RestoreTalkgroupVersion :one
INSERT INTO talkgroups( INSERT INTO talkgroups(
system_id, system_id,

View file

@ -112,13 +112,16 @@ func (tga *talkgroupAPI) postPaginated(w http.ResponseWriter, r *http.Request) {
return return
} }
var res interface{} res := struct {
Talkgroups []*talkgroups.Talkgroup `json:"talkgroups"`
Count int `json:"count"`
}{}
switch { switch {
case p.System != nil: case p.System != nil:
res, err = tgs.SystemTGs(ctx, int32(*p.System), tgstore.WithPagination(input, DefaultPerPage)) res.Talkgroups, err = tgs.SystemTGs(ctx, int32(*p.System), tgstore.WithPagination(input, DefaultPerPage, &res.Count))
default: default:
// get all talkgroups // get all talkgroups
res, err = tgs.TGs(ctx, nil, tgstore.WithPagination(input, DefaultPerPage)) res.Talkgroups, err = tgs.TGs(ctx, nil, tgstore.WithPagination(input, DefaultPerPage, &res.Count))
} }
if err != nil { if err != nil {

View file

@ -66,6 +66,7 @@ type Store interface {
type options struct { type options struct {
pagination *Pagination pagination *Pagination
totalDest *int
perPageDefault int perPageDefault int
} }
@ -78,10 +79,11 @@ func sOpt(opts []option) (o options) {
type option func(*options) type option func(*options)
func WithPagination(p *Pagination, defPerPage int) option { func WithPagination(p *Pagination, defPerPage int, totalDest *int) option {
return func(o *options) { return func(o *options) {
o.pagination = p o.pagination = p
o.perPageDefault = defPerPage o.perPageDefault = defPerPage
o.totalDest = totalDest
} }
} }
@ -284,11 +286,30 @@ func (t *cache) TGs(ctx context.Context, tgs tgsp.IDs, opts ...option) ([]*tgsp.
if opt.pagination != nil { if opt.pagination != nil {
offset, perPage := opt.pagination.OffsetPerPage(opt.perPageDefault) offset, perPage := opt.pagination.OffsetPerPage(opt.perPageDefault)
tgRecords, err := db.GetTalkgroupsWithLearnedP(ctx, offset, perPage) var tgRecords []database.GetTalkgroupsWithLearnedPRow
var err error
err = db.InTx(ctx, func(db database.Store) error {
tgRecords, err = db.GetTalkgroupsWithLearnedP(ctx, offset, perPage)
if err != nil {
return err
}
if opt.totalDest != nil {
count, err := db.GetTalkgroupsWithLearnedPCount(ctx)
if err != nil {
return err
}
*opt.totalDest = int(count)
}
return nil
}, pgx.TxOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return addToRowListS(t, r, tgRecords), nil return addToRowListS(t, r, tgRecords), nil
} }

View file

@ -40,7 +40,10 @@ WHERE tg.system_id = @system
ORDER BY tg.system_id ASC, tg.tgid ASC ORDER BY tg.system_id ASC, tg.tgid ASC
OFFSET sqlc.arg('offset') ROWS OFFSET sqlc.arg('offset') ROWS
FETCH NEXT sqlc.arg('per_page') ROWS ONLY; FETCH NEXT sqlc.arg('per_page') ROWS ONLY;
;
-- name: GetTalkgroupsWithLearnedBySystemCount :one
SELECT COUNT(*) FROM talkgroups tg
WHERE tg.system_id = @system;
-- name: GetTalkgroupsWithLearnedBySystem :many -- name: GetTalkgroupsWithLearnedBySystem :many
SELECT SELECT
@ -66,6 +69,10 @@ ORDER BY tg.system_id ASC, tg.tgid ASC
OFFSET sqlc.arg('offset') ROWS OFFSET sqlc.arg('offset') ROWS
FETCH NEXT sqlc.arg('per_page') ROWS ONLY; FETCH NEXT sqlc.arg('per_page') ROWS ONLY;
-- name: GetTalkgroupsWithLearnedPCount :one
SELECT COUNT(*) FROM talkgroups tg
WHERE ignored IS NOT TRUE;
-- name: GetSystemName :one -- name: GetSystemName :one
SELECT name FROM systems WHERE id = @system_id; SELECT name FROM systems WHERE id = @system_id;