style: run prettier
This commit is contained in:
@ -1,4 +1,10 @@
|
|||||||
import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
|
import {
|
||||||
|
BaseEntity,
|
||||||
|
Column,
|
||||||
|
Entity,
|
||||||
|
ManyToOne,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
} from "typeorm";
|
||||||
import type { User } from "./User";
|
import type { User } from "./User";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
@ -7,12 +13,12 @@ export class Hazard extends BaseEntity {
|
|||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
type: "float"
|
type: "float",
|
||||||
})
|
})
|
||||||
latitude: number;
|
latitude: number;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
type: "float"
|
type: "float",
|
||||||
})
|
})
|
||||||
longitude: number;
|
longitude: number;
|
||||||
|
|
||||||
|
|||||||
34
src/main.ts
34
src/main.ts
@ -166,30 +166,39 @@ if (process.env.REVIEWS_ENABLED) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const VALID_HAZARD_TYPES = [
|
const VALID_HAZARD_TYPES = ["bumpy-road"];
|
||||||
"bumpy-road"
|
|
||||||
];
|
|
||||||
|
|
||||||
if (process.env.HAZARDS_ENABLED) {
|
if (process.env.HAZARDS_ENABLED) {
|
||||||
app.get("/api/hazards", async (c) => {
|
app.get("/api/hazards", async (c) => {
|
||||||
const { lat, lon, radius } = c.req.query();
|
const { lat, lon, radius } = c.req.query();
|
||||||
if (!lat || !lon || !radius) {
|
if (!lat || !lon || !radius) {
|
||||||
return c.json({ error: "Latitude, longitude, and radius are required" }, 400);
|
return c.json(
|
||||||
|
{ error: "Latitude, longitude, and radius are required" },
|
||||||
|
400,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// Remove unnecessary precision from lat/lon
|
// Remove unnecessary precision from lat/lon
|
||||||
const nlat = Number(parseFloat(lat).toFixed(4));
|
const nlat = Number(parseFloat(lat).toFixed(4));
|
||||||
const nlon = Number(parseFloat(lon).toFixed(4));
|
const nlon = Number(parseFloat(lon).toFixed(4));
|
||||||
const nradius = Number(radius);
|
const nradius = Number(radius);
|
||||||
if(isNaN(nradius) || nradius <= 0) {
|
if (isNaN(nradius) || nradius <= 0) {
|
||||||
return c.json({ error: "Invalid radius" }, 400);
|
return c.json({ error: "Invalid radius" }, 400);
|
||||||
}
|
}
|
||||||
if(nradius > 1000) {
|
if (nradius > 1000) {
|
||||||
return c.json({ error: "Radius too large, max is 1000 km" }, 400);
|
return c.json({ error: "Radius too large, max is 1000 km" }, 400);
|
||||||
}
|
}
|
||||||
console.log(`Fetching hazards for lat: ${lat}, lon: ${lon}, radius: ${radius} km`);
|
console.log(
|
||||||
|
`Fetching hazards for lat: ${lat}, lon: ${lon}, radius: ${radius} km`,
|
||||||
|
);
|
||||||
const hazards = await Hazard.createQueryBuilder("hazard") // Crazy math to get a rough bounding box for the radius in km
|
const hazards = await Hazard.createQueryBuilder("hazard") // Crazy math to get a rough bounding box for the radius in km
|
||||||
.where("hazard.latitude BETWEEN :minLat AND :maxLat", { minLat: nlat - nradius / 110.574, maxLat: nlat + nradius / 110.574 })
|
.where("hazard.latitude BETWEEN :minLat AND :maxLat", {
|
||||||
.andWhere("hazard.longitude BETWEEN :minLon AND :maxLon", { minLon: nlon - nradius / (111.320 * Math.cos(nlat * (Math.PI / 180))), maxLon: nlon + nradius / (111.320 * Math.cos(nlat * (Math.PI / 180))) })
|
minLat: nlat - nradius / 110.574,
|
||||||
|
maxLat: nlat + nradius / 110.574,
|
||||||
|
})
|
||||||
|
.andWhere("hazard.longitude BETWEEN :minLon AND :maxLon", {
|
||||||
|
minLon: nlon - nradius / (111.32 * Math.cos(nlat * (Math.PI / 180))),
|
||||||
|
maxLon: nlon + nradius / (111.32 * Math.cos(nlat * (Math.PI / 180))),
|
||||||
|
})
|
||||||
.getMany();
|
.getMany();
|
||||||
return c.json(hazards);
|
return c.json(hazards);
|
||||||
});
|
});
|
||||||
@ -202,11 +211,8 @@ if (process.env.HAZARDS_ENABLED) {
|
|||||||
400,
|
400,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if(!VALID_HAZARD_TYPES.includes(type)) {
|
if (!VALID_HAZARD_TYPES.includes(type)) {
|
||||||
return c.json(
|
return c.json({ error: "Invalid hazard type" }, 400);
|
||||||
{ error: "Invalid hazard type" },
|
|
||||||
400,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const authHeader = c.req.header("Authorization");
|
const authHeader = c.req.header("Authorization");
|
||||||
|
|||||||
Reference in New Issue
Block a user