-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tu 482 #14
base: main
Are you sure you want to change the base?
Tu 482 #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { UUIDV4 } from 'sequelize'; | ||
import { sensorsDB } from '../models/db.index'; | ||
import { Sensor } from '../models/sensors.model'; | ||
import { RequestHandler } from 'express'; | ||
|
||
export const getSensors: RequestHandler = async (req, res) => { | ||
try { | ||
const sensors = await sensorsDB.findAll().then((sensors) => sensors.map((sensors) => { | ||
const jsonSensors:Sensor = sensors.toJSON(); | ||
return jsonSensors; | ||
})) as Sensor[]; | ||
res.status(200).json({ sensors }); | ||
} catch (e) { | ||
res.status(500).json({ message: e }); | ||
} | ||
}; | ||
|
||
export const getSensor: RequestHandler = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
if (!id) { | ||
res.status(400).json({ message: 'Missing required fields' }); | ||
return; | ||
} | ||
const sensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => { | ||
const jsonSensor:Sensor|undefined = sensor?.toJSON(); | ||
return jsonSensor; | ||
}); | ||
if (!sensor){ | ||
res.status(400).json({ message: 'Sensor not found' }); | ||
return; | ||
} | ||
res.status(200).json({ sensor }); | ||
} catch (e) { | ||
res.status(500).json({ message: e }); | ||
} | ||
}; | ||
|
||
interface CreateSensorBody { | ||
sensor:Partial<Sensor>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are leaving this partial, but which fields are allowed to be omitted? Maybe we type this more explicitly, as well as check in the route if req fields are missing and reply with an error if so. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also do we want to check for auth? verifying a usertype is what they said earlier? |
||
} | ||
|
||
export const createSensor: RequestHandler = async (req, res) => { | ||
try { | ||
const createSensorBody:CreateSensorBody = req.body; | ||
const thisID = UUIDV4(); | ||
await sensorsDB.create({ | ||
...createSensorBody.sensor, | ||
id: createSensorBody.sensor.id ? thisID : null, | ||
}); | ||
const sensor = await sensorsDB.findOne({ where: { name: thisID } }).then((sensor) => sensor?.toJSON()) as Sensor; | ||
res.status(200).json({ sensor }); | ||
} catch (e) { | ||
res.status(500).json({ message: e }); | ||
} | ||
}; | ||
|
||
interface UpdateSensorBody { | ||
sensor?:Partial<Sensor>; | ||
} | ||
|
||
export const updateSensor: RequestHandler = async(req, res) => { | ||
try { | ||
const updateSensorBody:UpdateSensorBody = req.body; | ||
const id = req.params.id; | ||
if (!id) { | ||
res.status(400).json({ message: 'Missing required fields' }); | ||
return; | ||
} | ||
if (!updateSensorBody.sensor){ | ||
res.status(400).json({ message: 'Missing required fields' }); | ||
return; | ||
} | ||
const sensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => sensor?.toJSON()) as Sensor; | ||
if (!sensor){ | ||
res.status(400).json({ message: 'Sensor not found' }); | ||
return; | ||
} | ||
sensorsDB.update({ | ||
...updateSensorBody.sensor, | ||
}, { where: { id: id }, | ||
}); | ||
const updatedSensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => sensor?.toJSON()) as Sensor; | ||
|
||
res.status(200).json({ machine: updatedSensor, message: 'Sensor updated' }); | ||
} catch (e) { | ||
res.status(500).json({ message: e }); | ||
} | ||
}; | ||
|
||
export const deleteSensor: RequestHandler = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
if (!id) { | ||
res.status(400).json({ message: 'Missing required fields' }); | ||
return; | ||
} | ||
const sensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => sensor?.toJSON()) as Sensor; | ||
if (!sensor){ | ||
res.status(400).json({ message: 'Sensor not found' }); | ||
return; | ||
} | ||
await sensorsDB.destroy({ where: { id: id } }); | ||
res.status(200).json({ message: 'Sensor deleted' }); | ||
} catch (e) { | ||
res.status(500).json({ message: e }); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should meet to discuss message/measurement creation (its a special topic. Lets omit of any message creation routes for this PR