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) { cellContent: function(h) {
switch (this.col.id) { switch (this.col.id) {
case 'name': case 'author_name':
return this.link(h, 'user', this.item.name, this.authorUrl(this.item.id)); 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': case 'count':
return this.item.count; return this.item.count;
case 'title': case 'title':
@ -42,9 +44,11 @@ Vue.component('result-cell', {
case 'authors': case 'authors':
var elts = []; var elts = [];
var authors = this.item.authors; var authors = this.item.authors;
if (authors) {
for (i=0;i<authors.length;i++) { for (i=0;i<authors.length;i++) {
elts[i] = this.link(h, 'user', authors[i].name, this.authorUrl(authors[i].id)); elts[i] = this.link(h, 'user', authors[i].name, this.authorUrl(authors[i].id));
} }
}
return elts; return elts;
case 'series': case 'series':
var series = this.item.series; var series = this.item.series;
@ -84,7 +88,7 @@ var index = new Vue({
}, },
methods: { methods: {
showSeries: function() { showSeries: function() {
console.log("Series"); this.sendQuery('/series/', this.stdError, this.loadResults);
}, },
showAuthors: function() { showAuthors: function() {
this.sendQuery('/authors/', this.stdError, this.loadResults); this.sendQuery('/authors/', this.stdError, this.loadResults);
@ -103,14 +107,14 @@ var index = new Vue({
break; break;
case 'series': case 'series':
this.cols = [ this.cols = [
{ name: 'Nom', sortId: 'name' }, { id: 'serie_name', name: 'Nom', sortable: true },
{ name: 'Livre(s)' }, { id: 'count', name: 'Livre(s)' },
{ name: 'Auteur(s)' } { id: 'authors', name: 'Auteur(s)' }
]; ];
break; break;
case 'authors': case 'authors':
this.cols = [ this.cols = [
{ id: 'name', name: 'Nom', sortable: true }, { id: 'author_name', name: 'Nom', sortable: true },
{ id: 'count', name: 'Livre(s)' } { id: 'count', name: 'Livre(s)' }
]; ];
break; break;

View File

@ -180,13 +180,13 @@ func NewAuthorsResultsModel(authors []*AuthorAdv) *AuthorsResultsModel {
return &AuthorsResultsModel{ResultsModel{"authors"}, authors} return &AuthorsResultsModel{ResultsModel{"authors"}, authors}
} }
type SerieResultsModel struct { type SeriesResultsModel struct {
ResultsModel ResultsModel
Results []*SeriesAdv `json:"results,omitempty"` Results []*SeriesAdv `json:"results,omitempty"`
} }
func NewSerieResultsModel(series []*SeriesAdv) *SerieResultsModel { func NewSeriesResultsModel(series []*SeriesAdv) *SeriesResultsModel {
return &SerieResultsModel{ResultsModel{"series"}, series} return &SeriesResultsModel{ResultsModel{"series"}, series}
} }
type BookModel struct { type BookModel struct {
@ -362,23 +362,42 @@ func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) {
http.Error(res, err.Error(), 500) http.Error(res, err.Error(), 500)
} }
} }
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { func (app *Bouquins) SeriesListPage(res http.ResponseWriter, req *http.Request) error {
if !strings.HasPrefix(req.URL.Path, URL_SERIES) { if isJson(req) {
// FIXME 404 series, err := app.SeriesAdv(10, 0, "", "")
log.Fatalln("Invalid URL")
}
id, err := strconv.Atoi(req.URL.Path[len(URL_SERIES):])
if err != nil { if err != nil {
// FIXME 404 return err
log.Fatalln(err) }
return writeJson(res, NewSeriesResultsModel(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 {
return err
} }
series, err := app.SeriesFull(int64(id)) series, err := app.SeriesFull(int64(id))
if err != nil { if err != nil {
// FIXME 500 return err
log.Fatalln(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_DATA QueryType = iota
BOOK_AUTHORS QueryType = iota BOOK_AUTHORS QueryType = iota
BOOKS_COUNT QueryType = iota BOOKS_COUNT QueryType = iota
SERIE QueryType = iota
SERIES QueryType = iota SERIES QueryType = iota
SERIES_AUTHORS QueryType = iota SERIES_AUTHORS QueryType = iota
SERIES_BOOKS QueryType = iota SERIE_AUTHORS QueryType = iota
SERIE_BOOKS QueryType = iota
AUTHORS QueryType = iota AUTHORS QueryType = iota
AUTHOR QueryType = iota AUTHOR QueryType = iota
AUTHOR_BOOKS 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_DATA, false, false}: STMT_BOOK_DATA,
Query{BOOK_AUTHORS, false, false}: STMT_BOOK_AUTHORS, Query{BOOK_AUTHORS, false, false}: STMT_BOOK_AUTHORS,
Query{BOOKS_COUNT, false, false}: STMT_BOOKS_COUNT, Query{BOOKS_COUNT, false, false}: STMT_BOOKS_COUNT,
Query{SERIES, false, false}: STMT_SERIE, Query{SERIE, false, false}: STMT_SERIE,
Query{SERIES_AUTHORS, false, false}: STMT_SERIE_AUTHORS, Query{SERIES, true, true}: STMT_SERIES_NAME_DESC,
Query{SERIES_BOOKS, false, false}: STMT_SERIE_BOOKS, 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, true}: STMT_AUTHORS_NAME_DESC,
Query{AUTHORS, true, false}: STMT_AUTHORS_NAME_ASC, Query{AUTHORS, true, false}: STMT_AUTHORS_NAME_ASC,
Query{AUTHORS, false, true}: STMT_AUTHORS_ID_DESC, 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) { func (app *Bouquins) psSortAuthors(qt QueryType, sort, order string) (*sql.Stmt, error) {
return app.psSort("name", qt, sort, order) 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) { func (app *Bouquins) psSort(sortNameField string, qt QueryType, sort, order string) (*sql.Stmt, error) {
q := Query{qt, sort == sortNameField, order == "desc"} q := Query{qt, sort == sortNameField, order == "desc"}
query := QUERIES[q] query := QUERIES[q]

View File

@ -1,9 +1,69 @@
package bouquins package bouquins
// MERGE SUB QUERIES //
func assignAuthorsSeries(series []*SeriesAdv, authors map[int64][]*Author) {
for _, s := range series {
s.Authors = authors[s.Id]
}
}
// SUB QUERIES // // 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) { func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
stmt, err := app.ps(SERIES) stmt, err := app.ps(SERIE)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -12,7 +72,7 @@ func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
return series, nil return series, nil
} }
func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error { func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error {
stmt, err := app.ps(SERIES_AUTHORS) stmt, err := app.ps(SERIE_AUTHORS)
if err != nil { if err != nil {
return err return err
} }
@ -34,7 +94,7 @@ func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error {
return nil return nil
} }
func (app *Bouquins) querySeriesBooks(series *SeriesFull) error { func (app *Bouquins) querySeriesBooks(series *SeriesFull) error {
stmt, err := app.ps(SERIES_BOOKS) stmt, err := app.ps(SERIE_BOOKS)
if err != nil { if err != nil {
return err 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) { 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
} }