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
This commit is contained in:
Ro 2023-09-07 14:31:25 -07:00
parent d5efbd96a3
commit 0c2b8bae7c
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
5 changed files with 74 additions and 10 deletions

View file

@ -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),

View file

@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class LocationCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
//return parent::toArray($request);
return [
'listingCount' => count($this->collection),
'locations' => LocationResource::collection($this->collection),
];
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class LocationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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,
];
}
}

View file

@ -59,6 +59,7 @@
<pre>
<code>{
data:{
"listingCount":1,
"locations":
[
@ -69,6 +70,7 @@
"link":"bad-space-instance-link"
}
]
}
}</code>
</pre>
</p>

View file

@ -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);
});