diff --git a/packages/common/src/IAPIDef.ts b/packages/common/src/IAPIDef.ts index 6b82df1..c8307e7 100644 --- a/packages/common/src/IAPIDef.ts +++ b/packages/common/src/IAPIDef.ts @@ -183,7 +183,7 @@ export interface IAPIDef { } put: { response: IComment, - body: IComment, + body: INewComment, params: { commentId: number } diff --git a/packages/server/src/comment/CommentRoutes.test.ts b/packages/server/src/comment/CommentRoutes.test.ts index 547b65b..a98595f 100644 --- a/packages/server/src/comment/CommentRoutes.test.ts +++ b/packages/server/src/comment/CommentRoutes.test.ts @@ -1,5 +1,5 @@ import * as CommentTestUtils from './CommentTestUtils' -import {IStory} from '@rondo/common' +import {IComment, IStory} from '@rondo/common' import {createSite} from '../site/SiteTestUtils' import {getStory} from '../story/StoryTestUtils' import {test} from '../test' @@ -78,13 +78,33 @@ describe('comment', () => { }) describe('PUT /comments/:commentId', () => { - it('updates a comment', () => { + let comment: IComment + beforeEach(async () => { + comment = await CommentTestUtils.createRootComment(t, { + storyId: story.id, + message: 'test', + }) + }) + it('updates a comment', async () => { + await t.put('/comments/:commentId', { + params: { + commentId: comment.id, + }, + }) + .send({ + message: 'test2', + }) + .expect(200) + + const c = await CommentTestUtils.getCommentById(t, comment.id) + expect(c.message).toEqual('test2') + // TODO save edit history }) - it('fails to update a comment if user is not the owner') + // it('fails to update a comment if user is not the owner') - it('updates a comment if user is site moderator') // TODO later + // it('updates a comment if user is site moderator') // TODO later }) describe('DELETE /comments/:commentId', () => { diff --git a/packages/server/src/comment/CommentRoutes.ts b/packages/server/src/comment/CommentRoutes.ts index aa6ae04..a8a5bb4 100644 --- a/packages/server/src/comment/CommentRoutes.ts +++ b/packages/server/src/comment/CommentRoutes.ts @@ -53,9 +53,13 @@ export class CommentRoutes extends BaseRoute { }) t.put('/comments/:commentId', async req => { - const comment = req.body - comment.id = req.params.commentId - return this.commentService.edit(comment, req.user!.id) + const commentId = Number(req.params.commentId) + const {message} = req.body + return this.commentService.edit({ + id: commentId, + message, + userId: req.user!.id, + }) }) t.delete('/comments/:commentId', async req => { diff --git a/packages/server/src/comment/CommentService.ts b/packages/server/src/comment/CommentService.ts index c4fa5b3..c25101c 100644 --- a/packages/server/src/comment/CommentService.ts +++ b/packages/server/src/comment/CommentService.ts @@ -1,6 +1,7 @@ import {BaseService} from '../services/BaseService' import {Comment} from '../entities/Comment' -import {IComment, ICommentTree} from '@rondo/common' +import {ICommentTree} from '@rondo/common' +import {IEditCommentParams} from './IEditCommentParams' import {ICommentService} from './ICommentService' import {INewCommentParams} from './INewCommentParams' import {INewRootCommentParams} from './INewRootCommentParams' @@ -92,22 +93,26 @@ export class CommentService extends BaseService implements ICommentService { }) } - async edit(comment: IComment, userId: number) { + async edit(comment: IEditCommentParams) { new Validator(comment) .ensure('id') .ensure('message') + .ensure('userId') .throw() + const {id, message, userId} = comment + await this.getRepository(Comment) .update({ - id: comment.id, + id, userId, }, { - message: comment.message, + message, }) const editedComment = await this.findOne(comment.id) if (!editedComment) { + // TODO 400 or 404 throw new Error('Comment not found') } return editedComment diff --git a/packages/server/src/comment/CommentTestUtils.ts b/packages/server/src/comment/CommentTestUtils.ts index d3d3a55..35ad9c3 100644 --- a/packages/server/src/comment/CommentTestUtils.ts +++ b/packages/server/src/comment/CommentTestUtils.ts @@ -52,3 +52,16 @@ export async function getComments( return response.body! } + +export async function getCommentById( + t: RequestTester, + commentId: number, +) { + const response = await t + .get('/comments/:commentId', { + params: {commentId}, + }) + .expect(200) + + return response.body! +} diff --git a/packages/server/src/comment/ICommentService.ts b/packages/server/src/comment/ICommentService.ts index 9b87667..03e902e 100644 --- a/packages/server/src/comment/ICommentService.ts +++ b/packages/server/src/comment/ICommentService.ts @@ -1,4 +1,5 @@ import {IComment, ICommentTree} from '@rondo/common' +import {IEditCommentParams} from './IEditCommentParams' import {INewCommentParams} from './INewCommentParams' import {INewRootCommentParams} from './INewRootCommentParams' @@ -11,7 +12,7 @@ export interface ICommentService { save(comment: INewCommentParams): Promise - edit(comment: IComment, userId: number): Promise + edit(comment: IEditCommentParams): Promise delete(commentId: number, userId: number): Promise diff --git a/packages/server/src/comment/IEditCommentParams.ts b/packages/server/src/comment/IEditCommentParams.ts new file mode 100644 index 0000000..44675f8 --- /dev/null +++ b/packages/server/src/comment/IEditCommentParams.ts @@ -0,0 +1,5 @@ +export interface IEditCommentParams { + id: number + message: string + userId: number +}