From 4eb9b4e2e740117d26b2078591dc0ed5f9b4341a Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Mon, 30 Mar 2020 23:22:20 -0400 Subject: [PATCH] Add ability to search by language. Note that this expects ISO language code (e.g., `de` for German) --- app/index.html | 1 + js/src/SearchController.js | 2 +- main/db.go | 12 ++++++++---- main/handler.go | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/index.html b/app/index.html index d26603d..5871355 100644 --- a/app/index.html +++ b/app/index.html @@ -12,6 +12,7 @@ Author: Title: Series: + Language:
diff --git a/js/src/SearchController.js b/js/src/SearchController.js index 54d76fa..6f48c6c 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', 'tit', 'ser']; + const terms = ['aut', 'lan', 'ser', 'tit']; // ============== // Public methods diff --git a/main/db.go b/main/db.go index b636cff..d903b26 100644 --- a/main/db.go +++ b/main/db.go @@ -20,6 +20,7 @@ type Book struct { DDC string // Dewey Decimal Classification Description string // Back cover / inside flap blurb, describing the book Genre string // e.g. "adventure", "historical", "mystery", "romance", "sf" (Science Fiction) + Language string LCC string // Library of Congress Classification SeriesName string Title string @@ -29,7 +30,7 @@ type Book struct { // --------------------------------------------------------------------------- type Field string const ( - Author, Series, Title Field = "aut", "ser", "tit" + Author, Language, Series, Title Field = "aut", "lan", "ser", "tit" ) func (f Field) String() string { @@ -97,7 +98,7 @@ func openDb(user, pass, dbName string) (*sql.DB) { } func queryBooksByIds(ids []int) []Book { - query := `SELECT s.age,a.grouping,a.reading,a.sort,b.cover,c.ddc,b.description,s.genre,c.lcc,s.descr,b.title,b.volume + query := `SELECT s.age,a.grouping,a.reading,a.sort,b.cover,c.ddc,b.description,s.genre,b.language,c.lcc,s.descr,b.title,b.volume FROM Authors a INNER JOIN Books b ON a.id=b.author LEFT OUTER JOIN Classifications c ON c.id=b.classification @@ -128,10 +129,10 @@ func queryBooksByIds(ids []int) []Book { row := ps.QueryRow(id) - var age, grouping, reading, sort, ddc, description, genre, lcc, name, title, volume sql.NullString + var age, grouping, reading, sort, ddc, description, genre, language, lcc, name, title, volume sql.NullString var cover sql.NullInt64 - err = row.Scan(&age, &grouping, &reading, &sort, &cover, &ddc, &description, &genre, &lcc, &name, &title, &volume) + err = row.Scan(&age, &grouping, &reading, &sort, &cover, &ddc, &description, &genre, &language, &lcc, &name, &title, &volume) if err != nil { report("Error: Failed to read book:" + strconv.Itoa(id) + ":", err) } else { @@ -145,6 +146,7 @@ func queryBooksByIds(ids []int) []Book { b.DDC = nsVal(ddc) b.Description = nsVal(description) b.Genre = nsVal(genre) + b.Language = nsVal(language) b.LCC = nsVal(lcc) b.SeriesName = nsVal(name) b.Title = nsVal(title) @@ -203,6 +205,8 @@ func queryIds(criteria []SearchTerm) []int { case Author: query += " UPPER(a.grouping) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" text = strings.Replace(text, " ", "", -1) // Remove spaces + case Language: + query += " UPPER(b.language) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" case Series: query += " UPPER(s.descr) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" case Title: diff --git a/main/handler.go b/main/handler.go index cd35a76..599f5d5 100644 --- a/main/handler.go +++ b/main/handler.go @@ -180,7 +180,7 @@ func handleInfo(w http.ResponseWriter, r *http.Request) { func handleSearch(w http.ResponseWriter, r *http.Request) { var err error - fields := []Field{Author, Title, Series} + fields := []Field{Author, Language, Series, Title} terms := make([]SearchTerm, len(fields)) -- 2.39.2