From 3ab0198f21055b385e90cfa59d79c9389bc7030b Mon Sep 17 00:00:00 2001 From: Meutel Date: Thu, 3 Aug 2017 20:36:46 +0200 Subject: [PATCH] WIP: author page --- bouquins/bouquins.go | 39 ++++++++++++++++------- bouquins/db.go | 26 ++++++++++++++- bouquins/dbauthors.go | 73 +++++++++++++++++++++++++++++++++++++++++++ templates/author.html | 40 +++++++++++++++++------- 4 files changed, 154 insertions(+), 24 deletions(-) create mode 100644 bouquins/dbauthors.go diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index fe89082..257d065 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -162,6 +162,11 @@ type SeriesModel struct { *SeriesFull } +type AuthorModel struct { + BouquinsModel + *AuthorFull +} + func (app *Bouquins) render(res http.ResponseWriter, tpl string, model interface{}) { err := app.Template.ExecuteTemplate(res, tpl, model) if err != nil { @@ -243,18 +248,24 @@ func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { app.render(res, TPL_BOOKS, model) } func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) { - /* - if !strings.HasPrefix(req.URL.Path, URL_AUTHORS) { - // FIXME 404 - log.Fatalln("Invalid URL") - } - id, err := strconv.Atoi(req.URL.Path[len(URL_AUTHORS):]) - if err != nil { - // FIXME 404 - log.Fatalln(err) - } - */ - app.render(res, TPL_AUTHORS, nil) + if !strings.HasPrefix(req.URL.Path, URL_AUTHORS) { + // FIXME 404 + log.Fatalln("Invalid URL") + } + id, err := strconv.Atoi(req.URL.Path[len(URL_AUTHORS):]) + if err != nil { + // FIXME 404 + log.Fatalln(err) + } + author, err := app.AuthorFull(int64(id)) + if err != nil { + // FIXME 500 + log.Fatalln(err) + } + app.render(res, TPL_AUTHORS, &AuthorModel{ + *NewBouquinsModel(author.Name), + author, + }) } func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { if !strings.HasPrefix(req.URL.Path, URL_SERIES) { @@ -267,6 +278,10 @@ func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { log.Fatalln(err) } series, err := app.SeriesFull(int64(id)) + if err != nil { + // FIXME 500 + log.Fatalln(err) + } app.render(res, TPL_SERIES, &SeriesModel{ *NewBouquinsModel(series.Name), series, diff --git a/bouquins/db.go b/bouquins/db.go index df925d5..ca64918 100644 --- a/bouquins/db.go +++ b/bouquins/db.go @@ -8,7 +8,6 @@ import ( ) const ( - STMT_PAGE = " LIMIT ? OFFSET ?" STMT_BOOKS0 = `SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book LEFT OUTER JOIN series ON series.id = books_series_link.series ` @@ -29,6 +28,12 @@ const ( STMT_SERIES_SEARCH = "SELECT series.id, series.name FROM series WHERE " STMT_SEARCH_TERM_SERIES = " series.sort like ? " + STMT_AUTHORS0 = `SELECT authors.id, authors.name, count(book) as count FROM authors, books_authors_link + WHERE authors.id = books_authors_link.author GROUP BY author` + STMT_AUTHORS_SEARCH = "SELECT id, name FROM authors WHERE " + STMT_SEARCH_TERM_AUTHOR = " sort like ? " + + STMT_PAGE = " LIMIT ? OFFSET ?" STMT_WHERE = " WHERE " STMT_BOOL_AND = " AND " STMT_BOOL_OR = " OR " @@ -81,6 +86,21 @@ const ( WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id AND books_series_link.series = ?` + STMT_AUTHORS_ID_ASC = STMT_AUTHORS0 + "ORDER BY authors.id " + STMT_PAGE + STMT_AUTHORS_ID_DESC = STMT_AUTHORS0 + "ORDER BY authors.id DESC " + STMT_PAGE + STMT_AUTHORS_NAME_ASC = STMT_AUTHORS0 + "ORDER BY authors.sort " + STMT_PAGE + STMT_AUTHORS_NAME_DESC = STMT_AUTHORS0 + "ORDER BY authors.sort DESC " + STMT_PAGE + STMT_AUTHOR_BOOKS = `SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id + FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book + LEFT OUTER JOIN series ON series.id = books_series_link.series + LEFT OUTER JOIN books_authors_link ON books.id = books_authors_link.book + WHERE books_authors_link.author = ? ORDER BY id` + STMT_AUTHORS_AUTHORS = `SELECT authors.id, authors.name, books_authors_link.book as book + FROM authors, books_authors_link WHERE books_authors_link.author = authors.id + AND books_authors_link.book IN ( SELECT books.id FROM books LEFT OUTER JOIN books_authors_link + ON books.id = books_authors_link.book WHERE books_authors_link.author = ? ORDER BY books.id)` + STMT_AUTHOR = "SELECT name FROM authors WHERE id = ?" + DEF_LIM = 10 BOOKS QueryType = iota @@ -94,6 +114,8 @@ const ( SERIES QueryType = iota SERIES_AUTHORS QueryType = iota SERIES_BOOKS QueryType = iota + AUTHOR QueryType = iota + AUTHOR_BOOKS QueryType = iota ) var QUERIES = map[Query]string{ @@ -117,6 +139,8 @@ var QUERIES = map[Query]string{ Query{SERIES, false, false}: STMT_SERIE, Query{SERIES_AUTHORS, false, false}: STMT_SERIE_AUTHORS, Query{SERIES_BOOKS, false, false}: STMT_SERIE_BOOKS, + Query{AUTHOR, false, false}: STMT_AUTHOR, + Query{AUTHOR_BOOKS, false, false}: STMT_AUTHOR_BOOKS, } var STMTS = make(map[Query]*sql.Stmt) diff --git a/bouquins/dbauthors.go b/bouquins/dbauthors.go new file mode 100644 index 0000000..d21baa4 --- /dev/null +++ b/bouquins/dbauthors.go @@ -0,0 +1,73 @@ +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 +} diff --git a/templates/author.html b/templates/author.html index 6a1e96f..3fda07f 100644 --- a/templates/author.html +++ b/templates/author.html @@ -1,43 +1,61 @@ {{ template "header.html" . }}
- {{ template "footer.html" . }}