go-bouquins/bouquins/bouquins.go

226 lines
4.7 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-31 18:49:27 +00:00
"encoding/json"
2017-07-30 15:20:48 +00:00
"html/template"
"log"
"net/http"
2017-08-01 18:00:20 +00:00
"strconv"
"strings"
2017-07-30 15:20:48 +00:00
)
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-31 18:14:57 +00:00
PARAM_LIST = "list"
PARAM_ORDER = "order"
PARAM_SORT = "sort"
LIST_AUTHORS = "authors"
LIST_SERIES = "series"
LIST_BOOKS = "books"
2017-08-01 18:00:20 +00:00
URL_INDEX = "/"
URL_BOOKS = "/books/"
URL_AUTHORS = "/authors/"
URL_SERIES = "/series/"
URL_JS = "/js/"
URL_CSS = "/css/"
URL_FONTS = "/fonts/"
2017-07-30 15:20:48 +00:00
)
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 {
2017-07-31 18:49:27 +00:00
Id int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
2017-07-30 14:42:18 +00:00
}
/*
* A book. Generic data.
*/
type Book struct {
2017-07-31 18:49:27 +00:00
Id int64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
2017-08-01 18:00:20 +00:00
SeriesIndex float64 `json:"series_idx,omitempty"`
2017-07-31 18:49:27 +00:00
Series *Series `json:"series,omitempty"`
2017-07-30 14:42:18 +00:00
}
/*
* An author.
*/
type Author struct {
2017-07-31 18:49:27 +00:00
Id int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
2017-07-30 14:42:18 +00:00
}
/*
* Author and number of books.
*/
type AuthorAdv struct {
Author
2017-07-31 18:49:27 +00:00
Count int `json:"count,omitempty"`
2017-07-30 14:42:18 +00:00
}
/*
* Downloadable book data.
*/
type BookData struct {
2017-07-31 18:49:27 +00:00
Size int64 `json:"size,omitempty"`
Format string `json:"format,omitempty"`
Name string `json:"name,omitempty"`
2017-07-30 14:42:18 +00:00
}
/*
* A book. Advanced data: authors, tags.
*/
type BookAdv struct {
Book
2017-07-31 18:49:27 +00:00
Authors []*Author `json:"authors,omitempty"`
Tags []string `json:"tags,omitempty"`
2017-07-30 14:42:18 +00:00
}
type AuthorFull struct {
Author
2017-07-31 18:49:27 +00:00
Books []*BookAdv `json:"books,omitempty"`
2017-07-30 14:42:18 +00:00
}
type BookFull struct {
BookAdv
2017-08-01 18:00:20 +00:00
Data []*BookData `json:"data,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Pubdate int64 `json:"pubdate,omitempty"`
Isbn string `json:"isbn,omitempty"`
Lccn string `json:"lccn,omitempty"`
Path string `json:"path,omitempty"`
Uuid string `json:"uuid,omitempty"`
Has_cover bool `json:"has_cover,omitempty"`
Lang string `json:"lang,omitempty"`
Publisher string `json:"publisher,omitempty"`
2017-07-30 14:42:18 +00:00
}
type SeriesAdv struct {
Series
2017-07-31 18:49:27 +00:00
Count int64 `json:"count,omitempty"`
Authors []*Author `json:"authors,omitempty"`
2017-07-30 14:42:18 +00:00
}
type SeriesFull struct {
SeriesAdv
2017-07-31 18:49:27 +00:00
Books []*Book `json:"books,omitempty"`
2017-07-30 15:20:48 +00:00
}
2017-07-30 16:09:27 +00:00
type BouquinsModel struct {
2017-07-31 18:49:27 +00:00
Title string `json:"title,omitempty"`
2017-07-30 16:09:27 +00:00
}
// 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-31 18:49:27 +00:00
BooksCount int64 `json:"count"`
Books []*BookAdv `json:"books,omitempty"`
Series []*SeriesAdv `json:"series,omitempty"`
Authors []*AuthorAdv `json:"authors,omitempty"`
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
}
2017-08-01 13:38:23 +00:00
type BookModel struct {
BouquinsModel
*BookFull
}
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)
2017-07-31 18:14:57 +00:00
order, sort := req.URL.Query().Get(PARAM_ORDER), req.URL.Query().Get(PARAM_SORT)
switch req.URL.Query().Get(PARAM_LIST) {
case LIST_AUTHORS:
model.Authors, err = app.AuthorsAdv(0, 0, sort, order)
case LIST_SERIES:
model.Series, err = app.SeriesAdv(0, 0, sort, order)
case LIST_BOOKS:
fallthrough
default:
model.Books, err = app.BooksAdv(0, 0, sort, order)
}
2017-07-30 18:14:20 +00:00
if err != nil {
log.Print(err)
}
2017-07-31 18:49:27 +00:00
if req.Header.Get("Accept") == "application/json" {
res.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(res)
err := enc.Encode(model)
if err != nil {
log.Println(err)
http.Error(res, err.Error(), 500)
}
} else {
app.render(res, TPL_INDEX, model)
}
2017-07-30 16:09:27 +00:00
}
2017-07-30 15:20:48 +00:00
func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) {
2017-08-01 18:00:20 +00:00
if !strings.HasPrefix(req.URL.Path, URL_BOOKS) {
// FIXME 404
log.Fatalln("Invalid URL")
}
id, err := strconv.Atoi(req.URL.Path[len(URL_BOOKS):])
if err != nil {
// FIXME 404
log.Fatalln(err)
}
book, err := app.BookFull(int64(id))
2017-08-01 13:38:23 +00:00
if err != nil {
// FIXME 500
log.Fatalln(err)
}
model := &BookModel{
*NewBouquinsModel(book.Title),
book,
}
app.render(res, TPL_BOOKS, model)
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
}