HTML to templates (test index/Series)

This commit is contained in:
Meutel 2017-07-30 17:20:48 +02:00
parent 7591a889ec
commit a158f158f2
6 changed files with 103 additions and 48 deletions

View File

@ -1,8 +1,20 @@
package bouquins 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 { type Bouquins struct {
*template.Template
} }
/* /*
@ -53,13 +65,13 @@ type BookData struct {
*/ */
type BookAdv struct { type BookAdv struct {
Book Book
Authors []Author Authors []*Author
Tags []string Tags []string
} }
type AuthorFull struct { type AuthorFull struct {
Author Author
Books []BookAdv Books []*BookAdv
} }
type BookFull struct { type BookFull struct {
@ -78,24 +90,51 @@ type BookFull struct {
type SeriesAdv struct { type SeriesAdv struct {
Series Series
Books int64 Count int64
Authors []Author Authors []*Author
} }
type SeriesFull struct { type SeriesFull struct {
SeriesAdv SeriesAdv
Bools []Book Books []*Book
} }
func (*Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { type IndexModel struct {
http.Redirect(res, req, "/html/index.html", http.StatusSeeOther) 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") 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") 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") panic("not implemented")
} }

14
main.go
View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"html/template"
"log"
"net/http" "net/http"
"meutel.net/meutel/go-bouquins/bouquins" "meutel.net/meutel/go-bouquins/bouquins"
@ -11,19 +13,25 @@ const (
BOOKS = "/books" BOOKS = "/books"
AUTHORS = "/authors" AUTHORS = "/authors"
SERIES = "/series" SERIES = "/series"
HTML = "/html/"
JS = "/js/" JS = "/js/"
CSS = "/css/" CSS = "/css/"
) )
func init() { func init() {
app := new(bouquins.Bouquins) tpl, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalln(err)
}
app := &bouquins.Bouquins{
tpl,
}
assets() assets()
router(app) router(app)
} }
func assets() { func assets() {
http.Handle(HTML, http.FileServer(http.Dir("assets")))
http.Handle(JS, http.FileServer(http.Dir("assets"))) http.Handle(JS, http.FileServer(http.Dir("assets")))
http.Handle(CSS, http.FileServer(http.Dir("assets"))) http.Handle(CSS, http.FileServer(http.Dir("assets")))
} }

View File

@ -29,7 +29,7 @@
<div class="page-header" v-if="author.id"> <div class="page-header" v-if="author.id">
<h1> <h1>
<span class="glyphicon glyphicon-user"></span> <span class="glyphicon glyphicon-user"></span>
{{ author.name }} {{ .Author.Name }}
</h1> </h1>
</div> </div>
<div class="alert alert-danger" role="alert" v-else>Aucun livre sélectionné</div> <div class="alert alert-danger" role="alert" v-else>Aucun livre sélectionné</div>
@ -42,7 +42,7 @@
<div class="panel-body"> <div class="panel-body">
<ul v-for="book in author.books" class="list-unstyled"> <ul v-for="book in author.books" class="list-unstyled">
<li><span class="glyphicon glyphicon-book"></span> <li><span class="glyphicon glyphicon-book"></span>
<a :href="'book.html?id='+book.id">{{ book.title }}</a> <a :href="'book.html?id='+book.id">{{ .Book.Title }}</a>
</li> </li>
</ul> </ul>
</div> </div>
@ -51,7 +51,7 @@
<div class="panel-body"> <div class="panel-body">
<ul v-for="series in author.series" class="list-unstyled"> <ul v-for="series in author.series" class="list-unstyled">
<li><span class="glyphicon glyphicon-list"></span> <li><span class="glyphicon glyphicon-list"></span>
<a :href="'series.html?id='+series.id">{{ series.name }}</a> <a :href="'series.html?id='+series.id">{{ .Series.Name }}</a>
</li> </li>
</ul> </ul>
</div> </div>
@ -60,7 +60,7 @@
<div class="panel-body"> <div class="panel-body">
<ul v-for="coauthor in author.authors" class="list-unstyled"> <ul v-for="coauthor in author.authors" class="list-unstyled">
<li> <span class="glyphicon glyphicon-user"></span> <li> <span class="glyphicon glyphicon-user"></span>
<a :href="'author.html?id='+coauthor.id">{{ coauthor.name }}</a> <a :href="'author.html?id='+coauthor.id">{{ .Coauthor.Name }}</a>
</li> </li>
</ul> </ul>
</div> </div>

View File

@ -34,14 +34,14 @@
<div class="col-xs-12 col-md-9"> <div class="col-xs-12 col-md-9">
<h1> <h1>
<span class="glyphicon glyphicon-book"></span> <span class="glyphicon glyphicon-book"></span>
{{ book.title }} {{ .Book.Title }}
</h1> </h1>
</div> </div>
<div class="col-xs-12 col-md-3 text-right"> <div class="col-xs-12 col-md-3 text-right">
<template v-for="data in book.data"> <template v-for="data in book.data">
<a :href="bookLink(book, data)" class="btn btn-success"> <a :href="bookLink(book, data)" class="btn btn-success">
<span class="glyphicon glyphicon-download-alt"></span> Télécharger <span class="glyphicon glyphicon-download-alt"></span> Télécharger
{{ data.format }} ({{ formatBytes(data.size) }}) {{ .Data.Format }} ({{/* .FormatBytes(data.size) */}})
</a> </a>
</template> </template>
</div> </div>
@ -50,11 +50,11 @@
<div class="alert alert-danger" role="alert" v-else>Aucun livre sélectionné</div> <div class="alert alert-danger" role="alert" v-else>Aucun livre sélectionné</div>
<div class="row" v-if="book.id"> <div class="row" v-if="book.id">
<h2> <h2>
<span class="glyphicon glyphicon-user"></span> Auteur{{ book.authors.length > 1 ? 's' : '' }} <span class="glyphicon glyphicon-user"></span> Auteur{{/* .Book.Authors.Length > 1 ? 's' : '' */}}
</h2> </h2>
<ul> <ul>
<li v-for="author in book.authors"> <li v-for="author in book.authors">
<a :href="'author.html?id='+author.id">{{ author.name }}</a> <a :href="'author.html?id='+author.id">{{ .Author.Name }}</a>
</li> </li>
</ul> </ul>
@ -62,26 +62,26 @@
<span class="glyphicon glyphicon-list"></span> Serie <span class="glyphicon glyphicon-list"></span> Serie
</h2> </h2>
<div v-if="book.series"> <div v-if="book.series">
<a :href="'series.html?id='+book.series.id">{{ book.series.name }}</a> <a :href="'series.html?id='+book.series.id">{{ .Book.Series.Name }}</a>
<span class="badge">{{ book.series.idx }}</span> <span class="badge">{{ .Book.Series.Idx }}</span>
</div> </div>
<h2><span class="glyphicon glyphicon-globe"></span> Langue</h2> <h2><span class="glyphicon glyphicon-globe"></span> Langue</h2>
<ul><li>{{ book.lang.toUpperCase() }}</li></ul> <ul><li>{{/* .Book.Lang.toUpperCase() */}}</li></ul>
<h2 v-if="book.tags"> <h2 v-if="book.tags">
<span class="glyphicon glyphicon-tags"></span> Tags <span class="glyphicon glyphicon-tags"></span> Tags
</h2> </h2>
<div v-if="book.tags"> <div v-if="book.tags">
<template v-for="tag in book.tags"> <template v-for="tag in book.tags">
<span class="label label-info">{{ tag }}</span>&nbsp; <span class="label label-info">{{ .Tag }}</span>&nbsp;
</template> </template>
</div> </div>
<h2>Détails</h2> <h2>Détails</h2>
<ul> <ul>
<li v-if="book.pubdate"><strong>Date de publication</strong> {{ book.pubdate }}</li> <li v-if="book.pubdate"><strong>Date de publication</strong> {{ .Book.Pubdate }}</li>
<li v-if="book.publisher"><strong>Editeur</strong> {{ book.publisher }}</li> <li v-if="book.publisher"><strong>Editeur</strong> {{ .Book.Publisher }}</li>
</ul> </ul>
</div> </div>
<!-- TODO cover --> <!-- TODO cover -->

View File

@ -28,7 +28,7 @@
<div class="container" id="app"> <div class="container" id="app">
<div class="jumbotron"> <div class="jumbotron">
<h1>Bouquins</h1> <h1>Bouquins</h1>
<p>Cette bibliothèque contient actuellement <strong>{{ booksCount }}</strong> livres et BD en format papier ou électronique.</p> <p>Cette bibliothèque contient actuellement <strong>{{ .BooksCount }}</strong> livres et BD en format papier ou électronique.</p>
<button class="btn btn-primary" type="button" @click="showBooks">Livres</button> <button class="btn btn-primary" type="button" @click="showBooks">Livres</button>
<button class="btn btn-primary" type="button" @click="showAuthors">Auteurs</button> <button class="btn btn-primary" type="button" @click="showAuthors">Auteurs</button>
<button class="btn btn-primary" type="button" @click="showSeries">Series</button> <button class="btn btn-primary" type="button" @click="showSeries">Series</button>
@ -40,7 +40,8 @@
<li class="next"><a href="#" @click="nextPage">Suivants <span aria-hidden="true">&rarr;</span></a></li> <li class="next"><a href="#" @click="nextPage">Suivants <span aria-hidden="true">&rarr;</span></a></li>
</ul> </ul>
</nav> </nav>
<table class="table table-striped" v-if="series.length > 0"> {{ if .Series }}
<table class="table table-striped">
<tr> <tr>
<th> <th>
<a href="#" @click="sortBy('name')">Nom</a> <a href="#" @click="sortBy('name')">Nom</a>
@ -50,20 +51,24 @@
<th>Auteur(s)</th> <th>Auteur(s)</th>
</tr> </tr>
<tr v-for="serie in series"> <tr v-for="serie in series">
{{ range .Series }}
<td> <td>
<span class="glyphicon glyphicon-list"></span> <span class="glyphicon glyphicon-list"></span>
<a :href="'series.html?id='+serie.id">{{ serie.name }}</a> <a href="/series?id={{ .Id }}">{{ .Name }}</a>
</td> </td>
<td>{{ serie.count }}</td> <td>{{ .Count }}</td>
<td> <td>
<template v-for="author in serie.authors"> {{ range .Authors }}
<span class="glyphicon glyphicon-user"></span> <span class="glyphicon glyphicon-user"></span>
<a :href="'author.html?id='+author.id">{{ author.name }}</a>&nbsp; <a :href="/authors?id={{ .Id }}">{{ .Name }}</a>&nbsp;
</template> {{ end }}
</td> </td>
{{ end }}
</tr> </tr>
</table> </table>
<table class="table table-striped" v-if="authors.length > 0"> {{ end }}
{{ if .Authors }}
<table class="table table-striped">
<tr> <tr>
<th> <th>
<a href="#" @click="sortBy('name')">Nom</a> <a href="#" @click="sortBy('name')">Nom</a>
@ -74,12 +79,14 @@
<tr v-for="author in authors"> <tr v-for="author in authors">
<td> <td>
<span class="glyphicon glyphicon-user"></span> <span class="glyphicon glyphicon-user"></span>
<a :href="'author.html?id='+author.id">{{ author.name }}</a> <a :href="'author.html?id='+author.id">{{ .Author.Name }}</a>
</td> </td>
<td>{{ author.count }}</td> <td>{{ .Author.Count }}</td>
</tr> </tr>
</table> </table>
<table class="table table-striped" v-if="books.length > 0"> {{ end }}
{{ if .Books }}
<table class="table table-striped">
<tr> <tr>
<th> <th>
<a href="#" @click="sortBy('title')">Nom</a> <a href="#" @click="sortBy('title')">Nom</a>
@ -90,22 +97,23 @@
</tr> </tr>
<tr v-for="book in books"> <tr v-for="book in books">
<td><span class="glyphicon glyphicon-book"></span> <td><span class="glyphicon glyphicon-book"></span>
<a :href="'book.html?id='+book.id">{{ book.title }}</a></td> <a :href="'book.html?id='+book.id">{{ .Book.Title }}</a></td>
<td> <td>
<template v-for="author in book.authors"> <template v-for="author in book.authors">
<span class="glyphicon glyphicon-user"></span> <span class="glyphicon glyphicon-user"></span>
<a :href="'author.html?id='+author.id">{{ author.name }}</a> <a :href="'author.html?id='+author.id">{{ .Author.Name }}</a>
</template> </template>
</td> </td>
<td> <td>
<template v-if="book.series"> <template v-if="book.series">
<span class="glyphicon glyphicon-list"></span> <span class="glyphicon glyphicon-list"></span>
<a :href="'series.html?id='+book.series.id">{{ book.series.name }}</a> <a :href="'series.html?id='+book.series.id">{{ .Book.Series.Name }}</a>
<span class="badge">{{ book.series ? book.series.idx : '' }}</span> <span class="badge">{{/* .Book.Series ? Book.Series.Idx : '' */}}</span>
</template> </template>
</td> </td>
</tr> </tr>
</table> </table>
{{ end }}
<nav aria-label="Pages"> <nav aria-label="Pages">
<ul class="pager"> <ul class="pager">
<li class="previous" v-bind:class="{ disabled: page <= 1 }"><a href="#" @click="prevPage"><span aria-hidden="true">&larr;</span> Précédents</a></li> <li class="previous" v-bind:class="{ disabled: page <= 1 }"><a href="#" @click="prevPage"><span aria-hidden="true">&larr;</span> Précédents</a></li>

View File

@ -29,7 +29,7 @@
<div class="page-header" v-if="series"> <div class="page-header" v-if="series">
<h1> <h1>
<span class="glyphicon glyphicon-list"></span> <span class="glyphicon glyphicon-list"></span>
{{ series.name }} {{ .Series.Name }}
</h1> </h1>
</div> </div>
<div class="alert alert-danger" role="alert" v-else>Aucune série sélectionnée</div> <div class="alert alert-danger" role="alert" v-else>Aucune série sélectionnée</div>
@ -38,8 +38,8 @@
<span class="glyphicon glyphicon-book"></span> Livre(s) <span class="glyphicon glyphicon-book"></span> Livre(s)
</h2> </h2>
<ul> <ul>
<li v-for="book in series.books" class="list-unstyled">{{ book.series.idx }}. <li v-for="book in series.books" class="list-unstyled">{{ .Book.Series.Idx }}.
<a :href="'book.html?id='+book.id">{{ book.title }}</a> <a :href="'book.html?id='+book.id">{{ .Book.Title }}</a>
</li> </li>
</ul> </ul>
<h2> <h2>
@ -48,7 +48,7 @@
<ul> <ul>
<li v-for="author in series.authors" class="list-unstyled"> <li v-for="author in series.authors" class="list-unstyled">
<span class="glyphicon glyphicon-user"></span> <span class="glyphicon glyphicon-user"></span>
<a :href="'author.html?id='+author.id">{{ author.name }}</a> <a :href="'author.html?id='+author.id">{{ .Author.Name }}</a>
</li> </li>
</ul> </ul>
</template> </template>