2017-08-03 18:36:46 +00:00
|
|
|
package bouquins
|
|
|
|
|
2017-08-04 16:57:15 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
2017-08-03 18:36:46 +00:00
|
|
|
|
|
|
|
// SUB QUERIES //
|
|
|
|
|
2017-08-05 15:48:06 +00:00
|
|
|
func (app *Bouquins) queryAuthors(limit, offset int, sort, order string) ([]*AuthorAdv, error) {
|
|
|
|
authors := make([]*AuthorAdv, 0, limit)
|
|
|
|
stmt, err := app.psSortAuthors(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(AuthorAdv)
|
|
|
|
if err := rows.Scan(&author.Id, &author.Name, &author.Count); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
authors = append(authors, author)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return authors, nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:36:46 +00:00
|
|
|
func (app *Bouquins) queryAuthorBooks(author *AuthorFull) error {
|
|
|
|
stmt, err := app.ps(AUTHOR_BOOKS)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := stmt.Query(author.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2017-08-04 16:57:15 +00:00
|
|
|
series := make(map[int64]*Series, 0)
|
2017-08-03 18:36:46 +00:00
|
|
|
for rows.Next() {
|
2017-08-04 16:57:15 +00:00
|
|
|
book := new(Book)
|
2017-08-03 18:36:46 +00:00
|
|
|
var seriesId sql.NullInt64
|
|
|
|
var seriesName sql.NullString
|
|
|
|
if err = rows.Scan(&book.Id, &book.Title, &book.SeriesIndex, &seriesName, &seriesId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if seriesId.Valid && seriesName.Valid {
|
2017-08-04 16:57:15 +00:00
|
|
|
series[seriesId.Int64] = &Series{
|
2017-08-03 18:36:46 +00:00
|
|
|
seriesId.Int64,
|
|
|
|
seriesName.String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
author.Books = append(author.Books, book)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-04 16:57:15 +00:00
|
|
|
author.Series = make([]*Series, 0, len(series))
|
|
|
|
for _, s := range series {
|
|
|
|
author.Series = append(author.Series, s)
|
|
|
|
}
|
2017-08-03 18:36:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Bouquins) queryAuthorAuthors(author *AuthorFull) error {
|
2017-08-04 16:57:15 +00:00
|
|
|
stmt, err := app.ps(AUTHOR_COAUTHORS)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := stmt.Query(author.Id, author.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
coauthor := new(Author)
|
|
|
|
if err = rows.Scan(&coauthor.Id, &coauthor.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
author.CoAuthors = append(author.CoAuthors, coauthor)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-03 18:36:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Bouquins) queryAuthor(id int64) (*AuthorFull, error) {
|
|
|
|
stmt, err := app.ps(AUTHOR)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
author := new(AuthorFull)
|
|
|
|
author.Id = id
|
|
|
|
err = stmt.QueryRow(id).Scan(&author.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return author, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB LOADS //
|
|
|
|
|
2017-08-05 15:48:06 +00:00
|
|
|
func (app *Bouquins) AuthorsAdv(limit, offset int, sort, order string) ([]*AuthorAdv, error) {
|
|
|
|
if limit == 0 {
|
|
|
|
limit = DEF_LIM
|
|
|
|
}
|
|
|
|
authors, err := app.queryAuthors(limit, offset, sort, order)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return authors, nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:36:46 +00:00
|
|
|
func (app *Bouquins) AuthorFull(id int64) (*AuthorFull, error) {
|
|
|
|
author, err := app.queryAuthor(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = app.queryAuthorBooks(author)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = app.queryAuthorAuthors(author)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return author, nil
|
|
|
|
}
|