Skip to content

Commit 1303440

Browse files
committed
curls working
1 parent 3bbf3d2 commit 1303440

7 files changed

Lines changed: 44 additions & 5 deletions

File tree

go/samples/http/curls/delete.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl --header "Content-Type: application/json" \
2+
--request DELETE \
3+
http://localhost:8080/todos/$1
4+
echo

go/samples/http/curls/index.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl http://localhost:8080
2+
echo

go/samples/http/curls/insert.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl --header "Content-Type: application/json" \
2+
--request POST \
3+
--data '{"task":"'$1'"}' \
4+
http://localhost:8080/todos
5+
echo

go/samples/http/curls/list.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl http://localhost:8080/todos?search=$1
2+
echo

go/samples/http/curls/update.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl --header "Content-Type: application/json" \
2+
--request PUT \
3+
--data '{"task":"'$2'"}' \
4+
http://localhost:8080/todos/$1
5+
echo

go/samples/http/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ require (
99
)
1010

1111
require (
12-
github.com/julienschmidt/httprouter v1.3.0 // indirect
13-
github.com/lib/pq v1.10.7 // indirect
12+
github.com/julienschmidt/httprouter v1.3.0
13+
github.com/lib/pq v1.10.7
1414
go.opentelemetry.io/otel v1.10.0 // indirect
1515
)
1616

go/samples/http/todos/controller.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package todos
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log"
78
"net/http"
89

@@ -26,7 +27,8 @@ type TodosController struct {
2627
}
2728

2829
func (c *TodosController) CreateTodosTableIfNotExists() error {
29-
return nil
30+
_, err := c.DB.Exec(c.SQL.CreateTodosTableIfNotExists())
31+
return err
3032
}
3133

