From b5051d2358e12f877e5c52053dcf1a16959e4ded Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 17:44:12 +0200 Subject: irgendwie angefangen 'models' zu verwenden, wir muessen mal gucken wie wir das genau gestalten wollen diff --git a/models/gril.go b/models/gril.go new file mode 100644 index 0000000..f0b8f8f --- /dev/null +++ b/models/gril.go @@ -0,0 +1,61 @@ +package models + +import ( + "database/sql" + "fmt" + "strconv" + "strings" + "time" + + "fagott.pw/grilist/modules/series" + + "github.com/lib/pq" +) + +type DataSource int + +const ( + DataSourceACD DataSource = iota + DataSourceAniDB + DataSourceAnilist +) + +type Trait struct { + Name string + Value string + OfficialValue string +} + +type Gril struct { + ID int + KanjiName string + RomajiName string + OtherNames []string + Age sql.NullInt64 + Birthday pq.NullTime + Tags []string + Traits []Trait + ForeignIDs map[DataSource]int + UpdatedAt time.Time + Lists []int + Series []series.Series +} + +// Der kram hier sollte eigentlich auch eher wo anders hin als ins Model, oder?! +func (g *Gril) Slug() string { + if g.RomajiName == "" { + return strconv.Itoa(g.ID) + } + return fmt.Sprintf( + "%d/%s", + g.ID, + strings.Replace(g.RomajiName, " ", "", -1)) +} + +// Das hier sollte auch irgendwo anders hin. +func (g *Gril) ImagePath(useThumbnail bool) string { + if useThumbnail { + return fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", g.ID) + } + return fmt.Sprintf("http://img.grilist.moe/gril/full/%d.jpg", g.ID) +} 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 @@ -package grils - -import ( - "database/sql" - "fmt" - "strconv" - "strings" - "time" - - "fagott.pw/grilist/modules/series" - - "github.com/lib/pq" -) - -type DataSource int - -const ( - DataSourceACD DataSource = iota - DataSourceAniDB - DataSourceAnilist -) - -type Trait struct { - Name string - Value string - OfficialValue string -} - -type Gril struct { - ID int - KanjiName string - RomajiName string - OtherNames []string - Age sql.NullInt64 - Birthday pq.NullTime - Tags []string - Traits []Trait - ForeignIDs map[DataSource]int - UpdatedAt time.Time - Lists []int - Series []series.Series -} - -func (g *Gril) Slug() string { - if g.RomajiName == "" { - return strconv.Itoa(g.ID) - } - return fmt.Sprintf( - "%d/%s", - g.ID, - strings.Replace(g.RomajiName, " ", "", -1)) -} - -func ImagePath(id int, useThumbnail bool) string { - if useThumbnail { - return fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", id) - } - return fmt.Sprintf("http://img.grilist.moe/gril/full/%d.jpg", id) -} - -func (g *Gril) ImagePath(prioritizeThumbnail bool) string { - return ImagePath(g.ID, prioritizeThumbnail) -} 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 ( "fagott.pw/grilist/cache" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" + "fagott.pw/grilist/models" "github.com/julienschmidt/httprouter" ) -type CachedGril struct { - Created time.Time - Gril *Gril -} - var ( pgArrayReg = regexp.MustCompile(`(((?P(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) pgValueIdx int @@ -54,8 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) { m.c = cache.New() } -func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { - var grils []*Gril +func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*models.Gril, error) { + var grils []*models.Gril 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...) if err != nil { @@ -64,7 +60,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr defer rows.Close() for rows.Next() { - gril := &Gril{} + gril := &models.Gril{} var tags []byte var otherNames []byte 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 return grils, nil } -func (m *GrilsModule) GetListsOfGril(gril *Gril) error { +func (m *GrilsModule) GetListsOfGril(gril *models.Gril) error { rows, err := m.g.DB.Query(`SELECT list_id FROM grilist.lists_grils WHERE gril_id = $1`, gril.ID) if err != nil { return err @@ -120,7 +116,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das } for rows.Next() { - var g Gril + var g models.Gril if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil { log.Println(err) continue @@ -143,12 +139,12 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das return categories } -func (m *GrilsModule) FromID(id int) (*Gril, error) { +func (m *GrilsModule) FromID(id int) (*models.Gril, error) { if g, ok := m.c.Get(id); ok { - return g.(*Gril), nil + return g.(*models.Gril), nil } - gril := &Gril{ + gril := &models.Gril{ ID: id, } t1 := time.Now() @@ -219,9 +215,9 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) { return gril, nil } -func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { +func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) { if len(ids) == 0 { - return make([]*Gril, 0), nil + return make([]*models.Gril, 0), nil } idList := "(" @@ -274,7 +270,7 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou } defer rows.Close() - var similar []*Gril + var similar []*models.Gril for rows.Next() { var id int if err := rows.Scan(&id); err != nil { diff --git a/modules/lists/lists.go b/modules/lists/lists.go index d1913be..5934e9a 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -16,6 +16,7 @@ import ( "fagott.pw/grilist/cache" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" + "fagott.pw/grilist/models" "fagott.pw/grilist/modules/grils" "github.com/julienschmidt/httprouter" "github.com/lib/pq" @@ -41,7 +42,7 @@ type List struct { // ListGril ist ein geranktes Gril type ListGril struct { - Gril *grils.Gril + Gril *models.Gril Order int } diff --git a/modules/search/search.go b/modules/search/search.go index a614c08..4048dbe 100644 --- a/modules/search/search.go +++ b/modules/search/search.go @@ -1,6 +1,7 @@ package search import ( + "fmt" "log" "net/http" @@ -72,7 +73,9 @@ func (m *Module) instantSearchGril(w http.ResponseWriter, r *http.Request, p htt continue } - result.ImagePath = grils.ImagePath(result.ID, true) + // Jan: Das ist hier irgendwie scheisse, aber wir bauen ja grad irgendwie den kram auf Models um. Wir brauchen da + // irgendwas besseres. + result.ImagePath = fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", result.ID) results = append(results, result) } -- cgit v0.10.1