Skip to content

Commit a6fde35

Browse files
committed
chore(golang): add stubs in order to pass the tests without database
Part of #9
1 parent 2daee7a commit a6fde35

File tree

2 files changed

+110
-17
lines changed

2 files changed

+110
-17
lines changed

examples/go/routes.go

+55-11
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,79 @@
11
package main
22

3+
import "encoding/json"
4+
import "fmt"
35
import "net/http"
6+
import "strconv"
47
import "github.com/go-chi/chi"
58

9+
type Category struct {
10+
Id int `json:"id"`
11+
Name *string `json:"name"`
12+
NameRu *string `json:"name_ru"`
13+
Slug *string `json:"slug"`
14+
}
15+
616
func registerRoutes(r chi.Router) {
17+
categories := make(map[int]Category)
18+
cnt := 0
19+
20+
r.Get("/v1/categories/count", func(w http.ResponseWriter, r *http.Request) {
21+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
22+
fmt.Fprintf(w, `{"counter": %d}`, len(categories))
723

8-
r.Get("/v1/categories/count", func(w http.ResponseWriter, _ *http.Request) {
9-
w.Write([]byte("TODO"))
1024
})
1125

12-
r.Get("/v1/collections/{collectionId}/categories/count", func(w http.ResponseWriter, _ *http.Request) {
13-
w.Write([]byte("TODO"))
26+
r.Get("/v1/collections/{collectionId}/categories/count", func(w http.ResponseWriter, r *http.Request) {
27+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
28+
category, exist := categories[id]
29+
if !exist {
30+
w.WriteHeader(http.StatusNotFound)
31+
return
32+
}
33+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
34+
json.NewEncoder(w).Encode(&category)
35+
1436
})
1537

16-
r.Get("/v1/categories", func(w http.ResponseWriter, _ *http.Request) {
17-
w.Write([]byte("TODO"))
38+
r.Get("/v1/categories", func(w http.ResponseWriter, r *http.Request) {
39+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
40+
list := []Category{categories[1]}
41+
json.NewEncoder(w).Encode(&list)
42+
1843
})
1944

20-
r.Post("/v1/categories", func(w http.ResponseWriter, _ *http.Request) {
45+
r.Post("/v1/categories", func(w http.ResponseWriter, r *http.Request) {
46+
var category Category
47+
json.NewDecoder(r.Body).Decode(&category)
48+
cnt += 1
49+
category.Id = cnt
50+
categories[cnt] = category
2151
w.WriteHeader(http.StatusNoContent)
2252
})
2353

24-
r.Get("/v1/categories/{categoryId}", func(w http.ResponseWriter, _ *http.Request) {
25-
w.Write([]byte("TODO"))
54+
r.Get("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
55+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
56+
category, exist := categories[id]
57+
if !exist {
58+
w.WriteHeader(http.StatusNotFound)
59+
return
60+
}
61+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
62+
json.NewEncoder(w).Encode(&category)
63+
2664
})
2765

28-
r.Put("/v1/categories/{categoryId}", func(w http.ResponseWriter, _ *http.Request) {
66+
r.Put("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
67+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
68+
var category Category
69+
json.NewDecoder(r.Body).Decode(&category)
70+
categories[id] = category
2971
w.WriteHeader(http.StatusNoContent)
3072
})
3173

32-
r.Delete("/v1/categories/{categoryId}", func(w http.ResponseWriter, _ *http.Request) {
74+
r.Delete("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
75+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
76+
delete(categories, id)
3377
w.WriteHeader(http.StatusNoContent)
3478
})
3579

src/templates/routes.go.ejs

+55-6
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,85 @@
11
package main
22

3+
import "encoding/json"
4+
import "fmt"
35
import "net/http"
6+
import "strconv"
47
import "github.com/go-chi/chi"
58

9+
type Category struct {
10+
Id int `json:"id"`
11+
Name *string `json:"name"`
12+
NameRu *string `json:"name_ru"`
13+
Slug *string `json:"slug"`
14+
}
15+
616
func registerRoutes(r chi.Router) {
17+
categories := make(map[int]Category)
18+
cnt := 0
719
<%
820
endpoints.forEach(function(endpoint) {
921
const path = convertPathPlaceholders(endpoint.path);
10-
if (endpoint.hasOwnProperty('get') || endpoint.hasOwnProperty('get_list')) {
22+
const hasGetOne = endpoint.hasOwnProperty('get');
23+
const hasGetMany = endpoint.hasOwnProperty('get_list');
24+
if (hasGetOne || hasGetMany) {
25+
%>
26+
r.Get("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
27+
<%
28+
if (path === '/v1/categories/count') {
29+
-%>
30+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
31+
fmt.Fprintf(w, `{"counter": %d}`, len(categories))
32+
<%
33+
} else if (hasGetMany) {
34+
-%>
35+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
36+
list := []Category{categories[1]}
37+
json.NewEncoder(w).Encode(&list)
38+
<%
39+
} else {
40+
-%>
41+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
42+
category, exist := categories[id]
43+
if !exist {
44+
w.WriteHeader(http.StatusNotFound)
45+
return
46+
}
47+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
48+
json.NewEncoder(w).Encode(&category)
49+
<%
50+
}
1151
%>
12-
r.Get("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
13-
w.Write([]byte("TODO"))
1452
})
1553
<%
1654
}
1755
if (endpoint.hasOwnProperty('post')) {
1856
%>
19-
r.Post("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
57+
r.Post("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
58+
var category Category
59+
json.NewDecoder(r.Body).Decode(&category)
60+
cnt += 1
61+
category.Id = cnt
62+
categories[cnt] = category
2063
w.WriteHeader(http.StatusNoContent)
2164
})
2265
<%
2366
}
2467
if (endpoint.hasOwnProperty('put')) {
2568
%>
26-
r.Put("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
69+
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
70+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
71+
var category Category
72+
json.NewDecoder(r.Body).Decode(&category)
73+
categories[id] = category
2774
w.WriteHeader(http.StatusNoContent)
2875
})
2976
<%
3077
}
3178
if (endpoint.hasOwnProperty('delete')) {
3279
%>
33-
r.Delete("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
80+
r.Delete("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
81+
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
82+
delete(categories, id)
3483
w.WriteHeader(http.StatusNoContent)
3584
})
3685
<%

0 commit comments

Comments
 (0)