3234
func (c *TodosController) ActionList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -35,17 +37,19 @@ func (c *TodosController) ActionList(w http.ResponseWriter, r *http.Request, p h
3537
query := r.URL.Query()
3638
search := query.Get("search")
3739

38-
rows, err := c.DB.QueryContext(ctx, c.SQL.ListTodos(), search)
40+
rows, err := c.DB.QueryContext(ctx, c.SQL.ListTodos(), "%"+search+"%")
3941
if err != nil {
42+
fmt.Println(err)
4043
writeServerErrorResponse(w, "query failed")
4144
return
4245
}
4346
defer rows.Close()
4447

45-
var todos []Todo
48+
var todos []Todo = make([]Todo, 0)
4649
for rows.Next() {
4750
var todo Todo
4851
if err := rows.Scan(&todo.Id, &todo.Task); err != nil {
52+
fmt.Println(err)
4953
writeServerErrorResponse(w, "scan row failed")
5054
return
5155
}
@@ -64,6 +68,7 @@ func (c *TodosController) ActionInsert(w http.ResponseWriter, r *http.Request, p
6468
var dto TodoDTO
6569
err := json.NewDecoder(r.Body).Decode(&dto)
6670
if err != nil {
71+
fmt.Println(err)
6772
writeBadRequestResponse(w, "parsing body failed")
6873
return
6974
}
@@ -80,13 +85,15 @@ func (c *TodosController) ActionInsert(w http.ResponseWriter, r *http.Request, p
8085
func (c *TodosController) insertTodoPG(ctx context.Context, w http.ResponseWriter, dto TodoDTO) {
8186
rows, err := c.DB.QueryContext(ctx, c.SQL.InsertTodo(), dto.Task)
8287
if err != nil {
88+
fmt.Println(err)
8389
writeServerErrorResponse(w, "insert failed")
8490
return
8591
}
8692

8793
if rows.Next() {
8894
var todo Todo
8995
if err := rows.Scan(&todo.Id, &todo.Task); err != nil {
96+
fmt.Println(err)
9097
writeServerErrorResponse(w, "scan row failed")
9198
return
9299
}
@@ -104,18 +111,21 @@ func (c *TodosController) insertTodoPG(ctx context.Context, w http.ResponseWrite
104111
func (c *TodosController) insertTodoMySQL(ctx context.Context, w http.ResponseWriter, dto TodoDTO) {
105112
dbRes, err := c.DB.ExecContext(ctx, c.SQL.InsertTodo(), dto.Task)
106113
if err != nil {
114+
fmt.Println(err)
107115
writeServerErrorResponse(w, "insert failed")
108116
return
109117
}
110118

111119
lId, err := dbRes.LastInsertId()
112120
if err != nil {
121+
fmt.Println(err)
113122
writeServerErrorResponse(w, "cannot get insert-id")
114123
return
115124
}
116125

117126
rows, err := c.DB.QueryContext(ctx, c.SQL.TodoById(), lId)
118127
if err != nil {
128+
fmt.Println(err)
119129
writeServerErrorResponse(w, "error in query")
120130
return
121131
}
@@ -144,19 +154,22 @@ func (c *TodosController) ActionUpdate(w http.ResponseWriter, r *http.Request, p
144154
var dto TodoDTO
145155
err := json.NewDecoder(r.Body).Decode(&dto)
146156
if err != nil {
157+
fmt.Println(err)
147158
writeBadRequestResponse(w, "parsing body failed")
148159
return
149160
}
150161

151162
var todo Todo
152163
rows, err := c.DB.QueryContext(ctx, c.SQL.TodoById(), id)
153164
if err != nil {
165+
fmt.Println(err)
154166
writeServerErrorResponse(w, "error in query")
155167
return
156168
}
157169

158170
if rows.Next() {
159171
if err := rows.Scan(&todo.Id, &todo.Task); err != nil {
172+
fmt.Println(err)
160173
writeServerErrorResponse(w, "scan row failed")
161174
return
162175
}
@@ -167,6 +180,7 @@ func (c *TodosController) ActionUpdate(w http.ResponseWriter, r *http.Request, p
167180

168181
_, err = c.DB.ExecContext(ctx, c.SQL.UpdateTodo(), dto.Task, todo.Id)
169182
if err != nil {
183+
fmt.Println(err)
170184
writeServerErrorResponse(w, "update todo query failed")
171185
return
172186
}
@@ -184,12 +198,14 @@ func (c *TodosController) ActionDelete(w http.ResponseWriter, r *http.Request, p
184198
var todo Todo
185199
rows, err := c.DB.QueryContext(ctx, c.SQL.TodoById(), id)
186200
if err != nil {
201+
fmt.Println(err)
187202
writeServerErrorResponse(w, "error in query")
188203
return
189204
}
190205

191206
if rows.Next() {
192207
if err := rows.Scan(&todo.Id, &todo.Task); err != nil {
208+
fmt.Println(err)
193209
writeServerErrorResponse(w, "scan row failed")
194210
return
195211
}
@@ -200,6 +216,7 @@ func (c *TodosController) ActionDelete(w http.ResponseWriter, r *http.Request, p
200216

201217
_, err = c.DB.ExecContext(ctx, c.SQL.DeleteTodo(), todo.Id)
202218
if err != nil {
219+
fmt.Println(err)
203220
writeServerErrorResponse(w, "update todo query failed")
204221
return
205222
}
@@ -215,6 +232,7 @@ func writeJsonResponse(w http.ResponseWriter, res any) {
215232

216233
resJson, err := json.Marshal(res)
217234
if err != nil {
235+
fmt.Println(err)
218236
log.Fatalf("error: json marshall: %v", res)
219237
}
220238

@@ -232,6 +250,7 @@ func writeServerErrorResponse(w http.ResponseWriter, reason string) {
232250

233251
resJson, err := json.Marshal(res)
234252
if err != nil {
253+
fmt.Println(err)
235254
log.Fatalf("error: json marshall: %v", res)
236255
}
237256

@@ -248,6 +267,7 @@ func writeNotFoundResponse(w http.ResponseWriter) {
248267

249268
resJson, err := json.Marshal(res)
250269
if err != nil {
270+
fmt.Println(err)
251271
log.Fatalf("error: json marshall: %v", res)
252272
}
253273

@@ -265,6 +285,7 @@ func writeBadRequestResponse(w http.ResponseWriter, reason string) {
265285

266286
resJson, err := json.Marshal(res)
267287
if err != nil {
288+
fmt.Println(err)
268289
log.Fatalf("error: json marshall: %v", res)
269290
}
270291

0 commit comments

Comments
 (0)