Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert icon functionality from filename to number #108

Merged
merged 7 commits into from
Sep 4, 2023
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
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 @@ -199,11 +199,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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an enum is compiled down to an object, this should check -- using the backwards mapping -- if the number is a value assigned to an enum member.

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 @@ -223,7 +234,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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this doesn't collide too much with any changes made in #112

description: description
}
})
Expand Down
Loading
Loading