Skip to content

Commit

Permalink
feat: comment_react
Browse files Browse the repository at this point in the history
  • Loading branch information
LongTCH committed Mar 8, 2024
1 parent 7d3eecc commit cb6412f
Show file tree
Hide file tree
Showing 47 changed files with 845 additions and 98 deletions.
23 changes: 23 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@babel/runtime": "^7.14.0",
"@commitlint/cli": "^17.5.0",
"@commitlint/config-conventional": "^17.4.4",
"@faker-js/faker": "^8.4.1",
"babel-eslint": "^10.1.0",
"babel-plugin-module-resolver": "^4.1.0",
"babel-plugin-transform-runtime": "^6.23.0",
Expand Down
43 changes: 43 additions & 0 deletions backend/src/core/api/comment/comment.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ValidHttpResponse } from 'packages/handler/response/validHttp.response';
import { DEFAULT_PAGE, DEFAULT_PAGE_SIZE } from 'core/common/constants';
import { MessageDto } from 'core/common/dto';
import {
AddCommentDto,
COMMENT_PARENT_TYPE,
CommentService,
} from 'core/modules/comment';

class Controller {
constructor() {
this.commentService = CommentService;
}

createComment = async req => {
const { parentType, parentId, content } = AddCommentDto(req.body);
await this.commentService.createComment(
parentId,
parentType,
content,
req.user.payload.id,
);
return ValidHttpResponse.toOkResponse(MessageDto('Comment created'));
};

getCommentPaginationByParentIdAndType = async req => {
const parentId = req.params.id;
const page = req.query.page || DEFAULT_PAGE;
const size = req.query.size || DEFAULT_PAGE_SIZE;
const parentType = req.query.keyword || 'POST';
const data =
await this.commentService.getCommentPaginationByParentIdAndType(
parentId,
COMMENT_PARENT_TYPE[parentType],
page,
size,
);

return ValidHttpResponse.toOkResponse(data);
};
}

export const CommentController = new Controller();
29 changes: 29 additions & 0 deletions backend/src/core/api/comment/comment.resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Module } from 'packages/handler/Module';
import { RecordId, page, size, keyword } from 'core/common/swagger';
import { CommentController } from './comment.controller';

export const CommentResolver = Module.builder()
.addPrefix({
prefixPath: '/comments',
tag: 'comments',
module: 'CommentModule',
})
.register([
{
route: '',
method: 'post',
controller: CommentController.createComment,
body: 'AddCommentDto',
model: { $ref: 'MessageDto' },
preAuthorization: true,
},
{
route: '/:id/nested',
method: 'get',
params: [page, size, keyword, RecordId],
controller: CommentController.getCommentPaginationByParentIdAndType,
model: { $ref: 'PaginationCommentDto' },
description:
'Get list nested comments of a post or comment (Keyword POST or COMMENT)',
},
]);
9 changes: 6 additions & 3 deletions backend/src/core/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { HelpSignalResolver } from 'core/api/helpSignal';
import { HandlerResolver } from '../../packages/handler/HandlerResolver';
import { AuthResolver } from './auth/auth.resolver';
import { EmergencyResolver } from './emergency';
import { CommentResolver } from './comment/comment.resolver';
import { UserReactResolver } from './user-react/user-react.resolver';

export const ModuleResolver = HandlerResolver
.builder()
export const ModuleResolver = HandlerResolver.builder()
.addSwaggerBuilder(ApiDocument)
.addModule([
AuthResolver,
UserResolver,
MediaResolver,
HelpSignalResolver,
EmergencyResolver
EmergencyResolver,
CommentResolver,
UserReactResolver,
]);
38 changes: 38 additions & 0 deletions backend/src/core/api/user-react/user-react.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ValidHttpResponse } from 'packages/handler/response/validHttp.response';
import { MessageDto } from 'core/common/dto';
import { UserReactService } from 'core/modules/user-react/service/user-react.service';
import {
AddUserReactDto,
DeleteUserReactDto,
} from 'core/modules/user-react/dto';

class Controller {
constructor() {
this.userReactService = UserReactService;
}

createUserReact = async req => {
const { reactableId, reactableType, reactType } = AddUserReactDto(
req.body,
);
await this.userReactService.createUserReact(
req.user.payload.id,
reactableId,
reactableType,
reactType,
);
return ValidHttpResponse.toOkResponse(MessageDto('UserReact created'));
};

deleteUserReact = async req => {
const { reactableType, reactableId } = DeleteUserReactDto(req.body);
await this.userReactService.deleteUserReact(
req.user.payload.id,
reactableId,
reactableType,
);
return ValidHttpResponse.toOkResponse(MessageDto('UserReact deleted'));
};
}

export const UserReactController = new Controller();
31 changes: 31 additions & 0 deletions backend/src/core/api/user-react/user-react.resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Module } from 'packages/handler/Module';
import { UserReactController } from './user-react.controller';

