Merge branch 'fix/typesense' into 'dev'
Fix/typesense See merge request sae-but2/2025-26/gestion-benevoles-association/backend!24
This commit is contained in:
+1
-1
@@ -23,5 +23,5 @@ Homestead.json
|
||||
Homestead.yaml
|
||||
Thumbs.db
|
||||
|
||||
/public/*
|
||||
/public/**
|
||||
!/public/index.php
|
||||
|
||||
@@ -2,12 +2,9 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Typesense\Client;
|
||||
use Exception;
|
||||
use App\Models\Events;
|
||||
|
||||
class SearchController extends Controller
|
||||
{
|
||||
@@ -27,147 +24,42 @@ class SearchController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
private function createEventsCollection()
|
||||
{
|
||||
try {
|
||||
$collections = $this->client->collections->retrieve();
|
||||
|
||||
if (!in_array('events', array_column($collections, 'name'))) {
|
||||
$this->client->collections->create([
|
||||
'name' => 'events',
|
||||
'fields' => [
|
||||
['name' => 'name', 'type' => 'string'],
|
||||
['name' => 'description', 'type' => 'string'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error while creating the events collection : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function indexEvents()
|
||||
{
|
||||
try {
|
||||
$events = Events::all();
|
||||
|
||||
foreach ($events as $event) {
|
||||
|
||||
$this->client->collections['events']->documents->upsert([
|
||||
'id' => (string) $event->id,
|
||||
'name' => $event->name,
|
||||
'description' => $event->description,
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error while indexing events : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function searchEvents(Request $request)
|
||||
{
|
||||
try {
|
||||
try {
|
||||
$this->client->collections['events']->delete();
|
||||
} catch (\Exception $e) {
|
||||
// ignore si la collection n'existe pas
|
||||
}
|
||||
|
||||
$this->createEventsCollection();
|
||||
$this->indexEvents();
|
||||
$query = $request->input('query', '');
|
||||
|
||||
$searchResults = $this->client->collections['events']->documents->search([
|
||||
'q' => $query,
|
||||
'query_by' => 'name,description',
|
||||
]);
|
||||
|
||||
return response()->json($searchResults);
|
||||
return response()->json(
|
||||
$this->client->collections['events']
|
||||
->documents
|
||||
->search([
|
||||
'q' => $query,
|
||||
'query_by' => 'name,description',
|
||||
])
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
return response()->json([
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private function createUsersCollection()
|
||||
{
|
||||
try {
|
||||
$collections = $this->client->collections->retrieve();
|
||||
|
||||
if (!in_array('users', array_column($collections, 'name'))) {
|
||||
$this->client->collections->create([
|
||||
'name' => 'users',
|
||||
'fields' => [
|
||||
['name' => 'name', 'type' => 'string'],
|
||||
['name' => 'lastname', 'type' => 'string'],
|
||||
['name' => 'validate', 'type' => 'bool'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error while creating the users collection : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function indexUsers()
|
||||
{
|
||||
try {
|
||||
$users = User::where('validate', 1)->get();
|
||||
|
||||
foreach ($users as $user) {
|
||||
|
||||
$this->client->collections['users']->documents->upsert([
|
||||
'id' => (string) $user->id,
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'validate' => (bool) $user->validate,
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error while indexing users : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function searchUsers(Request $request)
|
||||
{
|
||||
try {
|
||||
try {
|
||||
$this->client->collections['users']->delete();
|
||||
} catch (\Exception $e) {
|
||||
// ignore si la collection n'existe pas
|
||||
}
|
||||
|
||||
|
||||
$this->createUsersCollection();
|
||||
|
||||
$this->indexUsers();
|
||||
$query = $request->input('query', '');
|
||||
|
||||
$searchResults = $this->client->collections['users']->documents->search([
|
||||
'q' => $query,
|
||||
'query_by' => 'name,lastname',
|
||||
'filter_by' => 'validate:=true',
|
||||
]);
|
||||
|
||||
return response()->json($searchResults);
|
||||
return response()->json(
|
||||
$this->client->collections['users']
|
||||
->documents
|
||||
->search([
|
||||
'q' => $query,
|
||||
'query_by' => 'name,lastname',
|
||||
'filter_by' => 'validate:=true',
|
||||
])
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
return response()->json([
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Events;
|
||||
use App\Services\TypesenseService;
|
||||
|
||||
class EventObserver
|
||||
{
|
||||
public function created(Events $event)
|
||||
{
|
||||
app(TypesenseService::class)->upsertEvent($event);
|
||||
}
|
||||
|
||||
public function updated(Events $event)
|
||||
{
|
||||
app(TypesenseService::class)->upsertEvent($event);
|
||||
}
|
||||
|
||||
public function deleted(Events $event)
|
||||
{
|
||||
app(TypesenseService::class)->deleteEvent($event->id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\TypesenseService;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
public function created(User $user)
|
||||
{
|
||||
app(TypesenseService::class)->upsertUser($user);
|
||||
}
|
||||
|
||||
public function updated(User $user)
|
||||
{
|
||||
app(TypesenseService::class)->upsertUser($user);
|
||||
}
|
||||
|
||||
public function deleted(User $user)
|
||||
{
|
||||
app(TypesenseService::class)->deleteUser($user->id);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Events;
|
||||
use App\Models\User;
|
||||
use App\Observers\EventObserver;
|
||||
use App\Observers\UserObserver;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
@@ -25,5 +29,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
Route::middleware('web')->prefix('api')->group(base_path('routes/events.php'));
|
||||
Route::middleware('web')->prefix('api')->group(base_path('routes/notifications.php'));
|
||||
Route::middleware('web')->prefix('api')->group(base_path('routes/channels.php'));
|
||||
User::observe(UserObserver::class);
|
||||
Events::observe(EventObserver::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Events;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Typesense\Client;
|
||||
|
||||
class TypesenseService
|
||||
{
|
||||
protected Client $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client([
|
||||
'api_key' => env('TYPESENSE_API_KEY'),
|
||||
'nodes' => [
|
||||
[
|
||||
'host' => env('TYPESENSE_CONNECTION'),
|
||||
'port' => '8108',
|
||||
'protocol' => 'http',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function ensureCollectionsExist(): void
|
||||
{
|
||||
static $checked = false;
|
||||
|
||||
if ($checked) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$collections = $this->client->collections->retrieve();
|
||||
$names = array_column($collections, 'name');
|
||||
|
||||
if (!in_array('events', $names)) {
|
||||
$this->client->collections->create([
|
||||
'name' => 'events',
|
||||
'fields' => [
|
||||
['name' => 'name', 'type' => 'string'],
|
||||
['name' => 'description', 'type' => 'string'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
if (!in_array('users', $names)) {
|
||||
$this->client->collections->create([
|
||||
'name' => 'users',
|
||||
'fields' => [
|
||||
['name' => 'name', 'type' => 'string'],
|
||||
['name' => 'lastname', 'type' => 'string'],
|
||||
['name' => 'validate', 'type' => 'bool'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$checked = true;
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function upsertEvent(Events $event): void
|
||||
{
|
||||
$this->ensureCollectionsExist();
|
||||
|
||||
try {
|
||||
$this->client->collections['events']
|
||||
->documents
|
||||
->upsert([
|
||||
'id' => (string) $event->id,
|
||||
'name' => $event->name,
|
||||
'description' => $event->description,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteEvent(int $eventId): void
|
||||
{
|
||||
$this->ensureCollectionsExist();
|
||||
|
||||
try {
|
||||
$this->client->collections['events']
|
||||
->documents[(string) $eventId]
|
||||
->delete();
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function upsertUser(User $user): void
|
||||
{
|
||||
$this->ensureCollectionsExist();
|
||||
|
||||
if (!$user->validate) {
|
||||
$this->deleteUser($user->id);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->client->collections['users']
|
||||
->documents
|
||||
->upsert([
|
||||
'id' => (string) $user->id,
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'validate' => true,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteUser(int $userId): void
|
||||
{
|
||||
$this->ensureCollectionsExist();
|
||||
|
||||
try {
|
||||
$this->client->collections['users']
|
||||
->documents[(string) $userId]
|
||||
->delete();
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg width="800px" height="800px" viewBox="0 -8 72 72" id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg"><defs><style>.cls-1{fill:#010002;}</style></defs><title>tools</title><path class="cls-1" d="M13.32,44.31a4.5,4.5,0,0,0,6.36,6.36L34.44,35.91l-6.36-6.36Z"/><polygon class="cls-1" points="50.2 16.96 56.63 13.63 60 7.13 56.86 3.99 50.36 7.36 47.03 13.78 39.21 21.61 42.38 24.79 50.2 16.96"/><path class="cls-1" d="M51.61,34,51,34a8.9,8.9,0,0,0-3.11.58L29.45,16.09A8.9,8.9,0,0,0,30,13l-.06-.6A8.9,8.9,0,0,0,17.19,4.89l6,6a3,3,0,0,1,.68,1.08A3,3,0,0,1,21,16a2.92,2.92,0,0,1-1-.21,3.15,3.15,0,0,1-1.08-.67l-6-6a8.9,8.9,0,0,0,7.49,12.78L21,22a8.9,8.9,0,0,0,3.11-.58L42.6,39.84A8.9,8.9,0,0,0,42,43l.06.6A9,9,0,0,0,51,51.94a8.72,8.72,0,0,0,3.85-.9l-6-6A3.08,3.08,0,0,1,48.21,44,3,3,0,0,1,51,40a2.92,2.92,0,0,1,1,.21,2.72,2.72,0,0,1,1.08.67l6,6A8.9,8.9,0,0,0,51.61,34Z"/></svg>
|
||||
|
Before Width: | Height: | Size: 1009 B |
@@ -1,14 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="logoIcone.png" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Comité des fêtes de Beaupont</title>
|
||||
<script type="module" crossorigin src="/assets/index-cYGdr2ka.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-DYjA7P7p.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
||||
Regular → Executable
Reference in New Issue
Block a user