Skip to content

Commit

Permalink
Merge pull request #59 from Northeastern-Electric-Racing/#33-add-data
Browse files Browse the repository at this point in the history
#33-add-data-uhhhhh
  • Loading branch information
Peyton-McKee authored Oct 6, 2023
2 parents 2814e06 + 50a3d1f commit b1b016b
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 97 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"author": "",
"license": "ISC",
"devDependencies": {
"prettier": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"eslint": "^8.49.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.33.2",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1"
"prettier": "^3.0.3"
}
}
2 changes: 1 addition & 1 deletion scylla-server/src/odyssey-base
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- CreateTable
CREATE TABLE "Run" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"locationName" TEXT NOT NULL,
"driverId" TEXT,
"systemId" TEXT,
"locationName" TEXT,
"driverName" TEXT,
"systemName" TEXT,
"time" INTEGER NOT NULL,
CONSTRAINT "Run_locationName_fkey" FOREIGN KEY ("locationName") REFERENCES "Location" ("name") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Run_driverId_fkey" FOREIGN KEY ("driverId") REFERENCES "Driver" ("username") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Run_systemId_fkey" FOREIGN KEY ("systemId") REFERENCES "System" ("name") ON DELETE SET NULL ON UPDATE CASCADE
CONSTRAINT "Run_locationName_fkey" FOREIGN KEY ("locationName") REFERENCES "Location" ("name") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Run_driverName_fkey" FOREIGN KEY ("driverName") REFERENCES "Driver" ("username") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Run_systemName_fkey" FOREIGN KEY ("systemName") REFERENCES "System" ("name") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
Expand Down
14 changes: 7 additions & 7 deletions scylla-server/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ datasource db {
}

model Run {
id Int @id @unique @default(autoincrement())
locationName String
location Location @relation(fields: [locationName], references: [name])
driverId String?
driver Driver? @relation(fields: [driverId], references: [username])
systemId String?
system System? @relation(fields: [systemId], references: [name])
id Int @id @unique @default(autoincrement())
locationName String?
location Location? @relation(fields: [locationName], references: [name])
driverName String?
driver Driver? @relation(fields: [driverName], references: [username])
systemName String?
system System? @relation(fields: [systemName], references: [name])
data Data[]
time Int
}
Expand Down
63 changes: 49 additions & 14 deletions scylla-server/src/services/data.services.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
import { JsonObject } from '@prisma/client/runtime/library';
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/response-function';
import { InvalidDataError } from '../utils/errors.utils';
import { NotFoundError } from '../utils/errors.utils';
import { Data } from '@prisma/client';
/**
* Type of data needed for getting data by dataTypeName
*/
export type DataTypeName = {
dataTypeName: string;
};
import { ServerData } from '../odyssey-base/src/types/message.types';

