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
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")
}

14
main.go
View File

@ -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")))
}

View File

@ -29,7 +29,7 @@
<div class="page-header" v-if="author.id">
<h1>
<span class="glyphicon glyphicon-user"></span>
{{ author.name }}
{{ .Author.Name }}
</h1>
</div>
<div class="alert alert-danger" role="alert" v-else>Aucun livre sélectionné</div>
@ -42,7 +42,7 @@
<div class="panel-body">
<ul v-for="book in author.books" class="list-unstyled">
<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>
</ul>
</div>
@ -51,7 +51,7 @@
<div class="panel-body">
<ul v-for="series in author.series" class="list-unstyled">
<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>
</ul>
</div>
@ -60,7 +60,7 @@
<div class="panel-body">
<ul v-for="coauthor in author.authors" class="list-unstyled">
<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>
</ul>
</div>

View File

@ -34,14 +34,14 @@
<div class="col-xs-12 col-md-9">
<h1>
<span class="glyphicon glyphicon-book"></span>
{{ book.title }}
{{ .Book.Title }}
</h1>
</div>
<div class="col-xs-12 col-md-3 text-right">
<template v-for="data in book.data">
<a :href="bookLink(book, data)" class="btn btn-success">
<span class="glyphicon glyphicon-download-alt"></span> Télécharger
{{ data.format }} ({{ formatBytes(data.size) }})
{{ .Data.Format }} ({{/* .FormatBytes(data.size) */}})
</a>
</template>
</div>
@ -50,11 +50,11 @@
<div class="alert alert-danger" role="alert" v-else>Aucun livre sélectionné</div>
<div class="row" v-if="book.id">
<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>
<ul>
<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>
</ul>
@ -62,26 +62,26 @@
<span class="glyphicon glyphicon-list"></span> Serie
</h2>
<div v-if="book.series">
<a :href="'series.html?id='+book.series.id">{{ book.series.name }}</a>
<span class="badge">{{ book.series.idx }}</span>
<a :href="'series.html?id='+book.series.id">{{ .Book.Series.Name }}</a>
<span class="badge">{{ .Book.Series.Idx }}</span>
</div>
<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">
<span class="glyphicon glyphicon-tags"></span> Tags
</h2>
<div v-if="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>
</div>
<h2>Détails</h2>
<ul>
<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.pubdate"><strong>Date de publication</strong> {{ .Book.Pubdate }}</li>
<li v-if="book.publisher"><strong>Editeur</strong> {{ .Book.Publisher }}</li>
</ul>
</div>
<!-- TODO cover -->

View File

@ -28,7 +28,7 @@
<div class="container" id="app">
<div class="jumbotron">
<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="showAuthors">Auteurs</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>
</ul>
</nav>
<table class="table table-striped" v-if="series.length > 0">
{{ if .Series }}
<table class="table table-striped">
<tr>
<th>
<a href="#" @click="sortBy('name')">Nom</a>
@ -50,20 +51,24 @@
<th>Auteur(s)</th>
</tr>
<tr v-for="serie in series">
{{ range .Series }}
<td>
<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>{{ serie.count }}</td>
<td>{{ .Count }}</td>
<td>
<template v-for="author in serie.authors">
<span class="glyphicon glyphicon-user"></span>
<a :href="'author.html?id='+author.id">{{ author.name }}</a>&nbsp;
</template>
{{ range .Authors }}
<span class="glyphicon glyphicon-user"></span>
<a :href="/authors?id={{ .Id }}">{{ .Name }}</a>&nbsp;
{{ end }}
</td>
{{ end }}
</tr>
</table>
<table class="table table-striped" v-if="authors.length > 0">
{{ end }}
{{ if .Authors }}
<table class="table table-striped">
<tr>
<th>
<a href="#" @click="sortBy('name')">Nom</a>
@ -74,12 +79,14 @@
<tr v-for="author in authors">
<td>
<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>{{ author.count }}</td>
<td>{{ .Author.Count }}</td>
</tr>
</table>
<table class="table table-striped" v-if="books.length > 0">
{{ end }}
{{ if .Books }}
<table class="table table-striped">
<tr>
<th>
<a href="#" @click="sortBy('title')">Nom</a>
@ -90,22 +97,23 @@
</tr>
<tr v-for="book in books">
<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>
<template v-for="author in book.authors">
<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>
</td>
<td>
<template v-if="book.series">
<span class="glyphicon glyphicon-list"></span>
<a :href="'series.html?id='+book.series.id">{{ book.series.name }}</a>
<span class="badge">{{ book.series ? book.series.idx : '' }}</span>
<a :href="'series.html?id='+book.series.id">{{ .Book.Series.Name }}</a>
<span class="badge">{{/* .Book.Series ? Book.Series.Idx : '' */}}</span>
</template>
</td>
</tr>
</table>
{{ end }}
<nav aria-label="Pages">
<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>

View File

@ -29,7 +29,7 @@
<div class="page-header" v-if="series">
<h1>
<span class="glyphicon glyphicon-list"></span>
{{ series.name }}
{{ .Series.Name }}
</h1>
</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)
</h2>
<ul>
<li v-for="book in series.books" class="list-unstyled">{{ book.series.idx }}.
<a :href="'book.html?id='+book.id">{{ book.title }}</a>
<li v-for="book in series.books" class="list-unstyled">{{ .Book.Series.Idx }}.
<a :href="'book.html?id='+book.id">{{ .Book.Title }}</a>
</li>
</ul>
<h2>
@ -48,7 +48,7 @@
<ul>
<li v-for="author in series.authors" class="list-unstyled">
<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>
</ul>
</template>