Skip to content

Commit

Permalink
Merge pull request #108 from kieler/107-poi-icon
Browse files Browse the repository at this point in the history
Convert icon functionality from filename to number
  • Loading branch information
liamboddin authored Sep 4, 2023
2 parents 50d0b5c + fcf11f3 commit eea9be3
Show file tree
Hide file tree
Showing 28 changed files with 862 additions and 166 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 VehicleService from "../services/vehicle.service"
import { Feature, FeatureCollection, GeoJsonProperties, LineString, Point } from "geojson"
Expand Down Expand Up @@ -196,11 +196,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 POIService.getPOITypeById(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 @@ -220,7 +231,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
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.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 @@ -116,7 +118,7 @@ export class PoiTypeRoute {
return
}

type = await database.pois.updateType(typeId, userData.name, userData.icon, userData.description)
type = await database.pois.updateType(typeId, userData.name, userData.icon.toString(), userData.description)
if (!type) {
logger.error(`Could not update poi type with id ${userData.id}`)
res.sendStatus(500)
Expand Down
1 change: 1 addition & 0 deletions Server/src/services/db/poi.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class POIController {
},
data: {
name: name,
icon: icon,
description: description
}
})
Expand Down
Loading

0 comments on commit eea9be3

Please sign in to comment.