Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-Server into develop
  • Loading branch information
HUNG-rushb committed Nov 5, 2023
2 parents fa89dff + b2db70f commit 964072a
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 48 deletions.
6 changes: 4 additions & 2 deletions src/Type_Definitions/Comment_Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const commentDefs = gql`
}
extend type Query {
getCommentChild(id: ID!): Comment!
getCommentsByPostId(data: GetCommentsByPostIdInput, limit: Int, after: String): CommentPagination!
getCommentsByStoryId(data: GetCommentsByStoryIdInput, limit: Int, after: String): CommentPagination!
}
Expand Down Expand Up @@ -46,6 +47,7 @@ const commentDefs = gql`
input VoteCommentInput {
commentId: ID!
action: voteCommentAction!
userId: ID!
}
input CreateCommentInput {
Expand Down Expand Up @@ -75,9 +77,9 @@ const commentDefs = gql`
storyId: Story!
votes: Int!
children: [Comment]
child: [Comment]
userVotePost: [String]!
userVoteComment: [String]!
}
`;

Expand Down
2 changes: 1 addition & 1 deletion src/Type_Definitions/Post_Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const postDefs = gql`
allPosts: [Post]!
postInfo(data: PostInfoInput!): Post!
getNewFeed(userId: String, after: String, timeCall: Int): PostConnection!
getNewFeed(userId: String, categoryIds: [String], after: String, timeCall: Int): PostConnection!
getAllUserPosts(
userId: String
currentUserId: String
Expand Down
27 changes: 27 additions & 0 deletions src/Type_Definitions/Profile_Profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import gql from 'graphql-tag';
import commonDefs from './Common_Common.js'

/**
* @description {import('./User_User.js)}
* ? profileDefs is a part of userDefs
*/
const profileDefs = gql`
extend type Query {
getProfile(data: ProfileInfoInput!): Profile!
}
input ProfileInfoInput {
userId: ID!
}
type Profile {
id: ID!
isAdmin: Int!
createdAt: String!
updatedAt: String
endosers: Level!
}
`;

export default profileDefs;
14 changes: 13 additions & 1 deletion src/Type_Definitions/User_User.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import gql from 'graphql-tag';
import commonDefs from './Common_Common.js'

const userDefs = gql`
extend type Query {
allUsers: [User]!
userInfo(data: UserInfoInput!): User!
verifyUser(data: VerifyUserInput!): User!
suggestUserToFollow(data: SuggestUserToFollowInput!): [User]!
suggestUserToFollow(data: SuggestUserToFollowInput!, limit: Int, after: String): UserPagination!
}
type UserPagination {
edges: [UserEdge!]!
pageInfo: PageInfo!
}
${commonDefs}
type UserEdge {
node: User
cursor: String!
}
input VerifyUserInput {
Expand Down
2 changes: 2 additions & 0 deletions src/Type_Definitions/_typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import followingDefs from './Following_Following.js';
import followerDefs from './Follower_Follower.js';
import reportDefs from './Report_Report.js';
import contestDefs from './Contest_Contest.js';
import profileDefs from './Profile_Profile.js';

const baseDefs = gql`
type Query {
Expand All @@ -39,6 +40,7 @@ const typeDefs = [
storyDefs,
commentDefs,
tagDefs,
// profileDefs,
categoryDefs,
albumDefs,
followingDefs,
Expand Down
2 changes: 1 addition & 1 deletion src/prisma/ERD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 27 additions & 6 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ model User {
isAdmin Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// biography String
level Level?
Expand All @@ -53,6 +54,8 @@ model User {
courseIDs String[] @db.ObjectId
user_to_course Course[] @relation(fields: [courseIDs], references: [id])
// user_to_endorsement Endorsement[]
}

// model Notification {
Expand All @@ -63,6 +66,23 @@ model User {
// userId String @db.ObjectId
// notification_to_user User @relation(fields: [userId], references: [id])
// }
// model Endorsement {
// id String @id @default(auto()) @map("_id") @db.ObjectId
//
// endorser String[] @db.ObjectId
//
// userId String @db.ObjectId
// user User @relation(fields: [userId], references: [id])
//
// skillId String @db.ObjectId
// skill Skill @relation(fields: [skillId], references: [id])
// }
//
// model Skill {
// id String @id @default(auto()) @map("_id") @db.ObjectId
// name String
// endorsements Endorsement[]
// }

model Report {
id String @id @default(auto()) @map("_id") @db.ObjectId
Expand Down Expand Up @@ -219,17 +239,18 @@ model Comment {
userId String @db.ObjectId
cmt_to_user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
postId String @db.ObjectId
cmt_to_post Post @relation(fields: [postId], references: [id], onDelete: Cascade, onUpdate: Cascade)
postId String? @db.ObjectId
cmt_to_post Post? @relation(fields: [postId], references: [id], onDelete: Cascade, onUpdate: Cascade)
storyId String @db.ObjectId
cmt_to_story Story @relation(fields: [storyId], references: [id], onDelete: Cascade, onUpdate: Cascade)
storyId String? @db.ObjectId
cmt_to_story Story? @relation(fields: [storyId], references: [id], onDelete: Cascade, onUpdate: Cascade)
parentId String? @db.ObjectId
parent Comment? @relation(name: "Comment_Children", fields: [parentId], references: [id], onDelete: NoAction, onUpdate: NoAction)
children Comment[] @relation(name: "Comment_Children")
parent Comment? @relation(name: "Comment_Child", fields: [parentId], references: [id], onDelete: NoAction, onUpdate: NoAction)
child Comment[] @relation(name: "Comment_Child")
votes Int @default(0)
userVoteComment String[]
}

model Contest {
Expand Down
45 changes: 39 additions & 6 deletions src/resolvers/Mutation/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,34 @@ const commentMutation = {
comment = await prisma.comment.create({
data: {
content: content,
userId: userId,
postId: postId,
storyId: storyId,
parentId: parentCommentId ? parentCommentId : null
cmt_to_user: {
connect: {
id: userId
}
},
...(parentCommentId && {
parent: {
connect: {
id: parentCommentId
}
},
}),
...((postId || postId === '000000000000000000000000') && {
cmt_to_post: {
connect: {
id: postId
}
},
}),
...((storyId || storyId === '000000000000000000000000') && {
cmt_to_story: {
connect: {
id: storyId
}
},
}),
},

});
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -64,12 +87,12 @@ const commentMutation = {
/**
*
* @param {*} parent
* @param {{data: {commentId: string, action: UPVOTE | DOWNVOTE}}} args
* @param {{data: {commentId: string, userId: string, action: UPVOTE | DOWNVOTE}}} args
* @param {*} info
* @returns
*/
voteComment: async (parent, args, info) => {
const { action, commentId } = args.data;
const { action, commentId, userId } = args.data;

const existedComment = await prisma.comment.findUnique({
where: {
Expand All @@ -81,6 +104,10 @@ const commentMutation = {
throw Error('Comment Not Exsited');
}

if (existedComment.userVoteComment.length > 0 && existedComment.userVoteComment.includes(userId)) {
throw Error('User has voted this comment already');
}

switch (action) {
case VOTE_COMMENT_ACTION.DOWNVOTE:
return await prisma.comment.update({
Expand All @@ -89,6 +116,9 @@ const commentMutation = {
},
data: {
votes: existedComment.votes - 1,
userVoteComment: {
push: userId
}
},
});

Expand All @@ -99,6 +129,9 @@ const commentMutation = {
},
data: {
votes: existedComment.votes + 1,
userVoteComment: {
push: userId
}
},
});
default:
Expand Down
40 changes: 31 additions & 9 deletions src/resolvers/Query/COMMENT.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const commentQuery = {
postId: postId,
},
include: {
children: {
child: {
include: {
children: true,
child: true,
},
},
},
Expand All @@ -42,7 +42,7 @@ const commentQuery = {
console.log('count', count);

const sortedResult = result.sort((before, after) => after.votes - before.votes);
const hasNextPage = sortedResult.length !== 0 && sortedResult.length <= count;
const hasNextPage = sortedResult.length !== 0 && sortedResult.length < count;
console.log('hasNextPage', hasNextPage);

const nodes = sortedResult.map((each) => ({
Expand Down Expand Up @@ -82,9 +82,9 @@ const commentQuery = {
storyId: storyId,
},
include: {
children: {
child: {
include: {
children: true,
child: true,
},
},
},
Expand All @@ -101,7 +101,7 @@ const commentQuery = {
console.log('count', count);

const sortedResult = result.sort((before, after) => after.votes - before.votes);
const hasNextPage = sortedResult.length !== 0 && sortedResult.length <= count;
const hasNextPage = sortedResult.length !== 0 && sortedResult.length < count;
console.log('hasNextPage', hasNextPage);

const nodes = sortedResult.map((each) => ({
Expand All @@ -119,9 +119,31 @@ const commentQuery = {
},
};
},
// userComments: async (parent, args, info) => {
// return await prisma.post.findMany();
// },

/**
*
* @param {*} parent
* @param {{id: string}} args
* @param {*} info
*/
getCommentChild: async (parent, args, info) => {
const { id } = args
const result = await prisma.comment.findFirst({
where: {
id: id,
},
include: {
child: {
include: {
child: true,
},
},
},
})

return result
}

};

export default commentQuery;
Loading

0 comments on commit 964072a

Please sign in to comment.