package bouquins
import (
"database/sql"
"html/template"
"log"
"net/http"
)
const (
TPL_BOOKS = "book.html"
TPL_AUTHORS = "author.html"
TPL_SERIES = "series.html"
TPL_INDEX = "index.html"
)
type Bouquins struct {
*template.Template
*sql.DB
}
/*
* A book series.
*/
type Series struct {
Id int64
Name string
}
/*
* A book. Generic data.
*/
type Book struct {
Id int64
Title string
SeriesIndex int
Series *Series
}
/*
* An author.
*/
type Author struct {
Id int64
Name string
}
/*
* Author and number of books.
*/
type AuthorAdv struct {
Author
Count int
}
/*
* Downloadable book data.
*/
type BookData struct {
Size int64
Format string
Name string
}
/*
* A book. Advanced data: authors, tags.
*/
type BookAdv struct {
Book
Authors []*Author
Tags []string
}
type AuthorFull struct {
Author
Books []*BookAdv
}
type BookFull struct {
BookAdv
Data []BookData
Timestamp int64
Pubdate int64
Isbn string
Lccn string
Path string
Uuid string
Has_cover bool
Lang string
Publisher string
}
type SeriesAdv struct {
Series
Count int64
Authors []*Author
}
type SeriesFull struct {
SeriesAdv
Books []*Book
}
type BouquinsModel struct {
Title string
}
// Constructor BouquinsModel
func NewBouquinsModel(title string) *BouquinsModel {
return &BouquinsModel{
title,
}
}
type IndexModel struct {
BouquinsModel
BooksCount int64
Books []*BookAdv
Series []*SeriesAdv
Authors []*AuthorAdv
}
// Constructor IndexModel
func NewIndexModel(title string, count int64) *IndexModel {
return &IndexModel{
*NewBouquinsModel(title),
count,
nil,
nil,
nil,
}
}
func (app *Bouquins) render(res http.ResponseWriter, tpl string, model interface{}) {
err := app.Template.ExecuteTemplate(res, tpl, model)
if err != nil {
log.Print(err)
}
}
// ROUTES //
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) {
count, err := app.BookCount()
if err != nil {
log.Print(err)
}
model := NewIndexModel("", count)
model.Books, err = app.BooksAdv(0, 0)
if err != nil {
log.Print(err)
}
app.render(res, TPL_INDEX, model)
}
func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) {
app.render(res, TPL_BOOKS, nil)
}
func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) {
app.render(res, TPL_AUTHORS, nil)
}
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) {
app.render(res, TPL_SERIES, nil)
}