Backend for songs.zachdecook.com
searchVerse: Use the parser to validate the references
Zach DeCook 2022-12-10
parent 426f68d · commit 8af58ae
-rw-r--r--laravel/app/Song.php7
-rwxr-xr-xlaravel/public/searchVerse.gmi7
2 files changed, 11 insertions, 3 deletions
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index efd30a6..e2f2589 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -15,8 +15,11 @@ class Song extends Model
}
public static function whereVerse($passage)
{
- $book = explode(' ',$passage)[0];
- $chapter = (int)explode(' ', $passage)[1];
+ $parser = new BiblePassageParser();
+ // Can throw an exception.
+ $refs = $parser->parse($passage);
+ $book = (string)($refs[0]->from()->book());
+ $chapter = $refs[0]->from()->chapter();
return Song::where('verse', 'LIKE', "$book $chapter:%")->orWhere('verse',"$book $chapter")->orWhere('verse', 'LIKE', "%; $book $chapter:%");
}
diff --git a/laravel/public/searchVerse.gmi b/laravel/public/searchVerse.gmi
index e6b38bd..7685979 100755
--- a/laravel/public/searchVerse.gmi
+++ b/laravel/public/searchVerse.gmi
@@ -14,7 +14,12 @@ if (!$q) {
exit(0);
}
-$results = Song::whereVerse($q)->get(['id', 'verse', 'title']);
+try {
+ $results = Song::whereVerse($q)->get(['id', 'verse', 'title']);
+} catch (Throwable $e) {
+ printf("51 ".$e->getMessage()."\r\n");
+ exit(0);
+}
if (!count($results)) {
printf("51 No songs found for '$q'.\r\n");