go-bouquins/bouquins/dbseries.go
2017-08-03 19:51:56 +02:00

76 lines
1.5 KiB
Go

package bouquins
// SUB QUERIES //
func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
stmt, err := app.ps(SERIES)
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 {
stmt, err := app.ps(SERIES_AUTHORS)
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 {
stmt, err := app.ps(SERIES_BOOKS)
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
}