-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BE: aggiunte API per i devices - FE: aggiunto backe button nel form d…
- Loading branch information
Showing
5 changed files
with
205 additions
and
51 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
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import * as express from 'express'; | ||
import * as HttpStatus from 'http-status-codes'; | ||
import DeviceStore from '../../stores/deviceStore'; | ||
|
||
import { IDevice, ISearchOpt } from '../../interfaces/interfaces'; | ||
const _ = require('lodash'); | ||
const factory = require('../../shared/factory'); | ||
const router = express.Router(); | ||
const deviceStore = new DeviceStore(); | ||
|
||
router.get( | ||
'/', async ( | ||
req: any, | ||
res: express.Response, | ||
next: express.NextFunction | ||
) => { | ||
const tenant_id = req.query.id; | ||
try { | ||
const deviceResponse = await deviceStore.findById(tenant_id); | ||
if (deviceResponse && deviceResponse.length == 1) { | ||
const device = deviceResponse[0]; | ||
const result = factory.generateSuccessResponse(device, null, "Device found"); | ||
res.status(HttpStatus.OK).send(result); | ||
} else { | ||
const result = factory.generateErrorResponse(null, null, "Device not found"); | ||
res.status(HttpStatus.NOT_FOUND).send(result); | ||
} | ||
} catch (error) { | ||
const result = factory.generateErrorResponse(null, error, "ERROR"); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send(result); | ||
res.end(); | ||
} | ||
return router; | ||
} | ||
); | ||
|
||
router.get( | ||
'/getAll', async ( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction | ||
) => { | ||
try { | ||
const options = req.query.options; | ||
const search = req.query.search; | ||
let searchOptions: ISearchOpt = options ? JSON.parse(options) : {}; | ||
searchOptions.itemsPerPage = searchOptions.itemsPerPage || 25; | ||
searchOptions.activePage = searchOptions.activePage || 1; | ||
searchOptions.needle = search || ""; | ||
const devicesRes = await deviceStore.getAll(searchOptions); | ||
|
||
|
||
if (devicesRes && devicesRes.length > 0) { | ||
const result = factory.generateSuccessResponse( | ||
{devices: devicesRes, options: searchOptions}, | ||
null, | ||
'Devices found' | ||
); | ||
res.status(HttpStatus.OK).json(result); | ||
} else { | ||
const result = factory.generateSuccessResponse( | ||
null, | ||
null, | ||
'Devices not found' | ||
); | ||
res.status(HttpStatus.OK).json(result); | ||
} | ||
} catch (error) { | ||
const result = factory.generateErrorResponse( | ||
null, | ||
error, | ||
'Devices not found' | ||
); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(result); | ||
} | ||
} | ||
); | ||
|
||
router.post('/create', async (req, res, next) => { | ||
const device = req.body.params; | ||
try { | ||
const devices = await deviceStore.findBy(device); | ||
let message = ''; | ||
if (!devices || devices.length == 0) { | ||
const resCreation = await deviceStore.create(device); | ||
if (resCreation && resCreation.length == 1) { | ||
message = 'Device successfully created' | ||
const result = factory.generateSuccessResponse(null, null, message); | ||
res.status(HttpStatus.OK).json(result); | ||
} else { | ||
const result = factory.generateErrorResponse(null, null, 'Error'); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(result); | ||
} | ||
} else { | ||
message = 'Device already exists' | ||
const result = factory.generateSuccessResponse(null, null, message); | ||
res.status(HttpStatus.OK).json(result); | ||
} | ||
} catch (error) { | ||
console.log("ERR", error); | ||
const result = factory.generateErrorResponse(null, null, 'Error'); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(result); | ||
} | ||
}); | ||
|
||
router.put('/update', async (req, res, next) => { | ||
const device = req.body.params; | ||
try { | ||
if (!device.id) { | ||
const result = factory.generateErrorResponse(null, null, 'Error'); | ||
res.status(HttpStatus.BAD_REQUEST).json(result); | ||
} | ||
const devices = await deviceStore.findById(device.id); | ||
let message = ''; | ||
if (devices.length == 1) { | ||
const resUpdate = await deviceStore.update(device); | ||
if (resUpdate && resUpdate.length == 1) { | ||
message = 'Device successfully updated' | ||
const result = factory.generateSuccessResponse(null, null, message); | ||
res.status(HttpStatus.OK).json(result); | ||
} else { | ||
const result = factory.generateErrorResponse(null, null, 'Error'); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(result); | ||
} | ||
} else { | ||
message = 'Device not found' | ||
const result = factory.generateSuccessResponse(null, null, message); | ||
res.status(HttpStatus.NOT_FOUND).json(result); | ||
} | ||
} catch (error) { | ||
const result = factory.generateErrorResponse(null, null, 'Error'); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(result); | ||
} | ||
}); | ||
|
||
module.exports = router; |
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