export const UserReactResolver = Module.builder()
.addPrefix({
prefixPath: '/user-reacts',
tag: 'user-reacts',
module: 'UserReactModule',
})
.register([
{
route: '',
method: 'post',
controller: UserReactController.createUserReact,
body: 'AddUserReactDto',
model: { $ref: 'MessageDto' },
preAuthorization: true,
description:
'Do LIKE or DISLIKE to a comment or post actioned by user',
},
{
route: '',
method: 'delete',
controller: UserReactController.deleteUserReact,
body: 'DeleteUserReactDto',
model: { $ref: 'MessageDto' },
description:
'Delete a LIKE or DISLIKE of a comment or post actioned by owner',
preAuthorization: true,
},
]);
3 changes: 3 additions & 0 deletions backend/src/core/common/constants/default-params.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_PAGE_SIZE = 50;
export const DEFAULT_PAGE = 1;
export const DEFAULT_KEYWORD = '';
1 change: 1 addition & 0 deletions backend/src/core/common/constants/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './cloudinary.constant';
export * from './default-params.constant';
2 changes: 2 additions & 0 deletions backend/src/core/common/dto/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './id-array.dto';
export * from './message.dto';
10 changes: 10 additions & 0 deletions backend/src/core/common/dto/message.dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiDocument } from 'core/config/swagger.config';
import { SwaggerDocument } from 'packages/swagger';

ApiDocument.addModel('MessageDto', {
message: SwaggerDocument.ApiProperty({ type: 'string', readOnly: true }),
});

export const MessageDto = message => ({
message,
});
3 changes: 3 additions & 0 deletions backend/src/core/common/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export * from './upload-file';
export * from './help-signal-id';
export * from './user-id';
export * from './emergency-id';
export * from './keyword';
export * from './page';
export * from './page-size';
9 changes: 9 additions & 0 deletions backend/src/core/common/swagger/keyword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SwaggerDocument } from '../../../packages/swagger';

export const keyword = SwaggerDocument.ApiParams({
name: 'keyword',
paramsIn: 'query',
required: false,
type: 'string',
description: 'keyword is used to query search',
});
9 changes: 9 additions & 0 deletions backend/src/core/common/swagger/page-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SwaggerDocument } from '../../../packages/swagger';

export const size = SwaggerDocument.ApiParams({
name: 'size',
paramsIn: 'query',
required: false,
type: 'number',
description: 'Number of items per page',
});
9 changes: 9 additions & 0 deletions backend/src/core/common/swagger/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SwaggerDocument } from '../../../packages/swagger';

export const page = SwaggerDocument.ApiParams({
name: 'page',
paramsIn: 'query',
required: false,
type: 'number',
description: 'get current page',
});
7 changes: 6 additions & 1 deletion backend/src/core/database/migrations/20240201073309_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exports.up = async knex => {
table.dateTime('birthday').defaultTo(null);
table.string('address').defaultTo(null);
table.dateTime('deleted_at').defaultTo(null);
table.decimal('latitude');
table.decimal('longitude');
table.timestamps(false, true);
});

Expand All @@ -29,4 +31,7 @@ exports.up = async knex => {
`);
};

exports.down = knex => knex.schema.dropTable(tableName);
exports.down = async knex => {
await knex.schema.dropTable(tableName);
await knex.raw(`DROP TRIGGER IF EXISTS update_timestamp ON ${tableName};`);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param { import("knex").Knex } knex
*/
const tableName = 'posts';
exports.up = async knex => {
await knex.schema.createTable(tableName, table => {
table.increments('id').unsigned().primary();
table.text('content');
table
.integer('owner_id')
.unsigned()
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE');
table.timestamp('updated_at').defaultTo(knex.fn.now());
});

await knex.raw(`
CREATE TRIGGER update_timestamp
BEFORE UPDATE
ON ${tableName}
FOR EACH ROW
EXECUTE PROCEDURE update_timestamp();
`);
};
/**
* @param { import("knex").Knex } knex
*/
exports.down = async knex => {
await knex.schema.dropTable(tableName);
await knex.raw(`DROP TRIGGER IF EXISTS update_timestamp ON ${tableName};`);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param { import("knex").Knex } knex
*/
const tableName = 'user_reacts';
exports.up = async knex => {
await knex.schema.createTable(tableName, table => {
table
.integer('user_id')
.unsigned()
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE');
table.integer('reactable_id').unsigned().notNullable();
table.integer('reactable_type').unsigned().notNullable();
table.integer('react_type').unsigned().notNullable();
table.primary(['user_id', 'reactable_id', 'reactable_type']);
});
};
/**
* @param { import("knex").Knex } knex
*/
exports.down = async knex => {
await knex.schema.dropTable(tableName);
};
Loading

0 comments on commit cb6412f

Please sign in to comment.