Skip to content

Commit

Permalink
Adjust LogController
Browse files Browse the repository at this point in the history
  • Loading branch information
Niatsuna authored and Kevin Ebsen committed Aug 30, 2023
1 parent 8908905 commit 4060697
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 100 deletions.
122 changes: 32 additions & 90 deletions Server/src/services/db/log.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Log, PrismaClient, Prisma } from "@prisma/client"
import { logger } from "../../utils/logger"

/**
* LogController class
Expand All @@ -22,6 +21,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.
Expand All @@ -32,41 +33,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: [number, number],
heading: number,
speed: number,
battery?: number,
data?: any,
trackerId?: string
): Promise<Log | null> {
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
}
public async save(args: Prisma.LogUncheckedCreateInput): Promise<Log> {
//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.
Expand All @@ -77,59 +57,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?: [number, number],
heading?: number,
speed?: number,
battery?: number,
data?: any,
vehicleId?: number,
trackerId?: string
): Promise<Log | null> {
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
}
public async update(uid: number, args: Prisma.LogUpdateInput): Promise<Log | null> {
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<boolean> {
try {
await this.prisma.log.delete({
where: {
uid: uid
}
})
return true
} catch (e) {
logger.debug(e)
return false
}
public async remove(uid: number): Promise<void> {
await this.prisma.log.delete({
where: {
uid: uid
}
})
}

/**
Expand Down Expand Up @@ -165,20 +112,15 @@ export default class LogController {
* @returns Log | null depending on if the log could be found.
*/
public async getLog(uid: number): Promise<Log | null> {
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
}
})
}

/**
Expand Down
21 changes: 11 additions & 10 deletions Server/src/services/vehicle.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ export default class VehicleService {
heading: number,
speed: number
): Promise<Log | null> {
return await database.logs.save(
new Date(),
vehicleId,
[position.lng, position.lat],
heading,
speed,
undefined,
undefined,
undefined
)
// TODO: Is this the right way? Maybe needs a fix when merging related PR for refining DB
return await database.logs.save({
timestamp: new Date(),
vehicleId: vehicleId,
position: [position.lng, position.lat],
heading: heading,
speed: speed,
battery: undefined,
data: undefined,
trackerId: undefined
})
}

/**
Expand Down

0 comments on commit 4060697

Please sign in to comment.