mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
feat(dashboard): search every trip by title, date or a place on it (#2190)
This commit is contained in:
@@ -23,7 +23,7 @@ import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import type { Request, Response } from 'express';
|
||||
import type { Options } from 'multer';
|
||||
import path from 'path';
|
||||
import { MAX_TRIP_DAYS, type ActiveTripResponse } from '@trek/shared';
|
||||
import { MAX_TRIP_DAYS, type ActiveTripResponse, type TripSearchResponse } from '@trek/shared';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import type { User } from '../../types';
|
||||
import { TripsService } from './trips.service';
|
||||
@@ -86,6 +86,12 @@ export class TripsController {
|
||||
return { trip: { id, title, start_date, end_date } };
|
||||
}
|
||||
|
||||
/** Places across the user's trips for the dashboard search (#2190). Above @Get(':id'). */
|
||||
@Get('search')
|
||||
search(@CurrentUser() user: User, @Query('q') q?: string): TripSearchResponse {
|
||||
return { matches: this.trips.searchPlaces(user.id, typeof q === 'string' ? q.slice(0, 100) : '') };
|
||||
}
|
||||
|
||||
@Get('cover-images/search')
|
||||
async coverImages(@CurrentUser() user: User, @Query('query') query?: string) {
|
||||
try {
|
||||
|
||||
@@ -306,6 +306,34 @@ export class TripsService {
|
||||
`).get({ userId, today }) as ActiveTrip & { relevance: number } | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's trips, archived ones included, with places whose name or address
|
||||
* contains `query` (#2190). Up to three names per trip; a query under two
|
||||
* characters matches nothing, it would match every place there is.
|
||||
*/
|
||||
searchPlaces(userId: number, query: string): { trip_id: number; places: string[] }[] {
|
||||
const q = query.trim();
|
||||
if (q.length < 2) return [];
|
||||
const like = `%${q.replace(/[\\%_]/g, ch => `\\${ch}`)}%`;
|
||||
const rows = this.db.prepare(`
|
||||
SELECT p.trip_id, p.name
|
||||
FROM places p
|
||||
JOIN trips t ON t.id = p.trip_id
|
||||
LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = :userId
|
||||
WHERE (t.user_id = :userId OR m.user_id IS NOT NULL)
|
||||
AND (p.name LIKE :like ESCAPE '\\' OR p.address LIKE :like ESCAPE '\\')
|
||||
ORDER BY p.trip_id, p.name
|
||||
LIMIT 500
|
||||
`).all({ userId, like }) as { trip_id: number; name: string }[];
|
||||
const byTrip = new Map<number, string[]>();
|
||||
for (const row of rows) {
|
||||
const names = byTrip.get(row.trip_id) ?? [];
|
||||
if (names.length < 3 && !names.includes(row.name)) names.push(row.name);
|
||||
byTrip.set(row.trip_id, names);
|
||||
}
|
||||
return [...byTrip].map(([trip_id, places]) => ({ trip_id, places }));
|
||||
}
|
||||
|
||||
getRaw(tripId: string | number): Trip | undefined {
|
||||
return this.db.prepare('SELECT * FROM trips WHERE id = ?').get(tripId) as Trip | undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user