-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_note_mutation.go
95 lines (89 loc) · 1.79 KB
/
resolver_note_mutation.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"log"
graphql "github.com/graph-gophers/graphql-go"
)
type NoteInput struct {
NoteID graphql.ID
Data string
}
func (r *RootResolver) CreateNote(ctx context.Context, args struct{ NoteInput NoteInput }) (*bool, error) {
userID, ok := ctx.Value(UserIDKey).(string)
if !ok {
return nil, ErrUserMustBeAuth
}
tx, err := db.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
_, err = tx.Exec(`
insert into notes (
user_id,
note_id,
data
) values ( $1, $2, $3 )
`, userID, args.NoteInput.NoteID, args.NoteInput.Data)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
return nil, nil
}
func (r *RootResolver) UpdateNote(ctx context.Context, args struct{ NoteInput NoteInput }) (*bool, error) {
userID, ok := ctx.Value(UserIDKey).(string)
if !ok {
return nil, ErrUserMustBeAuth
}
tx, err := db.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
_, err = tx.Exec(`
update notes
set data = $3
where
user_id = $1 and
note_id = $2
`, userID, args.NoteInput.NoteID, args.NoteInput.Data)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
return nil, nil
}
func (r *RootResolver) DeleteNote(ctx context.Context, args struct{ NoteID graphql.ID }) (*bool, error) {
userID, ok := ctx.Value(UserIDKey).(string)
if !ok {
return nil, ErrUserMustBeAuth
}
tx, err := db.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
_, err = tx.Exec(`
delete
from notes
where
user_id = $1 and
note_id = $2
`, userID, args.NoteID)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
log.Printf("deleted note noteID=%s from user userID=%s", args.NoteID, userID)
return nil, nil
}