-
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.
Added pagination to tags and users route(s)
- Loading branch information
1 parent
6336077
commit 77d0323
Showing
5 changed files
with
94 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules/ | ||
*.swp |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,36 +1,34 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const knex = require('../connect'); | ||
const setupPaginator = require('knex-paginator'); | ||
|
||
setupPaginator(knex); | ||
|
||
//Select all tags (returns 15 results per query) | ||
router.get("/tags", (req, res) => { | ||
knex('Tag').select().then((tags) => { | ||
res.json({ | ||
data: tags | ||
}); | ||
} | ||
); | ||
knex.select().from('Tag') | ||
.paginate(15, req.query.page || 1, true) | ||
.then((tags) => { | ||
res.status(200); | ||
res.json({tags}); | ||
}); | ||
}); | ||
|
||
router.get("/tag/:tagname", (req, res) => { | ||
knex('Tag').select().where('name', req.params.tagname).then((tag) => { | ||
res.json({ | ||
data: tag | ||
}); | ||
} | ||
); | ||
res.json({tag}); | ||
}); | ||
}); | ||
|
||
// Selects all questions with specified tag name | ||
router.get("/tag/:tagname/questions", (req, res) => { | ||
// First make a subquery getting question ids with specified tag | ||
var subquery = knex('Question_Tags').select('question_id').where('tag_name', req.params.tagname); | ||
let subquery = knex('Question_Tags').select('question_id').where('tag_name', req.params.tagname); | ||
// Now use those ids to get questions | ||
knex('Question').select().whereIn('id', subquery).then((questions) => { | ||
res.json({ | ||
data: questions | ||
}); | ||
} | ||
); | ||
res.json({questions}); | ||
}); | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const knex = require('../connect'); | ||
const setupPaginator = require('knex-paginator'); | ||
setupPaginator(knex); | ||
|
||
//Select all users (returns 15 results per query) | ||
router.get("/users", (req, res) => { | ||
knex('_User').select().then((users) => { | ||
res.json({ | ||
data: users | ||
}); | ||
} | ||
); | ||
knex.select().from('_User') | ||
.paginate(15, req.query.page || 1, true) | ||
.then((users) => { | ||
res.status(200); | ||
res.json({users}); | ||
}); | ||
}); | ||
|
||
//Select Single User | ||
router.get("/user/:id", (req, res) => { | ||
knex('_User').select().where('id', Number(req.params.id)).then((user) => { | ||
res.json({ | ||
data: user | ||
}); | ||
} | ||
); | ||
knex.select().from('_User').where('id', Number(req.params.id)).then((user) => { | ||
res.json({user}); | ||
}); | ||
}); | ||
|
||
// Select all questions by specifed user | ||
//Select all questions by specifed user | ||
router.get("/user/:id/questions", (req, res) => { | ||
knex('Question').select().where('user_id', Number(req.params.id)).then((questions) => { | ||
res.json({ | ||
data: questions | ||
}); | ||
} | ||
); | ||
knex.select().from('Question').where('user_id', Number(req.params.id)).then((questions) => { | ||
res.json({questions}); | ||
}); | ||
}); | ||
|
||
module.exports = router; |