JSON response
This commit is contained in:
parent
24baaf2175
commit
70a2ced313
@ -2,6 +2,7 @@ package bouquins
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
@ -31,26 +32,26 @@ type Bouquins struct {
|
||||
* A book series.
|
||||
*/
|
||||
type Series struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
* A book. Generic data.
|
||||
*/
|
||||
type Book struct {
|
||||
Id int64
|
||||
Title string
|
||||
SeriesIndex int
|
||||
Series *Series
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
SeriesIndex int `json:"series_idx,omitempty"`
|
||||
Series *Series `json:"series,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
* An author.
|
||||
*/
|
||||
type Author struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
@ -58,16 +59,16 @@ type Author struct {
|
||||
*/
|
||||
type AuthorAdv struct {
|
||||
Author
|
||||
Count int
|
||||
Count int `json:"count,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
* Downloadable book data.
|
||||
*/
|
||||
type BookData struct {
|
||||
Size int64
|
||||
Format string
|
||||
Name string
|
||||
Size int64 `json:"size,omitempty"`
|
||||
Format string `json:"format,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
@ -75,42 +76,42 @@ type BookData struct {
|
||||
*/
|
||||
type BookAdv struct {
|
||||
Book
|
||||
Authors []*Author
|
||||
Tags []string
|
||||
Authors []*Author `json:"authors,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
}
|
||||
|
||||
type AuthorFull struct {
|
||||
Author
|
||||
Books []*BookAdv
|
||||
Books []*BookAdv `json:"books,omitempty"`
|
||||
}
|
||||
|
||||
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
|
||||
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"`
|
||||
}
|
||||
|
||||
type SeriesAdv struct {
|
||||
Series
|
||||
Count int64
|
||||
Authors []*Author
|
||||
Count int64 `json:"count,omitempty"`
|
||||
Authors []*Author `json:"authors,omitempty"`
|
||||
}
|
||||
|
||||
type SeriesFull struct {
|
||||
SeriesAdv
|
||||
Books []*Book
|
||||
Books []*Book `json:"books,omitempty"`
|
||||
}
|
||||
|
||||
type BouquinsModel struct {
|
||||
Title string
|
||||
Title string `json:"title,omitempty"`
|
||||
}
|
||||
|
||||
// Constructor BouquinsModel
|
||||
@ -122,10 +123,10 @@ func NewBouquinsModel(title string) *BouquinsModel {
|
||||
|
||||
type IndexModel struct {
|
||||
BouquinsModel
|
||||
BooksCount int64
|
||||
Books []*BookAdv
|
||||
Series []*SeriesAdv
|
||||
Authors []*AuthorAdv
|
||||
BooksCount int64 `json:"count"`
|
||||
Books []*BookAdv `json:"books,omitempty"`
|
||||
Series []*SeriesAdv `json:"series,omitempty"`
|
||||
Authors []*AuthorAdv `json:"authors,omitempty"`
|
||||
}
|
||||
|
||||
// Constructor IndexModel
|
||||
@ -168,7 +169,17 @@ func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) {
|
||||
if err != nil {
|
||||
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) {
|
||||
app.render(res, TPL_BOOKS, nil)
|
||||
|
Loading…
Reference in New Issue
Block a user