diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index aee20cf..a2aaa94 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -1,8 +1,20 @@ package bouquins -import "net/http" +import ( + "html/template" + "log" + "net/http" +) + +const ( + TPL_BOOKS = "books.html" + TPL_AUTHORS = "authors.html" + TPL_SERIES = "series.html" + TPL_INDEX = "index.html" +) type Bouquins struct { + *template.Template } /* @@ -53,13 +65,13 @@ type BookData struct { */ type BookAdv struct { Book - Authors []Author + Authors []*Author Tags []string } type AuthorFull struct { Author - Books []BookAdv + Books []*BookAdv } type BookFull struct { @@ -78,24 +90,51 @@ type BookFull struct { type SeriesAdv struct { Series - Books int64 - Authors []Author + Count int64 + Authors []*Author } type SeriesFull struct { SeriesAdv - Bools []Book + Books []*Book } -func (*Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { - http.Redirect(res, req, "/html/index.html", http.StatusSeeOther) +type IndexModel struct { + BooksCount int64 + Books []*BookAdv + Series []*SeriesAdv + Authors []*AuthorAdv } -func (*Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { + +func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { + series := make([]*SeriesAdv, 0) + a1 := make([]*Author, 0) + s1 := &SeriesAdv{ + Series{ + 666, + "Serie 1", + }, + 12, + a1, + } + series = append(series, s1) + model := &IndexModel{ + 123, + nil, + series, + nil, + } + err := app.Template.ExecuteTemplate(res, TPL_INDEX, model) + if err != nil { + log.Print(err) + } +} +func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { panic("not implemented") } -func (*Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) { +func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) { panic("not implemented") } -func (*Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { +func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { panic("not implemented") } diff --git a/main.go b/main.go index ae2d5a4..dc5c061 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "html/template" + "log" "net/http" "meutel.net/meutel/go-bouquins/bouquins" @@ -11,19 +13,25 @@ const ( BOOKS = "/books" AUTHORS = "/authors" SERIES = "/series" - HTML = "/html/" JS = "/js/" CSS = "/css/" ) func init() { - app := new(bouquins.Bouquins) + tpl, err := template.ParseGlob("templates/*.html") + if err != nil { + log.Fatalln(err) + } + + app := &bouquins.Bouquins{ + tpl, + } + assets() router(app) } func assets() { - http.Handle(HTML, http.FileServer(http.Dir("assets"))) http.Handle(JS, http.FileServer(http.Dir("assets"))) http.Handle(CSS, http.FileServer(http.Dir("assets"))) } diff --git a/assets/html/author.html b/templates/author.html similarity index 91% rename from assets/html/author.html rename to templates/author.html index e7c881c..90a9264 100644 --- a/assets/html/author.html +++ b/templates/author.html @@ -29,7 +29,7 @@
Cette bibliothèque contient actuellement {{ booksCount }} livres et BD en format papier ou électronique.
+Cette bibliothèque contient actuellement {{ .BooksCount }} livres et BD en format papier ou électronique.
@@ -40,7 +40,8 @@Nom @@ -50,20 +51,24 @@ | Auteur(s) | ||
---|---|---|---|
- {{ serie.name }} + {{ .Name }} | -{{ serie.count }} | +{{ .Count }} | - - - {{ author.name }} - + {{ range .Authors }} + + {{ .Name }} + {{ end }} | + {{ end }}
Nom @@ -74,12 +79,14 @@ | ||
---|---|---|
- {{ author.name }} + {{ .Author.Name }} | -{{ author.count }} | +{{ .Author.Count }} |
Nom @@ -90,22 +97,23 @@ | ||
---|---|---|
- {{ book.title }} | + {{ .Book.Title }}- {{ author.name }} + {{ .Author.Name }} | - {{ book.series.name }} - {{ book.series ? book.series.idx : '' }} + {{ .Book.Series.Name }} + {{/* .Book.Series ? Book.Series.Idx : '' */}} |