-
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.
Merge pull request #4 from Billmike/feature/implement-id-validation
Ft:implement id validation
- Loading branch information
Showing
17 changed files
with
4,745 additions
and
1,804 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,50 @@ | ||
import db from '../models'; | ||
import serverError from '../errorHandler/serverError'; | ||
import validateInput from '../validators/validateAddCenter'; | ||
|
||
const { Center } = db; | ||
|
||
class CenterController { | ||
static createCenter(request, response) { | ||
|
||
const { errors, isValid } = validateInput(request.body); | ||
|
||
if (!isValid) { | ||
return response.status(400).json(errors); | ||
} | ||
|
||
const { | ||
name, state, location, | ||
description, image, capacity, equipments, availability, price | ||
} = request.body; | ||
|
||
if (request.userDetails.username !== 'adminuser') { | ||
return response.status(401).json({ | ||
message: 'You need admin priviledges to access this resource' | ||
}); | ||
} | ||
return Center.create({ | ||
name, | ||
state, | ||
image, | ||
description, | ||
location, | ||
capacity, | ||
equipments, | ||
availability, | ||
price, | ||
owner: request.userDetails.id | ||
}).then((center) => { | ||
return response.status(201).json({ | ||
message: 'Center created successfully.', | ||
centerDetails: center | ||
}); | ||
}).catch((error) => { | ||
return response.status(500).json({ | ||
message: error.message | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default CenterController; |
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,3 @@ | ||
const serverError = 'Something went wrong! We are currently working on resolving this issue.'; | ||
|
||
export default serverError; |
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,50 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import db from '../models'; | ||
import serverError from '../errorHandler/serverError'; | ||
|
||
const { User } = db; | ||
|
||
class SessionControl { | ||
static isAdmin(request, response, next) { | ||
|
||
} | ||
static isLoggedIn(request, response, next) { | ||
request.token = request.headers['x-access-token'] | ||
|| request.query.token || request.headers.token; | ||
if (!request.token) { | ||
return response.status(401).json({ | ||
message: 'Please sign into your account to access this resource.' | ||
}); | ||
} | ||
next(); | ||
} | ||
|
||
static isUser(request, response, next) { | ||
let verifyToken; | ||
request.userDetails = {}; | ||
try { | ||
verifyToken = jwt.verify(request.token, process.env.SECRET); | ||
} catch (error) { | ||
response.status(400).json({ | ||
message: 'Unable to verify user.' | ||
}); | ||
} | ||
User.findById(verifyToken.id) | ||
.then((user) => { | ||
if (!user) { | ||
response.status(400).json({ | ||
message: 'Unable to verify user.' | ||
}); | ||
return next(); | ||
} | ||
request.userDetails = verifyToken; | ||
return next(); | ||
}).catch(() => { | ||
return response.status(500).json({ | ||
message: serverError | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default SessionControl; |
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
Oops, something went wrong.