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

64 backend refining database connection #112

Merged
merged 22 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 20 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
6 changes: 3 additions & 3 deletions Server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ model POI {
typeId Int
type POIType @relation(fields: [typeId], references: [uid])
trackId Int
track Track @relation(fields: [trackId], references: [uid])
track Track @relation(fields: [trackId], references: [uid], onDelete: Cascade)
}

model Track {
Expand All @@ -62,12 +62,12 @@ model VehicleType {
model Vehicle {
uid Int @id @default(autoincrement())
name String

typeId Int
type VehicleType @relation(fields: [typeId], references: [uid])
trackId Int
track Track @relation(fields: [trackId], references: [uid])

tracker Tracker[]
logs Log[]

Expand Down
21 changes: 11 additions & 10 deletions Server/src/routes/poi.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Position, UpdatePointOfInterest } from "../models/api"
import { logger } from "../utils/logger"
import POIService from "../services/poi.service"
import { Feature, GeoJsonProperties, Point } from "geojson"
import { POI, POIType, Track } from "@prisma/client"
import { POI, POIType, Track, Prisma } from "@prisma/client"
import database from "../services/database.service"
import GeoJSONUtils from "../utils/geojsonUtils"
import please_dont_crash from "../utils/please_dont_crash"
Expand Down Expand Up @@ -199,15 +199,16 @@ export class PoiRoute {

const enrichedPoint = (await POIService.enrichPOIPosition(geopos, track)) ?? undefined

const updatedPOI: POI | null = await database.pois.update(
poiId,
userData.name,
userData.description,
userData.typeId,
userData.trackId,
enrichedPoint,
userData.isTurningPoint
)
// Note: geopos is from type GeoJSON.Feature and can't be parsed directly into Prisma.InputJsonValue
// Therefore we cast it into unknown first.
const updatedPOI: POI | null = await database.pois.update(userData.id!, {
name: userData.name,
description: userData.description,
position: enrichedPoint as unknown as Prisma.InputJsonValue,
isTurningPoint: userData.isTurningPoint,
typeId: userData.typeId,
trackId: track!.uid
})

if (!updatedPOI) {
logger.error(`Could not update poi with id ${userData.id}`)
Expand Down
8 changes: 6 additions & 2 deletions Server/src/routes/poitype.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class PoiTypeRoute {
private async createType(req: Request, res: Response): Promise<void> {
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, description })
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use the casted request body and pass it directly to the saveType function?
Maybe something like this?

const poiTypeInput: CreatePOIType = req.body
const poiType: POIType | null = await database.pois.saveType(poiTypeInput)

Or do we have to use the Prisma.POITypeCreateInput type and this is a magical case where both types line up?

Copy link
Contributor Author

@Niatsuna Niatsuna Sep 4, 2023

Choose a reason for hiding this comment

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

I just double checked this: It is indeed possible!

In my first iteration for this branch I just changed the database calls in the routes and services so that the methods are still working like intended but we can change it here to parse the poiTypeInput directly

if (!poiType) {
logger.error("Could not create poi type")
res.sendStatus(500)
Expand Down Expand Up @@ -116,7 +116,11 @@ export class PoiTypeRoute {
return
}

type = await database.pois.updateType(typeId, userData.name, userData.icon, userData.description)
type = await database.pois.updateType(typeId, {
name: userData.name,
icon: userData.icon,
description: userData.description
})
if (!type) {
logger.error(`Could not update poi type with id ${userData.id}`)
res.sendStatus(500)
Expand Down
13 changes: 10 additions & 3 deletions Server/src/routes/tracker.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { authenticateJWT, jsonParser } from "."
import TrackerService from "../services/tracker.service"
import { UplinkTracker } from "../models/api.tracker"
import please_dont_crash from "../utils/please_dont_crash"
import { Tracker, Vehicle } from "@prisma/client"
import { Prisma, Tracker, Vehicle } from "@prisma/client"
import VehicleService from "../services/vehicle.service"
import database from "../services/database.service"
import { Tracker as APITracker } from "../models/api"
Expand Down Expand Up @@ -83,7 +83,11 @@ export class TrackerRoute {
private async createTracker(req: Request, res: Response): Promise<void> {
const apiTracker: APITracker = req.body

const tracker: Tracker | null = await database.trackers.save(apiTracker.id, apiTracker.vehicleId, apiTracker.data)
const tracker: Tracker | null = await database.trackers.save({
uid: apiTracker.id,
vehicleId: apiTracker.vehicleId,
data: apiTracker.data as Prisma.InputJsonValue
})
if (!tracker) {
logger.error("Could not create tracker")
res.sendStatus(500)
Expand Down Expand Up @@ -115,7 +119,10 @@ export class TrackerRoute {
return
}

tracker = await database.trackers.update(trackerId, userData.vehicleId, userData.data)
tracker = await database.trackers.update(trackerId, {
vehicleId: userData.vehicleId,
data: userData.data as Prisma.InputJsonValue
})
if (!tracker) {
logger.error(`Could not update tracker with id ${userData.id}`)
res.sendStatus(500)
Expand Down
18 changes: 13 additions & 5 deletions Server/src/routes/vehicle.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,19 @@ export class VehicleRoute {
trackers.push(maybeTracker)
}

const vehicle: Vehicle | null = await database.vehicles.save(type.uid, userData.track, userData.name)
const vehicle: Vehicle | null = await database.vehicles.save({
name: userData.name,
typeId: type.uid,
trackId: userData.track
})
if (!vehicle) {
logger.error(`Could not create vehicle`)
res.sendStatus(500)
return
}

for (const tracker of trackers) {
const updatedTracker = await database.trackers.update(tracker.uid, vehicle.uid)
const updatedTracker = await database.trackers.update(tracker.uid, { vehicleId: vehicle.uid })
if (!updatedTracker) {
logger.error(`Could not attach tracker to created vehicle.`)
res.sendStatus(500)
Expand Down Expand Up @@ -216,19 +220,23 @@ export class VehicleRoute {
trackers.push(tracker)
}

const vehicle = await database.vehicles.update(vehicleId, type.uid, userData.track, userData.name)
const vehicle = await database.vehicles.update(vehicleId, {
typeId: type.uid,
trackId: userData.track,
name: userData.name
})
if (!vehicle) {
logger.error(`Could not update vehicle with id ${vehicleId}`)
res.sendStatus(500)
return
}

for (const tracker of prevTrackers) {
database.trackers.update(tracker.uid, null)
database.trackers.update(tracker.uid, { vehicleId: null })
}

for (const tracker of trackers) {
const trackerToUpdate: Tracker | null = await database.trackers.update(tracker.uid, vehicleId)
const trackerToUpdate: Tracker | null = await database.trackers.update(tracker.uid, { vehicleId: vehicleId })
if (!trackerToUpdate) {
logger.error(`Could not set tracker with tracker-id ${tracker}`)
res.sendStatus(500)
Expand Down
16 changes: 10 additions & 6 deletions Server/src/routes/vehicletype.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ export class VehicleTypeRoute {

// TODO: input validation

const vehicleType: VehicleType | null = await database.vehicles.saveType(
userData.name,
userData.icon,
userData.description
)
const vehicleType: VehicleType | null = await database.vehicles.saveType({
name: userData.name,
icon: userData.icon,
description: userData.description
})
if (!vehicleType) {
// TODO: differentiate different error causes:
// Constraint violation => 409
Expand Down Expand Up @@ -195,7 +195,11 @@ export class VehicleTypeRoute {
// type = await VehicleService.renameVehicleType(type, userData.name) // TODO: What about the description?!

// update all properties atomically, by directly talking to the database controller
type = await database.vehicles.updateType(type.uid, userData.name, userData.icon, userData.description)
type = await database.vehicles.updateType(type.uid, {
name: userData.name,
icon: userData.icon,
description: userData.description
})

if (!type) {
// TODO: differentiate different error causes:
Expand Down
6 changes: 4 additions & 2 deletions Server/src/services/db/log.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,23 @@ export default class LogController {
* Removes a log from the database.
*
* @param uid - Indicator which log should be removed
* @returns True if the removal was successful. Otherwise throws an Error.
*/
public async remove(uid: number): Promise<void> {
public async remove(uid: number): Promise<boolean> {
await this.prisma.log.delete({
where: {
uid: uid
}
})
return true
}

/**
* Return a list of all logs. (Sorted: Descending by timestamp)
* If a trackerId is given the list will be filtered for this specific tracker.
* If a vehicleId is given the list will be filtered for this specific vehicle.
*
* @param limit - Number of entries this method should deliver. Default is all (undefined)
* @param limit - Number of entries this method should deliver. Default is all (undefined).
* @param vehicleId - Vehicle to filter for (Optional)
* @param trackerId - Tracker to filter for (Optional)
* @returns Log[] - List of all logs
Expand Down
Loading
Loading