From cc857b1fd445a9b78946d37edb4b4796fd7a725e Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Sat, 4 Apr 2020 17:07:20 -0400 Subject: [PATCH] Add support to query books on specified lists (of award winners). --- app/index.html | 1 + js/src/SearchController.js | 10 +++++++--- main/db.go | 35 +++++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/app/index.html b/app/index.html index 5871355..acddcc5 100644 --- a/app/index.html +++ b/app/index.html @@ -13,6 +13,7 @@ Title: Series: Language: + Lists:
diff --git a/js/src/SearchController.js b/js/src/SearchController.js index 6f48c6c..a4f4e4a 100644 --- a/js/src/SearchController.js +++ b/js/src/SearchController.js @@ -5,7 +5,7 @@ var SearchController = (function () { var my = {}, booksModel = undefined; - const terms = ['aut', 'lan', 'ser', 'tit']; + const terms = ['aut', 'lan', 'lst', 'ser', 'tit']; // ============== // Public methods @@ -78,10 +78,14 @@ var SearchController = (function () { else { url += '&'; } - url += term + '=' + encodeURIComponent('%' + value + '%'); + if (term === 'lst') { + url += term + '=' + encodeURIComponent('' + value); + } + else { + url += term + '=' + encodeURIComponent('%' + value + '%'); + } } } - return url; } diff --git a/main/db.go b/main/db.go index d903b26..53feb90 100644 --- a/main/db.go +++ b/main/db.go @@ -30,7 +30,7 @@ type Book struct { // --------------------------------------------------------------------------- type Field string const ( - Author, Language, Series, Title Field = "aut", "lan", "ser", "tit" + Author, Language, List, Series, Title Field = "aut", "lan", "lst", "ser", "tit" ) func (f Field) String() string { @@ -188,34 +188,53 @@ func queryBookPathById(id int) (string) { func queryIds(criteria []SearchTerm) []int { query := "SELECT b.id FROM Books b" + " INNER JOIN Authors a ON a.id=b.author" + - " LEFT OUTER JOIN Series s ON s.id=b.series" + " LEFT OUTER JOIN Series s ON s.id=b.series" + + " LEFT OUTER JOIN Lists_Books lb ON b.id=lb.book" + + " LEFT OUTER JOIN Lists l ON l.id=lb.list" - args := make([]interface{}, len(criteria)) + args := make([]interface{}, 0) - for i, criterion := range criteria { + i := 0 + + for _, criterion := range criteria { if 0 == i { query += " WHERE " } else { query += " AND " } - text := criterion.Text - switch criterion.Attribute { case Author: query += " UPPER(a.grouping) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" - text = strings.Replace(text, " ", "", -1) // Remove spaces + args = append(args, strings.Replace(criterion.Text, " ", "", -1)) case Language: query += " UPPER(b.language) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" + args = append(args, criterion.Text) + case List: + codes := strings.Split(criterion.Text, ",") + query += " UPPER(l.code) IN (" + + for j, code := range codes { + if j > 0 { + query += "," + } + query += "UPPER($" + strconv.Itoa(i + j + 1) + ")" + args = append(args, code) + } + query += ")" + i += len(codes) - 1 case Series: query += " UPPER(s.descr) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" + args = append(args, criterion.Text) case Title: query += " UPPER(b.title) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" + args = append(args, criterion.Text) default: report("Error: unrecognized search field in queryIds(): " + criterion.Attribute.String(), nil) return nil } - args[i] = text + + i++ } query += " ORDER BY a.grouping,s.descr,b.volume,b.title,b.path" -- 2.39.2