Skip to content

Commit 2284755

Browse files
Andela DeveloperAndela Developer
Andela Developer
authored and
Andela Developer
committed
get all documents and paginations
1 parent 75f9717 commit 2284755

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

dist/server/controllers/documents.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,62 @@ function updateDocument(req, res) {
8585
});
8686
}
8787

88-
exports.default = { createDocument: createDocument, updateDocument: updateDocument };
88+
function getAllDocument(req, res) {
89+
var limit = req.query.limit;
90+
var offset = req.query.offset;
91+
if (req.decoded.roleId === 1) {
92+
return Document.findAndCountAll({
93+
limit: limit,
94+
offset: offset,
95+
where: {
96+
access: {
97+
$ne: 'private'
98+
}
99+
},
100+
include: [{
101+
model: User,
102+
attributes: ['userName', 'roleId']
103+
}]
104+
}).then(function (_ref) {
105+
var document = _ref.rows,
106+
count = _ref.count;
107+
108+
res.status(200).send({
109+
document: document,
110+
pagination: metaData(count, limit, offset)
111+
});
112+
}).catch(function (error) {
113+
return res.status(400).send(error);
114+
});
115+
} else if (req.decoded.roleId !== 1) {
116+
return Document.findAndCountAll({
117+
limit: limit,
118+
offset: offset,
119+
include: [{
120+
model: User,
121+
attributes: ['userName', 'roleId'],
122+
where: {
123+
roleId: req.decoded.roleId
124+
}
125+
}],
126+
where: {
127+
access: {
128+
$ne: 'private'
129+
}
130+
}
131+
132+
}).then(function (_ref2) {
133+
var document = _ref2.rows,
134+
count = _ref2.count;
135+
136+
res.status(200).send({
137+
document: document,
138+
pagination: metaData(count, limit, offset)
139+
});
140+
}).catch(function (error) {
141+
return res.status(400).send(error);
142+
});
143+
}
144+
}
145+
146+
exports.default = { createDocument: createDocument, updateDocument: updateDocument, getAllDocument: getAllDocument };

dist/server/routes/documents.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var router = _express2.default.Router();
2222

2323
router.route('/')
2424
/** GET /api/v1/documents - Get all documents */
25-
// .get(documentController.getDocuments)
25+
.get(_auth2.default.verifyToken, _documents2.default.getAllDocument)
2626

2727
/** POST /api/v1/documents - Create document */
2828
.post(_auth2.default.verifyToken, _documents2.default.createDocument);

server/controllers/documents.js

+57-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,60 @@ function updateDocument(req, res) {
6767
.catch(error => res.status(400).send(error));
6868
}
6969

70-
export default { createDocument, updateDocument };
70+
function getAllDocument(req, res) {
71+
const limit = req.query.limit;
72+
const offset = req.query.offset;
73+
if (req.decoded.roleId === 1) {
74+
return Document.findAndCountAll({
75+
limit,
76+
offset,
77+
where: {
78+
access: {
79+
$ne: 'private'
80+
}
81+
},
82+
include: [
83+
{
84+
model: User,
85+
attributes: ['userName', 'roleId']
86+
}
87+
]
88+
})
89+
.then(({ rows: document, count }) => {
90+
res.status(200).send({
91+
document,
92+
pagination: metaData(count, limit, offset),
93+
});
94+
})
95+
.catch(error => res.status(400).send(error));
96+
} else if (req.decoded.roleId !== 1) {
97+
return Document.findAndCountAll({
98+
limit,
99+
offset,
100+
include: [
101+
{
102+
model: User,
103+
attributes: ['userName', 'roleId'],
104+
where: {
105+
roleId: req.decoded.roleId
106+
},
107+
},
108+
],
109+
where: {
110+
access: {
111+
$ne: 'private'
112+
}
113+
},
114+
115+
})
116+
.then(({ rows: document, count }) => {
117+
res.status(200).send({
118+
document,
119+
pagination: metaData(count, limit, offset),
120+
});
121+
})
122+
.catch(error => res.status(400).send(error));
123+
}
124+
}
125+
126+
export default { createDocument, updateDocument, getAllDocument };

server/routes/documents.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const router = express.Router();
66

77
router.route('/')
88
/** GET /api/v1/documents - Get all documents */
9-
// .get(documentController.getDocuments)
9+
.get(auth.verifyToken, documentController.getAllDocument)
1010

1111
/** POST /api/v1/documents - Create document */
1212
.post(auth.verifyToken, documentController.createDocument);

0 commit comments

Comments
 (0)