diff --git a/pkg/database/mocks/Store.go b/pkg/database/mocks/Store.go index 8e33522..be3d593 100644 --- a/pkg/database/mocks/Store.go +++ b/pkg/database/mocks/Store.go @@ -1925,23 +1925,23 @@ func (_c *Store_GetShare_Call) RunAndReturn(run func(context.Context, string) (d } // GetSharesP provides a mock function with given fields: ctx, arg -func (_m *Store) GetSharesP(ctx context.Context, arg database.GetSharesPParams) ([]database.Share, error) { +func (_m *Store) GetSharesP(ctx context.Context, arg database.GetSharesPParams) ([]database.GetSharesPRow, error) { ret := _m.Called(ctx, arg) if len(ret) == 0 { panic("no return value specified for GetSharesP") } - var r0 []database.Share + var r0 []database.GetSharesPRow var r1 error - if rf, ok := ret.Get(0).(func(context.Context, database.GetSharesPParams) ([]database.Share, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context, database.GetSharesPParams) ([]database.GetSharesPRow, error)); ok { return rf(ctx, arg) } - if rf, ok := ret.Get(0).(func(context.Context, database.GetSharesPParams) []database.Share); ok { + if rf, ok := ret.Get(0).(func(context.Context, database.GetSharesPParams) []database.GetSharesPRow); ok { r0 = rf(ctx, arg) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]database.Share) + r0 = ret.Get(0).([]database.GetSharesPRow) } } @@ -1973,12 +1973,12 @@ func (_c *Store_GetSharesP_Call) Run(run func(ctx context.Context, arg database. return _c } -func (_c *Store_GetSharesP_Call) Return(_a0 []database.Share, _a1 error) *Store_GetSharesP_Call { +func (_c *Store_GetSharesP_Call) Return(_a0 []database.GetSharesPRow, _a1 error) *Store_GetSharesP_Call { _c.Call.Return(_a0, _a1) return _c } -func (_c *Store_GetSharesP_Call) RunAndReturn(run func(context.Context, database.GetSharesPParams) ([]database.Share, error)) *Store_GetSharesP_Call { +func (_c *Store_GetSharesP_Call) RunAndReturn(run func(context.Context, database.GetSharesPParams) ([]database.GetSharesPRow, error)) *Store_GetSharesP_Call { _c.Call.Return(run) return _c } diff --git a/pkg/database/querier.go b/pkg/database/querier.go index c519bed..36e68d7 100644 --- a/pkg/database/querier.go +++ b/pkg/database/querier.go @@ -42,7 +42,7 @@ type Querier interface { GetIncidentOwner(ctx context.Context, id uuid.UUID) (int, error) GetIncidentTalkgroups(ctx context.Context, incidentID uuid.UUID) ([]GetIncidentTalkgroupsRow, error) GetShare(ctx context.Context, id string) (Share, error) - GetSharesP(ctx context.Context, arg GetSharesPParams) ([]Share, error) + GetSharesP(ctx context.Context, arg GetSharesPParams) ([]GetSharesPRow, error) GetSharesPCount(ctx context.Context, owner *int32) (int64, error) GetSystemName(ctx context.Context, systemID int) (string, error) GetTalkgroup(ctx context.Context, systemID int32, tGID int32) (GetTalkgroupRow, error) diff --git a/pkg/database/share.sql.go b/pkg/database/share.sql.go index 20a77e1..081f6b6 100644 --- a/pkg/database/share.sql.go +++ b/pkg/database/share.sql.go @@ -55,14 +55,14 @@ func (q *Queries) DeleteShare(ctx context.Context, id string) error { const getShare = `-- name: GetShare :one SELECT - id, - entity_type, - entity_id, - entity_date, - owner, - expiration -FROM shares -WHERE id = $1 + s.id, + s.entity_type, + s.entity_id, + s.entity_date, + s.owner, + s.expiration +FROM shares s +WHERE s.id = $1 ` func (q *Queries) GetShare(ctx context.Context, id string) (Share, error) { @@ -81,13 +81,10 @@ func (q *Queries) GetShare(ctx context.Context, id string) (Share, error) { const getSharesP = `-- name: GetSharesP :many SELECT - s.id, - s.entity_type, - s.entity_id, - s.entity_date, - s.owner, - s.expiration + s.id, s.entity_type, s.entity_id, s.entity_date, s.owner, s.expiration, + u.username FROM shares s +JOIN users u ON (s.owner = u.id) WHERE CASE WHEN $1::INTEGER IS NOT NULL THEN s.owner = $1 ELSE TRUE END @@ -105,7 +102,12 @@ type GetSharesPParams struct { PerPage int32 `json:"perPage"` } -func (q *Queries) GetSharesP(ctx context.Context, arg GetSharesPParams) ([]Share, error) { +type GetSharesPRow struct { + Share Share `json:"share"` + Username string `json:"username"` +} + +func (q *Queries) GetSharesP(ctx context.Context, arg GetSharesPParams) ([]GetSharesPRow, error) { rows, err := q.db.Query(ctx, getSharesP, arg.Owner, arg.Direction, @@ -116,16 +118,17 @@ func (q *Queries) GetSharesP(ctx context.Context, arg GetSharesPParams) ([]Share return nil, err } defer rows.Close() - var items []Share + var items []GetSharesPRow for rows.Next() { - var i Share + var i GetSharesPRow if err := rows.Scan( - &i.ID, - &i.EntityType, - &i.EntityID, - &i.EntityDate, - &i.Owner, - &i.Expiration, + &i.Share.ID, + &i.Share.EntityType, + &i.Share.EntityID, + &i.Share.EntityDate, + &i.Share.Owner, + &i.Share.Expiration, + &i.Username, ); err != nil { return nil, err } diff --git a/pkg/shares/share.go b/pkg/shares/share.go index 3b75c50..67abc02 100644 --- a/pkg/shares/share.go +++ b/pkg/shares/share.go @@ -48,7 +48,8 @@ type Share struct { ID string `json:"id"` Type EntityType `json:"entityType"` Date *jsontypes.Time `json:"entityDate,omitempty"` // we handle this for the user - Owner users.UserID `json:"owner"` + Owner users.UserID `json:"-"` + OwnerUser *string `json:"owner,omitempty"` EntityID uuid.UUID `json:"entityID"` Expiration *jsontypes.Time `json:"expiration"` } diff --git a/pkg/shares/store.go b/pkg/shares/store.go index 85cae56..d0f1313 100644 --- a/pkg/shares/store.go +++ b/pkg/shares/store.go @@ -148,7 +148,9 @@ func (s *postgresStore) Shares(ctx context.Context, p SharesParams) (shares []*S shares = make([]*Share, 0, len(shs)) for _, v := range shs { - shares = append(shares, recToShare(v)) + s := recToShare(v.Share) + s.OwnerUser = &v.Username + shares = append(shares, s) } return shares, int(count), nil diff --git a/sql/postgres/queries/share.sql b/sql/postgres/queries/share.sql index c1e940b..08e9bce 100644 --- a/sql/postgres/queries/share.sql +++ b/sql/postgres/queries/share.sql @@ -1,13 +1,13 @@ -- name: GetShare :one SELECT - id, - entity_type, - entity_id, - entity_date, - owner, - expiration -FROM shares -WHERE id = @id; + s.id, + s.entity_type, + s.entity_id, + s.entity_date, + s.owner, + s.expiration +FROM shares s +WHERE s.id = @id; -- name: CreateShare :exec INSERT INTO shares ( @@ -27,13 +27,10 @@ DELETE FROM shares WHERE expiration < NOW(); -- name: GetSharesP :many SELECT - s.id, - s.entity_type, - s.entity_id, - s.entity_date, - s.owner, - s.expiration + sqlc.embed(s), + u.username FROM shares s +JOIN users u ON (s.owner = u.id) WHERE CASE WHEN sqlc.narg('owner')::INTEGER IS NOT NULL THEN s.owner = @owner ELSE TRUE END