go-bouquins/bouquins/bouquins.go

164 lines
2.5 KiB
Go
Raw Normal View History

2017-07-30 14:06:38 +00:00
package bouquins
2017-07-30 15:20:48 +00:00
import (
2017-07-30 18:14:20 +00:00
"database/sql"
2017-07-30 15:20:48 +00:00
"html/template"
"log"
"net/http"
)
const (
2017-07-30 18:14:20 +00:00
TPL_BOOKS = "book.html"
TPL_AUTHORS = "author.html"
2017-07-30 15:20:48 +00:00
TPL_SERIES = "series.html"
TPL_INDEX = "index.html"
)
2017-07-30 14:06:38 +00:00
type Bouquins struct {
2017-07-30 15:20:48 +00:00
*template.Template
2017-07-30 18:14:20 +00:00
*sql.DB
2017-07-30 14:06:38 +00:00
}
2017-07-30 14:42:18 +00:00
/*
* 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
2017-07-30 15:20:48 +00:00
Authors []*Author
2017-07-30 14:42:18 +00:00
Tags []string
}
type AuthorFull struct {
Author
2017-07-30 15:20:48 +00:00
Books []*BookAdv
2017-07-30 14:42:18 +00:00
}
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
2017-07-30 15:20:48 +00:00
Count int64
Authors []*Author
2017-07-30 14:42:18 +00:00
}
type SeriesFull struct {
SeriesAdv
2017-07-30 15:20:48 +00:00
Books []*Book
}
2017-07-30 16:09:27 +00:00
type BouquinsModel struct {
Title string
}
// Constructor BouquinsModel
func NewBouquinsModel(title string) *BouquinsModel {
return &BouquinsModel{
title,
}
}
2017-07-30 15:20:48 +00:00
type IndexModel struct {
2017-07-30 16:09:27 +00:00
BouquinsModel
2017-07-30 15:20:48 +00:00
BooksCount int64
Books []*BookAdv
Series []*SeriesAdv
Authors []*AuthorAdv
2017-07-30 14:42:18 +00:00
}
2017-07-30 16:09:27 +00:00
// Constructor IndexModel
func NewIndexModel(title string, count int64) *IndexModel {
return &IndexModel{
*NewBouquinsModel(title),
count,
nil,
2017-07-30 15:20:48 +00:00
nil,
nil,
}
2017-07-30 16:09:27 +00:00
}
func (app *Bouquins) render(res http.ResponseWriter, tpl string, model interface{}) {
err := app.Template.ExecuteTemplate(res, tpl, model)
2017-07-30 15:20:48 +00:00
if err != nil {
log.Print(err)
}
2017-07-30 14:06:38 +00:00
}
2017-07-30 16:09:27 +00:00
2017-07-30 18:14:20 +00:00
// ROUTES //
2017-07-30 16:09:27 +00:00
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) {
2017-07-30 18:14:20 +00:00
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)
}
2017-07-30 16:09:27 +00:00
app.render(res, TPL_INDEX, model)
}
2017-07-30 15:20:48 +00:00
func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) {
2017-07-30 16:09:27 +00:00
app.render(res, TPL_BOOKS, nil)
2017-07-30 14:06:38 +00:00
}
2017-07-30 15:20:48 +00:00
func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) {
2017-07-30 16:09:27 +00:00
app.render(res, TPL_AUTHORS, nil)
2017-07-30 14:06:38 +00:00
}
2017-07-30 15:20:48 +00:00
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) {
2017-07-30 16:09:27 +00:00
app.render(res, TPL_SERIES, nil)
2017-07-30 14:06:38 +00:00
}