|
1 | 1 | package main
|
2 | 2 |
|
| 3 | +import "encoding/json" |
| 4 | +import "fmt" |
3 | 5 | import "net/http"
|
| 6 | +import "strconv" |
4 | 7 | import "github.com/go-chi/chi"
|
5 | 8 |
|
| 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 | + |
6 | 16 | 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)) |
7 | 23 |
|
8 |
| - r.Get("/v1/categories/count", func(w http.ResponseWriter, _ *http.Request) { |
9 |
| - w.Write([]byte("TODO")) |
10 | 24 | })
|
11 | 25 |
|
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 | + |
14 | 36 | })
|
15 | 37 |
|
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 | + |
18 | 43 | })
|
19 | 44 |
|
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 |
21 | 51 | w.WriteHeader(http.StatusNoContent)
|
22 | 52 | })
|
23 | 53 |
|
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 | + |
26 | 64 | })
|
27 | 65 |
|
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 |
29 | 71 | w.WriteHeader(http.StatusNoContent)
|
30 | 72 | })
|
31 | 73 |
|
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) |
33 | 77 | w.WriteHeader(http.StatusNoContent)
|
34 | 78 | })
|
35 | 79 |
|
|
0 commit comments