From 8644a738dceb71a834ea84dafa73f560ca527a25 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 17:09:06 +0200 Subject: [PATCH 01/19] Cascading Deletion --- Server/prisma/schema.prisma | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Server/prisma/schema.prisma b/Server/prisma/schema.prisma index 71fa6014..1a3b6837 100644 --- a/Server/prisma/schema.prisma +++ b/Server/prisma/schema.prisma @@ -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 { @@ -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[] From 7c2c16b744948bfdf136a4d8eba9d24e1687a249 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 17:35:44 +0200 Subject: [PATCH 02/19] Remove unnecessary import --- Server/src/services/database.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/Server/src/services/database.service.ts b/Server/src/services/database.service.ts index 8b2b4426..6771b962 100644 --- a/Server/src/services/database.service.ts +++ b/Server/src/services/database.service.ts @@ -1,4 +1,3 @@ -import { config } from '../config'; import { PrismaClient } from '@prisma/client' import UserController from './db/user.controller'; import POIController from './db/poi.controller'; From 8dca9d22d32a1bc9e53022f748dcd29600b81c9c Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:45:26 +0200 Subject: [PATCH 03/19] Adjust LogController --- Server/src/services/db/log.controller.ts | 164 +++++++++-------------- 1 file changed, 63 insertions(+), 101 deletions(-) diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index e271045d..0ceafd24 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -1,5 +1,4 @@ import { Log, PrismaClient, Prisma } from "@prisma/client"; -import { logger } from "../../utils/logger"; /** * LogController class @@ -23,6 +22,8 @@ export default class LogController { /** * Saves a new log in the database. * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: * @param timestamp - Time of log. * @param vehicleId - Vehicle.uid which is associated with this log. * @param position - Current GPS position at the time of the creation of the log. @@ -33,32 +34,20 @@ export default class LogController { * @param data - optional addtional data field. * @returns Log | null if an error occurs. */ - public async save(timestamp: Date, vehicleId: number, position: JSON, heading: number, speed: number, battery?: number, data?: JSON, trackerId?: string): Promise { - try { - // Note: Prisma converts JSON into JSONValues for more functionality. - // Either JSON.parse(JSON.stringify(position)) as Prisma.InputJsonValue or position as unknown as Prisma.InputJsonValue is the solution. - return await this.prisma.log.create({ - data: { - timestamp: timestamp, - trackerId: trackerId, - position: (position as unknown as Prisma.InputJsonValue), - heading: heading, - speed: speed, - battery: battery, - vehicleId: vehicleId, - data: (data as unknown as Prisma.InputJsonValue) - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async save(args : Prisma.LogUncheckedCreateInput): Promise { + //LogUncheckCreateInput is used because of required relations with other models! + return await this.prisma.log.create({ + data: args + }) } /** * Updates a Log entry. - * + * * @param uid - Indicator for specific log. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: * @param timestamp - Time when the log was created. * @param position - gps position of the tracker/app. * @param heading - degree in which the tracker/app was pointing. @@ -69,49 +58,26 @@ export default class LogController { * @param trackerId - identifier for said tracker. For app data this field is always `null` * @returns Log | null if an error occurs. */ - public async update(uid: number, timestamp?: Date, position?: JSON, heading?: number, speed?: number, battery?: number, data?: JSON, vehicleId?: number, trackerId?: string): Promise { - try { - // Note: Prisma converts JSON into JSONValues for more functionality. - // Either JSON.parse(JSON.stringify(position)) as Prisma.InputJsonValue or position as unknown as Prisma.InputJsonValue is the solution. - return await this.prisma.log.update({ - where: { - uid: uid - }, - data: { - timestamp: timestamp, - position: (position as unknown as Prisma.InputJsonValue), - heading: heading, - speed: speed, - battery: battery, - data: (data as unknown as Prisma.InputJsonValue), - vehicleId: vehicleId, - trackerId: trackerId - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async update(uid: number, args : Prisma.LogUpdateInput): Promise { + return await this.prisma.log.update({ + where: { + uid: uid + }, + data: args + }) } /** * Removes a log from the database. * * @param uid - Indicator which log should be removed - * @returns True | False depending on if the log could be removed. */ - public async remove(uid: number): Promise { - try { - await this.prisma.log.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } + public async remove(uid: number): Promise { + await this.prisma.log.delete({ + where: { + uid: uid + } + }) } /** @@ -124,67 +90,63 @@ export default class LogController { * @returns Log[] - List of all logs */ public async getAll(vehicleId?: number, trackerId?: string): Promise { - try { - return await this.prisma.log.findMany({ - where: { - vehicleId: vehicleId, - trackerId: trackerId - }, - orderBy: [ - { - timestamp: 'desc' - } - ] - }) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + trackerId: trackerId + }, + orderBy: [ + { + timestamp: 'desc' + } + ] + }) } /** * Looks up a specific log in the database. * * @param uid - Indicator for log - * + * * @returns Log | null depending on if the log could be found. */ public async getLog(uid: number): Promise { - try { - return await this.prisma.log.findUnique({ - where: { - uid: uid, - }, - include: { - vehicle: true, - tracker: true - }, - }) - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.log.findUnique({ + where: { + uid: uid, + }, + include: { + vehicle: true, + tracker: true + }, + }) } /** * Returns a list of the newest logs for an vehicle. - * - * + * + * * @param vehicleId - Indicator which vehicle's logs should be considered. * @param max_sec - How old the logs can be at max. Default: 5 min - * + * * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. */ public async getNewestLogs(vehicleId: number, max_sec: number = 300): Promise { - let logs = await this.getAll(vehicleId = vehicleId) - let max_date = Date.now() - (max_sec * 1000) + // Earliest date which should be considered + let max_date = new Date(Date.now() - (max_sec * 1000)) - // Because the logs are sorted by timestamps in descending order we just need to find - // the log with an timestamp older then our max_date and don't need to bother with the rest of it - let i = 0 - while (new Date(logs[i].timestamp).getTime() >= max_date) { - i += 1 - } - return logs.slice(0, i + 1) + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + timestamp: { + gt: max_date + } + }, + orderBy: [ + { + timestamp: 'desc' + } + ] + }) } } \ No newline at end of file From be49322f3490e4f4803b7b163937961c7033df9d Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:45:35 +0200 Subject: [PATCH 04/19] Adjust POIController --- Server/src/services/db/poi.controller.ts | 237 ++++++++--------------- 1 file changed, 81 insertions(+), 156 deletions(-) diff --git a/Server/src/services/db/poi.controller.ts b/Server/src/services/db/poi.controller.ts index e9614d1a..e6c768fc 100644 --- a/Server/src/services/db/poi.controller.ts +++ b/Server/src/services/db/poi.controller.ts @@ -1,6 +1,5 @@ -import { PrismaClient, Prisma } from '.prisma/client'; -import type { POI, POIType } from '.prisma/client'; -import { logger } from '../../utils/logger'; +import { PrismaClient, Prisma } from '@prisma/client'; +import type { POI, POIType } from '@prisma/client'; /** * POIController class @@ -34,70 +33,51 @@ export default class POIController { /** * Saves a type for POIs in the database. * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: * @param name - **unique** name of the type of poi. * @param icon - unique icon name for visualization * @param description - an optional description for the type of poi. - * @returns POIType | null if an error occurs. + * @returns POIType */ - public async saveType(name: string, icon: string, description?: string): Promise { - try { - return await this.prisma.pOIType.create({ - data : { - name: name, - icon: icon, - description: description - } - }) - } catch(e) { - logger.debug(e) - return null - } + public async saveType(args : Prisma.POITypeCreateInput): Promise { + return await this.prisma.pOIType.create({ + data : args + }) } /** * Updates a type of poi in the database. * * @param uid - Indicator which type should be updated. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: * @param name - New name after change. (Optional) * @param icon - New unique icon name for visualization after change. (Optional) * @param description - New description after change. (Optional) * @returns POIType | null if an error occurs. */ - public async updateType(uid: number, name?: string, icon?: string, description?: string): Promise { - try { - return await this.prisma.pOIType.update({ - where: { - uid: uid - }, - data : { - name: name, - description: description - } - }) - } catch(e) { - logger.debug(e) - return null - } + public async updateType(uid: number, args : Prisma.POITypeUpdateInput): Promise { + return await this.prisma.pOIType.update({ + where: { + uid: uid + }, + data : args + }) } /** * Removes a poi type from the database. * * @param uid - Indicator which type should be removed. - * @returns True | False depending on if the type was removed or not. */ - public async removeType(uid: number): Promise { - try { - await this.prisma.pOIType.delete({ - where: { - uid: uid - } - }); - return true - } catch(e) { - logger.debug(e) - return false - } + public async removeType(uid: number): Promise { + await this.prisma.pOIType.delete({ + where: { + uid: uid + } + }) } /** @@ -106,12 +86,7 @@ export default class POIController { * @returns `POIType[]` - List of all types of poi. */ public async getAllTypes(): Promise { - try { - return await this.prisma.pOIType.findMany({}); - } catch(e) { - logger.debug(e) - return [] - } + return await this.prisma.pOIType.findMany({}); } /** @@ -121,16 +96,11 @@ export default class POIController { * @returns POIType | null depending on if the type could be found. */ public async getTypeById(uid: number): Promise { - try { - return await this.prisma.pOIType.findUnique({ - where: { - uid: uid - } - }); - } catch(e) { - logger.debug(e) - return null - } + return await this.prisma.pOIType.findUnique({ + where: { + uid: uid + } + }) } /** @@ -140,16 +110,11 @@ export default class POIController { * @returns POIType | null depending on if the type could be found. */ public async getTypeByName(name: string): Promise { - try { - return await this.prisma.pOIType.findUnique({ - where: { - name: name - } - }); - } catch(e) { - logger.debug(e) - return null - } + return await this.prisma.pOIType.findUnique({ + where: { + name: name + } + }) } // ========================================================= // @@ -158,83 +123,58 @@ export default class POIController { /** * Saves a point of interest (POI) in the database. * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: * @param name - **unique** name of the POI * @param typeId - POIType Identifier: Maps a POIType to said POI in the database * @param trackId - Track Identifier : Maps a Track to said POI in the database * @param position - Coordinates to pinpoint the location of said POI. * @param description - optional description of said POI * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI - * @returns POI | null if an error occurs. + * @returns POI */ - public async save(name: string, typeId: number, trackId: number, position: JSON, description?: string, isTurningPoint : boolean = false): Promise { - try { - return await this.prisma.pOI.create({ - data: { - name: name, - description: description, - typeId: typeId, - trackId: trackId, - position: (position as unknown as Prisma.InputJsonValue), - isTurningPoint: isTurningPoint - } - }) - } catch(e) { - logger.debug(e) - return null - } + public async save(args : Prisma.POIUncheckedCreateInput): Promise { + // POIUncheckCreateInput is used because of required relations based on the model! + return await this.prisma.pOI.create({ + data: args + }) } /** * Updates a POI in the database. * * @param uid - Indicator which poi should be updated. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: * @param name - New name after change. (Optional) * @param description - New description after change. (Optional) * @param typeId - New typeId after change. (Optional) * @param trackId - New trackId after change. (Optional) * @param position - New position after change. (Optional) * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) - * @returns POI | null if an error occurs. + * @returns POI */ - public async update(uid: number, name?: string, description?: string, typeId?: number, trackId?: number, position?: JSON, isTurningPoint?: boolean): Promise { - try { - return await this.prisma.pOI.update({ - where: { - uid: uid - }, - data: { - name: name, - description: description, - typeId: typeId, - trackId: trackId, - position: (position as unknown as Prisma.InputJsonValue), - isTurningPoint: isTurningPoint - } - }) - } catch(e) { - logger.debug(e) - return null - } + public async update(uid: number, args : Prisma.POIUpdateInput): Promise { + return await this.prisma.pOI.update({ + where: { + uid: uid + }, + data: args + }) } /** * Removes an poi from the database. * * @param uid - Indicator which poi should be removed. - * @returns True | False depending on if the user was removed or not. */ - public async remove(uid: number): Promise { - try { - await this.prisma.pOI.delete({ - where: { - uid: uid - } - }) - return true - } catch(e) { - logger.debug(e) - return false - } + public async remove(uid: number): Promise { + await this.prisma.pOI.delete({ + where: { + uid: uid + } + }) } /** @@ -244,16 +184,11 @@ export default class POIController { * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. */ public async getAll(trackId? : number): Promise { - try { - return await this.prisma.pOI.findMany({ - where: { - trackId: trackId - } - }) - } catch(e) { - logger.debug(e) - return [] - } + return await this.prisma.pOI.findMany({ + where: { + trackId: trackId + } + }) } /** @@ -263,20 +198,15 @@ export default class POIController { * @returns POI | null depending on if the poi could be found. */ public async getById(uid: number): Promise { - try { - return await this.prisma.pOI.findUnique({ - where: { - uid: uid - }, - include: { - type: true, - track: true - } - }) - } catch(e) { - logger.debug(e) - return null - } + return await this.prisma.pOI.findUnique({ + where: { + uid: uid + }, + include: { + type: true, + track: true + } + }) } /** @@ -287,16 +217,11 @@ export default class POIController { * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. */ public async getByName(name: string, trackId?: number): Promise { - try { - return await this.prisma.pOI.findMany({ - where: { - name: name, - trackId: trackId - }, - }); - } catch(e) { - logger.debug(e) - return [] - } + return await this.prisma.pOI.findMany({ + where: { + name: name, + trackId: trackId + }, + }) } } \ No newline at end of file From 74b5f2171a9ee8eeb3ec32259412eae3c7ce022c Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:45:43 +0200 Subject: [PATCH 05/19] Adjust TrackController --- Server/src/services/db/track.controller.ts | 137 ++++++++------------- 1 file changed, 51 insertions(+), 86 deletions(-) diff --git a/Server/src/services/db/track.controller.ts b/Server/src/services/db/track.controller.ts index ce733064..b8c92157 100644 --- a/Server/src/services/db/track.controller.ts +++ b/Server/src/services/db/track.controller.ts @@ -1,6 +1,5 @@ import { PrismaClient, Prisma } from "@prisma/client"; import type { Track } from '@prisma/client'; -import { logger } from "../../utils/logger"; /** * TrackController class @@ -21,71 +20,52 @@ export default class TrackController { /** * Saves a tracker in the database. * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: * @param start - Name of the start location. * @param stop - Name of the end location. * @param data - JSON Data of the track - * @returns Track | null if an error occurs + * @returns Track */ - public async save(start: string, stop: string, data: JSON): Promise { - try { - return await this.prisma.track.create({ - data: { - start: start, - stop: stop, - data: (data as unknown as Prisma.InputJsonValue) - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async save(args : Prisma.TrackCreateInput): Promise { + return await this.prisma.track.create({ + data: args + } + ) } /** * Updates a track in the database. * * @param uid - Indicator which track should be updated + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: * @param start - New name of the start location after change (Optional) * @param stop - New name of the end location after change (Optional) * @param data - New JSON Data of the track after change (Optional) - * @returns Track | null if an error occurs + * @returns Track */ - public async update(uid: number, start?: string, stop?: string, data?: JSON): Promise { - try { - return await this.prisma.track.update({ - where: { - uid: uid - }, - data: { - start: start, - stop: stop, - data: (data as unknown as Prisma.InputJsonValue) - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async update(uid : number, args : Prisma.TrackUpdateInput): Promise { + return await this.prisma.track.update({ + where : { + uid : uid + }, + data : args + }) } /** * Removes a track in the database. * * @param uid - Indicator which track should be removed. - * @returns True | False depending on if the track was removed or not. */ - public async remove(uid: number): Promise { - try { - await this.prisma.track.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } + public async remove(uid: number): Promise { + await this.prisma.track.delete({ + where: { + uid: uid + } + }) } /** @@ -94,12 +74,7 @@ export default class TrackController { * @returns Track[] - List of all tracks. */ public async getAll(): Promise { - try { - return await this.prisma.track.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.track.findMany({}) } /** @@ -109,20 +84,15 @@ export default class TrackController { * @returns Track | null depending on if the track could be found. */ public async getById(uid: number): Promise { - try { - return await this.prisma.track.findUnique({ - where: { - uid: uid - }, - include: { - poi: true, - vehicle: true - } - }) - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.track.findUnique({ + where: { + uid: uid + }, + include: { + poi: true, + vehicle: true + } + }) } /** @@ -132,26 +102,21 @@ export default class TrackController { * @returns Track[] - List of tracks that have either start and/or stop at the given location. */ public async getByLocation(location: string): Promise { - try { - return await this.prisma.track.findMany({ - where: { - OR: [ - { - start: location - }, - { - stop: location - } - ], - }, - include: { - poi: true, - vehicle: true - } - }) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.track.findMany({ + where: { + OR: [ + { + start: location + }, + { + stop: location + } + ], + }, + include: { + poi: true, + vehicle: true + } + }) } } \ No newline at end of file From 0d03fe2dbd3947dac7d9640fca33cf6c57ece24d Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:45:52 +0200 Subject: [PATCH 06/19] Adjust TrackerController --- Server/src/services/db/tracker.controller.ts | 105 +++++++------------ 1 file changed, 36 insertions(+), 69 deletions(-) diff --git a/Server/src/services/db/tracker.controller.ts b/Server/src/services/db/tracker.controller.ts index 317afe1e..5fb7ecf9 100644 --- a/Server/src/services/db/tracker.controller.ts +++ b/Server/src/services/db/tracker.controller.ts @@ -1,5 +1,4 @@ import { Prisma, PrismaClient, Tracker } from "@prisma/client"; -import { logger } from "../../utils/logger"; /** * TrackerController class @@ -21,49 +20,38 @@ export default class TrackerController { /** * Saves a new tracker in the database. * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: * @param uid - ID (EUI) of the tracker. * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. * @param data - optional additional data field. - * @returns Tracker | null if an error occurs + * @returns Tracker */ - public async save(uid: string, vehicleId?: number, data?: JSON): Promise { - try { - return await this.prisma.tracker.create({ - data: { - uid: uid, - vehicleId: vehicleId, - data: (data as unknown as Prisma.InputJsonValue) - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async save(args : Prisma.TrackerCreateInput): Promise { + return await this.prisma.tracker.create({ + data: args + }) } /** * Updates a tracker in the database. * * @param uid - Indicator which tracker should be updated + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: * @param vehicleId - New vehicleId (Optional) * @param data - New additional data field (Optional) - * @returns Tracker | null if an error occurs + * @returns Tracker */ - public async update(uid: string, vehicleId?: number, data?: JSON): Promise { - try { - return await this.prisma.tracker.update({ - where: { - uid: uid - }, - data: { - vehicleId: vehicleId, - data: (data as unknown as Prisma.InputJsonValue) - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async update(uid : string, args : Prisma.TrackerUncheckedUpdateInput): Promise { + //TrackerUncheckedUpdateInput is used + return await this.prisma.tracker.update({ + where: { + uid: uid + }, + data: args + }) } /** @@ -72,18 +60,12 @@ export default class TrackerController { * @param uid - Indicator which tracker should be removed. * @returns True | False depending on if the tracker was removed or not. */ - public async remove(uid: string): Promise { - try { - await this.prisma.tracker.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } + public async remove(uid: string): Promise { + await this.prisma.tracker.delete({ + where: { + uid: uid + } + }) } /** @@ -92,12 +74,7 @@ export default class TrackerController { * @returns Tracker[] - List of all trackers. */ public async getAll(): Promise { - try { - return await this.prisma.tracker.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.tracker.findMany({}) } /** @@ -107,16 +84,11 @@ export default class TrackerController { * @returns Tracker | null depending on if the tracker could be found. */ public async getById(uid: string): Promise { - try { - return await this.prisma.tracker.findUnique({ - where: { - uid: uid - } - }) - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.tracker.findUnique({ + where: { + uid: uid + } + }) } /** @@ -126,15 +98,10 @@ export default class TrackerController { * @returns List of trackers assigned to the vehicle. */ public async getByVehicleId(vehicleId: number): Promise { - try { - return await this.prisma.tracker.findMany({ - where: { - vehicleId: vehicleId - } - }) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.tracker.findMany({ + where: { + vehicleId: vehicleId + } + }) } } \ No newline at end of file From f0d3f2bf9601a9a5c1eb62a2862e2dac68fdda53 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:45:59 +0200 Subject: [PATCH 07/19] Adjust UserController --- Server/src/services/db/user.controller.ts | 89 ++++++++--------------- 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/Server/src/services/db/user.controller.ts b/Server/src/services/db/user.controller.ts index d160b604..1b90e65e 100644 --- a/Server/src/services/db/user.controller.ts +++ b/Server/src/services/db/user.controller.ts @@ -1,6 +1,5 @@ import { PrismaClient, Prisma } from '@prisma/client'; import type { User } from '@prisma/client'; -import { logger } from '../../utils/logger'; /** * UserController class @@ -22,67 +21,49 @@ export default class UserController { /** * Saves an user in the database. * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: * @param username - **unique** name of the user. * @param password - **hashed** password. - * @returns User | null if an error occurs. + * @returns User */ - public async save(username: string, password: string): Promise { - try { - return await this.prisma.user.create({ - data: { - username: username, - password: password - } - }); - } catch (e) { - logger.debug(e) - return null - } + public async save(args: Prisma.UserCreateInput): Promise { + return await this.prisma.user.create({ + data: args + }); } /** * Updates an user in the database. * * @param name - Old username before change. Indicator which user should be updated + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: * @param username - New username after change. (Optional) * @param password - New password after change. (Optional) - * @returns User | null if an error occurs. + * @returns User */ - public async update(name: string, username?: string, password?: string): Promise { - try { - return await this.prisma.user.update({ - where: { - username: name - }, - data: { - username: username, - password: password - } - }); - } catch (e) { - logger.debug(e) - return null - } + public async update(name: string, args: Prisma.UserUpdateInput): Promise { + return await this.prisma.user.update({ + where: { + username: name + }, + data: args + }); } /** * Removes an user from the database. * * @param username - Indicator which user should be removed - * @returns True | False depending on if the user was removed or not. */ - public async remove(username: string): Promise { - try { - await this.prisma.user.delete({ - where: { - username: username - } - }); - return true - } catch (e) { - logger.debug(e) - return false - } + public async remove(username: string): Promise { + await this.prisma.user.delete({ + where: { + username: username + } + }); } /** @@ -91,12 +72,7 @@ export default class UserController { * @returns `User[]` - List of all users. */ public async getAll(): Promise { - try { - return await this.prisma.user.findMany({}); - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.user.findMany({}); } /** @@ -106,15 +82,10 @@ export default class UserController { * @returns User | null depending on if the user could be found. */ public async getByUsername(username: string): Promise { - try { - return await this.prisma.user.findUnique({ - where: { - username: username - } - }); - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.user.findUnique({ + where: { + username: username + } + }); } } \ No newline at end of file From 635a330402bdce130fefc9ba708a1bb82b09514c Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:46:07 +0200 Subject: [PATCH 08/19] Adjust VehicleController --- Server/src/services/db/vehicle.controller.ts | 242 +++++++------------ 1 file changed, 87 insertions(+), 155 deletions(-) diff --git a/Server/src/services/db/vehicle.controller.ts b/Server/src/services/db/vehicle.controller.ts index 57c0b9fe..922504fb 100644 --- a/Server/src/services/db/vehicle.controller.ts +++ b/Server/src/services/db/vehicle.controller.ts @@ -1,6 +1,5 @@ import { PrismaClient, Prisma } from "@prisma/client"; import type { Vehicle, VehicleType } from '@prisma/client'; -import { logger } from "../../utils/logger"; /** * VehicleController class @@ -32,50 +31,37 @@ export default class VehicleController { /** * Saves a vehicle type in the database. * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: * @param name - **unique** Name for the type of vehicle. * @param icon - unique icon name for visualization * @param description - optional description for the type. * @returns VehicleType | null if an error occurs */ - public async saveType(name: string, icon: string, description?: string): Promise { - try { - return await this.prisma.vehicleType.create({ - data: { - name: name, - icon: icon, - description: description - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async saveType(args : Prisma.VehicleTypeCreateInput): Promise { + return await this.prisma.vehicleType.create({ + data: args + }) } /** * Updates a vehicle type in the database. * * @param uid - Indicator which vehicle type should be updated. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: * @param name - New name of the vehicle type after change. (Optional) * @param description - New description of the vehicle type after change. (Optional) * @returns */ - public async updateType(uid: number, name?: string, icon?: string, description?: string): Promise { - try { - return await this.prisma.vehicleType.update({ - where: { - uid: uid - }, - data: { - name: name, - icon: icon, - description: description - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async updateType(uid: number , args : Prisma.VehicleTypeUpdateInput): Promise { + return await this.prisma.vehicleType.update({ + where: { + uid: uid + }, + data: args + }) } /** @@ -84,18 +70,12 @@ export default class VehicleController { * @param uid - Indicator which vehicle type should be removed. * @returns True | False depending on if the track was removed or not. */ - public async removeType(uid: number): Promise { - try { - await this.prisma.vehicleType.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } + public async removeType(uid: number): Promise { + await this.prisma.vehicleType.delete({ + where: { + uid: uid + } + }) } /** @@ -104,12 +84,7 @@ export default class VehicleController { * @returns VehicleType[] - List of all vehicle types. */ public async getAllTypes(): Promise { - try { - return await this.prisma.vehicleType.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.vehicleType.findMany({}) } /** @@ -119,16 +94,11 @@ export default class VehicleController { * @returns VehicleType | null depending on if the vehicle type could be found. */ public async getTypeById(uid: number): Promise { - try { - return await this.prisma.vehicleType.findUnique({ - where: { - uid: uid - } - }) - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.vehicleType.findUnique({ + where: { + uid: uid + } + }) } /** @@ -138,16 +108,11 @@ export default class VehicleController { * @returns VehicleType | null depending on if the vehicle type could be found. */ public async getTypeByName(name: string): Promise { - try { - return await this.prisma.vehicleType.findUnique({ - where: { - name: name - } - }) - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.vehicleType.findUnique({ + where: { + name: name + } + }) } // ========================================================= // @@ -156,95 +121,72 @@ export default class VehicleController { /** * Saves a new vehicle in the database. * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: * @param typeId - VehicleType uid * @param trackId - Track uid * @param name - display name for the given vehicle (Optional) * @returns Vehicle | null if an error occurs. */ - public async save(typeId: number, trackId: number, name: string): Promise { - try { - return await this.prisma.vehicle.create({ - data: { - name: name, - typeId: typeId, - trackId: trackId - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async save(args: Prisma.VehicleUncheckedCreateInput): Promise { + // VehicleUncheckedCreateInput is used because of required relations + return await this.prisma.vehicle.create({ + data: args + }) } /** * Updadtes a vehicle in the database. * * @param uid - Indicator which vehicle should be updated + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: * @param typeId - New VehicleType.uid after change (Optional) * @param trackId - New Track.uid after change (Optional) * @param name - New display name after change (Optional) * @returns Vehicle | null if an error occurs */ - public async update(uid: number, typeId?: number, trackId?: number, name?: string): Promise { - try { - return await this.prisma.vehicle.update({ - where: { - uid: uid - }, - data: { - name: name, - typeId: typeId, - trackId: trackId - } - }) - } catch (e) { - logger.debug(e) - return null - } + public async update(uid: number, args: Prisma.VehicleUncheckedUpdateInput): Promise { + // VehicleUncheckCreateInput is used because of required relations + return await this.prisma.vehicle.update({ + where: { + uid: uid + }, + data: args + }) } /** * Removes a vehicle in the database. * * @param uid - Indicator which vehicle should be removed. - * @returns True | False depending on if the vehicle was removed or not. */ - public async remove(uid: number): Promise { - try { - await this.prisma.vehicle.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } + public async remove(uid: number): Promise { + await this.prisma.vehicle.delete({ + where: { + uid: uid + } + }) } /** * Returns a list of all vehicles. - * + * * @param trackId - Track.uid for filtering list (Optional) * * @returns Vehicle[] */ public async getAll(trackId?: number): Promise { - try { - return await this.prisma.vehicle.findMany({ - where: { - trackId: trackId - }, - include: { - type: true, - track: true - } - }) - } catch (e) { - logger.debug(e) - return [] - } + return await this.prisma.vehicle.findMany({ + where: { + trackId: trackId + }, + include: { + type: true, + track: true + } + }) } /** @@ -254,20 +196,15 @@ export default class VehicleController { * @returns Vehicle | null depending on if the vehicle could be found. */ public async getById(uid: number): Promise { - try { - return await this.prisma.vehicle.findUnique({ - where: { - uid: uid - }, - include: { - type: true, - track: true - } - }) - } catch (e) { - logger.debug(e) - return null - } + return await this.prisma.vehicle.findUnique({ + where: { + uid: uid + }, + include: { + type: true, + track: true + } + }) } /** @@ -277,22 +214,17 @@ export default class VehicleController { * @returns Vehicle | null depending on if the vehicle could be found. */ public async getByName(name: string, trackId: number): Promise { - try { - return await this.prisma.vehicle.findUnique({ - where: { - name_trackId: { - name: name, - trackId: trackId - } - }, - include: { - type: true, - track: true + return await this.prisma.vehicle.findUnique({ + where: { + name_trackId: { + name: name, + trackId: trackId } - }) - } catch (e) { - logger.debug(e) - return null - } + }, + include: { + type: true, + track: true + } + }) } } \ No newline at end of file From 059e43299c682617cf01315762a433ec7112954f Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Tue, 15 Aug 2023 18:48:19 +0200 Subject: [PATCH 09/19] Removed unnecessary imports & ensured formatting --- Server/src/services/database.service.ts | 34 +- Server/src/services/db/log.controller.ts | 263 ++++++------ Server/src/services/db/poi.controller.ts | 407 +++++++++--------- Server/src/services/db/track.controller.ts | 204 ++++----- Server/src/services/db/tracker.controller.ts | 173 ++++---- Server/src/services/db/user.controller.ts | 145 ++++--- Server/src/services/db/vehicle.controller.ts | 427 ++++++++++--------- 7 files changed, 834 insertions(+), 819 deletions(-) diff --git a/Server/src/services/database.service.ts b/Server/src/services/database.service.ts index 6771b962..709edb7c 100644 --- a/Server/src/services/database.service.ts +++ b/Server/src/services/database.service.ts @@ -1,10 +1,10 @@ -import { PrismaClient } from '@prisma/client' -import UserController from './db/user.controller'; -import POIController from './db/poi.controller'; -import TrackController from './db/track.controller'; -import VehicleController from './db/vehicle.controller'; -import TrackerController from './db/tracker.controller'; -import LogController from './db/log.controller'; +import { PrismaClient } from "@prisma/client"; +import UserController from "./db/user.controller"; +import POIController from "./db/poi.controller"; +import TrackController from "./db/track.controller"; +import VehicleController from "./db/vehicle.controller"; +import TrackerController from "./db/tracker.controller"; +import LogController from "./db/log.controller"; /** * Database class @@ -14,17 +14,15 @@ import LogController from './db/log.controller'; * users, logs, vehicles, tracks, trackers & pois */ export class Database { + private prisma = new PrismaClient(); - private prisma = new PrismaClient(); - - public logs = new LogController(this.prisma); - public pois = new POIController(this.prisma); - public tracks = new TrackController(this.prisma); - public trackers = new TrackerController(this.prisma); - public users = new UserController(this.prisma); - public vehicles = new VehicleController(this.prisma); - + public logs = new LogController(this.prisma); + public pois = new POIController(this.prisma); + public tracks = new TrackController(this.prisma); + public trackers = new TrackerController(this.prisma); + public users = new UserController(this.prisma); + public vehicles = new VehicleController(this.prisma); } -const database : Database = new Database(); -export default database; \ No newline at end of file +const database: Database = new Database(); +export default database; diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index 0ceafd24..d62080f6 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -13,140 +13,145 @@ import { Log, PrismaClient, Prisma } from "@prisma/client"; * - getLog() */ export default class LogController { + constructor(private prisma: PrismaClient) {} - constructor(private prisma: PrismaClient) { } + // ========================================================= // + // [Tracker Logs] - // ========================================================= // - // [Tracker Logs] + /** + * Saves a new log in the database. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: + * @param timestamp - Time of log. + * @param vehicleId - Vehicle.uid which is associated with this log. + * @param position - Current GPS position at the time of the creation of the log. + * @param heading - Current GPS heading at the time of the creation of the log. + * @param speed - Current speed at the time of the creation of the log. + * @param battery - Current battery charge at the time of the creation of the log. (Optional for the case of app data) + * @param trackerId - Tracker.uid which caused the log. (Optional for the case of app data) + * @param data - optional addtional data field. + * @returns Log | null if an error occurs. + */ + public async save(args: Prisma.LogUncheckedCreateInput): Promise { + //LogUncheckCreateInput is used because of required relations with other models! + return await this.prisma.log.create({ + data: args, + }); + } - /** - * Saves a new log in the database. - * - * The parameter are given via object deconstruction from the model `Log`! - * Currently given parameters are: - * @param timestamp - Time of log. - * @param vehicleId - Vehicle.uid which is associated with this log. - * @param position - Current GPS position at the time of the creation of the log. - * @param heading - Current GPS heading at the time of the creation of the log. - * @param speed - Current speed at the time of the creation of the log. - * @param battery - Current battery charge at the time of the creation of the log. (Optional for the case of app data) - * @param trackerId - Tracker.uid which caused the log. (Optional for the case of app data) - * @param data - optional addtional data field. - * @returns Log | null if an error occurs. - */ - public async save(args : Prisma.LogUncheckedCreateInput): Promise { - //LogUncheckCreateInput is used because of required relations with other models! - return await this.prisma.log.create({ - data: args - }) - } + /** + * Updates a Log entry. + * + * @param uid - Indicator for specific log. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: + * @param timestamp - Time when the log was created. + * @param position - gps position of the tracker/app. + * @param heading - degree in which the tracker/app was pointing. + * @param speed - current speed of tracker/app. + * @param battery - current battery of tracker. + * @param data - GPS Data. + * @param vehicleId - which vehicle is connected with said tracker/app. For tracker this can be found in the tracker model. + * @param trackerId - identifier for said tracker. For app data this field is always `null` + * @returns Log | null if an error occurs. + */ + public async update( + uid: number, + args: Prisma.LogUpdateInput + ): Promise { + return await this.prisma.log.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Updates a Log entry. - * - * @param uid - Indicator for specific log. - * - * The parameter are given via object deconstruction from the model `Log`! - * Currently given parameters are: - * @param timestamp - Time when the log was created. - * @param position - gps position of the tracker/app. - * @param heading - degree in which the tracker/app was pointing. - * @param speed - current speed of tracker/app. - * @param battery - current battery of tracker. - * @param data - GPS Data. - * @param vehicleId - which vehicle is connected with said tracker/app. For tracker this can be found in the tracker model. - * @param trackerId - identifier for said tracker. For app data this field is always `null` - * @returns Log | null if an error occurs. - */ - public async update(uid: number, args : Prisma.LogUpdateInput): Promise { - return await this.prisma.log.update({ - where: { - uid: uid - }, - data: args - }) - } + /** + * Removes a log from the database. + * + * @param uid - Indicator which log should be removed + */ + public async remove(uid: number): Promise { + await this.prisma.log.delete({ + where: { + uid: uid, + }, + }); + } - /** - * Removes a log from the database. - * - * @param uid - Indicator which log should be removed - */ - public async remove(uid: number): Promise { - await this.prisma.log.delete({ - where: { - uid: uid - } - }) - } + /** + * 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 vehicleId - Vehicle to filter for (Optional) + * @param trackerId - Tracker to filter for (Optional) + * @returns Log[] - List of all logs + */ + public async getAll(vehicleId?: number, trackerId?: string): Promise { + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + trackerId: trackerId, + }, + orderBy: [ + { + timestamp: "desc", + }, + ], + }); + } - /** - * 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 vehicleId - Vehicle to filter for (Optional) - * @param trackerId - Tracker to filter for (Optional) - * @returns Log[] - List of all logs - */ - public async getAll(vehicleId?: number, trackerId?: string): Promise { - return await this.prisma.log.findMany({ - where: { - vehicleId: vehicleId, - trackerId: trackerId - }, - orderBy: [ - { - timestamp: 'desc' - } - ] - }) - } + /** + * Looks up a specific log in the database. + * + * @param uid - Indicator for log + * + * @returns Log | null depending on if the log could be found. + */ + public async getLog(uid: number): Promise { + return await this.prisma.log.findUnique({ + where: { + uid: uid, + }, + include: { + vehicle: true, + tracker: true, + }, + }); + } - /** - * Looks up a specific log in the database. - * - * @param uid - Indicator for log - * - * @returns Log | null depending on if the log could be found. - */ - public async getLog(uid: number): Promise { - return await this.prisma.log.findUnique({ - where: { - uid: uid, - }, - include: { - vehicle: true, - tracker: true - }, - }) - } + /** + * Returns a list of the newest logs for an vehicle. + * + * + * @param vehicleId - Indicator which vehicle's logs should be considered. + * @param max_sec - How old the logs can be at max. Default: 5 min + * + * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. + */ + public async getNewestLogs( + vehicleId: number, + max_sec: number = 300 + ): Promise { + // Earliest date which should be considered + let max_date = new Date(Date.now() - max_sec * 1000); - /** - * Returns a list of the newest logs for an vehicle. - * - * - * @param vehicleId - Indicator which vehicle's logs should be considered. - * @param max_sec - How old the logs can be at max. Default: 5 min - * - * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. - */ - public async getNewestLogs(vehicleId: number, max_sec: number = 300): Promise { - // Earliest date which should be considered - let max_date = new Date(Date.now() - (max_sec * 1000)) - - return await this.prisma.log.findMany({ - where: { - vehicleId: vehicleId, - timestamp: { - gt: max_date - } - }, - orderBy: [ - { - timestamp: 'desc' - } - ] - }) - } -} \ No newline at end of file + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + timestamp: { + gt: max_date, + }, + }, + orderBy: [ + { + timestamp: "desc", + }, + ], + }); + } +} diff --git a/Server/src/services/db/poi.controller.ts b/Server/src/services/db/poi.controller.ts index e6c768fc..78ac96ed 100644 --- a/Server/src/services/db/poi.controller.ts +++ b/Server/src/services/db/poi.controller.ts @@ -1,5 +1,4 @@ -import { PrismaClient, Prisma } from '@prisma/client'; -import type { POI, POIType } from '@prisma/client'; +import { POI, POIType, PrismaClient, Prisma } from "@prisma/client"; /** * POIController class @@ -24,204 +23,206 @@ import type { POI, POIType } from '@prisma/client'; * */ export default class POIController { - - constructor(private prisma: PrismaClient) {} - - // ========================================================= // - // [POI Types] - - /** - * Saves a type for POIs in the database. - * - * The parameter are given via object deconstruction from the model `POIType`! - * Currently given parameters are: - * @param name - **unique** name of the type of poi. - * @param icon - unique icon name for visualization - * @param description - an optional description for the type of poi. - * @returns POIType - */ - public async saveType(args : Prisma.POITypeCreateInput): Promise { - return await this.prisma.pOIType.create({ - data : args - }) - } - - /** - * Updates a type of poi in the database. - * - * @param uid - Indicator which type should be updated. - * - * The parameter are given via object deconstruction from the model `POIType`! - * Currently given parameters are: - * @param name - New name after change. (Optional) - * @param icon - New unique icon name for visualization after change. (Optional) - * @param description - New description after change. (Optional) - * @returns POIType | null if an error occurs. - */ - public async updateType(uid: number, args : Prisma.POITypeUpdateInput): Promise { - return await this.prisma.pOIType.update({ - where: { - uid: uid - }, - data : args - }) - } - - /** - * Removes a poi type from the database. - * - * @param uid - Indicator which type should be removed. - */ - public async removeType(uid: number): Promise { - await this.prisma.pOIType.delete({ - where: { - uid: uid - } - }) - } - - /** - * Returns a list of all existing types of poi. - * - * @returns `POIType[]` - List of all types of poi. - */ - public async getAllTypes(): Promise { - return await this.prisma.pOIType.findMany({}); - } - - /** - * Looks up a type given by its uid. - * - * @param uid - Indicator which type should be searched for. - * @returns POIType | null depending on if the type could be found. - */ - public async getTypeById(uid: number): Promise { - return await this.prisma.pOIType.findUnique({ - where: { - uid: uid - } - }) - } - - /** - * Looks up a type given by its name. - * - * @param name - Indicator which type should be searched for. - * @returns POIType | null depending on if the type could be found. - */ - public async getTypeByName(name: string): Promise { - return await this.prisma.pOIType.findUnique({ - where: { - name: name - } - }) - } - - // ========================================================= // - // [POI] - - /** - * Saves a point of interest (POI) in the database. - * - * The parameter are given via object deconstruction from the model `POI`! - * Currently given parameters are: - * @param name - **unique** name of the POI - * @param typeId - POIType Identifier: Maps a POIType to said POI in the database - * @param trackId - Track Identifier : Maps a Track to said POI in the database - * @param position - Coordinates to pinpoint the location of said POI. - * @param description - optional description of said POI - * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI - * @returns POI - */ - public async save(args : Prisma.POIUncheckedCreateInput): Promise { - // POIUncheckCreateInput is used because of required relations based on the model! - return await this.prisma.pOI.create({ - data: args - }) - } - - /** - * Updates a POI in the database. - * - * @param uid - Indicator which poi should be updated. - * - * The parameter are given via object deconstruction from the model `POI`! - * Currently given parameters are: - * @param name - New name after change. (Optional) - * @param description - New description after change. (Optional) - * @param typeId - New typeId after change. (Optional) - * @param trackId - New trackId after change. (Optional) - * @param position - New position after change. (Optional) - * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) - * @returns POI - */ - public async update(uid: number, args : Prisma.POIUpdateInput): Promise { - return await this.prisma.pOI.update({ - where: { - uid: uid - }, - data: args - }) - } - - /** - * Removes an poi from the database. - * - * @param uid - Indicator which poi should be removed. - */ - public async remove(uid: number): Promise { - await this.prisma.pOI.delete({ - where: { - uid: uid - } - }) - } - - /** - * Returns a list of all existing pois. - * - * @param trackId - Indicator for filtering all pois for a specific track (Optional) - * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. - */ - public async getAll(trackId? : number): Promise { - return await this.prisma.pOI.findMany({ - where: { - trackId: trackId - } - }) - } - - /** - * Looks up a poi given by its uid. - * - * @param uid - Indicator which poi should be searched for - * @returns POI | null depending on if the poi could be found. - */ - public async getById(uid: number): Promise { - return await this.prisma.pOI.findUnique({ - where: { - uid: uid - }, - include: { - type: true, - track: true - } - }) - } - - /** - * Looks up pois given by its name. - * - * @param name - Indicator which pois should be searched for - * @param trackId - optional filter indicator to filter for a given track. - * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. - */ - public async getByName(name: string, trackId?: number): Promise { - return await this.prisma.pOI.findMany({ - where: { - name: name, - trackId: trackId - }, - }) - } -} \ No newline at end of file + constructor(private prisma: PrismaClient) {} + + // ========================================================= // + // [POI Types] + + /** + * Saves a type for POIs in the database. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: + * @param name - **unique** name of the type of poi. + * @param icon - unique icon name for visualization + * @param description - an optional description for the type of poi. + * @returns POIType + */ + public async saveType(args: Prisma.POITypeCreateInput): Promise { + return await this.prisma.pOIType.create({ + data: args, + }); + } + + /** + * Updates a type of poi in the database. + * + * @param uid - Indicator which type should be updated. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: + * @param name - New name after change. (Optional) + * @param icon - New unique icon name for visualization after change. (Optional) + * @param description - New description after change. (Optional) + * @returns POIType | null if an error occurs. + */ + public async updateType( + uid: number, + args: Prisma.POITypeUpdateInput + ): Promise { + return await this.prisma.pOIType.update({ + where: { + uid: uid, + }, + data: args, + }); + } + + /** + * Removes a poi type from the database. + * + * @param uid - Indicator which type should be removed. + */ + public async removeType(uid: number): Promise { + await this.prisma.pOIType.delete({ + where: { + uid: uid, + }, + }); + } + + /** + * Returns a list of all existing types of poi. + * + * @returns `POIType[]` - List of all types of poi. + */ + public async getAllTypes(): Promise { + return await this.prisma.pOIType.findMany({}); + } + + /** + * Looks up a type given by its uid. + * + * @param uid - Indicator which type should be searched for. + * @returns POIType | null depending on if the type could be found. + */ + public async getTypeById(uid: number): Promise { + return await this.prisma.pOIType.findUnique({ + where: { + uid: uid, + }, + }); + } + + /** + * Looks up a type given by its name. + * + * @param name - Indicator which type should be searched for. + * @returns POIType | null depending on if the type could be found. + */ + public async getTypeByName(name: string): Promise { + return await this.prisma.pOIType.findUnique({ + where: { + name: name, + }, + }); + } + + // ========================================================= // + // [POI] + + /** + * Saves a point of interest (POI) in the database. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: + * @param name - **unique** name of the POI + * @param typeId - POIType Identifier: Maps a POIType to said POI in the database + * @param trackId - Track Identifier : Maps a Track to said POI in the database + * @param position - Coordinates to pinpoint the location of said POI. + * @param description - optional description of said POI + * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI + * @returns POI + */ + public async save(args: Prisma.POIUncheckedCreateInput): Promise { + // POIUncheckCreateInput is used because of required relations based on the model! + return await this.prisma.pOI.create({ + data: args, + }); + } + + /** + * Updates a POI in the database. + * + * @param uid - Indicator which poi should be updated. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: + * @param name - New name after change. (Optional) + * @param description - New description after change. (Optional) + * @param typeId - New typeId after change. (Optional) + * @param trackId - New trackId after change. (Optional) + * @param position - New position after change. (Optional) + * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) + * @returns POI + */ + public async update(uid: number, args: Prisma.POIUpdateInput): Promise { + return await this.prisma.pOI.update({ + where: { + uid: uid, + }, + data: args, + }); + } + + /** + * Removes an poi from the database. + * + * @param uid - Indicator which poi should be removed. + */ + public async remove(uid: number): Promise { + await this.prisma.pOI.delete({ + where: { + uid: uid, + }, + }); + } + + /** + * Returns a list of all existing pois. + * + * @param trackId - Indicator for filtering all pois for a specific track (Optional) + * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. + */ + public async getAll(trackId?: number): Promise { + return await this.prisma.pOI.findMany({ + where: { + trackId: trackId, + }, + }); + } + + /** + * Looks up a poi given by its uid. + * + * @param uid - Indicator which poi should be searched for + * @returns POI | null depending on if the poi could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.pOI.findUnique({ + where: { + uid: uid, + }, + include: { + type: true, + track: true, + }, + }); + } + + /** + * Looks up pois given by its name. + * + * @param name - Indicator which pois should be searched for + * @param trackId - optional filter indicator to filter for a given track. + * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. + */ + public async getByName(name: string, trackId?: number): Promise { + return await this.prisma.pOI.findMany({ + where: { + name: name, + trackId: trackId, + }, + }); + } +} diff --git a/Server/src/services/db/track.controller.ts b/Server/src/services/db/track.controller.ts index b8c92157..fbf21e30 100644 --- a/Server/src/services/db/track.controller.ts +++ b/Server/src/services/db/track.controller.ts @@ -1,5 +1,4 @@ -import { PrismaClient, Prisma } from "@prisma/client"; -import type { Track } from '@prisma/client'; +import { PrismaClient, Prisma, Track } from "@prisma/client"; /** * TrackController class @@ -14,109 +13,110 @@ import type { Track } from '@prisma/client'; * */ export default class TrackController { + constructor(private prisma: PrismaClient) {} - constructor(private prisma: PrismaClient) { } + /** + * Saves a tracker in the database. + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: + * @param start - Name of the start location. + * @param stop - Name of the end location. + * @param data - JSON Data of the track + * @returns Track + */ + public async save(args: Prisma.TrackCreateInput): Promise { + return await this.prisma.track.create({ + data: args, + }); + } - /** - * Saves a tracker in the database. - * - * The parameter are given via object deconstruction from the model `Track`! - * Currently given parameters are: - * @param start - Name of the start location. - * @param stop - Name of the end location. - * @param data - JSON Data of the track - * @returns Track - */ - public async save(args : Prisma.TrackCreateInput): Promise { - return await this.prisma.track.create({ - data: args - } - ) - } + /** + * Updates a track in the database. + * + * @param uid - Indicator which track should be updated + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: + * @param start - New name of the start location after change (Optional) + * @param stop - New name of the end location after change (Optional) + * @param data - New JSON Data of the track after change (Optional) + * @returns Track + */ + public async update( + uid: number, + args: Prisma.TrackUpdateInput + ): Promise { + return await this.prisma.track.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Updates a track in the database. - * - * @param uid - Indicator which track should be updated - * - * The parameter are given via object deconstruction from the model `Track`! - * Currently given parameters are: - * @param start - New name of the start location after change (Optional) - * @param stop - New name of the end location after change (Optional) - * @param data - New JSON Data of the track after change (Optional) - * @returns Track - */ - public async update(uid : number, args : Prisma.TrackUpdateInput): Promise { - return await this.prisma.track.update({ - where : { - uid : uid - }, - data : args - }) - } + /** + * Removes a track in the database. + * + * @param uid - Indicator which track should be removed. + */ + public async remove(uid: number): Promise { + await this.prisma.track.delete({ + where: { + uid: uid, + }, + }); + } - /** - * Removes a track in the database. - * - * @param uid - Indicator which track should be removed. - */ - public async remove(uid: number): Promise { - await this.prisma.track.delete({ - where: { - uid: uid - } - }) - } + /** + * Returns a list of all tracks. + * + * @returns Track[] - List of all tracks. + */ + public async getAll(): Promise { + return await this.prisma.track.findMany({}); + } - /** - * Returns a list of all tracks. - * - * @returns Track[] - List of all tracks. - */ - public async getAll(): Promise { - return await this.prisma.track.findMany({}) - } + /** + * Looks up a track given by its uid. + * + * @param uid - Indicator which track should be searched for. + * @returns Track | null depending on if the track could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.track.findUnique({ + where: { + uid: uid, + }, + include: { + poi: true, + vehicle: true, + }, + }); + } - /** - * Looks up a track given by its uid. - * - * @param uid - Indicator which track should be searched for. - * @returns Track | null depending on if the track could be found. - */ - public async getById(uid: number): Promise { - return await this.prisma.track.findUnique({ - where: { - uid: uid - }, - include: { - poi: true, - vehicle: true - } - }) - } - - /** - * Looks up any track that has a start or stop at the given location. - * - * @param location - Name of the location to check. - * @returns Track[] - List of tracks that have either start and/or stop at the given location. - */ - public async getByLocation(location: string): Promise { - return await this.prisma.track.findMany({ - where: { - OR: [ - { - start: location - }, - { - stop: location - } - ], - }, - include: { - poi: true, - vehicle: true - } - }) - } -} \ No newline at end of file + /** + * Looks up any track that has a start or stop at the given location. + * + * @param location - Name of the location to check. + * @returns Track[] - List of tracks that have either start and/or stop at the given location. + */ + public async getByLocation(location: string): Promise { + return await this.prisma.track.findMany({ + where: { + OR: [ + { + start: location, + }, + { + stop: location, + }, + ], + }, + include: { + poi: true, + vehicle: true, + }, + }); + } +} diff --git a/Server/src/services/db/tracker.controller.ts b/Server/src/services/db/tracker.controller.ts index 5fb7ecf9..d3b65a15 100644 --- a/Server/src/services/db/tracker.controller.ts +++ b/Server/src/services/db/tracker.controller.ts @@ -13,95 +13,96 @@ import { Prisma, PrismaClient, Tracker } from "@prisma/client"; * */ export default class TrackerController { + constructor(private prisma: PrismaClient) {} - constructor(private prisma: PrismaClient) { } + /** + * Saves a new tracker in the database. + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: + * @param uid - ID (EUI) of the tracker. + * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. + * @param data - optional additional data field. + * @returns Tracker + */ + public async save(args: Prisma.TrackerCreateInput): Promise { + return await this.prisma.tracker.create({ + data: args, + }); + } + /** + * Updates a tracker in the database. + * + * @param uid - Indicator which tracker should be updated + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: + * @param vehicleId - New vehicleId (Optional) + * @param data - New additional data field (Optional) + * @returns Tracker + */ + public async update( + uid: string, + args: Prisma.TrackerUncheckedUpdateInput + ): Promise { + //TrackerUncheckedUpdateInput is used + return await this.prisma.tracker.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Saves a new tracker in the database. - * - * The parameter are given via object deconstruction from the model `Tracker`! - * Currently given parameters are: - * @param uid - ID (EUI) of the tracker. - * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. - * @param data - optional additional data field. - * @returns Tracker - */ - public async save(args : Prisma.TrackerCreateInput): Promise { - return await this.prisma.tracker.create({ - data: args - }) - } + /** + * Removes a tracker from the database. + * + * @param uid - Indicator which tracker should be removed. + * @returns True | False depending on if the tracker was removed or not. + */ + public async remove(uid: string): Promise { + await this.prisma.tracker.delete({ + where: { + uid: uid, + }, + }); + } - /** - * Updates a tracker in the database. - * - * @param uid - Indicator which tracker should be updated - * - * The parameter are given via object deconstruction from the model `Tracker`! - * Currently given parameters are: - * @param vehicleId - New vehicleId (Optional) - * @param data - New additional data field (Optional) - * @returns Tracker - */ - public async update(uid : string, args : Prisma.TrackerUncheckedUpdateInput): Promise { - //TrackerUncheckedUpdateInput is used - return await this.prisma.tracker.update({ - where: { - uid: uid - }, - data: args - }) - } + /** + * Returns a list of all trackers + * + * @returns Tracker[] - List of all trackers. + */ + public async getAll(): Promise { + return await this.prisma.tracker.findMany({}); + } - /** - * Removes a tracker from the database. - * - * @param uid - Indicator which tracker should be removed. - * @returns True | False depending on if the tracker was removed or not. - */ - public async remove(uid: string): Promise { - await this.prisma.tracker.delete({ - where: { - uid: uid - } - }) - } + /** + * Looks up a tracker given by its uid. + * + * @param uid - Indicator which tracker should be looked up. + * @returns Tracker | null depending on if the tracker could be found. + */ + public async getById(uid: string): Promise { + return await this.prisma.tracker.findUnique({ + where: { + uid: uid, + }, + }); + } - /** - * Returns a list of all trackers - * - * @returns Tracker[] - List of all trackers. - */ - public async getAll(): Promise { - return await this.prisma.tracker.findMany({}) - } - - /** - * Looks up a tracker given by its uid. - * - * @param uid - Indicator which tracker should be looked up. - * @returns Tracker | null depending on if the tracker could be found. - */ - public async getById(uid: string): Promise { - return await this.prisma.tracker.findUnique({ - where: { - uid: uid - } - }) - } - - /** - * Looks up all trackers for a given vehicle. - * - * @param vehicleId - uid of the vehicle. - * @returns List of trackers assigned to the vehicle. - */ - public async getByVehicleId(vehicleId: number): Promise { - return await this.prisma.tracker.findMany({ - where: { - vehicleId: vehicleId - } - }) - } -} \ No newline at end of file + /** + * Looks up all trackers for a given vehicle. + * + * @param vehicleId - uid of the vehicle. + * @returns List of trackers assigned to the vehicle. + */ + public async getByVehicleId(vehicleId: number): Promise { + return await this.prisma.tracker.findMany({ + where: { + vehicleId: vehicleId, + }, + }); + } +} diff --git a/Server/src/services/db/user.controller.ts b/Server/src/services/db/user.controller.ts index 1b90e65e..5df6a99a 100644 --- a/Server/src/services/db/user.controller.ts +++ b/Server/src/services/db/user.controller.ts @@ -1,5 +1,4 @@ -import { PrismaClient, Prisma } from '@prisma/client'; -import type { User } from '@prisma/client'; +import { PrismaClient, Prisma, User } from "@prisma/client"; /** * UserController class @@ -13,79 +12,79 @@ import type { User } from '@prisma/client'; * - getByUsername() */ export default class UserController { + constructor(private prisma: PrismaClient) {} + /** + * Saves an user in the database. + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: + * @param username - **unique** name of the user. + * @param password - **hashed** password. + * @returns User + */ + public async save(args: Prisma.UserCreateInput): Promise { + return await this.prisma.user.create({ + data: args, + }); + } - constructor(private prisma: PrismaClient) { } + /** + * Updates an user in the database. + * + * @param name - Old username before change. Indicator which user should be updated + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: + * @param username - New username after change. (Optional) + * @param password - New password after change. (Optional) + * @returns User + */ + public async update( + name: string, + args: Prisma.UserUpdateInput + ): Promise { + return await this.prisma.user.update({ + where: { + username: name, + }, + data: args, + }); + } + /** + * Removes an user from the database. + * + * @param username - Indicator which user should be removed + */ + public async remove(username: string): Promise { + await this.prisma.user.delete({ + where: { + username: username, + }, + }); + } - /** - * Saves an user in the database. - * - * The parameter are given via object deconstruction from the model `User`! - * Currently given parameters are: - * @param username - **unique** name of the user. - * @param password - **hashed** password. - * @returns User - */ - public async save(args: Prisma.UserCreateInput): Promise { - return await this.prisma.user.create({ - data: args - }); - } + /** + * Returns a list of all existing users. + * + * @returns `User[]` - List of all users. + */ + public async getAll(): Promise { + return await this.prisma.user.findMany({}); + } - /** - * Updates an user in the database. - * - * @param name - Old username before change. Indicator which user should be updated - * - * The parameter are given via object deconstruction from the model `User`! - * Currently given parameters are: - * @param username - New username after change. (Optional) - * @param password - New password after change. (Optional) - * @returns User - */ - public async update(name: string, args: Prisma.UserUpdateInput): Promise { - return await this.prisma.user.update({ - where: { - username: name - }, - data: args - }); - } - - /** - * Removes an user from the database. - * - * @param username - Indicator which user should be removed - */ - public async remove(username: string): Promise { - await this.prisma.user.delete({ - where: { - username: username - } - }); - } - - /** - * Returns a list of all existing users. - * - * @returns `User[]` - List of all users. - */ - public async getAll(): Promise { - return await this.prisma.user.findMany({}); - } - - /** - * Looks up an user given by its username. - * - * @param username - Indicator which user should be searched for - * @returns User | null depending on if the user could be found. - */ - public async getByUsername(username: string): Promise { - return await this.prisma.user.findUnique({ - where: { - username: username - } - }); - } -} \ No newline at end of file + /** + * Looks up an user given by its username. + * + * @param username - Indicator which user should be searched for + * @returns User | null depending on if the user could be found. + */ + public async getByUsername(username: string): Promise { + return await this.prisma.user.findUnique({ + where: { + username: username, + }, + }); + } +} diff --git a/Server/src/services/db/vehicle.controller.ts b/Server/src/services/db/vehicle.controller.ts index 922504fb..3cc9389a 100644 --- a/Server/src/services/db/vehicle.controller.ts +++ b/Server/src/services/db/vehicle.controller.ts @@ -1,5 +1,4 @@ -import { PrismaClient, Prisma } from "@prisma/client"; -import type { Vehicle, VehicleType } from '@prisma/client'; +import { PrismaClient, Prisma, Vehicle, VehicleType } from "@prisma/client"; /** * VehicleController class @@ -22,209 +21,221 @@ import type { Vehicle, VehicleType } from '@prisma/client'; * - getByName() */ export default class VehicleController { - - constructor(private prisma: PrismaClient) { } - - // ========================================================= // - // [Vehicle Types] - - /** - * Saves a vehicle type in the database. - * - * The parameter are given via object deconstruction from the model `VehicleType`! - * Currently given parameters are: - * @param name - **unique** Name for the type of vehicle. - * @param icon - unique icon name for visualization - * @param description - optional description for the type. - * @returns VehicleType | null if an error occurs - */ - public async saveType(args : Prisma.VehicleTypeCreateInput): Promise { - return await this.prisma.vehicleType.create({ - data: args - }) - } - - /** - * Updates a vehicle type in the database. - * - * @param uid - Indicator which vehicle type should be updated. - * - * The parameter are given via object deconstruction from the model `VehicleType`! - * Currently given parameters are: - * @param name - New name of the vehicle type after change. (Optional) - * @param description - New description of the vehicle type after change. (Optional) - * @returns - */ - public async updateType(uid: number , args : Prisma.VehicleTypeUpdateInput): Promise { - return await this.prisma.vehicleType.update({ - where: { - uid: uid - }, - data: args - }) - } - - /** - * Removes a vehicle type in the database. - * - * @param uid - Indicator which vehicle type should be removed. - * @returns True | False depending on if the track was removed or not. - */ - public async removeType(uid: number): Promise { - await this.prisma.vehicleType.delete({ - where: { - uid: uid - } - }) - } - - /** - * Returns a list of all vehicle types. - * - * @returns VehicleType[] - List of all vehicle types. - */ - public async getAllTypes(): Promise { - return await this.prisma.vehicleType.findMany({}) - } - - /** - * Looks up a vehicle type given by its uid. - * - * @param uid - Indicator which vehicle type should be searched for. - * @returns VehicleType | null depending on if the vehicle type could be found. - */ - public async getTypeById(uid: number): Promise { - return await this.prisma.vehicleType.findUnique({ - where: { - uid: uid - } - }) - } - - /** - * Looks up a vehicle type by its name. - * - * @param uid - Indicator which vehicle type should be searched for. - * @returns VehicleType | null depending on if the vehicle type could be found. - */ - public async getTypeByName(name: string): Promise { - return await this.prisma.vehicleType.findUnique({ - where: { - name: name - } - }) - } - - // ========================================================= // - // [Vehicles] - - /** - * Saves a new vehicle in the database. - * - * The parameter are given via object deconstruction from the model `Vehicle`! - * Currently given parameters are: - * @param typeId - VehicleType uid - * @param trackId - Track uid - * @param name - display name for the given vehicle (Optional) - * @returns Vehicle | null if an error occurs. - */ - public async save(args: Prisma.VehicleUncheckedCreateInput): Promise { - // VehicleUncheckedCreateInput is used because of required relations - return await this.prisma.vehicle.create({ - data: args - }) - } - - /** - * Updadtes a vehicle in the database. - * - * @param uid - Indicator which vehicle should be updated - * - * The parameter are given via object deconstruction from the model `Vehicle`! - * Currently given parameters are: - * @param typeId - New VehicleType.uid after change (Optional) - * @param trackId - New Track.uid after change (Optional) - * @param name - New display name after change (Optional) - * @returns Vehicle | null if an error occurs - */ - public async update(uid: number, args: Prisma.VehicleUncheckedUpdateInput): Promise { - // VehicleUncheckCreateInput is used because of required relations - return await this.prisma.vehicle.update({ - where: { - uid: uid - }, - data: args - }) - } - - /** - * Removes a vehicle in the database. - * - * @param uid - Indicator which vehicle should be removed. - */ - public async remove(uid: number): Promise { - await this.prisma.vehicle.delete({ - where: { - uid: uid - } - }) - } - - /** - * Returns a list of all vehicles. - * - * @param trackId - Track.uid for filtering list (Optional) - * - * @returns Vehicle[] - */ - public async getAll(trackId?: number): Promise { - return await this.prisma.vehicle.findMany({ - where: { - trackId: trackId - }, - include: { - type: true, - track: true - } - }) - } - - /** - * Looks up a vehicle by its uid. - * - * @param uid - Indicator which vehicle should be looked for. - * @returns Vehicle | null depending on if the vehicle could be found. - */ - public async getById(uid: number): Promise { - return await this.prisma.vehicle.findUnique({ - where: { - uid: uid - }, - include: { - type: true, - track: true - } - }) - } - - /** - * Looks up a vehicle by its name. - * - * @param name - Indicator which vehicle should be looked for. - * @returns Vehicle | null depending on if the vehicle could be found. - */ - public async getByName(name: string, trackId: number): Promise { - return await this.prisma.vehicle.findUnique({ - where: { - name_trackId: { - name: name, - trackId: trackId - } - }, - include: { - type: true, - track: true - } - }) - } -} \ No newline at end of file + constructor(private prisma: PrismaClient) {} + + // ========================================================= // + // [Vehicle Types] + + /** + * Saves a vehicle type in the database. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: + * @param name - **unique** Name for the type of vehicle. + * @param icon - unique icon name for visualization + * @param description - optional description for the type. + * @returns VehicleType | null if an error occurs + */ + public async saveType( + args: Prisma.VehicleTypeCreateInput + ): Promise { + return await this.prisma.vehicleType.create({ + data: args, + }); + } + + /** + * Updates a vehicle type in the database. + * + * @param uid - Indicator which vehicle type should be updated. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: + * @param name - New name of the vehicle type after change. (Optional) + * @param description - New description of the vehicle type after change. (Optional) + * @returns + */ + public async updateType( + uid: number, + args: Prisma.VehicleTypeUpdateInput + ): Promise { + return await this.prisma.vehicleType.update({ + where: { + uid: uid, + }, + data: args, + }); + } + + /** + * Removes a vehicle type in the database. + * + * @param uid - Indicator which vehicle type should be removed. + * @returns True | False depending on if the track was removed or not. + */ + public async removeType(uid: number): Promise { + await this.prisma.vehicleType.delete({ + where: { + uid: uid, + }, + }); + } + + /** + * Returns a list of all vehicle types. + * + * @returns VehicleType[] - List of all vehicle types. + */ + public async getAllTypes(): Promise { + return await this.prisma.vehicleType.findMany({}); + } + + /** + * Looks up a vehicle type given by its uid. + * + * @param uid - Indicator which vehicle type should be searched for. + * @returns VehicleType | null depending on if the vehicle type could be found. + */ + public async getTypeById(uid: number): Promise { + return await this.prisma.vehicleType.findUnique({ + where: { + uid: uid, + }, + }); + } + + /** + * Looks up a vehicle type by its name. + * + * @param uid - Indicator which vehicle type should be searched for. + * @returns VehicleType | null depending on if the vehicle type could be found. + */ + public async getTypeByName(name: string): Promise { + return await this.prisma.vehicleType.findUnique({ + where: { + name: name, + }, + }); + } + + // ========================================================= // + // [Vehicles] + + /** + * Saves a new vehicle in the database. + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: + * @param typeId - VehicleType uid + * @param trackId - Track uid + * @param name - display name for the given vehicle (Optional) + * @returns Vehicle | null if an error occurs. + */ + public async save( + args: Prisma.VehicleUncheckedCreateInput + ): Promise { + // VehicleUncheckedCreateInput is used because of required relations + return await this.prisma.vehicle.create({ + data: args, + }); + } + + /** + * Updadtes a vehicle in the database. + * + * @param uid - Indicator which vehicle should be updated + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: + * @param typeId - New VehicleType.uid after change (Optional) + * @param trackId - New Track.uid after change (Optional) + * @param name - New display name after change (Optional) + * @returns Vehicle | null if an error occurs + */ + public async update( + uid: number, + args: Prisma.VehicleUncheckedUpdateInput + ): Promise { + // VehicleUncheckCreateInput is used because of required relations + return await this.prisma.vehicle.update({ + where: { + uid: uid, + }, + data: args, + }); + } + + /** + * Removes a vehicle in the database. + * + * @param uid - Indicator which vehicle should be removed. + */ + public async remove(uid: number): Promise { + await this.prisma.vehicle.delete({ + where: { + uid: uid, + }, + }); + } + + /** + * Returns a list of all vehicles. + * + * @param trackId - Track.uid for filtering list (Optional) + * + * @returns Vehicle[] + */ + public async getAll(trackId?: number): Promise { + return await this.prisma.vehicle.findMany({ + where: { + trackId: trackId, + }, + include: { + type: true, + track: true, + }, + }); + } + + /** + * Looks up a vehicle by its uid. + * + * @param uid - Indicator which vehicle should be looked for. + * @returns Vehicle | null depending on if the vehicle could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.vehicle.findUnique({ + where: { + uid: uid, + }, + include: { + type: true, + track: true, + }, + }); + } + + /** + * Looks up a vehicle by its name. + * + * @param name - Indicator which vehicle should be looked for. + * @returns Vehicle | null depending on if the vehicle could be found. + */ + public async getByName( + name: string, + trackId: number + ): Promise { + return await this.prisma.vehicle.findUnique({ + where: { + name_trackId: { + name: name, + trackId: trackId, + }, + }, + include: { + type: true, + track: true, + }, + }); + } +} From 84c27a3403e08c6128f48e4dfe2e00ef0ed9712d Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:27:38 +0200 Subject: [PATCH 10/19] Add limit field for LogController.getAll() --- Server/src/services/db/log.controller.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index d62080f6..c91b1092 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -87,11 +87,12 @@ export default class LogController { * 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 10. * @param vehicleId - Vehicle to filter for (Optional) * @param trackerId - Tracker to filter for (Optional) * @returns Log[] - List of all logs */ - public async getAll(vehicleId?: number, trackerId?: string): Promise { + public async getAll(limit : number = 10, vehicleId?: number, trackerId?: string): Promise { return await this.prisma.log.findMany({ where: { vehicleId: vehicleId, @@ -102,6 +103,7 @@ export default class LogController { timestamp: "desc", }, ], + take : limit }); } From 8b5869aab9afdda5143651a737677fde6f4a96f6 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:11:51 +0200 Subject: [PATCH 11/19] Adjust POIController --- Server/src/services/db/poi.controller.ts | 476 ++++++++++------------- 1 file changed, 198 insertions(+), 278 deletions(-) diff --git a/Server/src/services/db/poi.controller.ts b/Server/src/services/db/poi.controller.ts index 8ed14aec..1c4f887c 100644 --- a/Server/src/services/db/poi.controller.ts +++ b/Server/src/services/db/poi.controller.ts @@ -1,7 +1,4 @@ -import { PrismaClient, Prisma } from ".prisma/client" -import type { POI, POIType } from ".prisma/client" -import { logger } from "../../utils/logger" -import { Feature, Point } from "geojson" +import { POI, POIType, PrismaClient, Prisma } from "@prisma/client"; /** * POIController class @@ -26,292 +23,215 @@ import { Feature, Point } from "geojson" * */ export default class POIController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - // ========================================================= // - // [POI Types] + // ========================================================= // + // [POI Types] - /** - * Saves a type for POIs in the database. - * - * @param name - **unique** name of the type of poi. - * @param icon - unique icon name for visualization - * @param description - an optional description for the type of poi. - * @returns POIType | null if an error occurs. - */ - public async saveType(name: string, icon: string, description?: string): Promise { - try { - return await this.prisma.pOIType.create({ - data: { - name: name, - icon: icon, - description: description - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a type for POIs in the database. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: + * @param name - **unique** name of the type of poi. + * @param icon - unique icon name for visualization + * @param description - an optional description for the type of poi. + * @returns POIType + */ + public async saveType(args: Prisma.POITypeCreateInput): Promise { + return await this.prisma.pOIType.create({ + data: args, + }); + } - /** - * Updates a type of poi in the database. - * - * @param uid - Indicator which type should be updated. - * @param name - New name after change. (Optional) - * @param icon - New unique icon name for visualization after change. (Optional) - * @param description - New description after change. (Optional) - * @returns POIType | null if an error occurs. - */ - public async updateType(uid: number, name?: string, icon?: string, description?: string): Promise { - try { - return await this.prisma.pOIType.update({ - where: { - uid: uid - }, - data: { - name: name, - description: description - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates a type of poi in the database. + * + * @param uid - Indicator which type should be updated. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: + * @param name - New name after change. (Optional) + * @param icon - New unique icon name for visualization after change. (Optional) + * @param description - New description after change. (Optional) + * @returns POIType | null if an error occurs. + */ + public async updateType( + uid: number, + args: Prisma.POITypeUpdateInput + ): Promise { + return await this.prisma.pOIType.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes a poi type from the database. - * - * @param uid - Indicator which type should be removed. - * @returns True | False depending on if the type was removed or not. - */ - public async removeType(uid: number): Promise { - try { - await this.prisma.pOIType.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes a poi type from the database. + * + * @param uid - Indicator which type should be removed. + */ + public async removeType(uid: number): Promise { + await this.prisma.pOIType.delete({ + where: { + uid: uid, + }, + }); + if (await this.getById(uid) === null) { + return true + } + return false + } - /** - * Returns a list of all existing types of poi. - * - * @returns `POIType[]` - List of all types of poi. - */ - public async getAllTypes(): Promise { - try { - return await this.prisma.pOIType.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all existing types of poi. + * + * @returns `POIType[]` - List of all types of poi. + */ + public async getAllTypes(): Promise { + return await this.prisma.pOIType.findMany({}); + } - /** - * Looks up a type given by its uid. - * - * @param uid - Indicator which type should be searched for. - * @returns POIType | null depending on if the type could be found. - */ - public async getTypeById(uid: number): Promise { - try { - return await this.prisma.pOIType.findUnique({ - where: { - uid: uid - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a type given by its uid. + * + * @param uid - Indicator which type should be searched for. + * @returns POIType | null depending on if the type could be found. + */ + public async getTypeById(uid: number): Promise { + return await this.prisma.pOIType.findUnique({ + where: { + uid: uid, + }, + }); + } - /** - * Looks up a type given by its name. - * - * @param name - Indicator which type should be searched for. - * @returns POIType | null depending on if the type could be found. - */ - public async getTypeByName(name: string): Promise { - try { - return await this.prisma.pOIType.findUnique({ - where: { - name: name - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a type given by its name. + * + * @param name - Indicator which type should be searched for. + * @returns POIType | null depending on if the type could be found. + */ + public async getTypeByName(name: string): Promise { + return await this.prisma.pOIType.findUnique({ + where: { + name: name, + }, + }); + } - // ========================================================= // - // [POI] + // ========================================================= // + // [POI] - /** - * Saves a point of interest (POI) in the database. - * - * @param name - **unique** name of the POI - * @param typeId - POIType Identifier: Maps a POIType to said POI in the database - * @param trackId - Track Identifier : Maps a Track to said POI in the database - * @param position - Coordinates to pinpoint the location of said POI. - * @param description - optional description of said POI - * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI - * @returns POI | null if an error occurs. - */ - public async save( - name: string, - typeId: number, - trackId: number, - position: Feature, - description?: string, - isTurningPoint: boolean = false - ): Promise { - try { - return await this.prisma.pOI.create({ - data: { - name: name, - description: description, - typeId: typeId, - trackId: trackId, - position: position as any, // Required, as typescript will not otherwise know that keys in a Point are strings. - isTurningPoint: isTurningPoint - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a point of interest (POI) in the database. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: + * @param name - **unique** name of the POI + * @param typeId - POIType Identifier: Maps a POIType to said POI in the database + * @param trackId - Track Identifier : Maps a Track to said POI in the database + * @param position - Coordinates to pinpoint the location of said POI. + * @param description - optional description of said POI + * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI + * @returns POI + */ + public async save(args: Prisma.POIUncheckedCreateInput): Promise { + // POIUncheckCreateInput is used because of required relations based on the model! + return await this.prisma.pOI.create({ + data: args, + }); + } - /** - * Updates a POI in the database. - * - * @param uid - Indicator which poi should be updated. - * @param name - New name after change. (Optional) - * @param description - New description after change. (Optional) - * @param typeId - New typeId after change. (Optional) - * @param trackId - New trackId after change. (Optional) - * @param position - New position after change. (Optional) - * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) - * @returns POI | null if an error occurs. - */ - public async update( - uid: number, - name?: string, - description?: string, - typeId?: number, - trackId?: number, - position?: Feature, - isTurningPoint?: boolean - ): Promise { - try { - return await this.prisma.pOI.update({ - where: { - uid: uid - }, - data: { - name: name, - description: description, - typeId: typeId, - trackId: trackId, - position: position as any, // Required, as typescript will not otherwise know that keys in a Point are strings. - isTurningPoint: isTurningPoint - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates a POI in the database. + * + * @param uid - Indicator which poi should be updated. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: + * @param name - New name after change. (Optional) + * @param description - New description after change. (Optional) + * @param typeId - New typeId after change. (Optional) + * @param trackId - New trackId after change. (Optional) + * @param position - New position after change. (Optional) + * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) + * @returns POI + */ + public async update(uid: number, args: Prisma.POIUncheckedUpdateInput): Promise { + // POIUncheckUpdateInput is used because of required relations based on the model + return await this.prisma.pOI.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes an poi from the database. - * - * @param uid - Indicator which poi should be removed. - * @returns True | False depending on if the user was removed or not. - */ - public async remove(uid: number): Promise { - try { - await this.prisma.pOI.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes an poi from the database. + * + * @param uid - Indicator which poi should be removed. + */ + public async remove(uid: number): Promise { + await this.prisma.pOI.delete({ + where: { + uid: uid, + }, + }); + if (await this.getById(uid) === null) { + return true + } + return false + } - /** - * Returns a list of all existing pois. - * - * @param trackId - Indicator for filtering all pois for a specific track (Optional) - * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. - */ - public async getAll(trackId?: number): Promise { - try { - return await this.prisma.pOI.findMany({ - where: { - trackId: trackId - } - }) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all existing pois. + * + * @param trackId - Indicator for filtering all pois for a specific track (Optional) + * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. + */ + public async getAll(trackId?: number): Promise { + return await this.prisma.pOI.findMany({ + where: { + trackId: trackId, + }, + }); + } - /** - * Looks up a poi given by its uid. - * - * @param uid - Indicator which poi should be searched for - * @returns POI | null depending on if the poi could be found. - */ - public async getById(uid: number): Promise { - try { - return await this.prisma.pOI.findUnique({ - where: { - uid: uid - }, - include: { - type: true, - track: true - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a poi given by its uid. + * + * @param uid - Indicator which poi should be searched for + * @returns POI | null depending on if the poi could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.pOI.findUnique({ + where: { + uid: uid, + }, + include: { + type: true, + track: true, + }, + }); + } - /** - * Looks up pois given by its name. - * - * @param name - Indicator which pois should be searched for - * @param trackId - optional filter indicator to filter for a given track. - * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. - */ - public async getByName(name: string, trackId?: number): Promise { - try { - return await this.prisma.pOI.findMany({ - where: { - name: name, - trackId: trackId - } - }) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Looks up pois given by its name. + * + * @param name - Indicator which pois should be searched for + * @param trackId - optional filter indicator to filter for a given track. + * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. + */ + public async getByName(name: string, trackId?: number): Promise { + return await this.prisma.pOI.findMany({ + where: { + name: name, + trackId: trackId, + }, + }); + } } From 906858b2f885a1fe400a001fc31c4beb704b8561 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:11:59 +0200 Subject: [PATCH 12/19] Adjust removal operation for all controller --- Server/src/services/db/log.controller.ts | 323 ++++++------- Server/src/services/db/poi.controller.ts | 12 +- Server/src/services/db/track.controller.ts | 238 ++++------ Server/src/services/db/tracker.controller.ts | 206 ++++---- Server/src/services/db/user.controller.ts | 171 +++---- Server/src/services/db/vehicle.controller.ts | 472 ++++++++----------- 6 files changed, 613 insertions(+), 809 deletions(-) diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index 4d9302b9..517d0143 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -1,5 +1,4 @@ -import { Log, PrismaClient, Prisma } from "@prisma/client" -import { logger } from "../../utils/logger" +import { Log, PrismaClient, Prisma } from "@prisma/client"; /** * LogController class @@ -14,195 +13,149 @@ import { logger } from "../../utils/logger" * - getLog() */ export default class LogController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - // ========================================================= // - // [Tracker Logs] + // ========================================================= // + // [Tracker Logs] - /** - * Saves a new log in the database. - * - * @param timestamp - Time of log. - * @param vehicleId - Vehicle.uid which is associated with this log. - * @param position - Current GPS position at the time of the creation of the log. - * @param heading - Current GPS heading at the time of the creation of the log. - * @param speed - Current speed at the time of the creation of the log. - * @param battery - Current battery charge at the time of the creation of the log. (Optional for the case of app data) - * @param trackerId - Tracker.uid which caused the log. (Optional for the case of app data) - * @param data - optional addtional data field. - * @returns Log | null if an error occurs. - */ - public async save( - timestamp: Date, - vehicleId: number, - position: [number, number], - heading: number, - speed: number, - battery?: number, - data?: any, - trackerId?: string - ): Promise { - try { - // Note: Prisma converts JSON into JSONValues for more functionality. - // Either JSON.parse(JSON.stringify(position)) as Prisma.InputJsonValue or position as unknown as Prisma.InputJsonValue is the solution. - return await this.prisma.log.create({ - data: { - timestamp: timestamp, - trackerId: trackerId, - position: position, - heading: heading, - speed: speed, - battery: battery, - vehicleId: vehicleId, - data: data - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a new log in the database. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: + * @param timestamp - Time of log. + * @param vehicleId - Vehicle.uid which is associated with this log. + * @param position - Current GPS position at the time of the creation of the log. + * @param heading - Current GPS heading at the time of the creation of the log. + * @param speed - Current speed at the time of the creation of the log. + * @param battery - Current battery charge at the time of the creation of the log. (Optional for the case of app data) + * @param trackerId - Tracker.uid which caused the log. (Optional for the case of app data) + * @param data - optional addtional data field. + * @returns Log | null if an error occurs. + */ + public async save(args: Prisma.LogUncheckedCreateInput): Promise { + //LogUncheckCreateInput is used because of required relations with other models! + return await this.prisma.log.create({ + data: args, + }); + } - /** - * Updates a Log entry. - * - * @param uid - Indicator for specific log. - * @param timestamp - Time when the log was created. - * @param position - gps position of the tracker/app. - * @param heading - degree in which the tracker/app was pointing. - * @param speed - current speed of tracker/app. - * @param battery - current battery of tracker. - * @param data - GPS Data. - * @param vehicleId - which vehicle is connected with said tracker/app. For tracker this can be found in the tracker model. - * @param trackerId - identifier for said tracker. For app data this field is always `null` - * @returns Log | null if an error occurs. - */ - public async update( - uid: number, - timestamp?: Date, - position?: [number, number], - heading?: number, - speed?: number, - battery?: number, - data?: any, - vehicleId?: number, - trackerId?: string - ): Promise { - try { - // Note: Prisma converts JSON into JSONValues for more functionality. - // Either JSON.parse(JSON.stringify(position)) as Prisma.InputJsonValue or position as unknown as Prisma.InputJsonValue is the solution. - return await this.prisma.log.update({ - where: { - uid: uid - }, - data: { - timestamp: timestamp, - position: position, - heading: heading, - speed: speed, - battery: battery, - data: data, - vehicleId: vehicleId, - trackerId: trackerId - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates a Log entry. + * + * @param uid - Indicator for specific log. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: + * @param timestamp - Time when the log was created. + * @param position - gps position of the tracker/app. + * @param heading - degree in which the tracker/app was pointing. + * @param speed - current speed of tracker/app. + * @param battery - current battery of tracker. + * @param data - GPS Data. + * @param vehicleId - which vehicle is connected with said tracker/app. For tracker this can be found in the tracker model. + * @param trackerId - identifier for said tracker. For app data this field is always `null` + * @returns Log | null if an error occurs. + */ + public async update( + uid: number, + args: Prisma.LogUpdateInput + ): Promise { + return await this.prisma.log.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes a log from the database. - * - * @param uid - Indicator which log should be removed - * @returns True | False depending on if the log could be removed. - */ - public async remove(uid: number): Promise { - try { - await this.prisma.log.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * 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 { + 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 vehicleId - Vehicle to filter for (Optional) - * @param trackerId - Tracker to filter for (Optional) - * @returns Log[] - List of all logs - */ - public async getAll(vehicleId?: number, trackerId?: string): Promise { - try { - return await this.prisma.log.findMany({ - where: { - vehicleId: vehicleId, - trackerId: trackerId - }, - orderBy: [ - { - timestamp: "desc" - } - ] - }) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * 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 10. + * @param vehicleId - Vehicle to filter for (Optional) + * @param trackerId - Tracker to filter for (Optional) + * @returns Log[] - List of all logs + */ + public async getAll(limit : number = 10, vehicleId?: number, trackerId?: string): Promise { + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + trackerId: trackerId, + }, + orderBy: [ + { + timestamp: "desc", + }, + ], + take : limit + }); + } - /** - * Looks up a specific log in the database. - * - * @param uid - Indicator for log - * - * @returns Log | null depending on if the log could be found. - */ - public async getLog(uid: number): Promise { - try { - return await this.prisma.log.findUnique({ - where: { - uid: uid - }, - include: { - vehicle: true, - tracker: true - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a specific log in the database. + * + * @param uid - Indicator for log + * + * @returns Log | null depending on if the log could be found. + */ + public async getLog(uid: number): Promise { + return await this.prisma.log.findUnique({ + where: { + uid: uid, + }, + include: { + vehicle: true, + tracker: true, + }, + }); + } - /** - * Returns a list of the newest logs for an vehicle. - * - * - * @param vehicleId - Indicator which vehicle's logs should be considered. - * @param max_sec - How old the logs can be at max. Default: 5 min - * - * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. - */ - public async getNewestLogs(vehicleId: number, max_sec: number = 300): Promise { - let logs = await this.getAll((vehicleId = vehicleId)) - let max_date = Date.now() - max_sec * 1000 + /** + * Returns a list of the newest logs for an vehicle. + * + * + * @param vehicleId - Indicator which vehicle's logs should be considered. + * @param max_sec - How old the logs can be at max. Default: 5 min + * + * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. + */ + public async getNewestLogs( + vehicleId: number, + max_sec: number = 300 + ): Promise { + // Earliest date which should be considered + let max_date = new Date(Date.now() - max_sec * 1000); - // Because the logs are sorted by timestamps in descending order we just need to find - // the log with an timestamp older then our max_date and don't need to bother with the rest of it - let i = 0 - while (new Date(logs[i].timestamp).getTime() >= max_date) { - i += 1 - } - return logs.slice(0, i + 1) - } + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + timestamp: { + gt: max_date, + }, + }, + orderBy: [ + { + timestamp: "desc", + }, + ], + }); + } } diff --git a/Server/src/services/db/poi.controller.ts b/Server/src/services/db/poi.controller.ts index 1c4f887c..71f3748d 100644 --- a/Server/src/services/db/poi.controller.ts +++ b/Server/src/services/db/poi.controller.ts @@ -72,6 +72,7 @@ export default class POIController { * Removes a poi type from the database. * * @param uid - Indicator which type should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. */ public async removeType(uid: number): Promise { await this.prisma.pOIType.delete({ @@ -79,10 +80,7 @@ export default class POIController { uid: uid, }, }); - if (await this.getById(uid) === null) { - return true - } - return false + return true } /** @@ -174,6 +172,7 @@ export default class POIController { * Removes an poi from the database. * * @param uid - Indicator which poi should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. */ public async remove(uid: number): Promise { await this.prisma.pOI.delete({ @@ -181,10 +180,7 @@ export default class POIController { uid: uid, }, }); - if (await this.getById(uid) === null) { - return true - } - return false + return true } /** diff --git a/Server/src/services/db/track.controller.ts b/Server/src/services/db/track.controller.ts index e2e70f63..512432cc 100644 --- a/Server/src/services/db/track.controller.ts +++ b/Server/src/services/db/track.controller.ts @@ -1,6 +1,4 @@ -import { PrismaClient, Prisma } from "@prisma/client" -import type { Track } from "@prisma/client" -import { logger } from "../../utils/logger" +import { PrismaClient, Prisma, Track } from "@prisma/client"; /** * TrackController class @@ -15,142 +13,112 @@ import { logger } from "../../utils/logger" * */ export default class TrackController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - /** - * Saves a tracker in the database. - * - * @param start - Name of the start location. - * @param stop - Name of the end location. - * @param data - JSON Data of the track - * @returns Track | null if an error occurs - */ - public async save(start: string, stop: string, data: any): Promise { - try { - return await this.prisma.track.create({ - data: { - start: start, - stop: stop, - data: data - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a tracker in the database. + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: + * @param start - Name of the start location. + * @param stop - Name of the end location. + * @param data - JSON Data of the track + * @returns Track + */ + public async save(args: Prisma.TrackCreateInput): Promise { + return await this.prisma.track.create({ + data: args, + }); + } - /** - * Updates a track in the database. - * - * @param uid - Indicator which track should be updated - * @param start - New name of the start location after change (Optional) - * @param stop - New name of the end location after change (Optional) - * @param data - New JSON Data of the track after change (Optional) - * @returns Track | null if an error occurs - */ - public async update(uid: number, start?: string, stop?: string, data?: any): Promise { - try { - return await this.prisma.track.update({ - where: { - uid: uid - }, - data: { - start: start, - stop: stop, - data: data - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates a track in the database. + * + * @param uid - Indicator which track should be updated + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: + * @param start - New name of the start location after change (Optional) + * @param stop - New name of the end location after change (Optional) + * @param data - New JSON Data of the track after change (Optional) + * @returns Track + */ + public async update( + uid: number, + args: Prisma.TrackUpdateInput + ): Promise { + return await this.prisma.track.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes a track in the database. - * - * @param uid - Indicator which track should be removed. - * @returns True | False depending on if the track was removed or not. - */ - public async remove(uid: number): Promise { - try { - await this.prisma.track.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes a track in the database. + * + * @param uid - Indicator which track should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: number): Promise { + await this.prisma.track.delete({ + where: { + uid: uid, + }, + }); + return true + } - /** - * Returns a list of all tracks. - * - * @returns Track[] - List of all tracks. - */ - public async getAll(): Promise { - try { - return await this.prisma.track.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all tracks. + * + * @returns Track[] - List of all tracks. + */ + public async getAll(): Promise { + return await this.prisma.track.findMany({}); + } - /** - * Looks up a track given by its uid. - * - * @param uid - Indicator which track should be searched for. - * @returns Track | null depending on if the track could be found. - */ - public async getById(uid: number): Promise { - try { - return await this.prisma.track.findUnique({ - where: { - uid: uid - }, - include: { - poi: true, - vehicle: true - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a track given by its uid. + * + * @param uid - Indicator which track should be searched for. + * @returns Track | null depending on if the track could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.track.findUnique({ + where: { + uid: uid, + }, + include: { + poi: true, + vehicle: true, + }, + }); + } - /** - * Looks up any track that has a start or stop at the given location. - * - * @param location - Name of the location to check. - * @returns Track[] - List of tracks that have either start and/or stop at the given location. - */ - public async getByLocation(location: string): Promise { - try { - return await this.prisma.track.findMany({ - where: { - OR: [ - { - start: location - }, - { - stop: location - } - ] - }, - include: { - poi: true, - vehicle: true - } - }) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Looks up any track that has a start or stop at the given location. + * + * @param location - Name of the location to check. + * @returns Track[] - List of tracks that have either start and/or stop at the given location. + */ + public async getByLocation(location: string): Promise { + return await this.prisma.track.findMany({ + where: { + OR: [ + { + start: location, + }, + { + stop: location, + }, + ], + }, + include: { + poi: true, + vehicle: true, + }, + }); + } } diff --git a/Server/src/services/db/tracker.controller.ts b/Server/src/services/db/tracker.controller.ts index e3a128c1..987584e3 100644 --- a/Server/src/services/db/tracker.controller.ts +++ b/Server/src/services/db/tracker.controller.ts @@ -1,5 +1,4 @@ -import { PrismaClient, Tracker } from "@prisma/client" -import { logger } from "../../utils/logger" +import { Prisma, PrismaClient, Tracker } from "@prisma/client"; /** * TrackerController class @@ -14,126 +13,97 @@ import { logger } from "../../utils/logger" * */ export default class TrackerController { - constructor(private prisma: PrismaClient) { - } + constructor(private prisma: PrismaClient) {} - /** - * Saves a new tracker in the database. - * - * @param uid - ID (EUI) of the tracker. - * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. - * @param data - optional additional data field. - * @returns Tracker | null if an error occurs - */ - public async save(uid: string, vehicleId?: number | null, data?: any): Promise { - try { - return await this.prisma.tracker.create({ - data: { - uid: uid, - vehicleId: vehicleId, - data: data - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a new tracker in the database. + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: + * @param uid - ID (EUI) of the tracker. + * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. + * @param data - optional additional data field. + * @returns Tracker + */ + public async save(args: Prisma.TrackerCreateInput): Promise { + return await this.prisma.tracker.create({ + data: args, + }); + } - /** - * Updates a tracker in the database. - * - * @param uid - Indicator which tracker should be updated - * @param vehicleId - New vehicleId (Optional) - * @param data - New additional data field (Optional) - * @returns Tracker | null if an error occurs - */ - public async update(uid: string, vehicleId?: number | null, data?: any): Promise { - try { - return await this.prisma.tracker.update({ - where: { - uid: uid - }, - data: { - vehicleId: vehicleId, - data: data - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates a tracker in the database. + * + * @param uid - Indicator which tracker should be updated + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: + * @param vehicleId - New vehicleId (Optional) + * @param data - New additional data field (Optional) + * @returns Tracker + */ + public async update( + uid: string, + args: Prisma.TrackerUncheckedUpdateInput + ): Promise { + //TrackerUncheckedUpdateInput is used + return await this.prisma.tracker.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes a tracker from the database. - * - * @param uid - Indicator which tracker should be removed. - * @returns True | False depending on if the tracker was removed or not. - */ - public async remove(uid: string): Promise { - try { - await this.prisma.tracker.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes a tracker from the database. + * + * @param uid - Indicator which tracker should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: string): Promise { + await this.prisma.tracker.delete({ + where: { + uid: uid, + }, + }); + return true + } - /** - * Returns a list of all trackers - * - * @returns Tracker[] - List of all trackers. - */ - public async getAll(): Promise { - try { - return await this.prisma.tracker.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all trackers + * + * @returns Tracker[] - List of all trackers. + */ + public async getAll(): Promise { + return await this.prisma.tracker.findMany({}); + } - /** - * Looks up a tracker given by its uid. - * - * @param uid - Indicator which tracker should be looked up. - * @returns Tracker | null depending on if the tracker could be found. - */ - public async getById(uid: string): Promise { - try { - return await this.prisma.tracker.findUnique({ - where: { - uid: uid - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a tracker given by its uid. + * + * @param uid - Indicator which tracker should be looked up. + * @returns Tracker | null depending on if the tracker could be found. + */ + public async getById(uid: string): Promise { + return await this.prisma.tracker.findUnique({ + where: { + uid: uid, + }, + }); + } - /** - * Looks up all trackers for a given vehicle. - * - * @param vehicleId - uid of the vehicle. - * @returns List of trackers assigned to the vehicle. - */ - public async getByVehicleId(vehicleId: number): Promise { - try { - return await this.prisma.tracker.findMany({ - where: { - vehicleId: vehicleId - } - }) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Looks up all trackers for a given vehicle. + * + * @param vehicleId - uid of the vehicle. + * @returns List of trackers assigned to the vehicle. + */ + public async getByVehicleId(vehicleId: number): Promise { + return await this.prisma.tracker.findMany({ + where: { + vehicleId: vehicleId, + }, + }); + } } diff --git a/Server/src/services/db/user.controller.ts b/Server/src/services/db/user.controller.ts index 820d531b..3c4e4c0e 100644 --- a/Server/src/services/db/user.controller.ts +++ b/Server/src/services/db/user.controller.ts @@ -1,6 +1,4 @@ -import { PrismaClient, Prisma } from "@prisma/client" -import type { User } from "@prisma/client" -import { logger } from "../../utils/logger" +import { PrismaClient, Prisma, User } from "@prisma/client"; /** * UserController class @@ -14,104 +12,81 @@ import { logger } from "../../utils/logger" * - getByUsername() */ export default class UserController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - /** - * Saves an user in the database. - * - * @param username - **unique** name of the user. - * @param password - **hashed** password. - * @returns User | null if an error occurs. - */ - public async save(username: string, password: string): Promise { - try { - return await this.prisma.user.create({ - data: { - username: username, - password: password - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves an user in the database. + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: + * @param username - **unique** name of the user. + * @param password - **hashed** password. + * @returns User + */ + public async save(args: Prisma.UserCreateInput): Promise { + return await this.prisma.user.create({ + data: args, + }); + } - /** - * Updates an user in the database. - * - * @param name - Old username before change. Indicator which user should be updated - * @param username - New username after change. (Optional) - * @param password - New password after change. (Optional) - * @returns User | null if an error occurs. - */ - public async update(name: string, username?: string, password?: string): Promise { - try { - return await this.prisma.user.update({ - where: { - username: name - }, - data: { - username: username, - password: password - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates an user in the database. + * + * @param name - Old username before change. Indicator which user should be updated + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: + * @param username - New username after change. (Optional) + * @param password - New password after change. (Optional) + * @returns User + */ + public async update( + name: string, + args: Prisma.UserUpdateInput + ): Promise { + return await this.prisma.user.update({ + where: { + username: name, + }, + data: args, + }); + } - /** - * Removes an user from the database. - * - * @param username - Indicator which user should be removed - * @returns True | False depending on if the user was removed or not. - */ - public async remove(username: string): Promise { - try { - await this.prisma.user.delete({ - where: { - username: username - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes an user from the database. + * + * @param username - Indicator which user should be removed + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(username: string): Promise { + await this.prisma.user.delete({ + where: { + username: username, + }, + }); + return true + } - /** - * Returns a list of all existing users. - * - * @returns `User[]` - List of all users. - */ - public async getAll(): Promise { - try { - return await this.prisma.user.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all existing users. + * + * @returns `User[]` - List of all users. + */ + public async getAll(): Promise { + return await this.prisma.user.findMany({}); + } - /** - * Looks up an user given by its username. - * - * @param username - Indicator which user should be searched for - * @returns User | null depending on if the user could be found. - */ - public async getByUsername(username: string): Promise { - try { - return await this.prisma.user.findUnique({ - where: { - username: username - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up an user given by its username. + * + * @param username - Indicator which user should be searched for + * @returns User | null depending on if the user could be found. + */ + public async getByUsername(username: string): Promise { + return await this.prisma.user.findUnique({ + where: { + username: username, + }, + }); + } } diff --git a/Server/src/services/db/vehicle.controller.ts b/Server/src/services/db/vehicle.controller.ts index 31300e80..a084a0ed 100644 --- a/Server/src/services/db/vehicle.controller.ts +++ b/Server/src/services/db/vehicle.controller.ts @@ -1,6 +1,4 @@ -import { PrismaClient, Prisma } from "@prisma/client" -import type { Vehicle, VehicleType } from "@prisma/client" -import { logger } from "../../utils/logger" +import { PrismaClient, Prisma, Vehicle, VehicleType } from "@prisma/client"; /** * VehicleController class @@ -23,280 +21,224 @@ import { logger } from "../../utils/logger" * - getByName() */ export default class VehicleController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - // ========================================================= // - // [Vehicle Types] + // ========================================================= // + // [Vehicle Types] - /** - * Saves a vehicle type in the database. - * - * @param name - **unique** Name for the type of vehicle. - * @param icon - unique icon name for visualization - * @param description - optional description for the type. - * @returns VehicleType | null if an error occurs - */ - public async saveType(name: string, icon: string, description?: string): Promise { - try { - return await this.prisma.vehicleType.create({ - data: { - name: name, - icon: icon, - description: description - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a vehicle type in the database. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: + * @param name - **unique** Name for the type of vehicle. + * @param icon - unique icon name for visualization + * @param description - optional description for the type. + * @returns VehicleType | null if an error occurs + */ + public async saveType( + args: Prisma.VehicleTypeCreateInput + ): Promise { + return await this.prisma.vehicleType.create({ + data: args, + }); + } - /** - * Updates a vehicle type in the database. - * - * @param uid - Indicator which vehicle type should be updated. - * @param name - New name of the vehicle type after change. (Optional) - * @param description - New description of the vehicle type after change. (Optional) - * @returns - */ - public async updateType( - uid: number, - name?: string, - icon?: string, - description?: string - ): Promise { - try { - return await this.prisma.vehicleType.update({ - where: { - uid: uid - }, - data: { - name: name, - icon: icon, - description: description - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updates a vehicle type in the database. + * + * @param uid - Indicator which vehicle type should be updated. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: + * @param name - New name of the vehicle type after change. (Optional) + * @param description - New description of the vehicle type after change. (Optional) + * @returns + */ + public async updateType( + uid: number, + args: Prisma.VehicleTypeUpdateInput + ): Promise { + return await this.prisma.vehicleType.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes a vehicle type in the database. - * - * @param uid - Indicator which vehicle type should be removed. - * @returns True | False depending on if the track was removed or not. - */ - public async removeType(uid: number): Promise { - try { - await this.prisma.vehicleType.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes a vehicle type in the database. + * + * @param uid - Indicator which vehicle type should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async removeType(uid: number): Promise { + await this.prisma.vehicleType.delete({ + where: { + uid: uid, + }, + }); + return true + } - /** - * Returns a list of all vehicle types. - * - * @returns VehicleType[] - List of all vehicle types. - */ - public async getAllTypes(): Promise { - try { - return await this.prisma.vehicleType.findMany({}) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all vehicle types. + * + * @returns VehicleType[] - List of all vehicle types. + */ + public async getAllTypes(): Promise { + return await this.prisma.vehicleType.findMany({}); + } - /** - * Looks up a vehicle type given by its uid. - * - * @param uid - Indicator which vehicle type should be searched for. - * @returns VehicleType | null depending on if the vehicle type could be found. - */ - public async getTypeById(uid: number): Promise { - try { - return await this.prisma.vehicleType.findUnique({ - where: { - uid: uid - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a vehicle type given by its uid. + * + * @param uid - Indicator which vehicle type should be searched for. + * @returns VehicleType | null depending on if the vehicle type could be found. + */ + public async getTypeById(uid: number): Promise { + return await this.prisma.vehicleType.findUnique({ + where: { + uid: uid, + }, + }); + } - /** - * Looks up a vehicle type by its name. - * - * @param uid - Indicator which vehicle type should be searched for. - * @returns VehicleType | null depending on if the vehicle type could be found. - */ - public async getTypeByName(name: string): Promise { - try { - return await this.prisma.vehicleType.findUnique({ - where: { - name: name - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a vehicle type by its name. + * + * @param uid - Indicator which vehicle type should be searched for. + * @returns VehicleType | null depending on if the vehicle type could be found. + */ + public async getTypeByName(name: string): Promise { + return await this.prisma.vehicleType.findUnique({ + where: { + name: name, + }, + }); + } - // ========================================================= // - // [Vehicles] + // ========================================================= // + // [Vehicles] - /** - * Saves a new vehicle in the database. - * - * @param typeId - VehicleType uid - * @param trackId - Track uid - * @param name - display name for the given vehicle (Optional) - * @returns Vehicle | null if an error occurs. - */ - public async save(typeId: number, trackId: number, name: string): Promise { - try { - return await this.prisma.vehicle.create({ - data: { - name: name, - typeId: typeId, - trackId: trackId - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Saves a new vehicle in the database. + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: + * @param typeId - VehicleType uid + * @param trackId - Track uid + * @param name - display name for the given vehicle (Optional) + * @returns Vehicle | null if an error occurs. + */ + public async save( + args: Prisma.VehicleUncheckedCreateInput + ): Promise { + // VehicleUncheckedCreateInput is used because of required relations + return await this.prisma.vehicle.create({ + data: args, + }); + } - /** - * Updadtes a vehicle in the database. - * - * @param uid - Indicator which vehicle should be updated - * @param typeId - New VehicleType.uid after change (Optional) - * @param trackId - New Track.uid after change (Optional) - * @param name - New display name after change (Optional) - * @returns Vehicle | null if an error occurs - */ - public async update(uid: number, typeId?: number, trackId?: number, name?: string): Promise { - try { - return await this.prisma.vehicle.update({ - where: { - uid: uid - }, - data: { - name: name, - typeId: typeId, - trackId: trackId - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Updadtes a vehicle in the database. + * + * @param uid - Indicator which vehicle should be updated + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: + * @param typeId - New VehicleType.uid after change (Optional) + * @param trackId - New Track.uid after change (Optional) + * @param name - New display name after change (Optional) + * @returns Vehicle | null if an error occurs + */ + public async update( + uid: number, + args: Prisma.VehicleUncheckedUpdateInput + ): Promise { + // VehicleUncheckCreateInput is used because of required relations + return await this.prisma.vehicle.update({ + where: { + uid: uid, + }, + data: args, + }); + } - /** - * Removes a vehicle in the database. - * - * @param uid - Indicator which vehicle should be removed. - * @returns True | False depending on if the vehicle was removed or not. - */ - public async remove(uid: number): Promise { - try { - await this.prisma.vehicle.delete({ - where: { - uid: uid - } - }) - return true - } catch (e) { - logger.debug(e) - return false - } - } + /** + * Removes a vehicle in the database. + * + * @param uid - Indicator which vehicle should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: number): Promise { + await this.prisma.vehicle.delete({ + where: { + uid: uid, + }, + }); + return true + } - /** - * Returns a list of all vehicles. - * - * @param trackId - Track.uid for filtering list (Optional) - * - * @returns Vehicle[] - */ - public async getAll(trackId?: number): Promise { - try { - return await this.prisma.vehicle.findMany({ - where: { - trackId: trackId - }, - include: { - type: true, - track: true - } - }) - } catch (e) { - logger.debug(e) - return [] - } - } + /** + * Returns a list of all vehicles. + * + * @param trackId - Track.uid for filtering list (Optional) + * + * @returns Vehicle[] + */ + public async getAll(trackId?: number): Promise { + return await this.prisma.vehicle.findMany({ + where: { + trackId: trackId, + }, + include: { + type: true, + track: true, + }, + }); + } - /** - * Looks up a vehicle by its uid. - * - * @param uid - Indicator which vehicle should be looked for. - * @returns Vehicle | null depending on if the vehicle could be found. - */ - public async getById(uid: number): Promise { - try { - return await this.prisma.vehicle.findUnique({ - where: { - uid: uid - }, - include: { - type: true, - track: true - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a vehicle by its uid. + * + * @param uid - Indicator which vehicle should be looked for. + * @returns Vehicle | null depending on if the vehicle could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.vehicle.findUnique({ + where: { + uid: uid, + }, + include: { + type: true, + track: true, + }, + }); + } - /** - * Looks up a vehicle by its name. - * - * @param name - Indicator which vehicle should be looked for. - * @returns Vehicle | null depending on if the vehicle could be found. - */ - public async getByName(name: string, trackId: number): Promise { - try { - return await this.prisma.vehicle.findUnique({ - where: { - name_trackId: { - name: name, - trackId: trackId - } - }, - include: { - type: true, - track: true - } - }) - } catch (e) { - logger.debug(e) - return null - } - } + /** + * Looks up a vehicle by its name. + * + * @param name - Indicator which vehicle should be looked for. + * @returns Vehicle | null depending on if the vehicle could be found. + */ + public async getByName( + name: string, + trackId: number + ): Promise { + return await this.prisma.vehicle.findUnique({ + where: { + name_trackId: { + name: name, + trackId: trackId, + }, + }, + include: { + type: true, + track: true, + }, + }); + } } From ff589f77a43dd0893c8560d303a8cf8f51585d37 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:12:06 +0200 Subject: [PATCH 13/19] Adjust limit to undefined --- Server/src/services/db/log.controller.ts | 270 +++++++++++------------ 1 file changed, 132 insertions(+), 138 deletions(-) diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index 517d0143..684d081a 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -1,4 +1,4 @@ -import { Log, PrismaClient, Prisma } from "@prisma/client"; +import { Log, PrismaClient, Prisma } from "@prisma/client" /** * LogController class @@ -13,149 +13,143 @@ import { Log, PrismaClient, Prisma } from "@prisma/client"; * - getLog() */ export default class LogController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - // ========================================================= // - // [Tracker Logs] + // ========================================================= // + // [Tracker Logs] - /** - * Saves a new log in the database. - * - * The parameter are given via object deconstruction from the model `Log`! - * Currently given parameters are: - * @param timestamp - Time of log. - * @param vehicleId - Vehicle.uid which is associated with this log. - * @param position - Current GPS position at the time of the creation of the log. - * @param heading - Current GPS heading at the time of the creation of the log. - * @param speed - Current speed at the time of the creation of the log. - * @param battery - Current battery charge at the time of the creation of the log. (Optional for the case of app data) - * @param trackerId - Tracker.uid which caused the log. (Optional for the case of app data) - * @param data - optional addtional data field. - * @returns Log | null if an error occurs. - */ - public async save(args: Prisma.LogUncheckedCreateInput): Promise { - //LogUncheckCreateInput is used because of required relations with other models! - return await this.prisma.log.create({ - data: args, - }); - } + /** + * Saves a new log in the database. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: + * @param timestamp - Time of log. + * @param vehicleId - Vehicle.uid which is associated with this log. + * @param position - Current GPS position at the time of the creation of the log. + * @param heading - Current GPS heading at the time of the creation of the log. + * @param speed - Current speed at the time of the creation of the log. + * @param battery - Current battery charge at the time of the creation of the log. (Optional for the case of app data) + * @param trackerId - Tracker.uid which caused the log. (Optional for the case of app data) + * @param data - optional addtional data field. + * @returns Log | null if an error occurs. + */ + public async save(args: Prisma.LogUncheckedCreateInput): Promise { + //LogUncheckCreateInput is used because of required relations with other models! + return await this.prisma.log.create({ + data: args + }) + } - /** - * Updates a Log entry. - * - * @param uid - Indicator for specific log. - * - * The parameter are given via object deconstruction from the model `Log`! - * Currently given parameters are: - * @param timestamp - Time when the log was created. - * @param position - gps position of the tracker/app. - * @param heading - degree in which the tracker/app was pointing. - * @param speed - current speed of tracker/app. - * @param battery - current battery of tracker. - * @param data - GPS Data. - * @param vehicleId - which vehicle is connected with said tracker/app. For tracker this can be found in the tracker model. - * @param trackerId - identifier for said tracker. For app data this field is always `null` - * @returns Log | null if an error occurs. - */ - public async update( - uid: number, - args: Prisma.LogUpdateInput - ): Promise { - return await this.prisma.log.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updates a Log entry. + * + * @param uid - Indicator for specific log. + * + * The parameter are given via object deconstruction from the model `Log`! + * Currently given parameters are: + * @param timestamp - Time when the log was created. + * @param position - gps position of the tracker/app. + * @param heading - degree in which the tracker/app was pointing. + * @param speed - current speed of tracker/app. + * @param battery - current battery of tracker. + * @param data - GPS Data. + * @param vehicleId - which vehicle is connected with said tracker/app. For tracker this can be found in the tracker model. + * @param trackerId - identifier for said tracker. For app data this field is always `null` + * @returns Log | null if an error occurs. + */ + public async update(uid: number, args: Prisma.LogUpdateInput): Promise { + return await this.prisma.log.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * 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 { - await this.prisma.log.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * 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 { + 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 10. - * @param vehicleId - Vehicle to filter for (Optional) - * @param trackerId - Tracker to filter for (Optional) - * @returns Log[] - List of all logs - */ - public async getAll(limit : number = 10, vehicleId?: number, trackerId?: string): Promise { - return await this.prisma.log.findMany({ - where: { - vehicleId: vehicleId, - trackerId: trackerId, - }, - orderBy: [ - { - timestamp: "desc", - }, - ], - take : limit - }); - } + /** + * 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 10. + * @param vehicleId - Vehicle to filter for (Optional) + * @param trackerId - Tracker to filter for (Optional) + * @returns Log[] - List of all logs + */ + public async getAll(vehicleId?: number, trackerId?: string, limit?: number): Promise { + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + trackerId: trackerId + }, + orderBy: [ + { + timestamp: "desc" + } + ], + take: limit + }) + } - /** - * Looks up a specific log in the database. - * - * @param uid - Indicator for log - * - * @returns Log | null depending on if the log could be found. - */ - public async getLog(uid: number): Promise { - return await this.prisma.log.findUnique({ - where: { - uid: uid, - }, - include: { - vehicle: true, - tracker: true, - }, - }); - } + /** + * Looks up a specific log in the database. + * + * @param uid - Indicator for log + * + * @returns Log | null depending on if the log could be found. + */ + public async getLog(uid: number): Promise { + return await this.prisma.log.findUnique({ + where: { + uid: uid + }, + include: { + vehicle: true, + tracker: true + } + }) + } - /** - * Returns a list of the newest logs for an vehicle. - * - * - * @param vehicleId - Indicator which vehicle's logs should be considered. - * @param max_sec - How old the logs can be at max. Default: 5 min - * - * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. - */ - public async getNewestLogs( - vehicleId: number, - max_sec: number = 300 - ): Promise { - // Earliest date which should be considered - let max_date = new Date(Date.now() - max_sec * 1000); + /** + * Returns a list of the newest logs for an vehicle. + * + * + * @param vehicleId - Indicator which vehicle's logs should be considered. + * @param max_sec - How old the logs can be at max. Default: 5 min + * + * @returns Log[] - list of logs for said vehicleId from now until max_sec ago. + */ + public async getNewestLogs(vehicleId: number, max_sec: number = 300): Promise { + // Earliest date which should be considered + let max_date = new Date(Date.now() - max_sec * 1000) - return await this.prisma.log.findMany({ - where: { - vehicleId: vehicleId, - timestamp: { - gt: max_date, - }, - }, - orderBy: [ - { - timestamp: "desc", - }, - ], - }); - } + return await this.prisma.log.findMany({ + where: { + vehicleId: vehicleId, + timestamp: { + gt: max_date + } + }, + orderBy: [ + { + timestamp: "desc" + } + ] + }) + } } From 896c1ba7f1ddbf3e332c8e82797f456d2d547375 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:12:21 +0200 Subject: [PATCH 14/19] Fix typos --- Server/src/services/db/tracker.controller.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Server/src/services/db/tracker.controller.ts b/Server/src/services/db/tracker.controller.ts index 987584e3..b6b7b75b 100644 --- a/Server/src/services/db/tracker.controller.ts +++ b/Server/src/services/db/tracker.controller.ts @@ -25,7 +25,8 @@ export default class TrackerController { * @param data - optional additional data field. * @returns Tracker */ - public async save(args: Prisma.TrackerCreateInput): Promise { + public async save(args: Prisma.TrackerUncheckedCreateInput): Promise { + //TrackerUncheckedCreateInput is used because of the relation to vehicles. return await this.prisma.tracker.create({ data: args, }); @@ -46,7 +47,7 @@ export default class TrackerController { uid: string, args: Prisma.TrackerUncheckedUpdateInput ): Promise { - //TrackerUncheckedUpdateInput is used + //TrackerUncheckedUpdateInput is used because of the relation to vehicles. return await this.prisma.tracker.update({ where: { uid: uid, From e5513ee4ee4239c5ee7768cf03f6e272fcb92549 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:12:34 +0200 Subject: [PATCH 15/19] Adjust database calls for obj deconstruction --- Server/src/routes/poi.route.ts | 20 +++++++----- Server/src/routes/poitype.route.ts | 4 +-- Server/src/routes/track.route.ts | 10 +++--- Server/src/routes/tracker.route.ts | 13 ++++++-- Server/src/routes/vehicletypes.route.ts | 16 ++++++---- Server/src/services/login.service.ts | 2 +- Server/src/services/poi.service.ts | 42 ++++++++++++++++++------- Server/src/services/track.service.ts | 26 ++++++++++----- Server/src/services/tracker.service.ts | 19 ++++++++--- Server/src/services/user.service.ts | 8 +++-- Server/src/services/vehicle.service.ts | 16 +++++----- 11 files changed, 116 insertions(+), 60 deletions(-) diff --git a/Server/src/routes/poi.route.ts b/Server/src/routes/poi.route.ts index e016a781..5e28b068 100644 --- a/Server/src/routes/poi.route.ts +++ b/Server/src/routes/poi.route.ts @@ -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" @@ -192,15 +192,19 @@ export class PoiRoute { }, properties: null } - + + // 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, - userData.name, - userData.description, - userData.typeId, - userData.trackId, - geopos, - userData.isTurningPoint + { + name: userData.name, + description: userData.description, + position: (geopos as unknown as Prisma.InputJsonValue), + isTurningPoint: userData.isTurningPoint, + typeId: userData.typeId, + trackId: userData.trackId, + } ) if (!updatedPOI) { diff --git a/Server/src/routes/poitype.route.ts b/Server/src/routes/poitype.route.ts index 10cb4ef9..da41ba2c 100644 --- a/Server/src/routes/poitype.route.ts +++ b/Server/src/routes/poitype.route.ts @@ -83,7 +83,7 @@ export class PoiTypeRoute { private async createType(req: Request, res: Response): Promise { 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}) if (!poiType) { logger.error("Could not create poi type") res.sendStatus(500) @@ -115,7 +115,7 @@ 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) diff --git a/Server/src/routes/track.route.ts b/Server/src/routes/track.route.ts index 541a6430..586d7053 100644 --- a/Server/src/routes/track.route.ts +++ b/Server/src/routes/track.route.ts @@ -219,10 +219,12 @@ export class TrackRoute { // get the current position of the vehicle const geo_pos = await VehicleService.getVehiclePosition(vehicle) // If we know that, convert it in the API format. - const pos: Position | undefined = geo_pos ? { - lat: GeoJSONUtils.getLatitude(geo_pos), - lng: GeoJSONUtils.getLongitude(geo_pos) - } : undefined + const pos: Position | undefined = geo_pos + ? { + lat: GeoJSONUtils.getLatitude(geo_pos), + lng: GeoJSONUtils.getLongitude(geo_pos) + } + : undefined // Also acquire the percentage position. It might happen that a percentage position is known, while the position is not. // This might not make much sense. const percentagePosition = (await VehicleService.getVehicleTrackDistancePercentage(vehicle, track)) ?? undefined diff --git a/Server/src/routes/tracker.route.ts b/Server/src/routes/tracker.route.ts index 20fc4899..dbdf65b0 100644 --- a/Server/src/routes/tracker.route.ts +++ b/Server/src/routes/tracker.route.ts @@ -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" @@ -83,7 +83,11 @@ export class TrackerRoute { private async createTracker(req: Request, res: Response): Promise { 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) @@ -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) diff --git a/Server/src/routes/vehicletypes.route.ts b/Server/src/routes/vehicletypes.route.ts index a7e73856..339e3f20 100644 --- a/Server/src/routes/vehicletypes.route.ts +++ b/Server/src/routes/vehicletypes.route.ts @@ -124,11 +124,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 @@ -194,7 +194,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: diff --git a/Server/src/services/login.service.ts b/Server/src/services/login.service.ts index 5fe83263..79f65c53 100644 --- a/Server/src/services/login.service.ts +++ b/Server/src/services/login.service.ts @@ -54,7 +54,7 @@ export default class LoginService { if (!hashed_pass) { return } - const createdUser: User | null = await database.users.save(auth.username, hashed_pass) + const createdUser: User | null = await database.users.save({ username: auth.username, password: hashed_pass }) if (!createdUser) { return } diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index 9e8eaf27..dbd40183 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -1,4 +1,4 @@ -import { POI, POIType, Track, Vehicle } from ".prisma/client" +import { POI, POIType, Prisma, Track, Vehicle } from ".prisma/client" import database from "./database.service" import TrackService from "./track.service" import VehicleService from "./vehicle.service" @@ -44,7 +44,16 @@ export default class POIService { return null } - return database.pois.save(name, type.uid, track.uid, enrichedPoint, description, isTurningPoint) + // Note: geopos is from type GeoJSON.Feature and can't be parsed directly into Prisma.InputJsonValue + // Therefore we cast it into unknown first. + return database.pois.save({ + name, + typeId: type.uid, + trackId: track.uid, + position: enrichedPoint as unknown as Prisma.InputJsonValue, + description: description, + isTurningPoint: isTurningPoint ?? false + }) } /** @@ -327,7 +336,10 @@ export default class POIService { if (enrichedPoint == null) { return null } - return database.pois.update(poi.uid, undefined, undefined, undefined, undefined, enrichedPoint) + + // Note: Based on Feature it is not possible to cast to Prisma.InputJsonValue directly + // Therefore we cast it into unknown first. (Also recommended by Prisma itself) + return database.pois.update(poi.uid, { position: enrichedPoint as unknown as Prisma.InputJsonValue }) } /** @@ -337,7 +349,7 @@ export default class POIService { * @returns renamed `POI` if successful, `null` otherwise */ public static async renamePOI(poi: POI, newName: string): Promise { - return database.pois.update(poi.uid, newName) + return database.pois.update(poi.uid, { name: newName }) } /** @@ -347,7 +359,7 @@ export default class POIService { * @returns updated `POI` if successful, `null` otherwise */ public static async updateDescription(poi: POI, newDesc: string): Promise { - return database.pois.update(poi.uid, undefined, newDesc) + return database.pois.update(poi.uid, { description: newDesc }) } /** @@ -357,7 +369,7 @@ export default class POIService { * @returns updated `POI` if successful, `null` otherwise */ public static async setPOIType(poi: POI, type: POIType): Promise { - return database.pois.update(poi.uid, undefined, undefined, type.uid) + return database.pois.update(poi.uid, { typeId: type.uid }) } /** @@ -379,7 +391,13 @@ export default class POIService { } // update poi's track and track kilometer - return database.pois.update(poi.uid, undefined, undefined, undefined, track.uid, updatedPOIPos) + + // Note: Based on Feature it is not possible to cast to Prisma.InputJsonValue directly + // Therefore we cast it into unknown first. (Also recommended by Prisma itself) + return database.pois.update(poi.uid, { + trackId: track.uid, + position: updatedPOIPos as unknown as Prisma.InputJsonValue + }) } /** @@ -389,7 +407,7 @@ export default class POIService { * @returns updated `POI` if successful, `null` otherwise */ public static async setTurningPoint(poi: POI, isTurningPoint: boolean): Promise { - return database.pois.update(poi.uid, undefined, undefined, undefined, undefined, undefined, isTurningPoint) + return database.pois.update(poi.uid, { isTurningPoint }) } /** @@ -411,7 +429,7 @@ export default class POIService { * @returns created `POIType` if successful, `null` otherwise */ public static async createPOIType(type: string, icon: string, desc?: string): Promise { - return database.pois.saveType(type, icon, desc) + return database.pois.saveType({ name: type, icon, description: desc }) } /** @@ -438,7 +456,7 @@ export default class POIService { * @returns renamed `POIType` if successful, `null` otherwise */ public static async renamePOIType(type: POIType, newType: string): Promise { - return database.pois.updateType(type.uid, newType) + return database.pois.updateType(type.uid, { name: newType }) } /** @@ -448,7 +466,7 @@ export default class POIService { * @returns updated `POIType` if successful, `null` otherwise */ public static async setPOITypeDescription(type: POIType, desc: string): Promise { - return database.pois.updateType(type.uid, undefined, undefined, desc) + return database.pois.updateType(type.uid, { description: desc }) } /** @@ -458,7 +476,7 @@ export default class POIService { * @returns updated `POI` if successful, `null` otherwise */ public static async setPOITypeIcon(type: POIType, icon: string): Promise { - return database.pois.updateType(type.uid, undefined, icon) + return database.pois.updateType(type.uid, { icon }) } /** diff --git a/Server/src/services/track.service.ts b/Server/src/services/track.service.ts index 25ae6ac1..66c69a3a 100644 --- a/Server/src/services/track.service.ts +++ b/Server/src/services/track.service.ts @@ -1,4 +1,4 @@ -import { Track } from ".prisma/client" +import { Prisma, Track } from ".prisma/client" import database from "./database.service" import GeoJSONUtils from "../utils/geojsonUtils" @@ -25,8 +25,10 @@ export default class TrackService { dest: string ): Promise { const enrichedTrack = this.enrichTrackData(track) - // typecast to any, because JSON is expected - return database.tracks.save(start, dest, enrichedTrack) + + // Note: Based on FeatureCollection it is not possible to cast to Prisma.InputJsonValue directly + // Therefore we cast it into unknown first. (Also recommended by Prisma itself) + return database.tracks.save({ start, stop: dest, data: enrichedTrack as unknown as Prisma.InputJsonValue }) } /** @@ -45,7 +47,13 @@ export default class TrackService { ): Promise { const enrichedTrack = this.enrichTrackData(path) - return database.tracks.update(track.uid, start, dest, enrichedTrack) + // Note: Based on FeatureCollection it is not possible to cast to Prisma.InputJsonValue directly + // Therefore we cast it into unknown first. (Also recommended by Prisma itself) + return database.tracks.update(track.uid, { + start, + stop: dest, + data: enrichedTrack as unknown as Prisma.InputJsonValue + }) } /** @@ -240,8 +248,10 @@ export default class TrackService { path: GeoJSON.FeatureCollection ): Promise { const enrichedTrack = await this.enrichTrackData(path) - // typecast to any, because JSON is expected - return database.tracks.update(track.uid, undefined, undefined, enrichedTrack) + + // Note: Based on FeatureCollection it is not possible to cast to Prisma.InputJsonValue directly + // Therefore we cast it into unknown first. (Also recommended by Prisma itself) + return database.tracks.update(track.uid, { data: enrichedTrack as unknown as Prisma.InputJsonValue }) } /** @@ -251,7 +261,7 @@ export default class TrackService { * @returns updated `Track` if successful, `null` otherwise */ public static async setStart(track: Track, newStart: string): Promise { - return database.tracks.update(track.uid, newStart) + return database.tracks.update(track.uid, { start: newStart }) } /** @@ -261,7 +271,7 @@ export default class TrackService { * @returns updated `Track` if successful, `null` otherwise */ public static async setDestination(track: Track, newDest: string): Promise { - return database.tracks.update(track.uid, undefined, newDest) + return database.tracks.update(track.uid, { stop: newDest }) } /** diff --git a/Server/src/services/tracker.service.ts b/Server/src/services/tracker.service.ts index 4b30a8cb..6671c922 100644 --- a/Server/src/services/tracker.service.ts +++ b/Server/src/services/tracker.service.ts @@ -1,5 +1,5 @@ import { logger } from "../utils/logger" -import { Log, Tracker, Vehicle } from "@prisma/client" +import { Log, Prisma, Tracker, Vehicle } from "@prisma/client" import VehicleService from "./vehicle.service" import database from "./database.service" @@ -16,7 +16,7 @@ export default class TrackerService { public static async registerTracker(trackerId: string, data?: any): Promise { const tracker = await this.getTrackerById(trackerId) if (tracker == null) { - return await database.trackers.save(trackerId, null, data) + return await database.trackers.save({ uid: trackerId, data, vehicleId: null }) } else { return tracker } @@ -47,7 +47,7 @@ export default class TrackerService { * @returns `Tracker` that got assigned to a `Vehicle` if successful, `null` otherwise */ public static async setVehicle(tracker: Tracker, vehicle: Vehicle): Promise { - return database.trackers.update(tracker.uid, vehicle.uid) + return database.trackers.update(tracker.uid, { vehicleId: vehicle.uid }) } /** @@ -85,10 +85,19 @@ export default class TrackerService { ): Promise { // if no tracker id is given, the fields for battery and other data should be ignored if (trackerId == null) { - return database.logs.save(timestamp, vehicle.uid, position, heading, speed) + return database.logs.save({ timestamp, vehicleId: vehicle.uid, position, heading, speed }) } - return database.logs.save(timestamp, vehicle.uid, position, heading, speed, battery, data, trackerId) + return database.logs.save({ + timestamp, + vehicleId: vehicle.uid, + position, + heading, + speed, + battery, + data: data as Prisma.InputJsonValue, + trackerId + }) } /** diff --git a/Server/src/services/user.service.ts b/Server/src/services/user.service.ts index 0d525dc2..4c9e0a55 100644 --- a/Server/src/services/user.service.ts +++ b/Server/src/services/user.service.ts @@ -28,7 +28,7 @@ export default class UserService { logger.error(`Password could not be hashed`) return null } - const addedUser: User | null = await database.users.save(name, hashed_pass) + const addedUser: User | null = await database.users.save({ username: name, password: hashed_pass }) logger.info(`User ${name} was successfully added`) return addedUser } @@ -53,7 +53,7 @@ export default class UserService { logger.error("Hashing of password was not successful") return false } - const successfulUpdate: User | null = await database.users.update(user.username, undefined, hashedPassword) + const successfulUpdate: User | null = await database.users.update(user.username, { password: hashedPassword }) if (successfulUpdate) { logger.info(`Updated password of user ${username}`) } else { @@ -83,7 +83,9 @@ export default class UserService { return false } - const successfulUpdate: User | null = await database.users.update(user.username, usernameChangeRequest.newUsername) + const successfulUpdate: User | null = await database.users.update(user.username, { + username: usernameChangeRequest.newUsername + }) if (!successfulUpdate) { logger.error(`Updating username of user ${usernameChangeRequest.oldUsername} to new username ${usernameChangeRequest.newUsername} failed`) diff --git a/Server/src/services/vehicle.service.ts b/Server/src/services/vehicle.service.ts index a2d53a10..6a285d6d 100644 --- a/Server/src/services/vehicle.service.ts +++ b/Server/src/services/vehicle.service.ts @@ -21,7 +21,7 @@ export default class VehicleService { * @returns created `Vehicle` if successful, `null` otherwise */ public static async createVehicle(type: VehicleType, track_uid: number, name: string): Promise { - return database.vehicles.save(type.uid, track_uid, name) + return database.vehicles.save({ name, typeId: type.uid, trackId: track_uid }) } /** @@ -440,7 +440,7 @@ export default class VehicleService { return 0 } if (point0TrackKm > point1TrackKm) { - [trackPoint0, trackPoint1] = [trackPoint1, trackPoint0] + ;[trackPoint0, trackPoint1] = [trackPoint1, trackPoint0] } // get bearing of track segment (and adjust it for our format 0-359) @@ -478,7 +478,7 @@ export default class VehicleService { * @returns renamed `Vehicle` if successful, `null` otherwise */ public static async renameVehicle(vehicle: Vehicle, newName: string): Promise { - return database.vehicles.update(vehicle.uid, undefined, undefined, newName) + return database.vehicles.update(vehicle.uid, { name: newName }) } /** @@ -488,7 +488,7 @@ export default class VehicleService { * @returns updated `Vehicle` if successful, `null` otherwise */ public static async setVehicleType(vehicle: Vehicle, type: VehicleType): Promise { - return database.vehicles.update(vehicle.uid, type.uid) + return database.vehicles.update(vehicle.uid, { typeId: type.uid }) } /** @@ -520,7 +520,7 @@ export default class VehicleService { * @returns created `VehicleType` if successful, `null` otherwise */ public static async createVehicleType(type: string, icon: string, desc?: string): Promise { - return database.vehicles.saveType(type, icon, desc) + return database.vehicles.saveType({ name: type, icon, description: desc }) } /** @@ -547,7 +547,7 @@ export default class VehicleService { * @returns updated `VehicleType` if successful, `null` otherwise */ public static async renameVehicleType(type: VehicleType, newType: string): Promise { - return database.vehicles.updateType(type.uid, newType) + return database.vehicles.updateType(type.uid, { name: newType }) } /** @@ -557,7 +557,7 @@ export default class VehicleService { * @returns updated `VehicleType` if successful, `null` otherwise */ public static async setVehicleTypeDescription(type: VehicleType, desc: string): Promise { - return database.vehicles.updateType(type.uid, undefined, undefined, desc) + return database.vehicles.updateType(type.uid, { description: desc }) } /** @@ -567,7 +567,7 @@ export default class VehicleService { * @returns updated `VehicleType` if successful, `null` otherwise */ public static async setVehicleTypeIcon(type: VehicleType, icon: string): Promise { - return database.vehicles.updateType(type.uid, undefined, icon) + return database.vehicles.updateType(type.uid, { icon }) } /** From d7d46cb8d91aa870e5437e9ec9ad1b86c14f316a Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Wed, 23 Aug 2023 14:37:52 +0200 Subject: [PATCH 16/19] Fix comment & icon uniqueness --- Server/prisma/schema.prisma | 4 ++-- Server/src/services/db/log.controller.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Server/prisma/schema.prisma b/Server/prisma/schema.prisma index 1a3b6837..78961db4 100644 --- a/Server/prisma/schema.prisma +++ b/Server/prisma/schema.prisma @@ -21,7 +21,7 @@ model User { model POIType { uid Int @id @default(autoincrement()) name String @unique - icon String @unique + icon String description String? poi POI[] @@ -53,7 +53,7 @@ model Track { model VehicleType { uid Int @id @default(autoincrement()) name String @unique - icon String @unique + icon String description String? vehicle Vehicle[] diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index 684d081a..9eaea17b 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -86,7 +86,7 @@ export default class LogController { * 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 10. + * @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 From 1fe76b782d2f19ecc4cf4e19f624803909bd5c07 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Thu, 31 Aug 2023 15:37:47 +0200 Subject: [PATCH 17/19] Fixed merge conflicts / changes --- Server/src/routes/poi.route.ts | 2 +- Server/src/routes/vehicle.route.ts | 10 +++++----- Server/src/services/poi.service.ts | 2 +- Server/src/services/tracker.service.ts | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Server/src/routes/poi.route.ts b/Server/src/routes/poi.route.ts index 098ae0e1..f11714fa 100644 --- a/Server/src/routes/poi.route.ts +++ b/Server/src/routes/poi.route.ts @@ -209,7 +209,7 @@ export class PoiRoute { position: (enrichedPoint as unknown as Prisma.InputJsonValue), isTurningPoint: userData.isTurningPoint, typeId: userData.typeId, - trackId: track.uid + trackId: track!.uid } ) diff --git a/Server/src/routes/vehicle.route.ts b/Server/src/routes/vehicle.route.ts index df599dc0..3ea819b5 100644 --- a/Server/src/routes/vehicle.route.ts +++ b/Server/src/routes/vehicle.route.ts @@ -133,7 +133,7 @@ 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) @@ -141,7 +141,7 @@ export class VehicleRoute { } 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) @@ -216,7 +216,7 @@ 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) @@ -224,11 +224,11 @@ export class VehicleRoute { } 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) diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index ebdf0fda..a1723fa6 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -134,7 +134,7 @@ export default class POIService { return null } // try to update the poi in the database, now that we have enriched it - if ((await database.pois.update(poi.uid, undefined, undefined, undefined, undefined, enrichedPos)) == null) { + if ((await database.pois.update(poi.uid, {position: enrichedPos as unknown as Prisma.InputJsonValue})) == null) { logger.info(`Could not update POI with id ${poi.uid} after enriching it.`) } diff --git a/Server/src/services/tracker.service.ts b/Server/src/services/tracker.service.ts index 464fe2be..4d326231 100644 --- a/Server/src/services/tracker.service.ts +++ b/Server/src/services/tracker.service.ts @@ -16,7 +16,7 @@ export default class TrackerService { public static async registerTracker(trackerId: string, data?: unknown): Promise { const tracker = await this.getTrackerById(trackerId) if (tracker == null) { - return await database.trackers.save({ uid: trackerId, data, vehicleId: null }) + return await database.trackers.save({ uid: trackerId, data: data as Prisma.InputJsonValue, vehicleId: null }) } else { return tracker } From decda6d61535d29dcee2a4f9d30e0266764635c3 Mon Sep 17 00:00:00 2001 From: Niatsuna Date: Thu, 31 Aug 2023 16:11:03 +0200 Subject: [PATCH 18/19] Fixed format mishaps --- Server/src/routes/poi.route.ts | 21 +- Server/src/routes/poitype.route.ts | 8 +- Server/src/routes/vehicle.route.ts | 18 +- Server/src/services/db/poi.controller.ts | 385 +++++++++--------- Server/src/services/db/track.controller.ts | 203 +++++----- Server/src/services/db/tracker.controller.ts | 175 ++++---- Server/src/services/db/user.controller.ts | 143 ++++--- Server/src/services/db/vehicle.controller.ts | 401 +++++++++---------- Server/src/services/poi.service.ts | 4 +- 9 files changed, 672 insertions(+), 686 deletions(-) diff --git a/Server/src/routes/poi.route.ts b/Server/src/routes/poi.route.ts index f11714fa..88b72044 100644 --- a/Server/src/routes/poi.route.ts +++ b/Server/src/routes/poi.route.ts @@ -194,24 +194,21 @@ export class PoiRoute { }, properties: null } - + const track = (await database.tracks.getById(userData.trackId)) ?? undefined const enrichedPoint = (await POIService.enrichPOIPosition(geopos, track)) ?? undefined // 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 - } - ) + 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}`) diff --git a/Server/src/routes/poitype.route.ts b/Server/src/routes/poitype.route.ts index 37ae19cd..7f6182eb 100644 --- a/Server/src/routes/poitype.route.ts +++ b/Server/src/routes/poitype.route.ts @@ -84,7 +84,7 @@ export class PoiTypeRoute { private async createType(req: Request, res: Response): Promise { 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 }) if (!poiType) { logger.error("Could not create poi type") res.sendStatus(500) @@ -116,7 +116,11 @@ export class PoiTypeRoute { return } - type = await database.pois.updateType(typeId, {name: userData.name, icon: userData.icon, description: 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) diff --git a/Server/src/routes/vehicle.route.ts b/Server/src/routes/vehicle.route.ts index 3ea819b5..bf288b4f 100644 --- a/Server/src/routes/vehicle.route.ts +++ b/Server/src/routes/vehicle.route.ts @@ -133,7 +133,11 @@ export class VehicleRoute { trackers.push(maybeTracker) } - const vehicle: Vehicle | null = await database.vehicles.save({ name: userData.name, typeId: type.uid, trackId: userData.track }) + 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) @@ -141,7 +145,7 @@ export class VehicleRoute { } for (const tracker of trackers) { - const updatedTracker = await database.trackers.update(tracker.uid, {vehicleId: 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) @@ -216,7 +220,11 @@ export class VehicleRoute { trackers.push(tracker) } - const vehicle = await database.vehicles.update(vehicleId, {typeId: type.uid, trackId: userData.track, name: 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) @@ -224,11 +232,11 @@ export class VehicleRoute { } for (const tracker of prevTrackers) { - database.trackers.update(tracker.uid, {vehicleId: null}) + database.trackers.update(tracker.uid, { vehicleId: null }) } for (const tracker of trackers) { - const trackerToUpdate: Tracker | null = await database.trackers.update(tracker.uid, {vehicleId: 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) diff --git a/Server/src/services/db/poi.controller.ts b/Server/src/services/db/poi.controller.ts index 71f3748d..ce93ad65 100644 --- a/Server/src/services/db/poi.controller.ts +++ b/Server/src/services/db/poi.controller.ts @@ -1,4 +1,4 @@ -import { POI, POIType, PrismaClient, Prisma } from "@prisma/client"; +import { POI, POIType, PrismaClient, Prisma } from "@prisma/client" /** * POIController class @@ -23,211 +23,208 @@ import { POI, POIType, PrismaClient, Prisma } from "@prisma/client"; * */ export default class POIController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - // ========================================================= // - // [POI Types] + // ========================================================= // + // [POI Types] - /** - * Saves a type for POIs in the database. - * - * The parameter are given via object deconstruction from the model `POIType`! - * Currently given parameters are: - * @param name - **unique** name of the type of poi. - * @param icon - unique icon name for visualization - * @param description - an optional description for the type of poi. - * @returns POIType - */ - public async saveType(args: Prisma.POITypeCreateInput): Promise { - return await this.prisma.pOIType.create({ - data: args, - }); - } + /** + * Saves a type for POIs in the database. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: + * @param name - **unique** name of the type of poi. + * @param icon - unique icon name for visualization + * @param description - an optional description for the type of poi. + * @returns POIType + */ + public async saveType(args: Prisma.POITypeCreateInput): Promise { + return await this.prisma.pOIType.create({ + data: args + }) + } - /** - * Updates a type of poi in the database. - * - * @param uid - Indicator which type should be updated. - * - * The parameter are given via object deconstruction from the model `POIType`! - * Currently given parameters are: - * @param name - New name after change. (Optional) - * @param icon - New unique icon name for visualization after change. (Optional) - * @param description - New description after change. (Optional) - * @returns POIType | null if an error occurs. - */ - public async updateType( - uid: number, - args: Prisma.POITypeUpdateInput - ): Promise { - return await this.prisma.pOIType.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updates a type of poi in the database. + * + * @param uid - Indicator which type should be updated. + * + * The parameter are given via object deconstruction from the model `POIType`! + * Currently given parameters are: + * @param name - New name after change. (Optional) + * @param icon - New unique icon name for visualization after change. (Optional) + * @param description - New description after change. (Optional) + * @returns POIType | null if an error occurs. + */ + public async updateType(uid: number, args: Prisma.POITypeUpdateInput): Promise { + return await this.prisma.pOIType.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * Removes a poi type from the database. - * - * @param uid - Indicator which type should be removed. - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async removeType(uid: number): Promise { - await this.prisma.pOIType.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * Removes a poi type from the database. + * + * @param uid - Indicator which type should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async removeType(uid: number): Promise { + await this.prisma.pOIType.delete({ + where: { + uid: uid + } + }) + return true + } - /** - * Returns a list of all existing types of poi. - * - * @returns `POIType[]` - List of all types of poi. - */ - public async getAllTypes(): Promise { - return await this.prisma.pOIType.findMany({}); - } + /** + * Returns a list of all existing types of poi. + * + * @returns `POIType[]` - List of all types of poi. + */ + public async getAllTypes(): Promise { + return await this.prisma.pOIType.findMany({}) + } - /** - * Looks up a type given by its uid. - * - * @param uid - Indicator which type should be searched for. - * @returns POIType | null depending on if the type could be found. - */ - public async getTypeById(uid: number): Promise { - return await this.prisma.pOIType.findUnique({ - where: { - uid: uid, - }, - }); - } + /** + * Looks up a type given by its uid. + * + * @param uid - Indicator which type should be searched for. + * @returns POIType | null depending on if the type could be found. + */ + public async getTypeById(uid: number): Promise { + return await this.prisma.pOIType.findUnique({ + where: { + uid: uid + } + }) + } - /** - * Looks up a type given by its name. - * - * @param name - Indicator which type should be searched for. - * @returns POIType | null depending on if the type could be found. - */ - public async getTypeByName(name: string): Promise { - return await this.prisma.pOIType.findUnique({ - where: { - name: name, - }, - }); - } + /** + * Looks up a type given by its name. + * + * @param name - Indicator which type should be searched for. + * @returns POIType | null depending on if the type could be found. + */ + public async getTypeByName(name: string): Promise { + return await this.prisma.pOIType.findUnique({ + where: { + name: name + } + }) + } - // ========================================================= // - // [POI] + // ========================================================= // + // [POI] - /** - * Saves a point of interest (POI) in the database. - * - * The parameter are given via object deconstruction from the model `POI`! - * Currently given parameters are: - * @param name - **unique** name of the POI - * @param typeId - POIType Identifier: Maps a POIType to said POI in the database - * @param trackId - Track Identifier : Maps a Track to said POI in the database - * @param position - Coordinates to pinpoint the location of said POI. - * @param description - optional description of said POI - * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI - * @returns POI - */ - public async save(args: Prisma.POIUncheckedCreateInput): Promise { - // POIUncheckCreateInput is used because of required relations based on the model! - return await this.prisma.pOI.create({ - data: args, - }); - } + /** + * Saves a point of interest (POI) in the database. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: + * @param name - **unique** name of the POI + * @param typeId - POIType Identifier: Maps a POIType to said POI in the database + * @param trackId - Track Identifier : Maps a Track to said POI in the database + * @param position - Coordinates to pinpoint the location of said POI. + * @param description - optional description of said POI + * @param isTurningPoint - optional indicator whether it is possible to turn a vehicle around at this POI + * @returns POI + */ + public async save(args: Prisma.POIUncheckedCreateInput): Promise { + // POIUncheckCreateInput is used because of required relations based on the model! + return await this.prisma.pOI.create({ + data: args + }) + } - /** - * Updates a POI in the database. - * - * @param uid - Indicator which poi should be updated. - * - * The parameter are given via object deconstruction from the model `POI`! - * Currently given parameters are: - * @param name - New name after change. (Optional) - * @param description - New description after change. (Optional) - * @param typeId - New typeId after change. (Optional) - * @param trackId - New trackId after change. (Optional) - * @param position - New position after change. (Optional) - * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) - * @returns POI - */ - public async update(uid: number, args: Prisma.POIUncheckedUpdateInput): Promise { - // POIUncheckUpdateInput is used because of required relations based on the model - return await this.prisma.pOI.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updates a POI in the database. + * + * @param uid - Indicator which poi should be updated. + * + * The parameter are given via object deconstruction from the model `POI`! + * Currently given parameters are: + * @param name - New name after change. (Optional) + * @param description - New description after change. (Optional) + * @param typeId - New typeId after change. (Optional) + * @param trackId - New trackId after change. (Optional) + * @param position - New position after change. (Optional) + * @param isTurningPoint - indicator whether it is possible to turn a vehicle around at this POI (Optional) + * @returns POI + */ + public async update(uid: number, args: Prisma.POIUncheckedUpdateInput): Promise { + // POIUncheckUpdateInput is used because of required relations based on the model + return await this.prisma.pOI.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * Removes an poi from the database. - * - * @param uid - Indicator which poi should be removed. - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async remove(uid: number): Promise { - await this.prisma.pOI.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * Removes an poi from the database. + * + * @param uid - Indicator which poi should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: number): Promise { + await this.prisma.pOI.delete({ + where: { + uid: uid + } + }) + return true + } - /** - * Returns a list of all existing pois. - * - * @param trackId - Indicator for filtering all pois for a specific track (Optional) - * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. - */ - public async getAll(trackId?: number): Promise { - return await this.prisma.pOI.findMany({ - where: { - trackId: trackId, - }, - }); - } + /** + * Returns a list of all existing pois. + * + * @param trackId - Indicator for filtering all pois for a specific track (Optional) + * @returns POI[] - List of all pois. If an trackId was given: List of all pois on this specific track. + */ + public async getAll(trackId?: number): Promise { + return await this.prisma.pOI.findMany({ + where: { + trackId: trackId + } + }) + } - /** - * Looks up a poi given by its uid. - * - * @param uid - Indicator which poi should be searched for - * @returns POI | null depending on if the poi could be found. - */ - public async getById(uid: number): Promise { - return await this.prisma.pOI.findUnique({ - where: { - uid: uid, - }, - include: { - type: true, - track: true, - }, - }); - } + /** + * Looks up a poi given by its uid. + * + * @param uid - Indicator which poi should be searched for + * @returns POI | null depending on if the poi could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.pOI.findUnique({ + where: { + uid: uid + }, + include: { + type: true, + track: true + } + }) + } - /** - * Looks up pois given by its name. - * - * @param name - Indicator which pois should be searched for - * @param trackId - optional filter indicator to filter for a given track. - * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. - */ - public async getByName(name: string, trackId?: number): Promise { - return await this.prisma.pOI.findMany({ - where: { - name: name, - trackId: trackId, - }, - }); - } + /** + * Looks up pois given by its name. + * + * @param name - Indicator which pois should be searched for + * @param trackId - optional filter indicator to filter for a given track. + * @returns POI[] - List of all pois with the given name. If an trackId was given: List of all pois on this specific track with the given name. + */ + public async getByName(name: string, trackId?: number): Promise { + return await this.prisma.pOI.findMany({ + where: { + name: name, + trackId: trackId + } + }) + } } diff --git a/Server/src/services/db/track.controller.ts b/Server/src/services/db/track.controller.ts index 512432cc..15c1497b 100644 --- a/Server/src/services/db/track.controller.ts +++ b/Server/src/services/db/track.controller.ts @@ -1,4 +1,4 @@ -import { PrismaClient, Prisma, Track } from "@prisma/client"; +import { PrismaClient, Prisma, Track } from "@prisma/client" /** * TrackController class @@ -13,112 +13,109 @@ import { PrismaClient, Prisma, Track } from "@prisma/client"; * */ export default class TrackController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - /** - * Saves a tracker in the database. - * - * The parameter are given via object deconstruction from the model `Track`! - * Currently given parameters are: - * @param start - Name of the start location. - * @param stop - Name of the end location. - * @param data - JSON Data of the track - * @returns Track - */ - public async save(args: Prisma.TrackCreateInput): Promise { - return await this.prisma.track.create({ - data: args, - }); - } + /** + * Saves a tracker in the database. + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: + * @param start - Name of the start location. + * @param stop - Name of the end location. + * @param data - JSON Data of the track + * @returns Track + */ + public async save(args: Prisma.TrackCreateInput): Promise { + return await this.prisma.track.create({ + data: args + }) + } - /** - * Updates a track in the database. - * - * @param uid - Indicator which track should be updated - * - * The parameter are given via object deconstruction from the model `Track`! - * Currently given parameters are: - * @param start - New name of the start location after change (Optional) - * @param stop - New name of the end location after change (Optional) - * @param data - New JSON Data of the track after change (Optional) - * @returns Track - */ - public async update( - uid: number, - args: Prisma.TrackUpdateInput - ): Promise { - return await this.prisma.track.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updates a track in the database. + * + * @param uid - Indicator which track should be updated + * + * The parameter are given via object deconstruction from the model `Track`! + * Currently given parameters are: + * @param start - New name of the start location after change (Optional) + * @param stop - New name of the end location after change (Optional) + * @param data - New JSON Data of the track after change (Optional) + * @returns Track + */ + public async update(uid: number, args: Prisma.TrackUpdateInput): Promise { + return await this.prisma.track.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * Removes a track in the database. - * - * @param uid - Indicator which track should be removed. - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async remove(uid: number): Promise { - await this.prisma.track.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * Removes a track in the database. + * + * @param uid - Indicator which track should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: number): Promise { + await this.prisma.track.delete({ + where: { + uid: uid + } + }) + return true + } - /** - * Returns a list of all tracks. - * - * @returns Track[] - List of all tracks. - */ - public async getAll(): Promise { - return await this.prisma.track.findMany({}); - } + /** + * Returns a list of all tracks. + * + * @returns Track[] - List of all tracks. + */ + public async getAll(): Promise { + return await this.prisma.track.findMany({}) + } - /** - * Looks up a track given by its uid. - * - * @param uid - Indicator which track should be searched for. - * @returns Track | null depending on if the track could be found. - */ - public async getById(uid: number): Promise { - return await this.prisma.track.findUnique({ - where: { - uid: uid, - }, - include: { - poi: true, - vehicle: true, - }, - }); - } + /** + * Looks up a track given by its uid. + * + * @param uid - Indicator which track should be searched for. + * @returns Track | null depending on if the track could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.track.findUnique({ + where: { + uid: uid + }, + include: { + poi: true, + vehicle: true + } + }) + } - /** - * Looks up any track that has a start or stop at the given location. - * - * @param location - Name of the location to check. - * @returns Track[] - List of tracks that have either start and/or stop at the given location. - */ - public async getByLocation(location: string): Promise { - return await this.prisma.track.findMany({ - where: { - OR: [ - { - start: location, - }, - { - stop: location, - }, - ], - }, - include: { - poi: true, - vehicle: true, - }, - }); - } + /** + * Looks up any track that has a start or stop at the given location. + * + * @param location - Name of the location to check. + * @returns Track[] - List of tracks that have either start and/or stop at the given location. + */ + public async getByLocation(location: string): Promise { + return await this.prisma.track.findMany({ + where: { + OR: [ + { + start: location + }, + { + stop: location + } + ] + }, + include: { + poi: true, + vehicle: true + } + }) + } } diff --git a/Server/src/services/db/tracker.controller.ts b/Server/src/services/db/tracker.controller.ts index 1a51b268..968d0399 100644 --- a/Server/src/services/db/tracker.controller.ts +++ b/Server/src/services/db/tracker.controller.ts @@ -1,4 +1,4 @@ -import { Prisma, PrismaClient, Tracker } from "@prisma/client"; +import { Prisma, PrismaClient, Tracker } from "@prisma/client" /** * TrackerController class @@ -13,98 +13,95 @@ import { Prisma, PrismaClient, Tracker } from "@prisma/client"; * */ export default class TrackerController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - /** - * Saves a new tracker in the database. - * - * The parameter are given via object deconstruction from the model `Tracker`! - * Currently given parameters are: - * @param uid - ID (EUI) of the tracker. - * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. - * @param data - optional additional data field. - * @returns Tracker - */ - public async save(args: Prisma.TrackerUncheckedCreateInput): Promise { - //TrackerUncheckedCreateInput is used because of the relation to vehicles. - return await this.prisma.tracker.create({ - data: args, - }); - } + /** + * Saves a new tracker in the database. + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: + * @param uid - ID (EUI) of the tracker. + * @param vehicleId - assign a vehicle for the tracker. Multiple tracker can have the same vehicle. + * @param data - optional additional data field. + * @returns Tracker + */ + public async save(args: Prisma.TrackerUncheckedCreateInput): Promise { + //TrackerUncheckedCreateInput is used because of the relation to vehicles. + return await this.prisma.tracker.create({ + data: args + }) + } - /** - * Updates a tracker in the database. - * - * @param uid - Indicator which tracker should be updated - * - * The parameter are given via object deconstruction from the model `Tracker`! - * Currently given parameters are: - * @param vehicleId - New vehicleId (Optional) - * @param data - New additional data field (Optional) - * @returns Tracker - */ - public async update( - uid: string, - args: Prisma.TrackerUncheckedUpdateInput - ): Promise { - //TrackerUncheckedUpdateInput is used because of the relation to vehicles. - return await this.prisma.tracker.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updates a tracker in the database. + * + * @param uid - Indicator which tracker should be updated + * + * The parameter are given via object deconstruction from the model `Tracker`! + * Currently given parameters are: + * @param vehicleId - New vehicleId (Optional) + * @param data - New additional data field (Optional) + * @returns Tracker + */ + public async update(uid: string, args: Prisma.TrackerUncheckedUpdateInput): Promise { + //TrackerUncheckedUpdateInput is used because of the relation to vehicles. + return await this.prisma.tracker.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * Removes a tracker from the database. - * - * @param uid - Indicator which tracker should be removed. - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async remove(uid: string): Promise { - await this.prisma.tracker.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * Removes a tracker from the database. + * + * @param uid - Indicator which tracker should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: string): Promise { + await this.prisma.tracker.delete({ + where: { + uid: uid + } + }) + return true + } - /** - * Returns a list of all trackers - * - * @returns Tracker[] - List of all trackers. - */ - public async getAll(): Promise { - return await this.prisma.tracker.findMany({}); - } + /** + * Returns a list of all trackers + * + * @returns Tracker[] - List of all trackers. + */ + public async getAll(): Promise { + return await this.prisma.tracker.findMany({}) + } - /** - * Looks up a tracker given by its uid. - * - * @param uid - Indicator which tracker should be looked up. - * @returns Tracker | null depending on if the tracker could be found. - */ - public async getById(uid: string): Promise { - return await this.prisma.tracker.findUnique({ - where: { - uid: uid, - }, - }); - } + /** + * Looks up a tracker given by its uid. + * + * @param uid - Indicator which tracker should be looked up. + * @returns Tracker | null depending on if the tracker could be found. + */ + public async getById(uid: string): Promise { + return await this.prisma.tracker.findUnique({ + where: { + uid: uid + } + }) + } - /** - * Looks up all trackers for a given vehicle. - * - * @param vehicleId - uid of the vehicle. - * @returns List of trackers assigned to the vehicle. - */ - public async getByVehicleId(vehicleId: number): Promise { - return await this.prisma.tracker.findMany({ - where: { - vehicleId: vehicleId, - }, - }); - } + /** + * Looks up all trackers for a given vehicle. + * + * @param vehicleId - uid of the vehicle. + * @returns List of trackers assigned to the vehicle. + */ + public async getByVehicleId(vehicleId: number): Promise { + return await this.prisma.tracker.findMany({ + where: { + vehicleId: vehicleId + } + }) + } } diff --git a/Server/src/services/db/user.controller.ts b/Server/src/services/db/user.controller.ts index 3c4e4c0e..154d98ae 100644 --- a/Server/src/services/db/user.controller.ts +++ b/Server/src/services/db/user.controller.ts @@ -1,4 +1,4 @@ -import { PrismaClient, Prisma, User } from "@prisma/client"; +import { PrismaClient, Prisma, User } from "@prisma/client" /** * UserController class @@ -12,81 +12,78 @@ import { PrismaClient, Prisma, User } from "@prisma/client"; * - getByUsername() */ export default class UserController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - /** - * Saves an user in the database. - * - * The parameter are given via object deconstruction from the model `User`! - * Currently given parameters are: - * @param username - **unique** name of the user. - * @param password - **hashed** password. - * @returns User - */ - public async save(args: Prisma.UserCreateInput): Promise { - return await this.prisma.user.create({ - data: args, - }); - } + /** + * Saves an user in the database. + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: + * @param username - **unique** name of the user. + * @param password - **hashed** password. + * @returns User + */ + public async save(args: Prisma.UserCreateInput): Promise { + return await this.prisma.user.create({ + data: args + }) + } - /** - * Updates an user in the database. - * - * @param name - Old username before change. Indicator which user should be updated - * - * The parameter are given via object deconstruction from the model `User`! - * Currently given parameters are: - * @param username - New username after change. (Optional) - * @param password - New password after change. (Optional) - * @returns User - */ - public async update( - name: string, - args: Prisma.UserUpdateInput - ): Promise { - return await this.prisma.user.update({ - where: { - username: name, - }, - data: args, - }); - } + /** + * Updates an user in the database. + * + * @param name - Old username before change. Indicator which user should be updated + * + * The parameter are given via object deconstruction from the model `User`! + * Currently given parameters are: + * @param username - New username after change. (Optional) + * @param password - New password after change. (Optional) + * @returns User + */ + public async update(name: string, args: Prisma.UserUpdateInput): Promise { + return await this.prisma.user.update({ + where: { + username: name + }, + data: args + }) + } - /** - * Removes an user from the database. - * - * @param username - Indicator which user should be removed - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async remove(username: string): Promise { - await this.prisma.user.delete({ - where: { - username: username, - }, - }); - return true - } + /** + * Removes an user from the database. + * + * @param username - Indicator which user should be removed + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(username: string): Promise { + await this.prisma.user.delete({ + where: { + username: username + } + }) + return true + } - /** - * Returns a list of all existing users. - * - * @returns `User[]` - List of all users. - */ - public async getAll(): Promise { - return await this.prisma.user.findMany({}); - } + /** + * Returns a list of all existing users. + * + * @returns `User[]` - List of all users. + */ + public async getAll(): Promise { + return await this.prisma.user.findMany({}) + } - /** - * Looks up an user given by its username. - * - * @param username - Indicator which user should be searched for - * @returns User | null depending on if the user could be found. - */ - public async getByUsername(username: string): Promise { - return await this.prisma.user.findUnique({ - where: { - username: username, - }, - }); - } + /** + * Looks up an user given by its username. + * + * @param username - Indicator which user should be searched for + * @returns User | null depending on if the user could be found. + */ + public async getByUsername(username: string): Promise { + return await this.prisma.user.findUnique({ + where: { + username: username + } + }) + } } diff --git a/Server/src/services/db/vehicle.controller.ts b/Server/src/services/db/vehicle.controller.ts index a084a0ed..970a42d4 100644 --- a/Server/src/services/db/vehicle.controller.ts +++ b/Server/src/services/db/vehicle.controller.ts @@ -1,4 +1,4 @@ -import { PrismaClient, Prisma, Vehicle, VehicleType } from "@prisma/client"; +import { PrismaClient, Prisma, Vehicle, VehicleType } from "@prisma/client" /** * VehicleController class @@ -21,224 +21,211 @@ import { PrismaClient, Prisma, Vehicle, VehicleType } from "@prisma/client"; * - getByName() */ export default class VehicleController { - constructor(private prisma: PrismaClient) {} + constructor(private prisma: PrismaClient) {} - // ========================================================= // - // [Vehicle Types] + // ========================================================= // + // [Vehicle Types] - /** - * Saves a vehicle type in the database. - * - * The parameter are given via object deconstruction from the model `VehicleType`! - * Currently given parameters are: - * @param name - **unique** Name for the type of vehicle. - * @param icon - unique icon name for visualization - * @param description - optional description for the type. - * @returns VehicleType | null if an error occurs - */ - public async saveType( - args: Prisma.VehicleTypeCreateInput - ): Promise { - return await this.prisma.vehicleType.create({ - data: args, - }); - } + /** + * Saves a vehicle type in the database. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: + * @param name - **unique** Name for the type of vehicle. + * @param icon - unique icon name for visualization + * @param description - optional description for the type. + * @returns VehicleType | null if an error occurs + */ + public async saveType(args: Prisma.VehicleTypeCreateInput): Promise { + return await this.prisma.vehicleType.create({ + data: args + }) + } - /** - * Updates a vehicle type in the database. - * - * @param uid - Indicator which vehicle type should be updated. - * - * The parameter are given via object deconstruction from the model `VehicleType`! - * Currently given parameters are: - * @param name - New name of the vehicle type after change. (Optional) - * @param description - New description of the vehicle type after change. (Optional) - * @returns - */ - public async updateType( - uid: number, - args: Prisma.VehicleTypeUpdateInput - ): Promise { - return await this.prisma.vehicleType.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updates a vehicle type in the database. + * + * @param uid - Indicator which vehicle type should be updated. + * + * The parameter are given via object deconstruction from the model `VehicleType`! + * Currently given parameters are: + * @param name - New name of the vehicle type after change. (Optional) + * @param description - New description of the vehicle type after change. (Optional) + * @returns + */ + public async updateType(uid: number, args: Prisma.VehicleTypeUpdateInput): Promise { + return await this.prisma.vehicleType.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * Removes a vehicle type in the database. - * - * @param uid - Indicator which vehicle type should be removed. - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async removeType(uid: number): Promise { - await this.prisma.vehicleType.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * Removes a vehicle type in the database. + * + * @param uid - Indicator which vehicle type should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async removeType(uid: number): Promise { + await this.prisma.vehicleType.delete({ + where: { + uid: uid + } + }) + return true + } - /** - * Returns a list of all vehicle types. - * - * @returns VehicleType[] - List of all vehicle types. - */ - public async getAllTypes(): Promise { - return await this.prisma.vehicleType.findMany({}); - } + /** + * Returns a list of all vehicle types. + * + * @returns VehicleType[] - List of all vehicle types. + */ + public async getAllTypes(): Promise { + return await this.prisma.vehicleType.findMany({}) + } - /** - * Looks up a vehicle type given by its uid. - * - * @param uid - Indicator which vehicle type should be searched for. - * @returns VehicleType | null depending on if the vehicle type could be found. - */ - public async getTypeById(uid: number): Promise { - return await this.prisma.vehicleType.findUnique({ - where: { - uid: uid, - }, - }); - } + /** + * Looks up a vehicle type given by its uid. + * + * @param uid - Indicator which vehicle type should be searched for. + * @returns VehicleType | null depending on if the vehicle type could be found. + */ + public async getTypeById(uid: number): Promise { + return await this.prisma.vehicleType.findUnique({ + where: { + uid: uid + } + }) + } - /** - * Looks up a vehicle type by its name. - * - * @param uid - Indicator which vehicle type should be searched for. - * @returns VehicleType | null depending on if the vehicle type could be found. - */ - public async getTypeByName(name: string): Promise { - return await this.prisma.vehicleType.findUnique({ - where: { - name: name, - }, - }); - } + /** + * Looks up a vehicle type by its name. + * + * @param uid - Indicator which vehicle type should be searched for. + * @returns VehicleType | null depending on if the vehicle type could be found. + */ + public async getTypeByName(name: string): Promise { + return await this.prisma.vehicleType.findUnique({ + where: { + name: name + } + }) + } - // ========================================================= // - // [Vehicles] + // ========================================================= // + // [Vehicles] - /** - * Saves a new vehicle in the database. - * - * The parameter are given via object deconstruction from the model `Vehicle`! - * Currently given parameters are: - * @param typeId - VehicleType uid - * @param trackId - Track uid - * @param name - display name for the given vehicle (Optional) - * @returns Vehicle | null if an error occurs. - */ - public async save( - args: Prisma.VehicleUncheckedCreateInput - ): Promise { - // VehicleUncheckedCreateInput is used because of required relations - return await this.prisma.vehicle.create({ - data: args, - }); - } + /** + * Saves a new vehicle in the database. + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: + * @param typeId - VehicleType uid + * @param trackId - Track uid + * @param name - display name for the given vehicle (Optional) + * @returns Vehicle | null if an error occurs. + */ + public async save(args: Prisma.VehicleUncheckedCreateInput): Promise { + // VehicleUncheckedCreateInput is used because of required relations + return await this.prisma.vehicle.create({ + data: args + }) + } - /** - * Updadtes a vehicle in the database. - * - * @param uid - Indicator which vehicle should be updated - * - * The parameter are given via object deconstruction from the model `Vehicle`! - * Currently given parameters are: - * @param typeId - New VehicleType.uid after change (Optional) - * @param trackId - New Track.uid after change (Optional) - * @param name - New display name after change (Optional) - * @returns Vehicle | null if an error occurs - */ - public async update( - uid: number, - args: Prisma.VehicleUncheckedUpdateInput - ): Promise { - // VehicleUncheckCreateInput is used because of required relations - return await this.prisma.vehicle.update({ - where: { - uid: uid, - }, - data: args, - }); - } + /** + * Updadtes a vehicle in the database. + * + * @param uid - Indicator which vehicle should be updated + * + * The parameter are given via object deconstruction from the model `Vehicle`! + * Currently given parameters are: + * @param typeId - New VehicleType.uid after change (Optional) + * @param trackId - New Track.uid after change (Optional) + * @param name - New display name after change (Optional) + * @returns Vehicle | null if an error occurs + */ + public async update(uid: number, args: Prisma.VehicleUncheckedUpdateInput): Promise { + // VehicleUncheckCreateInput is used because of required relations + return await this.prisma.vehicle.update({ + where: { + uid: uid + }, + data: args + }) + } - /** - * Removes a vehicle in the database. - * - * @param uid - Indicator which vehicle should be removed. - * @returns True if the removal was successful. Otherwise throws an Error. - */ - public async remove(uid: number): Promise { - await this.prisma.vehicle.delete({ - where: { - uid: uid, - }, - }); - return true - } + /** + * Removes a vehicle in the database. + * + * @param uid - Indicator which vehicle should be removed. + * @returns True if the removal was successful. Otherwise throws an Error. + */ + public async remove(uid: number): Promise { + await this.prisma.vehicle.delete({ + where: { + uid: uid + } + }) + return true + } - /** - * Returns a list of all vehicles. - * - * @param trackId - Track.uid for filtering list (Optional) - * - * @returns Vehicle[] - */ - public async getAll(trackId?: number): Promise { - return await this.prisma.vehicle.findMany({ - where: { - trackId: trackId, - }, - include: { - type: true, - track: true, - }, - }); - } + /** + * Returns a list of all vehicles. + * + * @param trackId - Track.uid for filtering list (Optional) + * + * @returns Vehicle[] + */ + public async getAll(trackId?: number): Promise { + return await this.prisma.vehicle.findMany({ + where: { + trackId: trackId + }, + include: { + type: true, + track: true + } + }) + } - /** - * Looks up a vehicle by its uid. - * - * @param uid - Indicator which vehicle should be looked for. - * @returns Vehicle | null depending on if the vehicle could be found. - */ - public async getById(uid: number): Promise { - return await this.prisma.vehicle.findUnique({ - where: { - uid: uid, - }, - include: { - type: true, - track: true, - }, - }); - } + /** + * Looks up a vehicle by its uid. + * + * @param uid - Indicator which vehicle should be looked for. + * @returns Vehicle | null depending on if the vehicle could be found. + */ + public async getById(uid: number): Promise { + return await this.prisma.vehicle.findUnique({ + where: { + uid: uid + }, + include: { + type: true, + track: true + } + }) + } - /** - * Looks up a vehicle by its name. - * - * @param name - Indicator which vehicle should be looked for. - * @returns Vehicle | null depending on if the vehicle could be found. - */ - public async getByName( - name: string, - trackId: number - ): Promise { - return await this.prisma.vehicle.findUnique({ - where: { - name_trackId: { - name: name, - trackId: trackId, - }, - }, - include: { - type: true, - track: true, - }, - }); - } + /** + * Looks up a vehicle by its name. + * + * @param name - Indicator which vehicle should be looked for. + * @returns Vehicle | null depending on if the vehicle could be found. + */ + public async getByName(name: string, trackId: number): Promise { + return await this.prisma.vehicle.findUnique({ + where: { + name_trackId: { + name: name, + trackId: trackId + } + }, + include: { + type: true, + track: true + } + }) + } } diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index a1723fa6..e1e35e33 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -134,7 +134,9 @@ export default class POIService { return null } // try to update the poi in the database, now that we have enriched it - if ((await database.pois.update(poi.uid, {position: enrichedPos as unknown as Prisma.InputJsonValue})) == null) { + if ( + (await database.pois.update(poi.uid, { position: enrichedPos as unknown as Prisma.InputJsonValue })) == null + ) { logger.info(`Could not update POI with id ${poi.uid} after enriching it.`) } From d8d37d96e28b63111a5ee37638e63df424e6e9e3 Mon Sep 17 00:00:00 2001 From: Nico Biernat Date: Tue, 5 Sep 2023 13:42:32 +0200 Subject: [PATCH 19/19] Fix eslint errors --- Server/src/routes/init.route.ts | 1 - Server/src/routes/track.route.ts | 6 +++--- Server/src/services/db/log.controller.ts | 2 +- Server/src/services/poi.service.ts | 1 - Server/src/services/user.service.ts | 2 +- Server/src/services/vehicle.service.ts | 16 +++++++--------- 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Server/src/routes/init.route.ts b/Server/src/routes/init.route.ts index 6cae697f..008fb8e6 100644 --- a/Server/src/routes/init.route.ts +++ b/Server/src/routes/init.route.ts @@ -6,7 +6,6 @@ import { logger } from "../utils/logger" import TrackService from "../services/track.service" import { POI, Track } from "@prisma/client" import POIService from "../services/poi.service" -import VehicleService from "../services/vehicle.service" import { Feature, FeatureCollection, GeoJsonProperties, LineString, Point } from "geojson" import GeoJSONUtils from "../utils/geojsonUtils" import database from "../services/database.service" diff --git a/Server/src/routes/track.route.ts b/Server/src/routes/track.route.ts index b6f1133a..e1d6daf1 100644 --- a/Server/src/routes/track.route.ts +++ b/Server/src/routes/track.route.ts @@ -221,9 +221,9 @@ export class TrackRoute { // If we know that, convert it in the API format. const pos: Position | undefined = geo_pos ? { - lat: GeoJSONUtils.getLatitude(geo_pos), - lng: GeoJSONUtils.getLongitude(geo_pos) - } + lat: GeoJSONUtils.getLatitude(geo_pos), + lng: GeoJSONUtils.getLongitude(geo_pos) + } : undefined // Also acquire the percentage position. It might happen that a percentage position is known, while the position is not. // This might not make much sense. diff --git a/Server/src/services/db/log.controller.ts b/Server/src/services/db/log.controller.ts index 7a6a35a2..1a0f02d9 100644 --- a/Server/src/services/db/log.controller.ts +++ b/Server/src/services/db/log.controller.ts @@ -136,7 +136,7 @@ export default class LogController { */ public async getNewestLogs(vehicleId: number, max_sec: number = 300): Promise { // Earliest date which should be considered - let max_date = new Date(Date.now() - max_sec * 1000) + const max_date = new Date(Date.now() - max_sec * 1000) return await this.prisma.log.findMany({ where: { diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index e1e35e33..3c76192f 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -4,7 +4,6 @@ import TrackService from "./track.service" import VehicleService from "./vehicle.service" import GeoJSONUtils from "../utils/geojsonUtils" -import distance from "@turf/distance" import { logger } from "../utils/logger" /** diff --git a/Server/src/services/user.service.ts b/Server/src/services/user.service.ts index 58497dbb..5b1c629c 100644 --- a/Server/src/services/user.service.ts +++ b/Server/src/services/user.service.ts @@ -115,7 +115,7 @@ export default class UserService { return false } - const successful: Boolean = await database.users.remove(userToBeDeleted.username) + const successful: boolean = await database.users.remove(userToBeDeleted.username) if (!successful.valueOf()) { logger.error(`Could not remove user with username ${name}.`) return false diff --git a/Server/src/services/vehicle.service.ts b/Server/src/services/vehicle.service.ts index 939a7f3c..73b3e0de 100644 --- a/Server/src/services/vehicle.service.ts +++ b/Server/src/services/vehicle.service.ts @@ -6,8 +6,6 @@ import TrackerService from "./tracker.service" import GeoJSONUtils from "../utils/geojsonUtils" import along from "@turf/along" -import * as turfHelpers from "@turf/helpers" -import * as turfMeta from "@turf/meta" import nearestPointOnLine from "@turf/nearest-point-on-line" import { Position } from "../models/api" @@ -128,7 +126,7 @@ export default class VehicleService { return null } - allVehiclesOnTrack.filter(async function (vehicle, index, vehicles) { + allVehiclesOnTrack.filter(async function (vehicle, _index, _vehicles) { const vehicleTrackKm = await VehicleService.getVehicleTrackDistanceKm(vehicle) if (vehicleTrackKm == null) { // TODO: log this @@ -140,7 +138,7 @@ export default class VehicleService { // filter vehicles by distance if given if (maxDistance != null) { - allVehiclesOnTrack.filter(async function (vehicle, index, vehicles) { + allVehiclesOnTrack.filter(async function (vehicle, _index, _vehicles) { const vehicleTrackKm = await VehicleService.getVehicleTrackDistanceKm(vehicle) if (vehicleTrackKm == null) { return false @@ -202,7 +200,7 @@ export default class VehicleService { // get all vehicles for track and filter by type const vehicles: Vehicle[] = await database.vehicles.getAll(track.uid) - const filteredVehicles = vehicles.filter(function (vehicle, index, vehicles) { + const filteredVehicles = vehicles.filter(function (vehicle, _index, _vehicles) { return vehicle.typeId == type.uid }) return filteredVehicles @@ -249,7 +247,7 @@ export default class VehicleService { // create list of all positions used for computation (others in list above are just "helpers") // (this is more likely a list of track kilometer values and weights as we project every position on the track anyways) - let weightedPositions: [number, number][] = [] + const weightedPositions: [number, number][] = [] // add predicted tracker positions for (let i = 0; i < trackers.length; i++) { // create list of positions of this specific tracker (also filtered for last ten minutes) @@ -312,7 +310,7 @@ export default class VehicleService { // now also add predicted app positions to computation (at most three, might be inaccurate due to mobility) // (only adding last known positions here) - let appPositions: [number, Log][] = [] + const appPositions: [number, Log][] = [] for (let i = 0; i < allLogs.length; i++) { const log = allLogs[i] @@ -518,7 +516,7 @@ export default class VehicleService { } // get all last known tracker logs - let lastLogs: Log[] = [] + const lastLogs: Log[] = [] for (let i = 0; i < trackers.length; i++) { // get last log entry for this tracker const lastLog = await database.logs.getAll(vehicle.uid, trackers[i].uid, 1) @@ -592,7 +590,7 @@ export default class VehicleService { } // get all last known tracker logs - let lastLogs: Log[] = [] + const lastLogs: Log[] = [] for (let i = 0; i < trackers.length; i++) { // get last log entry for this tracker const lastLog = await database.logs.getAll(vehicle.uid, trackers[i].uid, 1)