API: Rig up mutation { update }

This commit is contained in:
Drew DeVault 2021-09-21 14:36:48 +02:00
parent 898db2a4eb
commit f54633537b
1 changed files with 21 additions and 1 deletions

View File

@ -182,7 +182,27 @@ func (r *mutationResolver) Create(ctx context.Context, files []*graphql.Upload,
}
func (r *mutationResolver) Update(ctx context.Context, id string, visibility model.Visibility) (*model.Paste, error) {
panic(fmt.Errorf("not implemented"))
var paste model.Paste
if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
row := tx.QueryRowContext(ctx, `
UPDATE paste
SET visibility = $1
WHERE sha = $2 AND user_id = $3
RETURNING
id, sha, created, user_id, visibility;`,
strings.ToLower(visibility.String()),
id, auth.ForContext(ctx).UserID)
return row.Scan(&paste.PKID, &paste.ID,
&paste.Created, &paste.UserID, &paste.RawVisibility)
}); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return &paste, nil
}
func (r *mutationResolver) Delete(ctx context.Context, id string) (*model.Paste, error) {