Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from soulsam480/dev
Browse files Browse the repository at this point in the history
Features and fixes
  • Loading branch information
soulsam480 authored Sep 7, 2021
2 parents 3e71ee5 + 13ea679 commit ca8fa5f
Show file tree
Hide file tree
Showing 35 changed files with 1,360 additions and 371 deletions.
6 changes: 2 additions & 4 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "soulsam480 <[email protected]>",
"private": true,
"scripts": {
"dev": "ts-node-dev --respawn --files -- src/index.ts",
"dev": "ts-node-dev --respawn --transpile-only --ignore-watch node_modules --files -- src/index.ts",
"build": "rm -rf dist && yarn && tsc && tsc-alias",
"build:swagger": "node swagger.js",
"build:types": "tsc --declaration --emitDeclarationOnly --declarationDir ./types && tsc-alias",
Expand All @@ -24,8 +24,7 @@
"@types/swagger-jsdoc": "^6.0.1",
"@types/swagger-ui-express": "^4.1.3",
"ts-node-dev": "^1.1.8",
"tsconfig-paths": "^3.10.1",
"typescript": "4.3.5"
"tsconfig-paths": "^3.10.1"
},
"dependencies": {
"@typegoose/typegoose": "^8.2.0",
Expand All @@ -46,7 +45,6 @@
},
"workspaces": {
"nohoist": [
"**/typescript/**/**",
"**/mongoose/**"
]
}
Expand Down
148 changes: 148 additions & 0 deletions api/src/controllers/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { createRoute } from 'dango-core';
import { DocumentDefinition } from 'mongoose';

import {
createCommentOnPost,
createReplyOnPost,
getCommentsForPost,
getRepliesForComment,
likeOnComment,
likeOnReply,
unLikeOnComment,
unLikeOnReply,
} from 'src/services/comments';
import { formatResponse } from 'src/utils/helpers';
import { Comment } from 'src/entities/post';

