Skip to content

Commit

Permalink
Merge 879e597 into a2dc3f4
Browse files Browse the repository at this point in the history
  • Loading branch information
olajide1234 committed Mar 18, 2019
2 parents a2dc3f4 + 879e597 commit 719d16b
Show file tree
Hide file tree
Showing 17 changed files with 954 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: node_js
node_js:
- "10"
services:
- postgresql
- postgresql
before_script:
- psql -c 'create database vidarapp;' -U postgres
script:
Expand Down
16 changes: 8 additions & 8 deletions controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { Article } from '../models';
* @export
*/
export default class ArticleController {
/**
* @description - Create a new article
* @static
* @param {Object} req - the request object
* @param {Object} res - the response object
* @memberof ArticleController
* @returns {Object} class instance
*/
/**
* @description - Create a new article
* @static
* @param {Object} req - the request object
* @param {Object} res - the response object
* @memberof ArticleController
* @returns {Object} class instance
*/
static async createArticle(req, res) {
const images = req.images || [];
const {
Expand Down
155 changes: 155 additions & 0 deletions controllers/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { Comment, User } from '../models';

/**
* @class CommentController
* @override
* @export
*
*/
export default class CommentController {
/**
* @description - Creates an article comment
* @static
*
* @param {object} req - HTTP Request
* @param {object} res - HTTP Response
*
* @memberof CommentController
*
* @returns {object} Article comment
*/
static async createComment(req, res) {
const { id } = req.user;
const { slug: articleSlug } = req.params;
const { comment } = req.body;
try {
const newComment = await Comment.create({
userId: id, articleSlug, comment
});
return res.status(205).json({
success: true,
message: 'New article comment created successfully',
comment: newComment,
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}

/**
* @description - Gets an article's comments
* @static
*
* @param {object} req - HTTP Request
* @param {object} res - HTTP Response
*
* @memberof CommentController
*
* @returns {object} Article comment
*/
static async getComments(req, res) {
const { slug: articleSlug } = req.params;
try {
const findComment = await Comment.findAll({
raw: true,
where: {
articleSlug
},
include: [
{
model: User,
attributes: ['username', 'bio', 'email']
}
]
});
return res.status(205).json({
success: true,
message: 'Comments returned successfully',
comments: findComment,
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}

/**
* @description - Edit an article comment
* @static
*
* @param {object} req - HTTP Request
* @param {object} res - HTTP Response
*
* @memberof CommentController
*
* @returns {object} Updated article comment
*/
static async editComment(req, res) {
const { id } = req.params;
const { comment } = req.body;
try {
const updatedComment = await Comment.update(
{
comment
},
{
returning: true,
raw: true,
where: {
id
}
}
);
const newComment = updatedComment[1][0];

return res.status(205).json({
success: true,
message: 'Comment updated successfully',
body: newComment
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}

/**
* @description - Delete an article comment
* @static
*
* @param {object} req - HTTP Request
* @param {object} res - HTTP Response
*
* @memberof CommentController
*
* @returns {string} Comment delete status
*/
static async deleteComment(req, res) {
const { id } = req.params;
try {
const rowsDeleted = await Comment.destroy({
where: {
id
}
});
if (rowsDeleted === 1) {
return res.status(205).json({
success: true,
message: 'Comment deleted successfully',
});
}
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}
}
180 changes: 172 additions & 8 deletions doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@
"description": "",
"schema": {
"$ref": "#/definitions/createcategory",
"in": "body",
"name": "Request body",
"description": "Reset user password",
"required": true,
"schema": {
"$ref": "#/definitions/requestPasswordReset"
"in": "body",
"name": "Request body",
"description": "Reset user password",
"required": true,
"schema": {
"$ref": "#/definitions/requestPasswordReset"
}
}
}
}
],
"responses": {
"201": {
Expand Down Expand Up @@ -385,6 +385,170 @@
}
}
},
"/articles/{slug}/comments": {
"post": {
"tags": [
"Comment"
],
"summary": "Create a comment",
"description": "",
"operationId": "createComment",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "A token to verify the user",
"required": true,
"type": "string"
},
{
"name": "slug",
"in": "path",
"description": "slug of article to relate comment to",
"required": true
},
{
"in": "body",
"name": "comment",
"description": "Comment body",
"required": true
}
],
"responses": {
"201": {
"description": "New comment created successfully"
},
"422": {
"description": "Incomplete request body"
}
}
},
"get": {
"tags": [
"Comment"
],
"summary": "Get all article comments",
"description": "",
"operationId": "getComments",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "A token to verify the user",
"required": true,
"type": "string"
},
{
"name": "slug",
"in": "path",
"description": "slug of article to get comments for",
"required": true
}
],
"responses": {
"201": {
"description": "Comments returned successfully"
},
"404": {
"description": "No comments found"
}
}
},
"/articles/{slug}/comments/{id}": {
"patch": {
"tags": [
"Comment"
],
"summary": "Update a comment",
"description": "",
"operationId": "updateComment",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "A token to verify the user",
"required": true,
"type": "string"
},
{
"in": "path",
"name": "id",
"description": "ID of comment to be updated",
"required": true
},
{
"in": "body",
"name": "comment",
"description": "New comment",
"required": true
}
],
"responses": {
"201": {
"description": "New comment created successfully"
},
"422": {
"description": "Incomplete request body"
}
}
},
"delete": {
"tags": [
"Comment"
],
"summary": "Delete a comment",
"description": "",
"operationId": "deleteComment",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "A token to verify the user",
"required": true,
"type": "string"
},
{
"in": "path",
"name": "id",
"description": "ID of comment to be deleted",
"required": true
}
],
"responses": {
"205": {
"description": "Comment deleted successfully"
},
"500": {
"description": "Server error"
}
}
}
}
},
"definitions": {
"user": {
"type": "object",
Expand Down Expand Up @@ -491,4 +655,4 @@
}
}
}
}
}
Loading

0 comments on commit 719d16b

Please sign in to comment.