Implemented basic messages api

This commit is contained in:
Chris Blanchard 2015-12-28 13:21:35 +00:00
parent 67e24897d1
commit 6d75b29edc
3 changed files with 150 additions and 25 deletions

View file

@ -1,10 +1,12 @@
"use strict"; "use strict";
var path = require("path"); const path = require("path");
var winston = require("winston"); const winston = require("winston");
var config = require("./config.js"); const config = require("./config.js");
var Gather = require("../lib/gather/gather_singleton"); const Gather = require("../lib/gather/gather_singleton");
var cors = require("cors"); const mongoose = require("mongoose");
const Message = mongoose.model("Message");
const cors = require("cors");
module.exports = app => { module.exports = app => {
app.use(cors()); app.use(cors());
@ -29,12 +31,43 @@ module.exports = app => {
}); });
app.get("/messages", (request, response) => { app.get("/messages", (request, response) => {
if (request.is("json")) { response.format({
// To Implement json: function() {
response.end("") const limit = parseInt(request.query.limit, 10) || 250;
} else { const page = parseInt(request.query.page, 10) || 0;
let query = {};
let searchTerm = request.query.query;
if (searchTerm) {
query = {
$text: {
$search: searchTerm
}
};
}
Message
.find(query)
.limit(limit)
.skip(page * limit)
.sort({createdAt: -1})
.exec((error, messages) => {
if (error) {
winston.error(error);
return response.status(500).json({
message: "An error occurred",
error: JSON.stringify(error)
});
}
response.status(200).json({
messages: messages,
page: page,
limit: limit
});
});
},
default: function() {
response.render("messages.hbs"); response.render("messages.hbs");
} }
})
}); });
app.get("*", (request, response) => { app.get("*", (request, response) => {

View file

@ -15,8 +15,7 @@ var messageSchema = new Schema({
messageSchema.index({ createdAt: -1 }); messageSchema.index({ createdAt: -1 });
messageSchema.index({ deleted: 1, createdAt: -1 }); messageSchema.index({ deleted: 1, createdAt: -1 });
messageSchema.index({ content: "text", "author.username": "text" });
// Instance Methods
messageSchema.methods.toJson = function () { messageSchema.methods.toJson = function () {
return { return {

View file

@ -1,33 +1,126 @@
"use strict"; "use strict";
var helper = require("./helpers/index.js"); const async = require("async");
var request = require("supertest"); const request = require("supertest");
var assert = require("chai").assert; const assert = require("chai").assert;
var app = helper.app; const helper = require("./helpers/index.js");
const Message = helper.Message;
const app = helper.app;
let user, messages;
describe("Messages", () => { describe("Messages", () => {
before(done => {
user = helper.createUser();
helper.clearDb(done);
});
beforeEach(done => { beforeEach(done => {
done(); messages = [];
async.timesSeries(6, (n, next) => {
Message.create({
author: {
username: user.username,
avatar: user.avatar
},
content: "Message " + n
}, (error, message) => {
if (error) return next(error);
messages.push(message);
next();
});
}, done);
}); });
afterEach(done => { afterEach(done => {
done(); helper.clearDb(done);
}); });
describe("#Index", () => { describe("#Index", () => {
describe("JSON Api", () => { describe("JSON Api", () => {
it ("returns most recent messages"); it ("returns most recent messages", done => {
it ("is sensitive to limit"); request(app)
it ("returns a maximum of last 250 messages"); .get("/messages")
it ("is sensitive to pagination"); .set("Accept", "application/json")
it ("is sensitive to user"); .expect("Content-Type", /json/)
it ("is sensitive to search terms"); .expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 6);
assert.equal(result.limit, 250);
assert.equal(result.page, 0);
done();
}); });
});
it ("is sensitive to limit", done => {
request(app)
.get("/messages")
.query({
limit: 1
})
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 1);
assert.equal(result.limit, 1);
assert.equal(result.page, 0);
assert.equal(result.messages[0].content, "Message 5");
done();
});
});
it ("returns a maximum of last 250 messages");
it ("is sensitive to pagination", done => {
request(app)
.get("/messages")
.query({
limit: 1,
page: 2
})
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 1);
assert.equal(result.limit, 1);
assert.equal(result.page, 2);
assert.equal(result.messages[0].content, "Message 3");
done();
});
});
it ("is sensitive to search terms", done => {
request(app)
.get("/messages")
.query({
query: "5"
})
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 1);
assert.equal(result.limit, 250);
assert.equal(result.page, 0);
assert.equal(result.messages[0].content, "Message 5");
done();
});
});
});
describe("HTML Browser", () => { describe("HTML Browser", () => {
it ("renders message browser", done => { it ("renders message browser", done => {
request(app) request(app)
.get("/messages") .get("/messages")
.set("Accept", "text/html; charset=utf-8") .set("Accept", "text/html; charset=utf-8")
.expect('Content-Type', /html/) .expect("Content-Type", /html/)
.expect(200) .expect(200)
.end((error, response) => { .end((error, response) => {
if (error) return done(error); if (error) return done(error);