Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Backend/src/gists/entities/gist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index } from
@Entity('gists')
@Index('idx_gists_location_cell')
@Index('idx_gists_author_address')
@Index('idx_gists_location_geography', { synchronize: false })
export class Gist {
@PrimaryGeneratedColumn('uuid')
id: string;
Expand Down
12 changes: 9 additions & 3 deletions Backend/src/gists/gist.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class GistRepository {

const extraWhere = `AND ${clauses.join(' AND ')}`;

const items = await this.dataSource.query<Gist[]>(
const rows = await this.dataSource.query<Array<Gist & { distance_meters: string }>>(
`
SELECT
g.id,
Expand All @@ -107,22 +107,28 @@ export class GistRepository {
ST_X(g.location::geometry) AS lon,
ST_Y(g.location::geometry) AS lat,
ST_Distance(
g.location,
g.location::geography,
ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography
) AS distance_meters
FROM gists g
WHERE ST_DWithin(
g.location,
g.location::geography,
ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography,
$3
)
AND g.expires_at > NOW()
${extraWhere}
ORDER BY distance_meters ASC, g.created_at DESC
LIMIT $4
`,
params,
);

const items: Gist[] = rows.map((r) => ({
...r,
distanceMeters: parseFloat(r.distance_meters),
}));

return PaginationHelper.buildResponse(items, limit);
}

Expand Down
Loading