74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
|
package bouquins
|
||
|
|
||
|
import "database/sql"
|
||
|
|
||
|
// SUB QUERIES //
|
||
|
|
||
|
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()
|
||
|
for rows.Next() {
|
||
|
book := new(BookAdv)
|
||
|
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 {
|
||
|
book.Series = &Series{
|
||
|
seriesId.Int64,
|
||
|
seriesName.String,
|
||
|
}
|
||
|
}
|
||
|
author.Books = append(author.Books, book)
|
||
|
}
|
||
|
if err := rows.Err(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (app *Bouquins) queryAuthorAuthors(author *AuthorFull) error {
|
||
|
// TODO
|
||
|
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 //
|
||
|
|
||
|
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
|
||
|
}
|