Skip to content

Commit

Permalink
Merge branch 'development' of github.com:kieler/RailTrail into 64-bac…
Browse files Browse the repository at this point in the history
…kend-refining-database-connection
  • Loading branch information
Niatsuna committed Sep 5, 2023
2 parents d8d37d9 + 35ec563 commit 58b4627
Show file tree
Hide file tree
Showing 41 changed files with 1,380 additions and 1,229 deletions.
12 changes: 11 additions & 1 deletion Server/src/models/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ export type FullTrack = BareTrack & {
/** @see {isTrackList} ts-auto-guard:type-guard */
export type TrackList = BareTrack[]

/** @see {isCreatePOITypeIcon} ts-auto-guard:type-guard */
export enum POITypeIcon {
Generic = 0,
LevelCrossing = 1,
LesserLevelCrossing = 2,
Picnic = 3,
TrackEnd = 4,
TurningPoint = 5,
}

/** @see {isCreatePOIType} ts-auto-guard:type-guard */
export type CreatePOIType = {
name: string
icon: string
icon: POITypeIcon
description?: string
}

Expand Down
19 changes: 15 additions & 4 deletions Server/src/routes/init.route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response, Router } from "express"
import { jsonParser } from "."
import { InitRequestApp, InitResponseApp, TrackListEntryApp } from "../models/api.app"
import { PointOfInterest, Position } from "../models/api"
import { PointOfInterest, POITypeIcon, Position } from "../models/api"
import { logger } from "../utils/logger"
import TrackService from "../services/track.service"
import { POI, Track } from "@prisma/client"
import { POI, POIType, Track } from "@prisma/client"
import POIService from "../services/poi.service"
import { Feature, FeatureCollection, GeoJsonProperties, LineString, Point } from "geojson"
import GeoJSONUtils from "../utils/geojsonUtils"
Expand Down Expand Up @@ -195,11 +195,22 @@ export class InitRoute {
private async getAppPoisFromDbPoi(pois: POI[]): Promise<PointOfInterest[]> {
const apiPois: PointOfInterest[] = []
for (const poi of pois) {
const type: number = poi.typeId
const type: POIType | null = await database.pois.getTypeById(poi.typeId)
if (!type) {
logger.error(`Could not determine type of poi with id ${poi.uid}`)
continue
}
const poiIcon: number = Number.parseInt(type.icon)
if (!Number.isInteger(poiIcon)) {
logger.error(`Icon of type with id ${type.uid} is not an integer.`)
continue
}
// Check if the icon number is a member of the enum.
if (!(poiIcon in POITypeIcon)) {
logger.warn(`Icon of type with id ${type.uid} is ${poiIcon}, not one of the known icons.`)
}
// ensure that the app always gets an enum member.
const appType: POITypeIcon = poiIcon in POITypeIcon ? poiIcon : POITypeIcon.Generic

const geoJsonPos: Feature<Point, GeoJsonProperties> | null = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
if (!geoJsonPos) {
Expand All @@ -219,7 +230,7 @@ export class InitRoute {
apiPois.push({
id: poi.uid,
name: poi.name,
typeId: 0 <= type && type <= 4 ? type : 0,
typeId: appType,
pos: pos,
percentagePosition: percentagePosition,
isTurningPoint: poi.isTurningPoint,
Expand Down
8 changes: 4 additions & 4 deletions Server/src/routes/poi.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class PoiRoute {
properties: null
}

const type: POIType | null = await POIService.getPOITypeById(userData.typeId)
const type: POIType | null = await database.pois.getTypeById(userData.typeId)
if (!type) {
logger.error(`Could not find poi type with id ${userData.typeId}`)
res.sendStatus(400)
Expand Down Expand Up @@ -179,7 +179,7 @@ export class PoiRoute {
return
}

const poiToUpdate: POI | null = await POIService.getPOIById(poiId)
const poiToUpdate: POI | null = await database.pois.getById(poiId)
if (!poiToUpdate) {
logger.error(`Could not find poi with id ${userData.id}`)
res.sendStatus(404)
Expand Down Expand Up @@ -234,13 +234,13 @@ export class PoiRoute {
}

// Look out for the POI
const poi: POI | null = await POIService.getPOIById(poiId)
const poi: POI | null = await database.pois.getById(poiId)
if (!poi) {
logger.error(`Could not find poi with id ${poiId}`)
res.sendStatus(500)
return
}
const success: boolean = await POIService.removePOI(poi)
const success: boolean = await database.pois.remove(poi.uid)
if (!success) {
logger.error(`Could not delete poi with id ${poiId}`)
res.sendStatus(500)
Expand Down
12 changes: 7 additions & 5 deletions Server/src/routes/poitype.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class PoiTypeRoute {
const type: APIPoiType = {
id: uid,
name,
icon,
icon: Number.parseInt(icon),
description: description ?? undefined
}
return type
Expand Down Expand Up @@ -74,17 +74,18 @@ export class PoiTypeRoute {
id: poiType.uid,
name: poiType.name,
description: poiType.description ?? undefined,
icon: poiType.icon
icon: Number.parseInt(poiType.icon)
}

res.json(apiPoiType)
return
}

private async createType(req: Request, res: Response): Promise<void> {
// TODO: ensure that the icon is a member of the enum (or check if the type guard checks that)
const { name, icon, description }: CreatePOIType = req.body

const poiType: POIType | null = await database.pois.saveType({ name, icon, description })
const poiType: POIType | null = await database.pois.saveType({ name, icon: icon.toString(), description })
if (!poiType) {
logger.error("Could not create poi type")
res.sendStatus(500)
Expand All @@ -94,7 +95,7 @@ export class PoiTypeRoute {
const responseType: APIPoiType = {
id: poiType.uid,
name: poiType.name,
icon: poiType.icon,
icon: Number.parseInt(poiType.icon),
description: poiType.description ?? undefined
}
res.status(201).json(responseType)
Expand All @@ -104,6 +105,7 @@ export class PoiTypeRoute {
private async updateType(req: Request, res: Response): Promise<void> {
const typeId: number = parseInt(req.params.typeId)
const userData: APIPoiType = req.body
// TODO: ensure that the icon is a member of the enum (or check if the type guard checks that)
if (userData.id !== typeId) {
res.sendStatus(400)
return
Expand All @@ -118,7 +120,7 @@ export class PoiTypeRoute {

type = await database.pois.updateType(typeId, {
name: userData.name,
icon: userData.icon,
icon: userData.icon.toString(),
description: userData.description
})
if (!type) {
Expand Down
5 changes: 2 additions & 3 deletions Server/src/routes/track.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import TrackService from "../services/track.service"
import { POI, Track, Vehicle } from "@prisma/client"
import please_dont_crash from "../utils/please_dont_crash"
import { logger } from "../utils/logger"
import { UpdateTrack, BareTrack, FullTrack, PointOfInterest, Position, Vehicle as APIVehicle } from "../models/api"
import { BareTrack, FullTrack, PointOfInterest, Position, UpdateTrack, Vehicle as APIVehicle } from "../models/api"
import VehicleService from "../services/vehicle.service"
import { Feature, GeoJsonProperties, LineString, Point } from "geojson"
import POIService from "../services/poi.service"
import GeoJSONUtils from "../utils/geojsonUtils"
import database from "../services/database.service"
import TrackerService from "../services/tracker.service"

/**
* The router class for the routing of the track uploads from the website.
Expand Down Expand Up @@ -234,7 +233,7 @@ export class TrackRoute {
track: vehicle.trackId,
name: vehicle.name ? vehicle.name : "Empty Name",
type: vehicle.typeId,
trackerIds: (await TrackerService.getTrackerByVehicle(vehicle.uid)).map(y => y.uid),
trackerIds: (await database.trackers.getByVehicleId(vehicle.uid)).map(y => y.uid),
pos,
percentagePosition,
heading
Expand Down
4 changes: 2 additions & 2 deletions Server/src/routes/tracker.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class TrackerRoute {
}
const trackerId = trackerData.end_device_ids.device_id
// load the tracker from the database
const tracker: Tracker | null = await TrackerService.getTrackerById(trackerId)
const tracker: Tracker | null = await database.trackers.getById(trackerId)
if (!tracker) {
logger.silly(`Tried to append log on unknown tracker with id ${trackerId}`)
res.sendStatus(401)
Expand All @@ -172,7 +172,7 @@ export class TrackerRoute {
}
// and get the vehicle the tracker is attached to. If it has no associated vehicle, do nothing.
const associatedVehicle: Vehicle | null = tracker.vehicleId
? await VehicleService.getVehicleById(tracker.vehicleId)
? await database.vehicles.getById(tracker.vehicleId)
: null
if (!associatedVehicle) {
logger.silly(`Got position from tracker ${trackerId} with no associated vehicle.`)
Expand Down
4 changes: 2 additions & 2 deletions Server/src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ export class UserRoute {

/**
* Delete a user with a certain uid.
* @param req A request containing a userId in its parameters.
* @param _req A request containing a userId in its parameters.
* @param res
* @returns Nothing
*/
private async deleteUser(req: Request, res: Response): Promise<void> {
private async deleteUser(_req: Request, res: Response): Promise<void> {
if (!res.locals || !res.locals.username) {
res.sendStatus(400)
return
Expand Down
Loading

0 comments on commit 58b4627

Please sign in to comment.