export const getCommentsByPostId = createRoute<
any,
{ id: string },
{ cursor: string; limit: string }
>({
path: '/:id/comments',
method: 'get',
handler: async (_, res, __, { id }, { cursor: pagination_cursor, limit: pagination_limit }) => {
try {
const cursor: number = parseInt(pagination_cursor);
const limit: number = parseInt(pagination_limit) || 10;

const comments = await getCommentsForPost(id, cursor, limit);

res.json(comments);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const addCommentByPostId = createRoute<
{ comment: DocumentDefinition<Comment> },
{ id: string }
>({
path: '/:id/comments',
method: 'post',
handler: async ({ userId }, res, { comment }, { id }) => {
try {
await createCommentOnPost(id, { ...comment, user: userId as string });

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const getRepliesByCommentId = createRoute<any, { id: string; commentId: string }>({
path: '/:id/comments/:commentId',
method: 'get',
handler: async (_, res, __, { id, commentId }) => {
try {
const replies = await getRepliesForComment(id, commentId);

res.json(formatResponse(replies));
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const addReplyByCommentId = createRoute<
{ comment: DocumentDefinition<Comment> },
{ id: string; commentId: string }
>({
path: '/:id/comments/:commentId',
method: 'post',
handler: async ({ userId }, res, { comment }, { id, commentId }) => {
try {
await createReplyOnPost(id, commentId, { ...comment, user: userId as string });

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const likeComment = createRoute<any, { id: string; commentId: string }>({
path: '/:id/comments/:commentId/like',
method: 'post',
handler: async ({ userId }, res, _, { id, commentId }) => {
try {
await likeOnComment(id, commentId, userId as string);

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const unLikeComment = createRoute<any, { id: string; commentId: string }>({
path: '/:id/comments/:commentId/unlike',
method: 'post',
handler: async ({ userId }, res, _, { id, commentId }) => {
try {
await unLikeOnComment(id, commentId, userId as string);

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const likeReply = createRoute<any, { id: string; commentId: string; replyId: string }>({
path: '/:id/comments/:commentId/replies/:replyId/like',
method: 'post',
handler: async ({ userId }, res, _, { commentId, id, replyId }) => {
try {
await likeOnReply(id, commentId, replyId, userId as string);

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

export const unLikeReply = createRoute<any, { id: string; commentId: string; replyId: string }>({
path: '/:id/comments/:commentId/replies/:replyId/unlike',
method: 'post',
handler: async ({ userId }, res, _, { commentId, id, replyId }) => {
try {
await unLikeOnReply(id, commentId, replyId, userId as string);

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});
85 changes: 15 additions & 70 deletions api/src/controllers/post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createController, createRoute } from 'dango-core';
import { DocumentDefinition, UpdateQuery } from 'mongoose';
import { UpdateQuery } from 'mongoose';
import { createError, formatResponse } from 'src/utils/helpers';
import { Comment, Post } from 'src/entities/post';
import { Post } from 'src/entities/post';
import {
createPost,
deletePostById,
Expand All @@ -13,11 +13,15 @@ import {
updatePostById,
} from 'src/services/post';
import {
createCommentOnPost,
createReplyOnPost,
getCommentsForPost,
getRepliesForComment,
} from 'src/services/comments';
getCommentsByPostId,
addCommentByPostId,
getRepliesByCommentId,
addReplyByCommentId,
likeComment,
unLikeComment,
likeReply,
unLikeReply,
} from 'src/controllers/comments';

/**
* @private only for seeding once
Expand Down Expand Up @@ -171,69 +175,6 @@ const deleteById = createRoute<any, { id: string }>({
},
});

const getCommentsByPostId = createRoute<any, { id: string }>({
path: '/:id/comments',
method: 'get',
handler: async (_, res, __, { id }) => {
try {
const comments = await getCommentsForPost(id);

res.json(formatResponse(comments));
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

const addCommentByPostId = createRoute<{ comment: DocumentDefinition<Comment> }, { id: string }>({
path: '/:id/comments',
method: 'post',
handler: async ({ userId }, res, { comment }, { id }) => {
try {
await createCommentOnPost(id, { ...comment, user: userId as string });

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

const getRepliesByCommentId = createRoute<any, { id: string; commentId: string }>({
path: '/:id/comments/:commentId',
method: 'get',
handler: async (_, res, __, { id, commentId }) => {
try {
const replies = await getRepliesForComment(id, commentId);

res.json(formatResponse(replies));
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

const addReplyByCommentId = createRoute<
{ reply: DocumentDefinition<Comment> },
{ id: string; commentId: string }
>({
path: '/:id/comments/:commentId',
method: 'post',
handler: async ({ userId }, res, { reply }, { id, commentId }) => {
try {
await createReplyOnPost(id, commentId, { ...reply, user: userId as string });

res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendError(500, error);
}
},
});

const postController = createController('/posts', [
getPosts,
postsByUserId,
Expand All @@ -247,6 +188,10 @@ const postController = createController('/posts', [
addCommentByPostId,
getRepliesByCommentId,
addReplyByCommentId,
likeComment,
unLikeComment,
likeReply,
unLikeReply,
]);

export { postController };
53 changes: 37 additions & 16 deletions api/src/entities/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,61 @@ import { User } from 'src/entities/user';
},
})
export class Comment extends TimeStamps {
@prop()
public comment: string;
@prop({ ref: 'Post' })
post_id: Ref<Post>;

@prop({ ref: 'User' })
public user: Ref<User>;

@prop({ ref: 'User' })
public likes?: Ref<User>[];

@prop({ type: () => Reply, default: [] })
public replies?: Reply[];
@prop({ ref: 'Reply' })
public replies?: Ref<Reply>[];

@prop()
public comment: string;

public get total_likes() {
return this.likes?.length;
}

public get total_replies() {
return this.likes?.length;
}
@prop({ ref: () => Reply, foreignField: 'comment_id', localField: '_id', count: true })
public total_replies?: number;

// @prop({
// ref: () => User,
// foreignField: 'liked_comments',
// localField: '_id',
// count: true,
// match: (doc) => ({ _id: doc._id }),
// })
// public total_likes?: number;
}

class Reply extends TimeStamps {
@prop()
public comment: string;
@modelOptions({
schemaOptions: {
timestamps: true,
toJSON: { virtuals: true, versionKey: false },
},
})
export class Reply extends TimeStamps {
@prop({ ref: 'Comment' })
public comment_id: Ref<Comment>;

@prop({ ref: 'Post' })
public post_id: Ref<Post>;

@prop({ ref: 'User' })
public user: Ref<User>;

@prop({ ref: 'User' })
public likes?: Ref<User>[];

public get total_likes() {
return this.likes?.length;
}
@prop()
public comment: string;

public get total_replies() {
public get total_likes() {
return this.likes?.length;
}
}
Expand All @@ -68,8 +87,8 @@ export class Post extends TimeStamps {
@prop({ ref: 'User' })
public likes?: Ref<User>[];

@prop({ type: () => Comment })
public comments?: Comment[];
@prop({ ref: 'Comment' })
public comments?: Ref<Comment>[];

@prop({ default: false })
is_archived?: boolean;
Expand All @@ -86,3 +105,5 @@ export class Post extends TimeStamps {
export const postModel = getModelForClass(Post);

export const commentModel = getModelForClass(Comment);

export const replyModel = getModelForClass(Reply);
Loading

0 comments on commit ca8fa5f

Please sign in to comment.