Skip to content
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

User and Stack Routes Structured #129

Merged
merged 4 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions stackle_api/app/lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Validator.prototype.validateAddingPost = function() {

if (!!!~Object.keys(this.input).indexOf('tags')) { throw new Error('Attribute tags is missing'); }

if (!!!~Object.keys(this.input).indexOf('link_issue')) { throw new Error('Attribute link_issue is missing'); }
if (!!!~Object.keys(this.input).indexOf('linkIssue')) { throw new Error('Attribute link_issue is missing'); }

if (!!!~Object.keys(this.input).indexOf('user')) { throw new Error('Attribute user is missing'); }

Expand All @@ -44,6 +44,18 @@ Validator.prototype.validateGetPost = function() {
return this.input;
};


Validator.prototype.validateUserId = function() {
if (!this.input) { throw new Error('Input is undefined'); }

if (!Object.keys(this.input).length) { throw new Error('Empty Object has been passed'); }

if (!!!~Object.keys(this.input).indexOf('userId')) { throw new Error('Attribute userId is missing'); }

return this.input;
};


Validator.prototype.validateDeletePost = function() {
if (!this.input) { throw new Error('Input is undefined'); }

Expand Down Expand Up @@ -84,6 +96,17 @@ Validator.prototype.validateGetOrganisationDetails = function() {
return this.input;
};


Validator.prototype.validateStackId = function() {
if (!this.input) { throw new Error('Input is undefined'); }

if (!Object.keys(this.input).length) { throw new Error('Empty Object has been passed'); }

if (!!!~Object.keys(this.input).indexOf('stackId')) { throw new Error('Attribute stackId is missing'); }

return this.input;
};

//Validating PostId
Validator.prototype.validateCommentOnPost = function() {
if (!this.input) { throw new Error('Input is undefined'); }
Expand Down Expand Up @@ -155,9 +178,9 @@ Validator.prototype.validateCreateStack = function() {

if (!!!~Object.keys(this.input).indexOf('stackleUrl')) { throw new Error('Attribute stackleUrl is missing'); }

if (!!!~Object.keys(this.input).indexOf('githubUrl')) { throw new Error('Attribute githubUrl is missing'); }
// if (!!!~Object.keys(this.input).indexOf('githubUrl')) { throw new Error('Attribute githubUrl is missing'); }

if (!!!~Object.keys(this.input).indexOf('created_user')) { throw new Error('Attribute created_user is missing'); }
if (!!!~Object.keys(this.input).indexOf('createdUser')) { throw new Error('Attribute createdUser is missing'); }

return this.input;
};
Expand All @@ -177,13 +200,14 @@ Validator.prototype.validateUserSubscribeStack = function() {

if (!Object.keys(this.input).length) { throw new Error('Empty Object has been passed'); }

if (!!!~Object.keys(this.input).indexOf('stackName')) { throw new Error('Attribute stackName is missing'); }
if (!!!~Object.keys(this.input).indexOf('stackId')) { throw new Error('Attribute stackId is missing'); }

if (!!!~Object.keys(this.input).indexOf('userId')) { throw new Error('Attribute userId is missing'); }

return this.input;
};


Validator.prototype.validateGetUserSubscribeStack = function() {
if (!this.input) { throw new Error('Input is undefined'); }

Expand All @@ -199,14 +223,13 @@ Validator.prototype.validateCreateNewUser = function() {

if (!Object.keys(this.input).length) { throw new Error('Empty Object has been passed'); }

if (!!!~Object.keys(this.input).indexOf('subscribed_stacks'))
{ throw new Error('Attribute subscribed_stacks is missing'); }

if (!!!~Object.keys(this.input).indexOf('gitlab')) { throw new Error('Attribute gitlab is missing'); }
if (!!!~Object.keys(this.input).indexOf('token')) { throw new Error('Attribute token is missing'); }

if (!!!~Object.keys(this.input).indexOf('userId')) { throw new Error('Attribute userId is missing'); }

if (!!!~Object.keys(this.input).indexOf('github')) { throw new Error('Attribute github is missing'); }
if (!!!~Object.keys(this.input).indexOf('email')) { throw new Error('Attribute email is missing'); }

if (!!!~Object.keys(this.input).indexOf('name')) { throw new Error('Attribute name is missing'); }

return this.input;
};
Expand Down
2 changes: 0 additions & 2 deletions stackle_api/app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ postSchema.statics.getAllByOrg = function(request, response){
}
}


//Comment on a post
postSchema.statics.commentById = function(request, response){
try {
Expand Down Expand Up @@ -156,7 +155,6 @@ postSchema.statics.commentById = function(request, response){
}
}


//get All comments for a single post
postSchema.statics.getAllComments = function(request, response){
try {
Expand Down
106 changes: 102 additions & 4 deletions stackle_api/app/models/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,109 @@ const returnWithResponse = require('../lib/returnWithResponse');


const stackSchema = mongoose.Schema({
name: { type: String, required: true },
name: { type: String, required: true, unique: true},
description: { type: String, required: true },
stackleUrl: { type: String, required: true },
stackleUrl: { type: String, required: true, unique: true},
githubUrl: String,
createdUser: { type: String, required: true }
createdUser: { type: String, required: true, unique: false}
});

module.exports = mongoose.model('Stack', stackSchema);

//create a stack
stackSchema.statics.createStack = function(request, response){
try {
const validator = new Validator(request.body);
const input = validator.validateCreateStack();
const stack = new Stack(input);
stack.save((error, insertedStack) => {
if (error) {
return returnWithResponse.configureReturnData({ status: 400, success: false, result: error }, response);
}

return returnWithResponse.configureReturnData({ status: 200, success: true, result: insertedStack._id }, response);
});
} catch (validationError) {
return returnWithResponse.configureReturnData({ status: 502, success: false, result: validationError.toString() }, response);
}
}

//to get All stacks
stackSchema.statics.getAll = function(request, response){
this.find({}, (error, stacksDetails) => {
if (error) {
return returnWithResponse.configureReturnData({ status: 400, success: false, result: error }, response);
}

return returnWithResponse.configureReturnData({ status: 200, success: true, result: stacksDetails }, response);
});
}

//to get stack by Name
stackSchema.statics.getByName = function(request, response){
try {
const validator = new Validator(request.params);
const input = validator.validateGetOrganisationDetails();
Stack.findOne({ name: input.organisationName }, (error, organisationDetails) => {
if (error) {
return returnWithResponse.configureReturnData({ status: 400, success: false, result: error }, response);
}

return returnWithResponse.configureReturnData({ status: 200, success: true, result: organisationDetails }, response);
});
} catch (validationError) {
return returnWithResponse.configureReturnData({ status: 502, success: false, result: validationError.toString() }, response);
}
}

//to get Stack by Id
stackSchema.statics.getById = function(request, response){
try {
const validator = new Validator(request.params);
const input = validator.validateStackId();
Stack.findOne({ _id: input.stackId }, (error, organisationDetails) => {
if (error) {
return returnWithResponse.configureReturnData({ status: 400, success: false, result: error }, response);
}

return returnWithResponse.configureReturnData({ status: 200, success: true, result: organisationDetails }, response);
});
} catch (validationError) {
return returnWithResponse.configureReturnData({ status: 502, success: false, result: validationError.toString() }, response);
}
}

//Only For Developer Mode.

//Delete Stack by ID
stackSchema.statics.deleteById = function(request, response){
try {
const validator = new Validator(request.params);
const input = validator.validateStackId();

this.remove({_id: input.stackId}, function(err){
if(err)
return returnWithResponse.configureReturnData({ status: 400, success: false, result: err }, response);

return returnWithResponse.configureReturnData({ status: 200, success: true, result: `Stack data removed with id : ${input.stackId}` }, response);

});
}
catch (validationError) {
return returnWithResponse.configureReturnData({ status: 502, success: false, result: validationError.toString() }, response);
}
}

//to delete all post
stackSchema.statics.clearAll = function(request, response){
this.remove({}, function(err){
if(err)
return returnWithResponse.configureReturnData({ status: 400, success: false, result: err }, response);


return returnWithResponse.configureReturnData({ status: 200, success: true, result: `All Stack data removed.` }, response);

});
}

const Stack = mongoose.model('Stack', stackSchema);
module.exports = Stack;
Loading