1
1
package main
2
2
3
+ import "database/sql"
3
4
import "encoding/json"
4
5
import "fmt"
5
6
import "net/http"
7
+ import "os"
6
8
import "strconv"
7
9
import "github.com/go-chi/chi"
10
+ import "github.com/jmoiron/sqlx"
8
11
9
12
type Category struct {
10
- Id int `json:"id"`
11
- Name * string `json:"name"`
12
- NameRu * string `json:"name_ru"`
13
- Slug * string `json:"slug"`
13
+ Id int `json:"id" db:"id" `
14
+ Name string `json:"name" db :"name"`
15
+ NameRu * string `json:"name_ru" db:"name_ru" `
16
+ Slug string `json:"slug" db :"slug"`
14
17
}
15
18
16
19
type Dto1 struct {
17
- Counter string `json:"counter"`
20
+ Counter * string `json:"counter,omitempty" db :"counter"`
18
21
}
19
22
20
23
type Dto3 struct {
21
- Id string `json:"id"`
22
- Name string `json:"name"`
23
- NameRu string `json:"nameRu "`
24
- Slug string `json:"slug"`
24
+ Id * string `json:"id,omitempty" db :"id"`
25
+ Name * string `json:"name,omitempty" db :"name"`
26
+ NameRu * string `json:"name_ru,omitempty" db:"name_ru "`
27
+ Slug * string `json:"slug,omitempty" db :"slug"`
25
28
}
26
29
27
30
type Dto4 struct {
28
- Name string `json:"name"`
29
- NameRu string `json:"nameRu "`
30
- Slug string `json:"slug"`
31
- UserId string `json:"userId "`
31
+ Name * string `json:"name,omitempty" db :"name"`
32
+ NameRu * string `json:"name_ru,omitempty" db:"name_ru "`
33
+ Slug * string `json:"slug,omitempty" db :"slug"`
34
+ UserId * string `json:"user_id,omitempty" db:"user_id "`
32
35
}
33
36
34
- func registerRoutes (r chi.Router ) {
37
+ func registerRoutes (r chi.Router , db * sqlx. DB ) {
35
38
categories := make (map [int ]Category )
36
39
cnt := 0
37
40
38
41
r .Get ("/v1/categories/count" , func (w http.ResponseWriter , r * http.Request ) {
39
- w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
40
- fmt .Fprintf (w , `{"counter": %d}` , len (categories ))
41
-
42
+ var result Dto1
43
+ err := db .Get (& result , "SELECT COUNT(*) AS counter FROM categories" )
44
+ switch err {
45
+ case sql .ErrNoRows :
46
+ w .WriteHeader (http .StatusNotFound )
47
+ case nil :
48
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
49
+ json .NewEncoder (w ).Encode (& result )
50
+ default :
51
+ fmt .Fprintf (os .Stderr , "Get failed: %v\n " , err )
52
+ w .WriteHeader (http .StatusInternalServerError )
53
+ }
42
54
})
43
55
44
56
r .Get ("/v1/collections/{collectionId}/categories/count" , func (w http.ResponseWriter , r * http.Request ) {
45
- id , _ := strconv . Atoi ( chi . URLParam ( r , "categoryId" ) )
46
- category , exist := categories [ id ]
47
- if ! exist {
48
- w .WriteHeader (http .StatusNotFound )
57
+ nstmt , err := db . PrepareNamed ( "SELECT COUNT(DISTINCT s.category_id) AS counter FROM collections_series cs JOIN series s ON s.id = cs.series_id WHERE cs.collection_id = :collectionId" )
58
+ if err != nil {
59
+ fmt . Fprintf ( os . Stderr , "PrepareNamed failed: %v \n " , err )
60
+ w .WriteHeader (http .StatusInternalServerError )
49
61
return
50
62
}
51
- w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
52
- json .NewEncoder (w ).Encode (& category )
53
63
64
+ var result Dto1
65
+ args := map [string ]interface {}{
66
+ "collectionId" : chi .URLParam (r , "collectionId" ),
67
+ }
68
+ err = nstmt .Get (& result , args )
69
+ switch err {
70
+ case sql .ErrNoRows :
71
+ w .WriteHeader (http .StatusNotFound )
72
+ case nil :
73
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
74
+ json .NewEncoder (w ).Encode (& result )
75
+ default :
76
+ fmt .Fprintf (os .Stderr , "Get failed: %v\n " , err )
77
+ w .WriteHeader (http .StatusInternalServerError )
78
+ }
54
79
})
55
80
56
81
r .Get ("/v1/categories" , func (w http.ResponseWriter , r * http.Request ) {
57
- w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
58
- list := []Category {categories [1 ]}
59
- json .NewEncoder (w ).Encode (& list )
60
-
82
+ var result []Dto3
83
+ err := db .Select (& result , "SELECT id , name , name_ru , slug FROM categories" )
84
+ switch err {
85
+ case sql .ErrNoRows :
86
+ w .WriteHeader (http .StatusNotFound )
87
+ case nil :
88
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
89
+ json .NewEncoder (w ).Encode (& result )
90
+ default :
91
+ fmt .Fprintf (os .Stderr , "Select failed: %v\n " , err )
92
+ w .WriteHeader (http .StatusInternalServerError )
93
+ }
61
94
})
62
95
63
96
r .Post ("/v1/categories" , func (w http.ResponseWriter , r * http.Request ) {
@@ -70,15 +103,28 @@ func registerRoutes(r chi.Router) {
70
103
})
71
104
72
105
r .Get ("/v1/categories/{categoryId}" , func (w http.ResponseWriter , r * http.Request ) {
73
- id , _ := strconv . Atoi ( chi . URLParam ( r , " categoryId") )
74
- category , exist := categories [ id ]
75
- if ! exist {
76
- w .WriteHeader (http .StatusNotFound )
106
+ nstmt , err := db . PrepareNamed ( "SELECT id , name , name_ru , slug FROM categories WHERE id = : categoryId" )
107
+ if err != nil {
108
+ fmt . Fprintf ( os . Stderr , "PrepareNamed failed: %v \n " , err )
109
+ w .WriteHeader (http .StatusInternalServerError )
77
110
return
78
111
}
79
- w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
80
- json .NewEncoder (w ).Encode (& category )
81
112
113
+ var result Dto3
114
+ args := map [string ]interface {}{
115
+ "categoryId" : chi .URLParam (r , "categoryId" ),
116
+ }
117
+ err = nstmt .Get (& result , args )
118
+ switch err {
119
+ case sql .ErrNoRows :
120
+ w .WriteHeader (http .StatusNotFound )
121
+ case nil :
122
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
123
+ json .NewEncoder (w ).Encode (& result )
124
+ default :
125
+ fmt .Fprintf (os .Stderr , "Get failed: %v\n " , err )
126
+ w .WriteHeader (http .StatusInternalServerError )
127
+ }
82
128
})
83
129
84
130
r .Put ("/v1/categories/{categoryId}" , func (w http.ResponseWriter , r * http.Request ) {
0 commit comments