Backend for songs.zachdecook.com
gemini: Add feature to search by bible verse
Zach DeCook 2022-05-11
parent b5b0dc2 · commit 6288883
-rw-r--r--laravel/app/Song.php7
-rwxr-xr-xlaravel/public/searchVerse.gmi29
2 files changed, 36 insertions, 0 deletions
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index 99b0aa4..6705dfa 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -12,6 +12,13 @@ class Song extends Model
{
return $this->belongsToMany('App\Playlist');
}
+ public static function whereVerse($passage)
+ {
+ $book = explode(' ',$passage)[0];
+ $chapter = (int)explode(' ', $passage)[1];
+
+ return Song::where('verse', 'LIKE', "$book $chapter:%")->orWhere('verse',"$book $chapter");
+ }
public function textTranspose($key)
{
$sc = new SongController();
diff --git a/laravel/public/searchVerse.gmi b/laravel/public/searchVerse.gmi
new file mode 100755
index 0000000..d779988
--- /dev/null
+++ b/laravel/public/searchVerse.gmi
@@ -0,0 +1,29 @@
+#!/usr/bin/php8
+<?php
+
+define('LARAVEL_START', microtime(true));
+require __DIR__.'/../vendor/autoload.php';
+require __DIR__.'/../load-eloquent.php';
+$app = require_once __DIR__.'/../bootstrap/app.php';
+$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
+use App\Song;
+
+$q = getenv('QUERY_STRING');
+if (!$q) {
+ printf("10 Enter a verse to search.\r\n");
+ exit(0);
+}
+
+$results = Song::whereVerse($q)->get(['id', 'verse', 'title']);
+
+if (!count($results)) {
+ printf("51 No songs found for '$q'.");
+ exit(0);
+}
+
+printf("20 text/gemini\r\n");
+?>
+<?php foreach($results as $song): ?>
+=> /song.gmi.php/<?=$song->id?> <?= $song->verse ?>: <?= $song->title ?>
+
+<?php endforeach; ?>