@@ -38,13 +38,19 @@ func List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
38
38
39
39
rows , err := db .Query ("SELECT body FROM annotations where target=?" , canvas [0 ])
40
40
if err != nil {
41
- log .Fatal ().Err (err )
41
+ log .Error ().Err (err ).Str ("action" , "list" ).Msg ("db error" )
42
+ w .WriteHeader (http .StatusInternalServerError )
43
+ w .Write ([]byte ("500" ))
44
+ return
42
45
}
43
46
defer rows .Close ()
44
47
for rows .Next () {
45
48
err := rows .Scan (& body )
46
49
if err != nil {
47
- log .Fatal ().Err (err )
50
+ log .Error ().Err (err ).Str ("action" , "list" ).Msg ("db error" )
51
+ w .WriteHeader (http .StatusInternalServerError )
52
+ w .Write ([]byte ("500" ))
53
+ return
48
54
}
49
55
list .Resources = append (list .Resources , body )
50
56
}
@@ -56,30 +62,31 @@ func List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
56
62
w .Header ().Set ("Content-Type" , "application/json" )
57
63
enc := json .NewEncoder (w )
58
64
if err := enc .Encode (list ); err != nil {
59
- fmt .Println (err )
65
+ log .Error ().Err (err ).Str ("action" , "list" ).Str ("canvas" , canvas [0 ]).Msg ("annotation list error" )
66
+ w .WriteHeader (http .StatusInternalServerError )
67
+ w .Write ([]byte ("500" ))
68
+ return
60
69
}
61
70
62
71
}
63
72
64
73
// Get retrieve a single annotation with database id
65
- // TODO: retrieve with annotation id
74
+ // NOTE: just for test, not used by mirador
66
75
func Get (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
67
76
w .Header ().Set ("Content-Type" , "application/json" )
68
77
69
78
var body string
70
- rows , err := db .Query ("SELECT body FROM annotations where id=?" , ps .ByName ("id" ))
79
+ err := db .QueryRow ("SELECT body FROM annotations where id=?" , ps .ByName ("id" )).Scan (& body )
80
+
71
81
if err != nil {
72
- log .Fatal ().Err (err )
73
- }
74
- defer rows .Close ()
75
- for rows .Next () {
76
- err := rows .Scan (& body )
77
- if err != nil {
78
- log .Fatal ().Err (err )
79
- }
80
- fmt .Fprintf (w , body )
82
+ log .Error ().Err (err ).Str ("action" , "get" ).Msg ("db error" )
83
+ w .WriteHeader (http .StatusInternalServerError )
84
+ w .Write ([]byte ("500" ))
85
+ return
81
86
}
82
87
88
+ w .Header ().Set ("Content-Type" , "application/json" )
89
+ fmt .Fprintf (w , body )
83
90
}
84
91
85
92
// Delete an annotation
@@ -89,14 +96,20 @@ func Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
89
96
var anno string
90
97
err := db .QueryRow ("SELECT body FROM annotations where annoid = ?" , ps .ByName ("id" )).Scan (& anno )
91
98
if err != nil {
92
- log .Fatal ().Err (err )
99
+ log .Error ().Err (err ).Str ("action" , "delete" ).Msg ("db error" )
100
+ w .WriteHeader (http .StatusInternalServerError )
101
+ w .Write ([]byte ("500" ))
102
+ return
93
103
}
94
104
95
105
_ , err = db .Exec ("DELETE FROM annotations where annoid=?" , ps .ByName ("id" ))
96
106
if err != nil {
97
- log .Fatal ().Err (err )
107
+ log .Error ().Err (err ).Str ("action" , "delete" ).Msg ("db error" )
108
+ w .WriteHeader (http .StatusInternalServerError )
109
+ w .Write ([]byte ("500" ))
110
+ return
98
111
}
99
- log .Info ().Str ("annotation-id " , ps .ByName ("id" )).Msg ("deleted " )
112
+ log .Info ().Str ("annotation_id " , ps .ByName ("id" )).Str ( "action" , "delete" ). Msg ("" )
100
113
w .Header ().Set ("Content-Type" , "application/json" )
101
114
fmt .Fprintf (w , anno )
102
115
}
@@ -106,14 +119,18 @@ func Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
106
119
body , err := ioutil .ReadAll (r .Body )
107
120
defer r .Body .Close ()
108
121
if err != nil {
109
- http .Error (w , err .Error (), 500 )
122
+ log .Error ().Err (err ).Str ("action" , "create" ).Msg ("" )
123
+ w .WriteHeader (http .StatusInternalServerError )
124
+ w .Write ([]byte ("500" ))
110
125
return
111
126
}
112
127
113
128
var annotation Annotation
114
129
err = json .Unmarshal (body , & annotation )
115
130
if err != nil {
116
- http .Error (w , err .Error (), 500 )
131
+ log .Error ().Err (err ).Str ("action" , "create" ).Msg ("" )
132
+ w .WriteHeader (http .StatusInternalServerError )
133
+ w .Write ([]byte ("500" ))
117
134
return
118
135
}
119
136
@@ -122,7 +139,7 @@ func Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
122
139
AnnotationWithID , _ := json .Marshal (annotation )
123
140
statement , _ := db .Prepare ("INSERT INTO annotations (annoid, created_at, target, manifest, body) VALUES (?, ?, ?, ?, ?)" )
124
141
statement .Exec (annoid , time .Now (), annotation .Canvas (), annotation .Manifest (), AnnotationWithID )
125
- log .Info ().Str ("annotation-id " , annoid ).Msg ( " create" )
142
+ log .Info ().Str ("annotation_id " , annoid ).Str ( "action" , " create" ). Msg ( " " )
126
143
127
144
fmt .Fprintf (w , string (AnnotationWithID ))
128
145
}
@@ -135,16 +152,21 @@ func Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
135
152
body , err := ioutil .ReadAll (r .Body )
136
153
defer r .Body .Close ()
137
154
if err != nil {
138
- http .Error (w , err .Error (), 500 )
155
+ log .Error ().Err (err ).Str ("action" , "update" ).Msg ("db error" )
156
+ w .WriteHeader (http .StatusInternalServerError )
157
+ w .Write ([]byte ("500" ))
139
158
return
140
159
}
141
160
142
161
_ , err = db .Exec ("UPDATE annotations SET body=? WHERE annoid=?" , body , annoid )
143
162
if err != nil {
144
- log .Fatal ().Err (err )
163
+ log .Error ().Err (err ).Str ("action" , "update" ).Msg ("db error" )
164
+ w .WriteHeader (http .StatusInternalServerError )
165
+ w .Write ([]byte ("500" ))
166
+ return
145
167
}
146
168
147
- log .Info ().Str ("annotation-id " , annoid ).Msg ( " update" )
169
+ log .Info ().Str ("annotation_id " , annoid ).Str ( "action" , " update" ). Msg ( " " )
148
170
149
171
fmt .Fprintf (w , string (body ))
150
172
}
0 commit comments