Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
anwarulislam committed May 18, 2019
1 parent c63f84d commit c8372d5
Show file tree
Hide file tree
Showing 6 changed files with 724 additions and 26 deletions.
25 changes: 15 additions & 10 deletions controllers/surahController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ module.exports.getAllSurah = async (req, res) => {
// console.log(data);
// })

const surah = require('./../surah.json')
const sura = require('./../surah.json')

res.json(surah)
res.json(sura)
}

module.exports.getSurah = async (req, res) => {
const options = {
page: req.query.p || 1,
limit: 10,
customLabels: 'hlaksdjf'
limit: 10
};
let surah = await Surah.paginate({ sura: req.params.surah_no }, options, 5, (error, pageCount, paginatedResults) => {
let sura = await Surah.paginate({ sura: req.params.sura }, options, 5, (error, pageCount, paginatedResults) => {
if (error) {
console.error(error);
} else {
Expand All @@ -29,12 +28,18 @@ module.exports.getSurah = async (req, res) => {
}
})

if (surah.prevPage) {
surah.prevLink = req.protocol + '://' + req.get('host') + req.path + `?p=${surah.prevPage}`
if (sura.prevPage) {
sura.prevLink = req.protocol + '://' + req.get('host') + req.path + `?p=${sura.prevPage}`
}
if (surah.nextPage) {
surah.nextLink = req.protocol + '://' + req.get('host') + req.path + `?p=${surah.nextPage}`
if (sura.nextPage) {
sura.nextLink = req.protocol + '://' + req.get('host') + req.path + `?p=${sura.nextPage}`
}

res.json(surah)
res.json(sura)
}

module.exports.getAyah = async (req, res) => {

let ayah = await Surah.findOne({ sura: req.params.sura, ayah: req.params.ayah })
res.json(ayah)
}
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express')
const app = express()
const expressSession = require('express-session')
const cookieParser = require('cookie-parser')
const graphqlHTTP = require('graphql-yoga')

require('dotenv').config()
require('./dbconnect')
Expand All @@ -15,6 +16,12 @@ app.use(expressSession({
}))
app.use(cookieParser())

const schema = require('./schema')
app.use('/gql', graphqlHTTP({
schema: schema,
graphiql: true
}))

app.listen(process.env.PORT, () => {
console.log(`Server is running at http://localhost:${process.env.PORT}`);
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"express-session": "^1.16.1",
"express-validator": "^5.3.1",
"graphql": "^14.3.0",
"graphql-yoga": "^1.17.4",
"mongoose": "^5.5.7",
"mongoose-paginate-v2": "^1.2.1",
"nodemon": "^1.19.0"
Expand Down
3 changes: 2 additions & 1 deletion routes/quran.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const surahController = require('./../controllers/surahController')

Router.get('/', surahController.getAllSurah)

Router.get('/:surah_no', surahController.getSurah)
Router.get('/:sura', surahController.getSurah)
Router.get('/:sura/:ayah', surahController.getAyah)

module.exports = Router
38 changes: 38 additions & 0 deletions schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const graphql = require('graphql');
const _ = require('lodash');

const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;

// dummy data
var books = [
{ name: 'Name of the Wind', genre: 'Fantasy', id: '1' },
{ name: 'The Final Empire', genre: 'Fantasy', id: '2' },
{ name: 'The Long Earth', genre: 'Sci-Fi', id: '3' },
];

const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: GraphQLString },
name: { type: GraphQLString },
genre: { type: GraphQLString }
})
});

const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLString } },
resolve(parent, args) {
// code to get data from db / other source
return _.find(books, { id: args.id });
}
}
}
});

module.exports = new GraphQLSchema({
query: RootQuery
});
Loading

0 comments on commit c8372d5

Please sign in to comment.