Skip to content

Commit

Permalink
add puffs endpoint (#29)
Browse files Browse the repository at this point in the history
* add puff endpoint.
  • Loading branch information
nandub authored Jul 9, 2018
1 parent 008aeec commit 4627c46
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 32 deletions.
6 changes: 4 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ const app = new Express();
const root = path.normalize(`${__dirname}/../..`);
app.set('appPath', `${root}client`);
app.use(bodyParser.urlencoded({
extended: true
extended: true,
limit: process.env.REQUEST_LIMIT
}));
app.use(bodyParser.json({
type: function() {
return true;
}
},
limit: process.env.REQUEST_LIMIT
}));

app.use(cookieParser(process.env.SESSION_SECRET));
Expand Down
6 changes: 3 additions & 3 deletions server/src/controllers/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const jwt = require('../../services/jwt');
* Login User
*/
exports.login = (req, res) => {
req.log.debug('POST/login');
req.log.debug('POST/Auth/login');
const email = req.body.email;
const password = req.body.password;
//
Expand All @@ -30,8 +30,8 @@ exports.login = (req, res) => {
res.status(200)
.send({
login: true,
token: jwt.createToken(user),
user: user,
username: user.username,
token: jwt.createToken(user)
});
} else {
res.status(400)
Expand Down
168 changes: 168 additions & 0 deletions server/src/controllers/puffs/puff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
const Puff = require('../../models/puff');
const User = require('../../models/user');

/**
* List Puffs
*/
exports.list = (req, res) => {
req.log.debug("GET/Puff/list");
return Puff.find({}).populate('author').then((puffs) => {
if (!puffs) {
res.status(404)
.send({
message: 'Puff not Found',
});
} else {
res.status(200)
.send({
puffs: puffs,
});
}
}).catch((err) => {
req.log.error(err);
res.status(500)
.send({
message: err,
});
});
};

/**
* Get Puff by ID
*/
exports.get = (req, res) => {
req.log.debug("GET/Puff/get");
return Puff.findOne({
puffId: req.params.id
}).populate('author').then((puff) => {
if (!puff) {
res.status(404)
.send({
message: 'Puff not Found',
});
}
else {
res.status(200)
.send({
puff: puff,
});
}
}).catch((err) => {
req.log.error(err);
res.status(500)
.send({
message: err,
});
});
};

/**
* POST a Puff
*/
exports.create = (req, res) => {
req.log.debug("POST/Puff/create");
var puffData = {
title: req.body.title,
content: req.body.content,
tags: req.body.tags,
comments: req.body.comments,
meta: req.body.meta
};
return Puff.create(puffData).then((puff) => {
if (puff) {
User.findOne({
username: req.body.username
}).then((user) => {
if (!user) {
res.status(404)
.send({
message: 'Puff not Found',
});
}
else {
puff.author = user._id;
puff.save();
user.puffs.push(puff);
user.save();
res.status(200)
.send({
message: "Puff created successfully",
puff: puff
});
}
});
}
}).catch((err) => {
req.log.error(err);
res.status(500)
.send({
message: err,
});
});
};

/**
* PUT | Update Puff by ID
*/
exports.update = (req, res) => {
req.log.debug("PUT/Puff/update");
return Puff.findOne({
puffId: req.params.id
}).then((puff) => {
if (!puff) {
res.status(404)
.send({
message: 'Puff not Found',
});
} else {
puff.title = req.body.title;
puff.content = req.body.content;
puff.tags = req.body.tags;
puff.comments = req.body.comments;
puff.meta = req.body.meta;
puff.hidden = req.body.hidden;
puff.save();
res.status(200)
.send({
message: "Puff data updated successfully",
puff: puff
});
}
}).catch((err) => {
req.log.error(err);
res.status(500)
.send({
message: err,
});
});
};

/**
* Delete Puff by ID
*/
exports.remove = (req, res) => {
req.log.debug("DELETE/Puff/remove");
return Puff.findOne({
puffId: req.params.id
}).then((puff) => {
if (!puff) {
res.status(404)
.send({
message: 'Puff not Found',
});
} else {
puff.remove();
res.status(200)
.send({
message: "Puff removed successfully",
puff: puff
});
}
}).catch((err) => {
req.log.error(err);
res.status(500)
.send({
message: err,
});
});
};
10 changes: 10 additions & 0 deletions server/src/controllers/puffs/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as express from 'express';
import puff from './puff';

export default express
.Router()
.post('/', puff.create)
.put('/:id', puff.update)
.get('/', puff.list)
.get('/:id', puff.get)
.delete('/:id', puff.remove);
33 changes: 17 additions & 16 deletions server/src/controllers/users/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const jwt = require('../../services/jwt');
* List Users
*/
exports.list = (req, res) => {
req.log.debug("GET/list");
return User.find({}).select('-password -__v').then((users) => {
req.log.debug("GET/User/list");
return User.find({}).populate('puffs').then((users) => {
if (!users) {
res.status(404)
.send({
message: 'No data found',
message: 'User not Found',
});
} else {
res.status(200)
Expand All @@ -32,10 +32,10 @@ exports.list = (req, res) => {
* Get User by ID
*/
exports.get = (req, res) => {
req.log.debug("GET/get");
req.log.debug("GET/User/get");
return User.findOne({
userId: req.params.id
}).then((user) => {
}).populate('puffs').then((user) => {
if (!user) {
res.status(404)
.send({
Expand All @@ -61,7 +61,7 @@ exports.get = (req, res) => {
* POST a User
*/
exports.create = (req, res) => {
req.log.debug("POST/create");
req.log.debug("POST/User/create");
var userData = {
firstname: req.body.firstname,
lastname: req.body.lastname,
Expand Down Expand Up @@ -93,7 +93,7 @@ exports.create = (req, res) => {
* PUT | Update User by ID
*/
exports.update = (req, res) => {
req.log.debug("PUT/update");
req.log.debug("PUT/User/update");
return User.findOne({
userId: req.params.id
}).then((user) => {
Expand All @@ -118,12 +118,12 @@ exports.update = (req, res) => {
}
user.password = hash;
user.save();
res.status(200)
.send({
message: "User data updated successfully",
user: user
});
});
res.status(200)
.send({
message: "User data updated successfully",
user: user
});
}
}).catch((err) => {
req.log.error(err);
Expand All @@ -138,7 +138,7 @@ exports.update = (req, res) => {
* Delete User by ID
*/
exports.remove = (req, res) => {
req.log.debug("DELETE/remove");
req.log.debug("DELETE/User/remove");
return User.findOne({
userId: req.params.id
}).then((user) => {
Expand All @@ -157,8 +157,9 @@ exports.remove = (req, res) => {
}
}).catch((err) => {
req.log.error(err);
return {
err: err
};
res.status(500)
.send({
message: err,
});
});
};
71 changes: 71 additions & 0 deletions server/src/models/puff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const mongoose = require('mongoose'),
autoIncrement = require('mongoose-plugin-autoinc');

const PuffSchema = mongoose.Schema({
title: {
type: String,
required: true
},
content: {
type: String,
required: true
},
tags: [{
type: String
}],
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
comments: [{
body: String,
date: Date
}],
hidden: {
type: Boolean,
default: false
},
meta: {
votes: Number,
favs: Number
}
},
{
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
},
minimize: false,
toObject: {
retainKeyOrder: true
}
});

PuffSchema.plugin(autoIncrement.plugin, {
model: 'Puff',
field: 'puffId',
});

/*
When a puff is removed, you want to remove the
puffs array element references to that puff's _id
from all users docs.
*/
PuffSchema.pre('remove', function(next) {
var puff = this;
puff.model('User').update(
{
puffs: puff._id
},
{
$pull: {
puffs: puff._id
}
},
{
multi: true
},
next);
});

module.exports = mongoose.model('Puff', PuffSchema);
Loading

0 comments on commit 4627c46

Please sign in to comment.