2017-08-03 17:51:56 +00:00
|
|
|
package bouquins
|
|
|
|
|
2017-08-05 16:24:57 +00:00
|
|
|
// MERGE SUB QUERIES //
|
|
|
|
|
|
|
|
func assignAuthorsSeries(series []*SeriesAdv, authors map[int64][]*Author) {
|
|
|
|
for _, s := range series {
|
|
|
|
s.Authors = authors[s.Id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:51:56 +00:00
|
|
|
// SUB QUERIES //
|
|
|
|
|
2017-08-05 17:40:58 +00:00
|
|
|
func (app *Bouquins) querySeriesList(limit, offset int, sort, order string) ([]*SeriesAdv, bool, error) {
|
2017-08-05 16:24:57 +00:00
|
|
|
series := make([]*SeriesAdv, 0, limit)
|
|
|
|
stmt, err := app.psSortSeries(SERIES, sort, order)
|
|
|
|
if err != nil {
|
2017-08-05 17:40:58 +00:00
|
|
|
return nil, false, err
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
2017-08-05 17:40:58 +00:00
|
|
|
rows, err := stmt.Query(limit+1, offset)
|
2017-08-05 16:24:57 +00:00
|
|
|
if err != nil {
|
2017-08-05 17:40:58 +00:00
|
|
|
return nil, false, err
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
2017-08-05 17:40:58 +00:00
|
|
|
more := false
|
2017-08-05 16:24:57 +00:00
|
|
|
for rows.Next() {
|
2017-08-05 17:40:58 +00:00
|
|
|
if len(series) == limit {
|
|
|
|
more = true
|
|
|
|
} else {
|
|
|
|
serie := new(SeriesAdv)
|
|
|
|
if err := rows.Scan(&serie.Id, &serie.Name, &serie.Count); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
series = append(series, serie)
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
2017-08-05 17:40:58 +00:00
|
|
|
return nil, false, err
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
2017-08-05 17:40:58 +00:00
|
|
|
return series, more, nil
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:51:56 +00:00
|
|
|
func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
|
2017-08-05 16:24:57 +00:00
|
|
|
stmt, err := app.ps(SERIE)
|
2017-08-03 17:51:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
series := new(SeriesFull)
|
|
|
|
err = stmt.QueryRow(id).Scan(&series.Id, &series.Name)
|
|
|
|
return series, nil
|
|
|
|
}
|
|
|
|
func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error {
|
2017-08-05 16:24:57 +00:00
|
|
|
stmt, err := app.ps(SERIE_AUTHORS)
|
2017-08-03 17:51:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := stmt.Query(series.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
author := new(Author)
|
|
|
|
if err = rows.Scan(&author.Id, &author.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
series.Authors = append(series.Authors, author)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (app *Bouquins) querySeriesBooks(series *SeriesFull) error {
|
2017-08-05 16:24:57 +00:00
|
|
|
stmt, err := app.ps(SERIE_BOOKS)
|
2017-08-03 17:51:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := stmt.Query(series.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
book := new(Book)
|
|
|
|
if err = rows.Scan(&book.Id, &book.Title, &book.SeriesIndex); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
series.Books = append(series.Books, book)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB LOADS //
|
|
|
|
|
|
|
|
func (app *Bouquins) SeriesFull(id int64) (*SeriesFull, error) {
|
|
|
|
series, err := app.querySeries(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = app.querySeriesBooks(series)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = app.querySeriesAuthors(series)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return series, nil
|
|
|
|
}
|
2017-08-05 15:48:06 +00:00
|
|
|
|
2017-08-05 17:40:58 +00:00
|
|
|
func (app *Bouquins) SeriesAdv(limit, offset int, sort, order string) ([]*SeriesAdv, bool, error) {
|
|
|
|
series, more, err := app.querySeriesList(limit, offset, sort, order)
|
2017-08-05 16:24:57 +00:00
|
|
|
if err != nil {
|
2017-08-05 17:40:58 +00:00
|
|
|
return nil, false, err
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
|
|
|
authors, err := app.querySeriesListAuthors(limit, offset, sort, order)
|
|
|
|
if err != nil {
|
2017-08-05 17:40:58 +00:00
|
|
|
return nil, false, err
|
2017-08-05 16:24:57 +00:00
|
|
|
}
|
|
|
|
assignAuthorsSeries(series, authors)
|
2017-08-05 17:40:58 +00:00
|
|
|
return series, more, nil
|
2017-08-05 15:48:06 +00:00
|
|
|
}
|