JSON response

This commit is contained in:
Meutel 2017-07-31 20:49:27 +02:00
parent 24baaf2175
commit 70a2ced313

View File

@ -2,6 +2,7 @@ package bouquins
import ( import (
"database/sql" "database/sql"
"encoding/json"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -31,26 +32,26 @@ type Bouquins struct {
* A book series. * A book series.
*/ */
type Series struct { type Series struct {
Id int64 Id int64 `json:"id,omitempty"`
Name string Name string `json:"name,omitempty"`
} }
/* /*
* A book. Generic data. * A book. Generic data.
*/ */
type Book struct { type Book struct {
Id int64 Id int64 `json:"id,omitempty"`
Title string Title string `json:"title,omitempty"`
SeriesIndex int SeriesIndex int `json:"series_idx,omitempty"`
Series *Series Series *Series `json:"series,omitempty"`
} }
/* /*
* An author. * An author.
*/ */
type Author struct { type Author struct {
Id int64 Id int64 `json:"id,omitempty"`
Name string Name string `json:"name,omitempty"`
} }
/* /*
@ -58,16 +59,16 @@ type Author struct {
*/ */
type AuthorAdv struct { type AuthorAdv struct {
Author Author
Count int Count int `json:"count,omitempty"`
} }
/* /*
* Downloadable book data. * Downloadable book data.
*/ */
type BookData struct { type BookData struct {
Size int64 Size int64 `json:"size,omitempty"`
Format string Format string `json:"format,omitempty"`
Name string Name string `json:"name,omitempty"`
} }
/* /*
@ -75,42 +76,42 @@ type BookData struct {
*/ */
type BookAdv struct { type BookAdv struct {
Book Book
Authors []*Author Authors []*Author `json:"authors,omitempty"`
Tags []string Tags []string `json:"tags,omitempty"`
} }
type AuthorFull struct { type AuthorFull struct {
Author Author
Books []*BookAdv Books []*BookAdv `json:"books,omitempty"`
} }
type BookFull struct { type BookFull struct {
BookAdv BookAdv
Data []BookData Data []BookData `json:"data,omitempty"`
Timestamp int64 Timestamp int64 `json:"timestamp,omitempty"`
Pubdate int64 Pubdate int64 `json:"pubdate,omitempty"`
Isbn string Isbn string `json:"isbn,omitempty"`
Lccn string Lccn string `json:"lccn,omitempty"`
Path string Path string `json:"path,omitempty"`
Uuid string Uuid string `json:"uuid,omitempty"`
Has_cover bool Has_cover bool `json:"has_cover,omitempty"`
Lang string Lang string `json:"lang,omitempty"`
Publisher string Publisher string `json:"publisher,omitempty"`
} }
type SeriesAdv struct { type SeriesAdv struct {
Series Series
Count int64 Count int64 `json:"count,omitempty"`
Authors []*Author Authors []*Author `json:"authors,omitempty"`
} }
type SeriesFull struct { type SeriesFull struct {
SeriesAdv SeriesAdv
Books []*Book Books []*Book `json:"books,omitempty"`
} }
type BouquinsModel struct { type BouquinsModel struct {
Title string Title string `json:"title,omitempty"`
} }
// Constructor BouquinsModel // Constructor BouquinsModel
@ -122,10 +123,10 @@ func NewBouquinsModel(title string) *BouquinsModel {
type IndexModel struct { type IndexModel struct {
BouquinsModel BouquinsModel
BooksCount int64 BooksCount int64 `json:"count"`
Books []*BookAdv Books []*BookAdv `json:"books,omitempty"`
Series []*SeriesAdv Series []*SeriesAdv `json:"series,omitempty"`
Authors []*AuthorAdv Authors []*AuthorAdv `json:"authors,omitempty"`
} }
// Constructor IndexModel // Constructor IndexModel
@ -168,7 +169,17 @@ func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) {
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
app.render(res, TPL_INDEX, model) 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)
}
} }
func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) {
app.render(res, TPL_BOOKS, nil) app.render(res, TPL_BOOKS, nil)