Server application for the ParkingLotTracker project.
Version: 0.8.0
- Installation
- How to run
- API Overview
- API Documentation
- Authenticate user
- GET all users
- GET a user
- POST new user
- GET all parkinglots
- GET a parkinglot
- POST new parkinglot
- PUT parkinglot
- GET all parkinglogs
- GET a parkinglog
- DELETE a parkinglog
- POST new parkinglog
- Increment parkinglog
- PUT parkinglog
- GET latest registered parkinglog
- GET a parkinglots latest registered parkinglog
- Troubleshoot
- Tests
$ npm install
The database script is located at scripts/database.sql.
To change the database settings go to config.
- config/default.json is the production database.
- config/dev.json is the development database.
- config/test.json is for the test database.
To enable additional settings modify src/dbconnection.js.
To find out which options that are available look at:
To start on systems that uses node
$ npm start
To start on systems that uses nodejs
$ npm run nodejs
To change which environmental mode that is used (including database) modify the package.json start script. If you have trouble starting the project look at
API Endpoint | Method | Auth | Description |
---|---|---|---|
auth | |||
/api/v0/auth | POST | no | Used to authenticate a user. |
users | |||
/api/v0/users | GET | admin | Gets all users |
/api/v0/users/:id | GET | user | Gets a single user, auth token must belong to the user. |
/api/v0/users | POST | no | Creates a new user. |
parkingLots | |||
/api/v0/parkinglots | GET | no | Gets all parkinglots |
/api/v0/parkinglots/:id | GET | no | Gets a Single parkinglot |
/api/v0/parkinglots | POST | admin | Creates new parkinglot |
/api/v0/parkinglots | PUT | admin | Updates a parkinglot |
parkingLogs | |||
/api/v0/parkinglogs | GET | no | Gets all parkinglogs |
/api/v0/parkinglogs/:id | GET | no | Gets a Single parkinglog |
/api/v0/parkinglogs/:id | DELETE | no | Deletes a single parkinglog |
/api/v0/parkinglogs | POST | admin | Creates new parkinglog |
/api/v0/parkinglogs/increment | POST | admin | Creates a new parkinglog with one more OR less parked car then the former latest parkinglog. |
/api/v0/parkinglogs | PUT | admin | Updates a parkinglog, only the currentParked value can be changed. |
/api/v0/parkinglogs/latest | GET | no | Gets a single parkinglog of latest date. |
/api/v0/parkinglogs/latest/:id | GET | no | Gets latest registered parkinglog of a specific parkinglot. |
Authenticates a user, if user credentials match whats in the database.
-
URL
/api/v0/auth
-
Method:
POST
-
Headers:
- Content-Type application/json
-
URL Params
None
-
Data Params
{"deviceId":"f07a13984f6d116a","password":"pwd"}
-
Success Response:
- Code: 200
Content:{"success":true,"message":"Enjoy your token!" ,"token":"eyJhbGciOiJIUzI1NiIhdhahadCI6IkpXVCJ5...}
- Code: 200
-
Error Response:
-
Code: 401
Content:{"success":false,"message":"Reason for failure."}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/auth", dataType: "json", type : "POST", Data: '{"deviceId":"f07a13984f6d116a","password":"pwd"}', success : function(r) { console.log(r); } });
Returns the all users in json data format.
-
URL
/api/v0/users
-
Method:
GET
-
Headers:
- Content-Type application/json
- x-access-token adminToken
-
URL Params
None
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"users":[{"deviceId":"f07a13984f6d116a","name":"Ole","admin":0,"password":"pwd"} ,{"deviceId":"fa912c713f6d932a","name":"Dole","admin":1,"password":"pwd"}]}
- Code: 200
-
Error Response:
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/users", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Gets a single user, auth token must belong to the user.
-
URL
/api/v0/users/:id
-
Method:
GET
-
Headers:
- Content-Type application/json
- x-access-token userToken
-
URL Params
id=[string]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"users":[{"deviceId":"f07a13984f6d116a","name":"Ole","admin":0,"password":"pwd"}]}
- Code: 200
-
Error Response:
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/users/f07a13984f6d116a", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Creates a new user
-
URL
/api/v0/users
-
Method:
POST
-
Headers:
- Content-Type application/json
-
URL Params
None
-
Data Params
{"deviceId":"f07a13984f6d116a","name":"Ole","password":"pwd"}
-
Success Response:
- Code: 201
Content:{"Message" : "User Added"}
- Code: 201
-
Error Response:
-
Code: 401
Content:{"success":false,"message":"Failed to authenticate token."}
OR{"success":false,"message":"No token provided."}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/users", dataType: "json", type : "POST", Data: '{"deviceId":"f07a13984f6d116a","name":"Ole","password":"pwd"}', success : function(r) { console.log(r); } });
Returns the all parkinglots
-
URL
/api/v0/parkinglots
-
Method:
GET
-
Headers:
- Content-Type application/json
-
URL Params
None
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"parkingLots":[{"id":1,"name":"Student Organisasjonen","capacity":100,"reservedSpaces":10,"lat":58.1634301,"lng":8.0063132} ,{"id":2,"name":"Hokus Pokus Barnehage","capacity":70,"reservedSpaces":7,"lat":58.1644578,"lng":8.0005553}]}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "No parking lots found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglots", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Gets a single parkinglot.
-
URL
/api/v0/parkinglots/:id
-
Method:
GET
-
Headers:
- Content-Type application/json
-
URL Params
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"parkingLots":[{"id":1,"name":"Student Organisasjonen","capacity":100,"reservedSpaces":10,"lat":58.1634301,"lng":8.0063132}]}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "Parking lot not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglots/1", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Creates a new parkinglots
-
URL
/api/v0/parkinglots
-
Method:
POST
-
Headers:
- Content-Type application/json
- x-access-token adminToken
-
URL Params
None
-
Data Params
{"name":"Student Organisasjonen","capacity":100,"reservedSpaces":10,"lat":58.1634301,"lng":8.0063132}
-
Success Response:
- Code: 201
Content:{"Message":"Parking Lot Added"}
- Code: 201
-
Error Response:
-
Code: 400
Content:{"success":false, "The parking lots id in the request body is not defined"}
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglots", dataType: "json", type : "POST", Data: '{"name":"Student Organisasjonen","capacity":100,"reservedSpaces":10,"lat":58.1634301,"lng":8.0063132}', success : function(r) { console.log(r); } });
Updates an existing parkinglot
-
URL
/api/v0/parkinglots
-
Method:
PUT
-
Headers:
- Content-Type application/json
- x-access-token adminToken
-
URL Params
None
-
Data Params
{"id":1,"name":"Student Organisasjonen","capacity":100,"reservedSpaces":20,"lat":58.1634301,"lng":9.0063132}
-
Success Response:
- Code: 200
Content:{"Message":"Parking Lot Updated"}
- Code: 200
-
Error Response:
-
Code: 400
Content:{"success":false, "The parking lots id in the request body is not defined"}
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 204
Content:{"success": false, "message": "Parking log not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglots", dataType: "json", type : "PUT", Data: '{"id":1,"name":"Student Organisasjonen","capacity":100,"reservedSpaces":20,"lat":58.1634301,"lng":9.0063132}', success : function(r) { console.log(r); } });
Returns the all parkinglogs
-
URL
/api/v0/parkinglogs
-
Method:
GET
-
Headers:
- Content-Type application/json
-
URL Params
None
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"parkingLogs":[{"id":1,"currentParked":28,"historicParkCount":1337,"logDate":"2016-12-31T09:59:59.000Z","parkingLot_id":1} ,{"id":2,"currentParked":29,"historicParkCount":1338,"logDate":"2017-01-30T10:53:54.000Z","parkingLot_id":1}]}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "No parking logs found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkingLogs", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Gets a single parkinglog.
-
URL
/api/v0/parkinglogs/:id
-
Method:
GET
-
Headers:
- Content-Type application/json
-
URL Params
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"parkingLogs":[{"id":1,"currentParked":28,"historicParkCount":1337,"logDate":"2016-12-31T09:59:59.000Z","parkingLot_id":1}]}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "Parking log not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs/1", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Deletes a parkinglog.
-
URL
/api/v0/parkinglogs/:id
-
Method:
DELETE
-
Headers:
- Content-Type application/json
-
URL Params
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"Message":"Deleted parking log with id 1"}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "Parking log not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs/1", dataType: "json", type : "DELETE", success : function(r) { console.log(r); } });
Creates a new parkinglog
-
URL
/api/v0/parkinglogs
-
Method:
POST
-
Headers:
- Content-Type application/json
- x-access-token adminToken
-
URL Params
None
-
Data Params
{"currentParked":10,"parkingLot_id":1}
-
Success Response:
- Code: 201
- Code: 201
-
Error Response:
-
Code: 400
Content:{"success":false, "message":"request body is missing parkingLot_id}
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs", dataType: "json", type : "POST", Data: '{"currentParked":10,"parkingLot_id":1}', success : function(r) { console.log(r); } });
Creates a new parkinglog with one more OR less parked car then the former latest parkinglog of that specific parkinglot
-
URL
/api/v0/parkinglogs/increment
-
Method:
POST
-
Headers:
- Content-Type application/json
- x-access-token adminToken
-
URL Params
None
-
Data Params examples:
{"increment":1,"parkingLot_id":1}
{"increment":5,"parkingLot_id":1}
{"increment":-1,"parkingLot_id":1}
-
Success Response:
- Code: 201
- Code: 201
-
Error Response:
-
Code: 400
Content:{"success":false, "message":"request body is missing parkingLot_id}
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs/increment", dataType: "json", type : "POST", Data: '{"increment":1,"parkingLot_id":1}', success : function(r) { console.log(r); } });
Updates an existing parkinglog
-
URL
/api/v0/parkinglogs
-
Method:
PUT
-
Headers:
- Content-Type application/json
- x-access-token adminToken
-
URL Params
None
-
Data Params
{"id":29,"currentParked":10,"parkingLot_id":1}
-
Success Response:
- Code: 200
- Code: 200
-
Error Response:
-
Code: 400
Content:{"success":false, "message":"request body is missing id and/or currentParked"}
-
Code: 401
Content:{"success":false, "message":"The reason for failure"}
-
Code: 204
Content:{"success": false, "message": "Parking log not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs", dataType: "json", type : "PUT", Data: '{"id":29,"currentParked":10,"parkingLot_id":1}', success : function(r) { console.log(r); } });
Gets latest registered parkinglog regardless of parkinglot.
-
URL
/api/v0/parkinglogs/latest
-
Method:
GET
-
Headers:
- Content-Type application/json
-
URL Params
None
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"parkingLogs":[{"id":1,"currentParked":28,"historicParkCount":1337,"logDate":"2016-12-31T09:59:59.000Z","parkingLot_id":1}]}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "Parking log not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs/latest", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Gets latest registered parkinglog of a specific parkinglot.
-
URL
/api/v0/parkinglogs/latest/:id
-
Method:
GET
-
Headers:
- Content-Type application/json
-
URL Params
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"parkingLogs":[{"id":1,"currentParked":28,"historicParkCount":1337,"logDate":"2016-12-31T09:59:59.000Z","parkingLot_id":1}]}
- Code: 200
-
Error Response:
-
Code: 204
Content:{"success": false, "message": "Parking log not found"}
-
Code: 500
Content:{"success":false,"message":"Internal Server Error."}
-
-
Sample Call:
$.ajax({ url: "/api/v0/parkinglogs/latest/1", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
If you have trouble starting the project try to run it by typing the command:
$ NODE_ENV=dev nodejs src/server.js"
If adding new test files, make certain that the file serverErrorPaths.test.js is the last test file to run. This test file will drop the test database, so this database needs to be regenerated.
To run tests
$ npm test
Warning, both these scripts will end up dropping the test database, the test database need to be regenerated or the next tests will fail. To avoid this, comment out the code in serverErrorPaths.test.js. HOWEVER! Be sure the uncomment this code before next git commit.
To run the complete test suite
$ npm run coveralls
These script uses the test database.