-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#33-add-data-uhhhhh
- Loading branch information
Showing
17 changed files
with
256 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...rations/20231004032121_init/migration.sql → ...rations/20231006025940_init/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
Oops, something went wrong.