Add support to query books on specified lists (of award winners).
authorChris Jaekl <chris@localhost>
Sat, 4 Apr 2020 21:07:20 +0000 (17:07 -0400)
committerChris Jaekl <chris@localhost>
Sat, 4 Apr 2020 21:07:20 +0000 (17:07 -0400)
app/index.html
js/src/SearchController.js
main/db.go

index 5871355dfbd3315fb697b18d169b48ce4bda4fad..acddcc58d1977218fab0fde1089a3169490bc46c 100644 (file)
@@ -13,6 +13,7 @@
       <span class="term">Title: <input id="tit" type="text"/></span>
       <span class="term">Series: <input id="ser" type="text"/></span>
       <span class="term">Language: <input id="lan" type="text"/></span>
+      <span class="term">Lists: <input id="lst" type="text"/></span>
     </form>
 
     <div class="pager">
index 6f48c6c828fc63ddb463f9a8fcd5ed2f5c897438..a4f4e4a72ace1bb3ade06e88610cb78dc8111d39 100644 (file)
@@ -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;
     }
 
index d903b26209d2caf501b1bdf8767b51f5e6f44fcb..53feb90908e7e6fc3d33bb5d579c602a1a06a238 100644 (file)
@@ -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"