aboutsummaryrefslogtreecommitdiff
path: root/modules/grils
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-10-11 15:44:12 (UTC)
committerjan <jan@ruken.pw>2016-10-11 15:44:12 (UTC)
commitb5051d2358e12f877e5c52053dcf1a16959e4ded (patch)
tree43c9edcd70facc9becd1d6de8c1a454625ac9762 /modules/grils
parente2d40c07c10ba46c7b32bbfbbf00493f40fe3c56 (diff)
irgendwie angefangen 'models' zu verwenden, wir muessen mal gucken wie wir das genau gestalten wollen
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 4913e11..1b1bb5f 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -13,15 +13,11 @@ import (
13 "fagott.pw/grilist/cache" 13 "fagott.pw/grilist/cache"
14 "fagott.pw/grilist/frontend" 14 "fagott.pw/grilist/frontend"
15 "fagott.pw/grilist/grilist" 15 "fagott.pw/grilist/grilist"
16 "fagott.pw/grilist/models"
16 17
17 "github.com/julienschmidt/httprouter" 18 "github.com/julienschmidt/httprouter"
18) 19)
19 20
20type CachedGril struct {
21 Created time.Time
22 Gril *Gril
23}
24
25var ( 21var (
26 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) 22 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`)
27 pgValueIdx int 23 pgValueIdx int
@@ -54,8 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) {
54 m.c = cache.New() 50 m.c = cache.New()
55} 51}
56 52
57func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { 53func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*models.Gril, error) {
58 var grils []*Gril 54 var grils []*models.Gril
59 55
60 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...) 56 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...)
61 if err != nil { 57 if err != nil {
@@ -64,7 +60,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr
64 60
65 defer rows.Close() 61 defer rows.Close()
66 for rows.Next() { 62 for rows.Next() {
67 gril := &Gril{} 63 gril := &models.Gril{}
68 var tags []byte 64 var tags []byte
69 var otherNames []byte 65 var otherNames []byte
70 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags) 66 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags)
@@ -83,7 +79,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr
83 return grils, nil 79 return grils, nil
84} 80}
85 81
86func (m *GrilsModule) GetListsOfGril(gril *Gril) error { 82func (m *GrilsModule) GetListsOfGril(gril *models.Gril) error {
87 rows, err := m.g.DB.Query(`SELECT list_id FROM grilist.lists_grils WHERE gril_id = $1`, gril.ID) 83 rows, err := m.g.DB.Query(`SELECT list_id FROM grilist.lists_grils WHERE gril_id = $1`, gril.ID)
88 if err != nil { 84 if err != nil {
89 return err 85 return err
@@ -120,7 +116,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
120 } 116 }
121 117
122 for rows.Next() { 118 for rows.Next() {
123 var g Gril 119 var g models.Gril
124 if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil { 120 if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil {
125 log.Println(err) 121 log.Println(err)
126 continue 122 continue
@@ -143,12 +139,12 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
143 return categories 139 return categories
144} 140}
145 141
146func (m *GrilsModule) FromID(id int) (*Gril, error) { 142func (m *GrilsModule) FromID(id int) (*models.Gril, error) {
147 if g, ok := m.c.Get(id); ok { 143 if g, ok := m.c.Get(id); ok {
148 return g.(*Gril), nil 144 return g.(*models.Gril), nil
149 } 145 }
150 146
151 gril := &Gril{ 147 gril := &models.Gril{
152 ID: id, 148 ID: id,
153 } 149 }
154 t1 := time.Now() 150 t1 := time.Now()
@@ -219,9 +215,9 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) {
219 return gril, nil 215 return gril, nil
220} 216}
221 217
222func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { 218func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
223 if len(ids) == 0 { 219 if len(ids) == 0 {
224 return make([]*Gril, 0), nil 220 return make([]*models.Gril, 0), nil
225 } 221 }
226 222
227 idList := "(" 223 idList := "("
@@ -274,7 +270,7 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou
274 } 270 }
275 271
276 defer rows.Close() 272 defer rows.Close()
277 var similar []*Gril 273 var similar []*models.Gril
278 for rows.Next() { 274 for rows.Next() {
279 var id int 275 var id int
280 if err := rows.Scan(&id); err != nil { 276 if err := rows.Scan(&id); err != nil {