aboutsummaryrefslogtreecommitdiff
path: root/modules/grils
diff options
context:
space:
mode:
Diffstat (limited to 'modules/grils')
-rw-r--r--modules/grils/gril.go63
-rw-r--r--modules/grils/grils.go28
2 files changed, 12 insertions, 79 deletions
diff --git a/modules/grils/gril.go b/modules/grils/gril.go
deleted file mode 100644
index 7b54ab1..0000000
--- a/modules/grils/gril.go
+++ /dev/null
@@ -1,63 +0,0 @@
1package grils
2
3import (
4 "database/sql"
5 "fmt"
6 "strconv"
7 "strings"
8 "time"
9
10 "fagott.pw/grilist/modules/series"
11
12 "github.com/lib/pq"
13)
14
15type DataSource int
16
17const (
18 DataSourceACD DataSource = iota
19 DataSourceAniDB
20 DataSourceAnilist
21)
22
23type Trait struct {
24 Name string
25 Value string
26 OfficialValue string
27}
28
29type Gril struct {
30 ID int
31 KanjiName string
32 RomajiName string
33 OtherNames []string
34 Age sql.NullInt64
35 Birthday pq.NullTime
36 Tags []string
37 Traits []Trait
38 ForeignIDs map[DataSource]int
39 UpdatedAt time.Time
40 Lists []int
41 Series []series.Series
42}
43
44func (g *Gril) Slug() string {
45 if g.RomajiName == "" {
46 return strconv.Itoa(g.ID)
47 }
48 return fmt.Sprintf(
49 "%d/%s",
50 g.ID,
51 strings.Replace(g.RomajiName, " ", "", -1))
52}
53
54func ImagePath(id int, useThumbnail bool) string {
55 if useThumbnail {
56 return fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", id)
57 }
58 return fmt.Sprintf("http://img.grilist.moe/gril/full/%d.jpg", id)
59}
60
61func (g *Gril) ImagePath(prioritizeThumbnail bool) string {
62 return ImagePath(g.ID, prioritizeThumbnail)
63}
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index debb77b..4a9990f 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -14,15 +14,11 @@ import (
14 "fagott.pw/grilist/eventlogging" 14 "fagott.pw/grilist/eventlogging"
15 "fagott.pw/grilist/frontend" 15 "fagott.pw/grilist/frontend"
16 "fagott.pw/grilist/grilist" 16 "fagott.pw/grilist/grilist"
17 "fagott.pw/grilist/models"
17 18
18 "github.com/julienschmidt/httprouter" 19 "github.com/julienschmidt/httprouter"
19) 20)
20 21
21type CachedGril struct {
22 Created time.Time
23 Gril *Gril
24}
25
26var ( 22var (
27 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) 23 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`)
28 pgValueIdx int 24 pgValueIdx int
@@ -55,8 +51,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) {
55 m.c = cache.New() 51 m.c = cache.New()
56} 52}
57 53
58func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { 54func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*models.Gril, error) {
59 var grils []*Gril 55 var grils []*models.Gril
60 56
61 rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, kanji_name, romaji_name, other_names, updated_at, age, birthday, tags FROM grilist.grils_flattened WHERE %s`, whereClause), params...) 57 rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, kanji_name, romaji_name, other_names, updated_at, age, birthday, tags FROM grilist.grils_flattened WHERE %s`, whereClause), params...)
62 if err != nil { 58 if err != nil {
@@ -65,7 +61,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr
65 61
66 defer rows.Close() 62 defer rows.Close()
67 for rows.Next() { 63 for rows.Next() {
68 gril := &Gril{} 64 gril := &models.Gril{}
69 var tags []byte 65 var tags []byte
70 var otherNames []byte 66 var otherNames []byte
71 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags) 67 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags)
@@ -84,7 +80,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr
84 return grils, nil 80 return grils, nil
85} 81}
86 82
87func (m *GrilsModule) GetListsOfGril(gril *Gril) error { 83func (m *GrilsModule) GetListsOfGril(gril *models.Gril) error {
88 rows, err := m.g.DB.Query(`SELECT list_id FROM grilist.lists_grils WHERE gril_id = $1`, gril.ID) 84 rows, err := m.g.DB.Query(`SELECT list_id FROM grilist.lists_grils WHERE gril_id = $1`, gril.ID)
89 if err != nil { 85 if err != nil {
90 return err 86 return err
@@ -121,7 +117,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
121 } 117 }
122 118
123 for rows.Next() { 119 for rows.Next() {
124 var g Gril 120 var g models.Gril
125 if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil { 121 if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil {
126 log.Println(err) 122 log.Println(err)
127 continue 123 continue
@@ -144,12 +140,12 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
144 return categories 140 return categories
145} 141}
146 142
147func (m *GrilsModule) FromID(id int) (*Gril, error) { 143func (m *GrilsModule) FromID(id int) (*models.Gril, error) {
148 if g, ok := m.c.Get(id); ok { 144 if g, ok := m.c.Get(id); ok {
149 return g.(*Gril), nil 145 return g.(*models.Gril), nil
150 } 146 }
151 147
152 gril := &Gril{ 148 gril := &models.Gril{
153 ID: id, 149 ID: id,
154 } 150 }
155 t1 := time.Now() 151 t1 := time.Now()
@@ -220,9 +216,9 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) {
220 return gril, nil 216 return gril, nil
221} 217}
222 218
223func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { 219func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
224 if len(ids) == 0 { 220 if len(ids) == 0 {
225 return make([]*Gril, 0), nil 221 return make([]*models.Gril, 0), nil
226 } 222 }
227 223
228 idList := "(" 224 idList := "("
@@ -276,7 +272,7 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou
276 } 272 }
277 273
278 defer rows.Close() 274 defer rows.Close()
279 var similar []*Gril 275 var similar []*models.Gril
280 for rows.Next() { 276 for rows.Next() {
281 var id int 277 var id int
282 if err := rows.Scan(&id); err != nil { 278 if err := rows.Scan(&id); err != nil {