Series list

This commit is contained in:
Meutel 2017-08-05 18:24:57 +02:00
parent 6c11b38f2b
commit c8fc680331
4 changed files with 143 additions and 35 deletions

View File

@ -33,8 +33,10 @@ Vue.component('result-cell', {
},
cellContent: function(h) {
switch (this.col.id) {
case 'name':
case 'author_name':
return this.link(h, 'user', this.item.name, this.authorUrl(this.item.id));
case 'serie_name':
return this.link(h, 'list', this.item.name, this.authorUrl(this.item.id));
case 'count':
return this.item.count;
case 'title':
@ -42,8 +44,10 @@ Vue.component('result-cell', {
case 'authors':
var elts = [];
var authors = this.item.authors;
for (i=0;i<authors.length;i++) {
elts[i] = this.link(h, 'user', authors[i].name, this.authorUrl(authors[i].id));
if (authors) {
for (i=0;i<authors.length;i++) {
elts[i] = this.link(h, 'user', authors[i].name, this.authorUrl(authors[i].id));
}
}
return elts;
case 'series':
@ -84,7 +88,7 @@ var index = new Vue({
},
methods: {
showSeries: function() {
console.log("Series");
this.sendQuery('/series/', this.stdError, this.loadResults);
},
showAuthors: function() {
this.sendQuery('/authors/', this.stdError, this.loadResults);
@ -96,22 +100,22 @@ var index = new Vue({
switch (type) {
case 'books':
this.cols = [
{ id: 'title', name: 'Titre', sortable: true },
{ id: 'title', name: 'Titre', sortable: true },
{ id: 'authors', name: 'Auteur(s)' },
{ id: 'series', name: 'Serie' }
{ id: 'series', name: 'Serie' }
];
break;
case 'series':
this.cols = [
{ name: 'Nom', sortId: 'name' },
{ name: 'Livre(s)' },
{ name: 'Auteur(s)' }
{ id: 'serie_name', name: 'Nom', sortable: true },
{ id: 'count', name: 'Livre(s)' },
{ id: 'authors', name: 'Auteur(s)' }
];
break;
case 'authors':
this.cols = [
{ id: 'name', name: 'Nom', sortable: true },
{ id:'count', name: 'Livre(s)' }
{ id: 'author_name', name: 'Nom', sortable: true },
{ id: 'count', name: 'Livre(s)' }
];
break;
}

View File

@ -180,13 +180,13 @@ func NewAuthorsResultsModel(authors []*AuthorAdv) *AuthorsResultsModel {
return &AuthorsResultsModel{ResultsModel{"authors"}, authors}
}
type SerieResultsModel struct {
type SeriesResultsModel struct {
ResultsModel
Results []*SeriesAdv `json:"results,omitempty"`
}
func NewSerieResultsModel(series []*SeriesAdv) *SerieResultsModel {
return &SerieResultsModel{ResultsModel{"series"}, series}
func NewSeriesResultsModel(series []*SeriesAdv) *SeriesResultsModel {
return &SeriesResultsModel{ResultsModel{"series"}, series}
}
type BookModel struct {
@ -362,23 +362,42 @@ func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) {
http.Error(res, err.Error(), 500)
}
}
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) {
if !strings.HasPrefix(req.URL.Path, URL_SERIES) {
// FIXME 404
log.Fatalln("Invalid URL")
func (app *Bouquins) SeriesListPage(res http.ResponseWriter, req *http.Request) error {
if isJson(req) {
series, err := app.SeriesAdv(10, 0, "", "")
if err != nil {
return err
}
return writeJson(res, NewSeriesResultsModel(series))
}
id, err := strconv.Atoi(req.URL.Path[len(URL_SERIES):])
return errors.New("Invalid mime")
}
func (app *Bouquins) SeriePage(idParam string, res http.ResponseWriter, req *http.Request) error {
id, err := strconv.Atoi(idParam)
if err != nil {
// FIXME 404
log.Fatalln(err)
return err
}
series, err := app.SeriesFull(int64(id))
if err != nil {
// FIXME 500
log.Fatalln(err)
return err
}
return app.render(res, TPL_SERIES, &SeriesModel{*NewBouquinsModel(series.Name), series})
}
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) {
var err error
var idParam = ""
if strings.HasPrefix(req.URL.Path, URL_SERIES) {
idParam = req.URL.Path[len(URL_SERIES):]
} else {
err = errors.New("Invalid URL") // FIXME 404
}
if len(idParam) == 0 {
err = app.SeriesListPage(res, req)
} else {
err = app.SeriePage(idParam, res, req)
}
if err != nil {
log.Println(err)
http.Error(res, err.Error(), 500)
}
app.render(res, TPL_SERIES, &SeriesModel{
*NewBouquinsModel(series.Name),
series,
})
}

View File

@ -112,9 +112,11 @@ const (
BOOK_DATA QueryType = iota
BOOK_AUTHORS QueryType = iota
BOOKS_COUNT QueryType = iota
SERIE QueryType = iota
SERIES QueryType = iota
SERIES_AUTHORS QueryType = iota
SERIES_BOOKS QueryType = iota
SERIE_AUTHORS QueryType = iota
SERIE_BOOKS QueryType = iota
AUTHORS QueryType = iota
AUTHOR QueryType = iota
AUTHOR_BOOKS QueryType = iota
@ -139,9 +141,17 @@ var QUERIES = map[Query]string{
Query{BOOK_DATA, false, false}: STMT_BOOK_DATA,
Query{BOOK_AUTHORS, false, false}: STMT_BOOK_AUTHORS,
Query{BOOKS_COUNT, false, false}: STMT_BOOKS_COUNT,
Query{SERIES, false, false}: STMT_SERIE,
Query{SERIES_AUTHORS, false, false}: STMT_SERIE_AUTHORS,
Query{SERIES_BOOKS, false, false}: STMT_SERIE_BOOKS,
Query{SERIE, false, false}: STMT_SERIE,
Query{SERIES, true, true}: STMT_SERIES_NAME_DESC,
Query{SERIES, true, false}: STMT_SERIES_NAME_ASC,
Query{SERIES, false, true}: STMT_SERIES_ID_DESC,
Query{SERIES, false, false}: STMT_SERIES_ID_ASC,
Query{SERIES_AUTHORS, true, true}: STMT_SERIES_AUTHORS_NAME_DESC,
Query{SERIES_AUTHORS, true, false}: STMT_SERIES_AUTHORS_NAME_ASC,
Query{SERIES_AUTHORS, false, true}: STMT_SERIES_AUTHORS_ID_DESC,
Query{SERIES_AUTHORS, false, false}: STMT_SERIES_AUTHORS_ID_ASC,
Query{SERIE_AUTHORS, false, false}: STMT_SERIE_AUTHORS,
Query{SERIE_BOOKS, false, false}: STMT_SERIE_BOOKS,
Query{AUTHORS, true, true}: STMT_AUTHORS_NAME_DESC,
Query{AUTHORS, true, false}: STMT_AUTHORS_NAME_ASC,
Query{AUTHORS, false, true}: STMT_AUTHORS_ID_DESC,
@ -183,6 +193,9 @@ func (app *Bouquins) psSortBooks(qt QueryType, sort, order string) (*sql.Stmt, e
func (app *Bouquins) psSortAuthors(qt QueryType, sort, order string) (*sql.Stmt, error) {
return app.psSort("name", qt, sort, order)
}
func (app *Bouquins) psSortSeries(qt QueryType, sort, order string) (*sql.Stmt, error) {
return app.psSort("name", qt, sort, order)
}
func (app *Bouquins) psSort(sortNameField string, qt QueryType, sort, order string) (*sql.Stmt, error) {
q := Query{qt, sort == sortNameField, order == "desc"}
query := QUERIES[q]

View File

@ -1,9 +1,69 @@
package bouquins
// MERGE SUB QUERIES //
func assignAuthorsSeries(series []*SeriesAdv, authors map[int64][]*Author) {
for _, s := range series {
s.Authors = authors[s.Id]
}
}
// SUB QUERIES //
func (app *Bouquins) querySeriesList(limit, offset int, sort, order string) ([]*SeriesAdv, error) {
series := make([]*SeriesAdv, 0, limit)
stmt, err := app.psSortSeries(SERIES, sort, order)
if err != nil {
return nil, err
}
rows, err := stmt.Query(limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
serie := new(SeriesAdv)
if err := rows.Scan(&serie.Id, &serie.Name, &serie.Count); err != nil {
return nil, err
}
series = append(series, serie)
}
if err := rows.Err(); err != nil {
return nil, err
}
return series, nil
}
func (app *Bouquins) querySeriesListAuthors(limit, offset int, sort, order string) (map[int64][]*Author, error) {
authors := make(map[int64][]*Author)
stmt, err := app.psSortBooks(SERIES_AUTHORS, sort, order)
if err != nil {
return nil, err
}
rows, err := stmt.Query(limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
author := new(Author)
var serId int64
if err := rows.Scan(&author.Id, &author.Name, &serId); err != nil {
return nil, err
}
if authors[serId] == nil {
authors[serId] = append(make([]*Author, 0), author)
} else {
authors[serId] = append(authors[serId], author)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return authors, nil
}
func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
stmt, err := app.ps(SERIES)
stmt, err := app.ps(SERIE)
if err != nil {
return nil, err
}
@ -12,7 +72,7 @@ func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
return series, nil
}
func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error {
stmt, err := app.ps(SERIES_AUTHORS)
stmt, err := app.ps(SERIE_AUTHORS)
if err != nil {
return err
}
@ -34,7 +94,7 @@ func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error {
return nil
}
func (app *Bouquins) querySeriesBooks(series *SeriesFull) error {
stmt, err := app.ps(SERIES_BOOKS)
stmt, err := app.ps(SERIE_BOOKS)
if err != nil {
return err
}
@ -75,5 +135,17 @@ func (app *Bouquins) SeriesFull(id int64) (*SeriesFull, error) {
}
func (app *Bouquins) SeriesAdv(limit, offset int, sort, order string) ([]*SeriesAdv, error) {
panic("not implemented")
if limit == 0 {
limit = DEF_LIM
}
series, err := app.querySeriesList(limit, offset, sort, order)
if err != nil {
return nil, err
}
authors, err := app.querySeriesListAuthors(limit, offset, sort, order)
if err != nil {
return nil, err
}
assignAuthorsSeries(series, authors)
return series, nil
}