From d88491be9119b447a9f110b51014590a9a3bd286 Mon Sep 17 00:00:00 2001 From: Ro Date: Thu, 7 Sep 2023 14:31:25 -0700 Subject: [PATCH] Added public search API Finally moved over the public search API from the old version and updated the about page to show the new data structure Also tweaked the location update script to change 'defederate' to 'suspend' for the sake of consistency --- app/Http/Controllers/LocationController.php | 2 +- app/Http/Resources/LocationCollection.php | 24 +++++++++++++++++++ app/Http/Resources/LocationResource.php | 26 +++++++++++++++++++++ resources/views/front/about.blade.php | 20 +++++++++------- routes/api.php | 12 ++++++++++ 5 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 app/Http/Resources/LocationCollection.php create mode 100644 app/Http/Resources/LocationResource.php diff --git a/app/Http/Controllers/LocationController.php b/app/Http/Controllers/LocationController.php index b45c26b..a82b1ba 100644 --- a/app/Http/Controllers/LocationController.php +++ b/app/Http/Controllers/LocationController.php @@ -147,7 +147,7 @@ class LocationController extends Controller 'url' => $item['url'], 'description' => ($item['comment'] != null) ? $item['comment'] : "no description", 'active' => true, - 'rating' => $item['rating'], + 'rating' => ($item['rating'] == 'defederate') ? 'suspend', 'added_by' => 1, 'tags' => 'poor moderation, hate speech', 'images' => json_encode($images), diff --git a/app/Http/Resources/LocationCollection.php b/app/Http/Resources/LocationCollection.php new file mode 100644 index 0000000..57e246a --- /dev/null +++ b/app/Http/Resources/LocationCollection.php @@ -0,0 +1,24 @@ + + */ + public function toArray(Request $request): array + { + //return parent::toArray($request); + + return [ + 'listingCount' => count($this->collection), + 'locations' => LocationResource::collection($this->collection), + ]; + } +} diff --git a/app/Http/Resources/LocationResource.php b/app/Http/Resources/LocationResource.php new file mode 100644 index 0000000..5424f27 --- /dev/null +++ b/app/Http/Resources/LocationResource.php @@ -0,0 +1,26 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'url' => $this->url, + 'name' => $this->name, + 'description' => $this->description, + 'rating' => $this->rating, + 'count' => $this->block_count, + 'link' => "/location/" . $this->uuid, + ]; + } +} diff --git a/resources/views/front/about.blade.php b/resources/views/front/about.blade.php index f3fb414..ad16afc 100644 --- a/resources/views/front/about.blade.php +++ b/resources/views/front/about.blade.php @@ -59,16 +59,18 @@
         {
-        "listingCount":1,
-          "locations":
-          [
-            {
-              "url":"search.url",
-              "name":"Instance Name",
-              "description":"instance description",
-              "link":"bad-space-instance-link"
+            data:{
+                "listingCount":1,
+                  "locations":
+                  [
+                    {
+                      "url":"search.url",
+                      "name":"Instance Name",
+                      "description":"instance description",
+                      "link":"bad-space-instance-link"
+                    }
+                  ]
             }
-          ]
         }
         

diff --git a/routes/api.php b/routes/api.php index 889937e..2a1f64a 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,6 +2,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; +use Illuminate\Support\Facades\DB; +use App\Http\Resources\LocationCollection; /* |-------------------------------------------------------------------------- @@ -17,3 +19,13 @@ use Illuminate\Support\Facades\Route; Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); }); + +// public search API +Route::post("/v1/search", function (Request $request) { + $data = json_decode($request->getContent()); + $search = $data->url; + $search = str_replace(",", "", $search); + $search = str_replace(" ", "|", $search); + $results = DB::select("SELECT * FROM searchlocations('$search')"); + return new LocationCollection($results); +});