API: Rig up user { pastes }
This commit is contained in:
parent
80518d74f4
commit
3f391d7d48
|
@ -15,6 +15,7 @@ import (
|
|||
"git.sr.ht/~sircmpwn/paste.sr.ht/api/graph/model"
|
||||
"git.sr.ht/~sircmpwn/paste.sr.ht/api/loaders"
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
)
|
||||
|
||||
func (r *mutationResolver) Create(ctx context.Context, files []*graphql.Upload, visibility model.Visibility) (*model.Paste, error) {
|
||||
|
@ -88,7 +89,33 @@ func (r *queryResolver) Paste(ctx context.Context, id string) (*model.Paste, err
|
|||
}
|
||||
|
||||
func (r *userResolver) Pastes(ctx context.Context, obj *model.User, cursor *coremodel.Cursor) (*model.PasteCursor, error) {
|
||||
panic(fmt.Errorf("not implemented"))
|
||||
if cursor == nil {
|
||||
cursor = coremodel.NewCursor(nil)
|
||||
}
|
||||
|
||||
var pastes []*model.Paste
|
||||
if err := database.WithTx(ctx, &sql.TxOptions{
|
||||
Isolation: 0,
|
||||
ReadOnly: true,
|
||||
}, func(tx *sql.Tx) error {
|
||||
paste := (&model.Paste{}).As(`paste`)
|
||||
query := database.
|
||||
Select(ctx, paste).
|
||||
From(`paste`).
|
||||
Where(sq.And{
|
||||
sq.Expr(`paste.user_id = ?`, obj.ID),
|
||||
sq.Or{
|
||||
sq.Expr(`paste.user_id = ?`, auth.ForContext(ctx).UserID),
|
||||
sq.Expr(`paste.visibility = 'public'`),
|
||||
},
|
||||
})
|
||||
pastes, cursor = paste.QueryWithCursor(ctx, tx, query, cursor)
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.PasteCursor{pastes, cursor}, nil
|
||||
}
|
||||
|
||||
// Mutation returns api.MutationResolver implementation.
|
||||
|
|
Loading…
Reference in New Issue