Added place search API

This commit is contained in:
Samy Avrillon 2024-07-22 01:33:37 +02:00
parent d6521842b2
commit 23ccfea82d
Signed by: Mysaa
GPG Key ID: 0220AC4A3D6A328B
4 changed files with 89 additions and 0 deletions

22
API.md
View File

@ -120,6 +120,28 @@ __Notes :__
--------------------- ---------------------
### /api/map/search_place?text=TEXT
> Return a list of dict representing all names (place name or user name)
> which matches the query TEXT, along with associated place id.
__Format of the dict :__
```json
[
{
'type': char, //'U' if matching text is an user name, 'P' if it is a place name
'txt':string, //the matching name (whichever type)
'pid': int, //id of the associated place
'floor' : string, //id of the lowest floor the place is part of
'rank': float //the «match» score of this txt against query TEXT
},
{...}
]
```
__Notes :__
- If _NAME_ doesn't correspond to any registered place : Return an Empty list.
---------------------
### /api/map/get_site_name/ID ### /api/map/get_site_name/ID
> Return the name of the Site ID. > Return the name of the Site ID.

View File

@ -238,6 +238,22 @@ class JsonReaderController extends AbstractController
$r->addCylinderRepresentation($cyl); $r->addCylinderRepresentation($cyl);
$manager->persist($r); $manager->persist($r);
$names = $room['names'];
$rusers = $room['inhabitants'];
foreach($names as $name) {
$n = new PlaceName();
$n->setName($name);
$n->setPlace($r);
$manager->persist($n);
}
foreach($rusers as $ruser) {
$ru = new RoomUserName();
$ru->setUserName($ruser);
$ru->setPlace($r);
$manager->persist($ru);
}
return $r; return $r;
} }

View File

@ -8,6 +8,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException; use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@ -159,6 +160,15 @@ class MapApiController extends AbstractController
return $this->json($result); return $this->json($result);
} }
#[Route('/map/search_place')]
public function search_place(Request $request, PlaceRepository $pnRepo) {
$text = $request->query->get('text');
$res = $pnRepo->searchPlaceNameAndUser($text);
return $this->json($res);
}
#[Route('/map/get_place_info/{id}')] #[Route('/map/get_place_info/{id}')]
public function get_place_info(PlaceRepository $rep, int $id): JsonResponse public function get_place_info(PlaceRepository $rep, int $id): JsonResponse

View File

@ -78,6 +78,47 @@ class PlaceRepository extends ServiceEntityRepository
return $this->createQueryBuilder('s')->orderBy('s.id','ASC')->getQuery()->getResult(); return $this->createQueryBuilder('s')->orderBy('s.id','ASC')->getQuery()->getResult();
} }
public function searchPlaceNameAndUser(string $text)
{
$sql = "
(
SELECT
run.place_id AS pid,
'U' AS type,
user_name AS txt,
MIN(pf.floor_id) AS floor,
SIMILARITY(user_name,:searchtext) AS rank
FROM room_user_name AS run
JOIN place_floor AS pf ON pf.place_id=run.place_id
GROUP BY run.id
ORDER BY rank DESC
LIMIT :limit
)
UNION
(
SELECT DISTINCT ON (pn.place_id)
pn.place_id AS pid,
'P' AS type,
name AS txt,
MIN(pf.floor_id) AS floor,
SIMILARITY(name,:searchtext) AS rank
FROM place_name pn
JOIN place_floor AS pf ON pf.place_id=pn.place_id
GROUP BY pn.id
ORDER BY pn.place_id
)
ORDER BY rank DESC
LIMIT :limit;
";
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->bindValue("searchtext",$text);
$stmt->bindValue("limit",5);
$res = $stmt->executeQuery() ;
$out = $res->fetchAllAssociative();
return $out;
}
// public function findOneBySomeField($value): ?Place // public function findOneBySomeField($value): ?Place
// { // {