From 8f3ef0128594f54653091579be7dcb00faebc966 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 3 Nov 2016 09:01:23 +0900 Subject: [PATCH] Added genre lists --- {pages/anime => components}/anime.pixy | 4 +- .../dashboard => components}/dashboard.pixy | 0 {pages/genres => components}/genres.pixy | 6 ++- components/icon.pixy | 2 + helper.go | 27 ++++++++++ main.go | 51 +++++++------------ 6 files changed, 52 insertions(+), 38 deletions(-) rename {pages/anime => components}/anime.pixy (97%) rename {pages/dashboard => components}/dashboard.pixy (100%) rename {pages/genres => components}/genres.pixy (53%) create mode 100644 components/icon.pixy create mode 100644 helper.go diff --git a/pages/anime/anime.pixy b/components/anime.pixy similarity index 97% rename from pages/anime/anime.pixy rename to components/anime.pixy index 567b6510..cad645d7 100644 --- a/pages/anime/anime.pixy +++ b/components/anime.pixy @@ -41,8 +41,8 @@ component Anime(anime *arn.Anime) .light-button-group each genre in anime.Genres if genre != "" - a.light-button.ajax(href="/anime/genres/" + arn.FixGenre(genre)) - i(class="fa fa-" + GenreIcons[genre] + " fa-fw") + a.light-button.ajax(href="/genres/" + arn.FixGenre(genre)) + Icon(GenreIcons[genre]) span= genre if len(anime.Studios) > 0 diff --git a/pages/dashboard/dashboard.pixy b/components/dashboard.pixy similarity index 100% rename from pages/dashboard/dashboard.pixy rename to components/dashboard.pixy diff --git a/pages/genres/genres.pixy b/components/genres.pixy similarity index 53% rename from pages/genres/genres.pixy rename to components/genres.pixy index 814353df..8529ec14 100644 --- a/pages/genres/genres.pixy +++ b/components/genres.pixy @@ -9,5 +9,7 @@ component GenreOverview Icon(GenreIcons[genre]) span= genre -component Icon(name string) - i(class="fa fa-fw fa-" + name) \ No newline at end of file +component AnimeInGenre(genre string, animeList []*arn.Anime) + h2= genre + each anime in animeList + p= anime.Title.Romaji + " (" + s(anime.Watching) + ")" \ No newline at end of file diff --git a/components/icon.pixy b/components/icon.pixy new file mode 100644 index 00000000..3ee06a4a --- /dev/null +++ b/components/icon.pixy @@ -0,0 +1,2 @@ +component Icon(name string) + i(class="fa fa-fw fa-" + name) \ No newline at end of file diff --git a/helper.go b/helper.go new file mode 100644 index 00000000..be9f6745 --- /dev/null +++ b/helper.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +// Converts anything into a string +func s(v interface{}) string { + return fmt.Sprintf("%v", v) +} + +// Contains ... +func Contains(list []string, a string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + +// Map ... +func Map(original []string, f func(string) string) []string { + mapped := make([]string, len(original)) + for index, value := range original { + mapped[index] = f(value) + } + return mapped +} diff --git a/main.go b/main.go index 8aaff89e..2969fa14 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io/ioutil" "sort" "strconv" @@ -12,10 +11,6 @@ import ( "github.com/animenotifier/arn" ) -func s(v interface{}) string { - return fmt.Sprintf("%v", v) -} - func main() { app := aero.New() @@ -107,39 +102,27 @@ func main() { ctx.HTML(Render.GenreOverview()) }) + app.Get("/genres/:name", func(ctx *aero.Context) { + genreName := ctx.Params.ByName("name") + + var animeList []*arn.Anime + results := make(chan *arn.Anime) + arn.Scan("Anime", results) + + for anime := range results { + genres := Map(anime.Genres, arn.FixGenre) + if Contains(genres, genreName) { + animeList = append(animeList, anime) + } + } + + ctx.HTML(Render.Layout(Render.AnimeInGenre(genreName, animeList), css)) + }) + app.Get("/scripts.js", func(ctx *aero.Context) { ctx.SetHeader("Content-Type", "application/javascript") ctx.Respond(js) }) - // layout := aero.NewTemplate("layout.pug") - // template := aero.NewTemplate("anime.pug") - - // app.Get("/anime/:id", func(ctx *aero.Context) { - // id, _ := strconv.Atoi(ctx.Params.ByName("id")) - // anime, err := arn.GetAnime(id) - - // if err != nil { - // ctx.Respond("Anime not found") - // return - // } - - // content := template.Render(map[string]interface{}{ - // "anime": anime, - // }) - - // final := layout.Render(map[string]interface{}{ - // "content": content, - // }) - - // final = strings.Replace(final, cssSearch, cssReplace, 1) - - // ctx.Respond(final) - // }) - - // app.Get("/t", func(ctx *aero.Context) { - // ctx.Respond(templates.Hello("abc")) - // }) - app.Run() }