-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#164796894 implement user comment on article (#33)
* 164796894 comment on article user can comment on articles [finishes #164796894] * 1647968964 format response format * rebase changes * #164796894 comment on article registered user can comment on article [finishes 164796894] * 164796894 comment on article user can comment on article [finishes #164796894] * #164796894 comment on aticle registered users can comment on article [finishes #164796894] * #164796894 impoving test coverage impoving test coverage [finishes $#164796894] * #164796894 comment on article user can comment on article [finishes #164796894] * #164796894 comment on article fixing error [finishes #164796894]
- Loading branch information
1 parent
6a8a6a3
commit 3850568
Showing
8 changed files
with
138 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Comment } from '../models'; | ||
import { errorResponseFormat } from '../utils/index'; | ||
/** | ||
* @method comment | ||
* @description enable authenticated users to comment on existing article | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} - JSON new comment response object | ||
*/ | ||
|
||
export const addComment = async (req, res) => { | ||
const { articleId } = req.params; | ||
const { body } = req; | ||
const { id } = req.user; | ||
|
||
try { | ||
const comment = await Comment.create({ ...body, articleId, userId: id }); | ||
|
||
return res.status(201).json({ | ||
data: comment, | ||
status: 'success', | ||
}); | ||
} catch (error) { | ||
if (error.parent.constraint === 'comments_articleId_fkey') { | ||
return res.status(404).json(errorResponseFormat({ | ||
status: 'error', | ||
message: 'invalid article id' | ||
})); | ||
} | ||
if (error.parent.file === 'uuid.c') { | ||
return res.status(404).json(errorResponseFormat({ | ||
status: 'fail', | ||
message: 'invalid id of type UUID' | ||
})); | ||
} | ||
} | ||
}; |
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
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,19 @@ | ||
import Validator from 'validatorjs'; | ||
|
||
/** | ||
* @description This is the method for validating comment before inserting | ||
* @param {object} payload The request object | ||
* @returns {function} Returns validation object | ||
*/ | ||
|
||
export const validateComment = (payload) => { | ||
const rules = { | ||
body: 'required', | ||
}; | ||
|
||
const errorMessages = { | ||
'required.body': 'comment body is required' | ||
|
||
}; | ||
return new Validator(payload, rules, errorMessages); | ||
}; |
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,46 @@ | ||
import 'chai/register-should'; | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import { startServer } from '../../src/server'; | ||
import { articleId } from '../mock/bookmark'; | ||
import { comment } from '../mock/comment'; | ||
import { JWT_TOKEN } from '../mock/user'; | ||
|
||
const { assert } = chai; | ||
let app = null; | ||
let agent = null; | ||
|
||
chai.use(chaiHttp); | ||
|
||
describe('POST comment on article', () => { | ||
beforeEach(async () => { | ||
app = await startServer(5500); | ||
agent = chai.request(app); | ||
}); | ||
|
||
it('comment added successfully', (done) => { | ||
agent.post(`/api/v1/articles/${articleId}/comments`) | ||
.set('Authorization', JWT_TOKEN) | ||
.send(comment) | ||
.end((_err, res) => { | ||
const { status, body } = res; | ||
assert.equal(status, 201); | ||
assert.isObject(body); | ||
assert.equal(body.status, 'success'); | ||
assert.property(body, 'data'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('empty body content ', (done) => { | ||
agent.post(`/api/v1/articles/${articleId}/comments`) | ||
.set('Authorization', JWT_TOKEN) | ||
.end((_err, res) => { | ||
const { status, body } = res; | ||
assert.equal(status, 400); | ||
assert.isObject(body); | ||
assert.equal(body.status, 'fail'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,15 @@ | ||
export const comment = { | ||
body: 'Lorem ipsum dolor sit amet, sit ut dolor alterum, sed malis referrentur cu. Aperiam fabulas eos ea. Sea mazim senserit tincidunt te. Mei volutpat delicatissimi ut, id mollis alienum argumentum has, semper efficiendi sed ea. Ius decore consul forensibus ne, enim verear corpora sit ut. Usu eu possit equidem menandri, quo et noster officiis iracundia.', | ||
}; | ||
|
||
export const uncompleteCommentBody = { | ||
body: '', | ||
}; | ||
|
||
export const invalidComment = { | ||
articleId: '1839374c-53ea-438c-815d-1fe301422120', | ||
}; | ||
|
||
export const invalidarticleUUID = { | ||
articleId: '1839374c-53ea-438c-xxxxxxxxx', | ||
}; |