-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add puff endpoint.
- Loading branch information
Showing
10 changed files
with
304 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.