aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/grils/grils.go60
-rw-r--r--modules/lists/lists.go20
2 files changed, 38 insertions, 42 deletions
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index d68c6df..0ba4c61 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -65,16 +65,23 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*mo
65 gril := &models.Gril{} 65 gril := &models.Gril{}
66 var tags []byte 66 var tags []byte
67 var otherNames []byte 67 var otherNames []byte
68 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags) 68
69 var kanjiName *string
70 err = rows.Scan(&gril.ID, &kanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags)
69 if err != nil { 71 if err != nil {
70 log.Println(gril.ID) 72 log.Println(gril.ID)
71 log.Println("error scanning in getGrils:", err) 73 log.Println("error scanning in getGrils:", err)
72 continue 74 continue
73 } 75 }
76 if kanjiName != nil {
77 gril.KanjiName = *kanjiName
78 }
74 79
75 gril.Tags = pgArray(tags) 80 gril.Tags = pgArray(tags)
76 gril.OtherNames = pgArray(otherNames) 81 gril.OtherNames = pgArray(otherNames)
77 82
83 m.c.Insert(gril.ID, gril)
84
78 grils = append(grils, gril) 85 grils = append(grils, gril)
79 } 86 }
80 87
@@ -166,7 +173,7 @@ func (m *GrilsModule) FromID(id int) (*models.Gril, error) {
166 defer rows.Close() 173 defer rows.Close()
167 174
168 for rows.Next() { 175 for rows.Next() {
169 var name string 176 var name *string
170 var name_type int 177 var name_type int
171 if err := rows.Scan(&name, &name_type); err != nil { 178 if err := rows.Scan(&name, &name_type); err != nil {
172 return nil, err 179 return nil, err
@@ -174,43 +181,14 @@ func (m *GrilsModule) FromID(id int) (*models.Gril, error) {
174 181
175 switch name_type { 182 switch name_type {
176 case 0: 183 case 0:
177 gril.KanjiName = name 184 gril.KanjiName = *name
178 break 185 break
179 case 1: 186 case 1:
180 gril.RomajiName = name 187 gril.RomajiName = *name
181 default: 188 default:
182 gril.OtherNames = append(gril.OtherNames, name) 189 gril.OtherNames = append(gril.OtherNames, *name)
183 } 190 }
184 } 191 }
185 /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id)
186 if err != nil {
187 return nil, err
188 }
189 defer rows.Close()
190
191 for rows.Next() {
192 var name string
193 if err := rows.Scan(&name); err != nil {
194 return nil, err
195 }
196 gril.OtherNames = append(gril.OtherNames, name)
197 }
198 rows.Close()
199
200 // Tags holen
201 rows, err := m.g.DB.Query(`SELECT name FROM grilist.tags WHERE id IN (SELECT tag_id FROM grilist.grils_tags WHERE gril_id = $1)`, id)
202 if err != nil {
203 return nil, err
204 }
205 defer rows.Close()
206
207 for rows.Next() {
208 var name string
209 if err := rows.Scan(&name); err != nil {
210 return nil, err
211 }
212 gril.Tags = append(gril.Tags, name)
213 }*/
214 192
215 m.c.Insert(id, gril) 193 m.c.Insert(id, gril)
216 log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000) 194 log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000)
@@ -222,9 +200,16 @@ func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
222 return make([]*models.Gril, 0), nil 200 return make([]*models.Gril, 0), nil
223 } 201 }
224 202
203 t1 := time.Now()
225 idList := "(" 204 idList := "("
226 first := true 205 first := true
206 var list []*models.Gril
227 for _, v := range ids { 207 for _, v := range ids {
208 if m.c.Has(v) {
209 g, _ := m.c.Get(v)
210 list = append(list, g.(*models.Gril))
211 continue
212 }
228 if first { 213 if first {
229 first = false 214 first = false
230 } else { 215 } else {
@@ -234,7 +219,12 @@ func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
234 } 219 }
235 idList += ")" 220 idList += ")"
236 grils, err := m.getGrils("id IN " + idList) 221 grils, err := m.getGrils("id IN " + idList)
237 return grils, err 222
223 for _, g := range grils {
224 list = append(list, g)
225 }
226 log.Printf("get_gril_from_ids: %dms", time.Since(t1).Nanoseconds()/1000000)
227 return list, err
238} 228}
239 229
240func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 230func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index c287dc5..65919e8 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -78,6 +78,7 @@ func (m *Module) getListGrils(list *models.List) error {
78 78
79 defer rows.Close() 79 defer rows.Close()
80 list.Grils = list.Grils[:0] 80 list.Grils = list.Grils[:0]
81 var ids []int
81 for rows.Next() { 82 for rows.Next() {
82 var grilID int 83 var grilID int
83 lg := &models.ListGril{} 84 lg := &models.ListGril{}
@@ -86,16 +87,21 @@ func (m *Module) getListGrils(list *models.List) error {
86 log.Println("error scanning row in getListGrils:", err) 87 log.Println("error scanning row in getListGrils:", err)
87 continue 88 continue
88 } 89 }
89 90 ids = append(ids, grilID)
90 gril, err := m.grils.FromID(grilID)
91 if err != nil {
92 log.Println("error getting listGril:", err)
93 continue
94 }
95 lg.Gril = gril
96 91
97 list.Grils = append(list.Grils, lg) 92 list.Grils = append(list.Grils, lg)
98 } 93 }
94
95 // grils holen
96 grils, err := m.grils.FromIDs(ids)
97 if err != nil {
98 return err
99 }
100
101 for id, gril := range grils {
102 list.Grils[id].Gril = gril
103 }
104
99 sort.Sort(ListGrils(list.Grils)) 105 sort.Sort(ListGrils(list.Grils))
100 return nil 106 return nil
101} 107}