Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user/favorite: use sqlc for user favorites #3288

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/initstores.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (app *App) initStores(ctx context.Context) error {
}

if app.FavoriteStore == nil {
app.FavoriteStore, err = favorite.NewStore(ctx, app.db)
app.FavoriteStore, err = favorite.NewStore(ctx)
}
if err != nil {
return errors.Wrap(err, "init favorite store")
Expand Down
135 changes: 135 additions & 0 deletions gadb/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion graphql2/graphqlapp/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (q *Query) Alert(ctx context.Context, alertID int) (*alert.Alert, error) {
* Merges favorites and user-specified serviceIDs in opts.FilterByServiceID
*/
func (q *Query) mergeFavorites(ctx context.Context, svcs []string) ([]string, error) {
targets, err := q.FavoriteStore.FindAll(ctx, permission.UserID(ctx), []assignment.TargetType{assignment.TargetTypeService})
targets, err := q.FavoriteStore.FindAll(ctx, q.DB, permission.UserID(ctx), []assignment.TargetType{assignment.TargetTypeService})
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion graphql2/graphqlapp/escalationpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (m *Mutation) CreateEscalationPolicy(ctx context.Context, input graphql2.Cr
return err
}
if input.Favorite != nil && *input.Favorite {
err = m.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.EscalationPolicyTarget(pol.ID))
err = m.FavoriteStore.Set(ctx, tx, permission.UserID(ctx), assignment.EscalationPolicyTarget(pol.ID))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions graphql2/graphqlapp/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func (a *App) Mutation() graphql2.MutationResolver { return (*Mutation)(a) }
func (a *Mutation) SetFavorite(ctx context.Context, input graphql2.SetFavoriteInput) (bool, error) {
var err error
if input.Favorite {
err = a.FavoriteStore.Set(ctx, permission.UserID(ctx), input.Target)
err = a.FavoriteStore.Set(ctx, a.DB, permission.UserID(ctx), input.Target)
} else {
err = a.FavoriteStore.Unset(ctx, permission.UserID(ctx), input.Target)
err = a.FavoriteStore.Unset(ctx, a.DB, permission.UserID(ctx), input.Target)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion graphql2/graphqlapp/rotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (m *Mutation) CreateRotation(ctx context.Context, input graphql2.CreateRota
}

if input.Favorite != nil && *input.Favorite {
err = m.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.RotationTarget(result.ID))
err = m.FavoriteStore.Set(ctx, tx, permission.UserID(ctx), assignment.RotationTarget(result.ID))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion graphql2/graphqlapp/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (m *Mutation) CreateSchedule(ctx context.Context, input graphql2.CreateSche
return err
}
if input.Favorite != nil && *input.Favorite {
err = m.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.ScheduleTarget(sched.ID))
err = m.FavoriteStore.Set(ctx, tx, permission.UserID(ctx), assignment.ScheduleTarget(sched.ID))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion graphql2/graphqlapp/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (m *Mutation) CreateService(ctx context.Context, input graphql2.CreateServi
}

if input.Favorite != nil && *input.Favorite {
err = m.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.ServiceTarget(result.ID))
err = m.FavoriteStore.Set(ctx, tx, permission.UserID(ctx), assignment.ServiceTarget(result.ID))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion graphql2/graphqlapp/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (a *Mutation) CreateUser(ctx context.Context, input graphql2.CreateUserInpu
return err
}
if input.Favorite != nil && *input.Favorite {
err = a.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.UserTarget(newUser.ID))
err = a.FavoriteStore.Set(ctx, tx, permission.UserID(ctx), assignment.UserTarget(newUser.ID))
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sql:
- engine/statusmgr/queries.sql
- auth/authlink/queries.sql
- alert/alertlog/queries.sql
- user/favorite/queries.sql
- user/contactmethod/queries.sql
- integrationkey/queries.sql
- apikey/queries.sql
Expand Down
37 changes: 37 additions & 0 deletions user/favorite/queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- name: UserFavSet :exec
INSERT INTO user_favorites(user_id, tgt_service_id, tgt_schedule_id, tgt_rotation_id, tgt_escalation_policy_id, tgt_user_id)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT
DO NOTHING;

-- name: UserFavUnset :exec
DELETE FROM user_favorites
WHERE user_id = $1
AND tgt_service_id = $2
OR tgt_schedule_id = $3
OR tgt_rotation_id = $4
OR tgt_escalation_policy_id = $5
OR tgt_user_id = $6;

-- name: UserFavFindAll :many
SELECT
tgt_service_id,
tgt_schedule_id,
tgt_rotation_id,
tgt_escalation_policy_id,
tgt_user_id
FROM
user_favorites
WHERE
user_id = @user_id
AND ((tgt_service_id NOTNULL
AND @allow_services::bool)
OR (tgt_schedule_id NOTNULL
AND @allow_schedules::bool)
OR (tgt_rotation_id NOTNULL
AND @allow_rotations::bool)
OR (tgt_escalation_policy_id NOTNULL
AND @allow_escalation_policies::bool)
OR (tgt_user_id NOTNULL
AND @allow_users::bool));

Loading