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

Feat/add get routes #9

Merged
merged 2 commits into from
Jun 30, 2019
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
1 change: 1 addition & 0 deletions models/Reply.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require("mongoose");

const ReplySchema = new mongoose.Schema({
thread_id: { type: mongoose.Schema.Types.ObjectId, ref: "Thread" },
text: {
type: String,
required: true,
Expand Down
34 changes: 34 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ module.exports = function(app) {
return res.redirect(`/b/${board}`);
});
})
.get((req, res) => {
const board = req.params.board;
Thread.find(
{ board },
{ __v: 0, delete_password: 0, reported: 0 },
{ sort: { bumped_on: -1 }, limit: 10 },
)
.populate({
path: "replies",
select: "-delete_password -reported -__v",
options: { limit: 3 },
})
.exec((err, result) => {
if (err) return res.status(506).send(err);
return res.status(200).json(result);
});
});

app.route("/api/replies/:board").post((req, res) => {
if (!req.body.text.trim())
Expand All @@ -61,4 +78,21 @@ module.exports = function(app) {
},
);
});
})
.get((req, res) => {
const thread_id = req.query.thread_id;
Thread.findOne(
{ _id: thread_id },
{ __v: 0, delete_password: 0, reported: 0 },
{ sort: { bumped_on: -1 } },
)
.populate({
path: "replies",
select: "-delete_password -reported -__v",
})
.exec((err, result) => {
if (err) return res.status(506).send(err);
return res.status(200).json(result);
});
});
};
47 changes: 40 additions & 7 deletions tests/2_functional-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var assert = chai.assert;
var server = require("../server");

chai.use(chaiHttp);
let test_thread_id = "";

suite("Functional Tests", function() {
suite("API ROUTING FOR /api/threads/:board", function() {
Expand Down Expand Up @@ -62,8 +63,21 @@ suite("Functional Tests", function() {
});
});

suite('GET', function() {

suite("GET", function() {
test("Show recent threads", function(done) {
chai
.request(server)
.get("/api/threads/test-board")
.end(function(err, res) {
test_thread_id = res.body[0]._id;
assert.equal(res.status, 200);
assert.isArray(res.body, "res should be an array");
assert.isObject(res.body[0]);
assert.notProperty(res.body[0], "delete_password");
assert.notProperty(res.body[0], "reported");
done();
});
});
});

/*suite("DELETE", function() {});
Expand All @@ -78,7 +92,7 @@ suite("Functional Tests", function() {
.request(server)
.post("/api/replies/test-board")
.send({
thread_id: "5d17d37af205d2493dc39e5b",
thread_id: test_thread_id,
text: "This is my first reply to message board - 54321",
delete_password: "0d&2",
})
Expand All @@ -99,19 +113,38 @@ suite("Functional Tests", function() {
delete_password: "02$d&2",
})
.end(function(err, res) {
assert.equal(res.status, 500);
assert.equal(res.status, 403);
assert.equal(
res.text,
'Cannot update thread. error: CastError: Cast to ObjectId failed for value "Th1si2aInval1d" at path "_id" for model "Thread"',
"invalid Thread id",
"Invalid thread id error",
);
done();
});
});
});
/*
suite("GET", function() {});

suite("GET", function() {
test("Show entire test-board thread with all its replies", function(done) {
chai
.request(server)
.get("/api/replies/test-board")
.query({
thread_id: test_thread_id,
})
.end(function(err, res) {
assert.equal(res.status, 200);
assert.isObject(res.body);
assert.notProperty(res.body, "delete_password");
assert.notProperty(res.body, "reported");
assert.property(res.body, text);
assert.property(res.body, _id);
assert.isArray(res.body.replies, "Replies should be an array");
done();
});
});
});
/*
suite("PUT", function() {});

suite("DELETE", function() {});*/
Expand Down