/**
* Service class for handling data
*/
export default class DataService {
/**
* CRUD operation to get all the data for a given datatype name
* @param dataTypeName name of the dataType to get data for
* @returns string contianing list of all data with dataype name
*/
static getDataByDataTypeName: ResponseFunction<Data[]> = async (data?: JsonObject) => {
const validData = data as DataTypeName;
if (!validData || !validData.dataTypeName) {
throw InvalidDataError(validData, 'DataTypeName');
static getDataByDataTypeName = async (dataTypeName: string) => {
const dataType = await prisma.dataType.findUnique({
where: {
name: dataTypeName
}
});

if (!dataType) {
throw NotFoundError('dataType', dataTypeName);
}

const queriedData = await prisma.data.findMany({
where: {
dataTypeName: validData.dataTypeName
dataTypeName
}
});

return queriedData;
};

/**
* Adds data to the database
* @param serverData The data to add
* @param unixTime the timestamp of the data
* @param value the value of the data
* @param runId the id of the run associated with the data
* @returns The created data type
*/
static addData = async (serverData: ServerData, unixTime: number, value: number, runId: number): Promise<Data> => {
const dataType = await prisma.dataType.findUnique({
where: {
name: serverData.name
}
});

if (!dataType) {
throw NotFoundError('dataType', serverData.name);
}

const run = await prisma.run.findUnique({
where: {
id: runId
}
});

if (!run) {
throw NotFoundError('run', runId);
}

return await prisma.data.create({
data: { dataType: { connect: { name: dataType.name } }, time: unixTime, run: { connect: { id: run.id } }, value }
});
};
}
23 changes: 21 additions & 2 deletions scylla-server/src/services/driver.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Driver } from '@prisma/client';
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/response-function';
import RunService from './runs.services';

/**
* Service class to handle drivers
Expand All @@ -18,10 +19,11 @@ export default class DriverService {
/**
* CRUD operation to create a driver in the database if it doesn't already exist, does nothing otherwise.
* @param driverName name of the driver as string
* @param runId id of the run that the driver is currently associated with
* @returns the created driver
*/
static upsertDriver = async (driverName: string): Promise<Driver> => {
return await prisma.driver.upsert({
static upsertDriver = async (driverName: string, runId: number): Promise<Driver> => {
const driver = await prisma.driver.upsert({
where: {
username: driverName
},
Expand All @@ -30,5 +32,22 @@ export default class DriverService {
username: driverName
}
});

await RunService.getRunById(runId);

await prisma.run.update({
where: {
id: runId
},
data: {
driver: {
connect: {
username: driverName
}
}
}
});

return driver;
};
}
25 changes: 24 additions & 1 deletion scylla-server/src/services/locations.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Location } from '@prisma/client';
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/response-function';
import RunService from './runs.services';

export default class LocationService {
/**
Expand All @@ -18,9 +19,16 @@ export default class LocationService {
* @param latitude the latitude of the location
* @param longitude the longitude of the location
* @param radius the radius of the location
* @param runId the id of the run associated with the location
* @returns the location
*/
static upsertLocation = async (name: string, latitude: number, longitude: number, radius: number): Promise<Location> => {
static upsertLocation = async (
name: string,
latitude: number,
longitude: number,
radius: number,
runId: number
): Promise<Location> => {
const location = await prisma.location.upsert({
where: {
name
Expand All @@ -38,6 +46,21 @@ export default class LocationService {
}
});

await RunService.getRunById(runId);

await prisma.run.update({
where: {
id: runId
},
data: {
location: {
connect: {
name
}
}
}
});

return location;
};
}
14 changes: 1 addition & 13 deletions scylla-server/src/services/runs.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,11 @@ export default class RunService {

/**
* Creates a new run in the database
* @param locationName locationName of run
* @returns Promise<Run>
*/
static createRun = async (locationName: string, timestamp: number) => {
const location = await prisma.location.findUnique({
where: {
name: locationName
}
});

if (!location) {
throw NotFoundError('location', locationName);
}

static createRun = async (timestamp: number) => {
const run = await prisma.run.create({
data: {
locationName,
time: timestamp
}
});
Expand Down
25 changes: 22 additions & 3 deletions scylla-server/src/services/systems.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { System } from '@prisma/client';
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/response-function';
import RunService from './runs.services';

export default class SystemService {
/**
Expand All @@ -13,13 +14,14 @@ export default class SystemService {
};

/**
* CRUD opertation that creates system if it doesn't exist, otherwise does nothing.
* CRUD opertation that creates system if it doesn't exist, connects the system to the current run
* Currently designated private so not hooked up to server.
* @param systemName name of the system as string
* @param runId id of the run that the system is currently associated with
* @returns the created system
*/
static upsertSystem = async (systemName: string): Promise<System> => {
return await prisma.system.upsert({
static upsertSystem = async (systemName: string, runId: number): Promise<System> => {
const system = await prisma.system.upsert({
where: {
name: systemName
},
Expand All @@ -28,5 +30,22 @@ export default class SystemService {
name: systemName
}
});

await RunService.getRunById(runId);

await prisma.run.update({
where: {
id: runId
},
data: {
system: {
connect: {
name: systemName
}
}
}
});

return system;
};
}
2 changes: 1 addition & 1 deletion scylla-server/src/utils/errors.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ type MissingType = 'location' | 'run' | 'dataType' | 'data';
/**
* Custom Error message based on given missing data and type of data
*/
export const NotFoundError = (type: MissingType, id: string | number) => {
export const NotFoundError = (type: MissingType, id?: string | number) => {
throw new Error(`${type} with id ${id} not found`);
};
4 changes: 1 addition & 3 deletions scylla-server/src/utils/response-function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { JsonObject } from '@prisma/client/runtime/library';

/**
* Response function type for all CRUD operations
*/
export type ResponseFunction<T> = (data?: JsonObject) => Promise<T>;
export type ResponseFunction<T> = (params?: string) => Promise<T>;
Loading

0 comments on commit b1b016b

Please sign in to comment.