Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
Add plate only if does not exists yet
Browse files Browse the repository at this point in the history
  • Loading branch information
norkator committed Sep 22, 2020
1 parent 5dbdce2 commit 03f2cbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/intelligence.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ initDb.initDatabase().then(() => {
// allowAccessOriginAll will let any origin client connect to this api
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, PATCH, PUT');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
next();
});
Expand Down
26 changes: 20 additions & 6 deletions api/routes/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,14 +786,28 @@ async function Site(router, sequelizeObjects) {
* Create license plate
*/
router.post('/manage/licence/plates', async (req, res) => {
sequelizeObjects.Plate.create({
licence_plate: req.body.licence_plate, owner_name: req.body.owner_name, enabled: 1
}).then(result => {
res.status(200);
res.send('New licence plate added.');
const licensePlate = req.body.licence_plate;
sequelizeObjects.Plate.findAll({
attributes: ['id',],
where: {licence_plate: licensePlate},
}).then(rows => {
if (rows.length > 0) {
res.status(409);
res.send('Plate already exists in records');
} else {
sequelizeObjects.Plate.create({
licence_plate: req.body.licence_plate, owner_name: req.body.owner_name, enabled: 1
}).then(result => {
res.status(200);
res.send('New licence plate added.');
}).catch(error => {
res.status(500);
res.send('Error adding licence plate. ' + error);
});
}
}).catch(error => {
res.status(500);
res.send('Error adding licence plate. ' + error);
res.send(error);
});
});

Expand Down

0 comments on commit 03f2cbb

Please sign in to comment.