Skip to content

Commit

Permalink
Updated post + comment endpoints to use hasId function with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed May 17, 2016
1 parent aba2d41 commit 67116c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
31 changes: 4 additions & 27 deletions server/src/controllers/comments-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export default class CommentsController extends Controller
router.use(bodyParser.json({ type: 'application/vnd.api+json' }));

router.get("/comments", <any>[isAdmin, this.getComments.bind(this)]);
router.get("/users/:user/comments/:id", <any>[hasId, this.getComment.bind(this)]);
router.delete("/users/:user/comments/:id", <any>[canEdit, hasId, this.remove.bind(this)]);
router.put("/users/:user/comments/:id", <any>[canEdit, hasId, this.update.bind(this)]);
router.post("/comments/:target", <any>[canEdit, this.verifyTarget, this.create.bind(this)]);
router.get("/users/:user/comments/:id", <any>[hasId("id", "ID"), this.getComment.bind(this)]);
router.delete("/users/:user/comments/:id", <any>[canEdit, hasId("id", "ID"), this.remove.bind(this)]);
router.put("/users/:user/comments/:id", <any>[canEdit, hasId("id", "ID"), this.update.bind(this)]);
router.post("/posts/:postId/comments/:target?", <any>[canEdit, hasId("postId", "Post ID"), hasId("target", "Target ID"), this.create.bind(this)]);

// Register the path
e.use( "/api", router );
Expand Down Expand Up @@ -184,29 +184,6 @@ export default class CommentsController extends Controller
};
}

/**
* Checks the request for a target ID. This will throw an error if none is found, or its invalid
* @param {mp.IAuthReq} req
* @param {express.Response} res
* @param {Function} next
*/
private verifyTarget(req: mp.IAuthReq, res: express.Response, next: Function)
{
// Make sure the target id
if (!req.params.target)
{
okJson<mp.IResponse>( {
error: true,
message: "Please specify a target ID"
}, res);
}
// Make sure the target id format is correct
else if ( !mongodb.ObjectID.isValid(req.params.target))
{
errJson(new Error("Invalid target ID format"), res);
}
}

/**
* Attempts to remove a comment by ID
* @param {express.Request} req
Expand Down
18 changes: 9 additions & 9 deletions server/src/controllers/posts-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ export default class PostsController extends Controller
router.use(bodyParser.json());
router.use(bodyParser.json({ type: 'application/vnd.api+json' }));

router.get("/get-posts", <any>[getUser, this.getPosts.bind(this)]);
router.get("/get-post/:slug", <any>[getUser, this.getPost.bind(this)]);
router.get("/get-categories", this.getCategories.bind(this));
router.delete("/remove-post/:id", <any>[isAdmin, hasId, this.removePost.bind(this)]);
router.delete("/remove-category/:id", <any>[isAdmin, hasId, this.removeCategory.bind(this)]);
router.put("/update-post/:id", <any>[isAdmin, hasId, this.updatePost.bind(this)]);
router.post("/create-post", <any>[isAdmin, this.createPost.bind(this)]);
router.post("/create-category", <any>[isAdmin, this.createCategory.bind(this)]);
router.get("posts/get-posts", <any>[getUser, this.getPosts.bind(this)]);
router.get("posts/get-post/:slug", <any>[getUser, this.getPost.bind(this)]);
router.get("posts/get-categories", this.getCategories.bind(this));
router.delete("posts/remove-post/:id", <any>[isAdmin, hasId("id", "ID"), this.removePost.bind(this)]);
router.delete("posts/remove-category/:id", <any>[isAdmin, hasId("id", "ID"), this.removeCategory.bind(this)]);
router.put("posts/update-post/:id", <any>[isAdmin, hasId("id", "ID"), this.updatePost.bind(this)]);
router.post("posts/create-post", <any>[isAdmin, this.createPost.bind(this)]);
router.post("posts/create-category", <any>[isAdmin, this.createCategory.bind(this)]);

// Register the path
e.use( "/api/posts", router );
e.use( "/api", router );
}

/**
Expand Down

0 comments on commit 67116c0

Please sign in to comment.