-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_note_query.go
53 lines (50 loc) · 1.07 KB
/
resolver_note_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"database/sql"
)
type NotesArgs struct {
Limit *int32
Offset *int32
Direction *string
}
func (r *RootResolver) Notes(ctx context.Context, args NotesArgs) ([]*NoteResolver, error) {
userID, ok := ctx.Value(UserIDKey).(string)
if !ok {
return nil, ErrUserMustBeAuth
}
var rxs []*NoteResolver
rows, err := db.Query(`
select
user_id,
note_id,
created_at,
updated_at,
data
from notes
where user_id = $1
order by
case when $4::text is null or $4 = 'desc' then updated_at end desc,
case when $4 = 'asc' then updated_at end asc
limit coalesce( $2, 25 )
offset $3
`, userID, args.Limit, args.Offset, args.Direction)
// NOTE: Guard sql.ErrNoRows
if err != nil && err != sql.ErrNoRows {
return nil, err
}
defer rows.Close()
for rows.Next() {
note := &Note{}
err := rows.Scan(¬e.UserID, ¬e.NoteID, ¬e.CreatedAt, ¬e.UpdatedAt, ¬e.Data)
if err != nil {
return nil, err
}
rxs = append(rxs, &NoteResolver{note})
}
err = rows.Err()
if err != nil {
return nil, err
}
return rxs, nil
}