From 9fb256c049baaf29d5d44845d2f53be8a4063144 Mon Sep 17 00:00:00 2001 From: Chetan Saini Date: Mon, 1 Jul 2024 10:46:26 +0530 Subject: [PATCH 1/2] Experience innerDangerHtml --- frontend/src/Pages/Experience/ExperienceById.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/Pages/Experience/ExperienceById.jsx b/frontend/src/Pages/Experience/ExperienceById.jsx index b6e3866..b56f81c 100644 --- a/frontend/src/Pages/Experience/ExperienceById.jsx +++ b/frontend/src/Pages/Experience/ExperienceById.jsx @@ -373,7 +373,7 @@ export const ExperienceById = () => { {/* //description */}
-
{experienceData.description}
+
From c98e3d2e13fcce570a404cbbf15ab2c0e760e17a Mon Sep 17 00:00:00 2001 From: Chetan Saini Date: Mon, 1 Jul 2024 13:13:40 +0530 Subject: [PATCH 2/2] Liked And Disliked Status Added for Question , Exeprience, Comment, Reply --- .../Experience/Comment/addCommentExp.js | 4 +- .../Comment/addReplyToExpComment.js | 4 +- .../Experience/Comment/likeExpComment.js | 39 +++++++++++++++++-- .../Experience/Comment/likeExpCommentReply.js | 7 +++- .../Experience/downvoteExperience.js | 27 ++++++++++--- .../src/controllers/Experience/formatExp.js | 35 +++++++++++++++++ .../Experience/getExperienceById.js | 23 +++++------ .../Experience/updateExperience.js | 3 +- .../Experience/upvoteExperience.js | 30 +++++++++++--- .../controllers/Question/downVoteQuestion.js | 13 +++++-- .../controllers/Question/getQuestionById.js | 6 ++- .../controllers/Question/upVoteQuestion.js | 13 +++++-- backend/src/graphql/Experience/resolvers.js | 4 +- backend/src/graphql/Experience/typedefs.js | 8 +++- backend/src/graphql/Questions/typedefs.js | 2 + backend/src/utils/addLikeStatus.js | 27 +++++++++++++ 16 files changed, 200 insertions(+), 45 deletions(-) create mode 100644 backend/src/controllers/Experience/formatExp.js create mode 100644 backend/src/utils/addLikeStatus.js diff --git a/backend/src/controllers/Experience/Comment/addCommentExp.js b/backend/src/controllers/Experience/Comment/addCommentExp.js index e19283f..1bebb85 100644 --- a/backend/src/controllers/Experience/Comment/addCommentExp.js +++ b/backend/src/controllers/Experience/Comment/addCommentExp.js @@ -1,6 +1,7 @@ import { prisma } from '../../../../prisma/index.js' import { GraphQLError } from 'graphql' import { addUserName } from './addUserName.js' +import { addLikeStatus } from '../../../utils/addLikeStatus.js' export const addCommentExp = async (_, payload, context) => { try { /* @@ -21,7 +22,8 @@ export const addCommentExp = async (_, payload, context) => { reply : true } }) - expComment = addUserName(expComment) + expComment = await addLikeStatus(expComment, context.userId, 'EXP_COMMENT') + expComment = await addUserName(expComment) return expComment } else { throw new GraphQLError('User is not authorised', { diff --git a/backend/src/controllers/Experience/Comment/addReplyToExpComment.js b/backend/src/controllers/Experience/Comment/addReplyToExpComment.js index 0fc1b64..b98dfc6 100644 --- a/backend/src/controllers/Experience/Comment/addReplyToExpComment.js +++ b/backend/src/controllers/Experience/Comment/addReplyToExpComment.js @@ -1,6 +1,7 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../../prisma/index.js' import { addUserName } from './addUserName.js' +import { addLikeStatus } from '../../../utils/addLikeStatus.js' export const addReplyToExpComment = async (_, payload, context) => { try { @@ -32,7 +33,8 @@ export const addReplyToExpComment = async (_, payload, context) => { replierId: context.userId, }, }) - expReply = addUserName(expReply) + expReply = await addLikeStatus(expReply, context.userId, 'EXP_REPLY') + expReply = await addUserName(expReply) return expReply } else { throw new GraphQLError('User is not authorised', { diff --git a/backend/src/controllers/Experience/Comment/likeExpComment.js b/backend/src/controllers/Experience/Comment/likeExpComment.js index 76c8349..1378e49 100644 --- a/backend/src/controllers/Experience/Comment/likeExpComment.js +++ b/backend/src/controllers/Experience/Comment/likeExpComment.js @@ -1,6 +1,7 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../../prisma/index.js' import { addUserName } from './addUserName.js' +import { addLikeStatus } from '../../../utils/addLikeStatus.js' export const likeExpComment = async (_, payload, context) => { try { @@ -32,7 +33,7 @@ export const likeExpComment = async (_, payload, context) => { if (voteEntry) { await prisma.userVotes.delete({ where: { - id: voteEntry.id + id: voteEntry.id, }, }) let likedComment = await prisma.expComment.update({ @@ -44,8 +45,23 @@ export const likeExpComment = async (_, payload, context) => { decrement: 1, }, }, + include: { + reply: true, + }, }) - likedComment = addUserName(likedComment) + likedComment = await addLikeStatus( + likedComment, + context.userId, + 'EXP_COMMENT' + ) + likedComment = await addUserName(likedComment) + for (let i = 0; i < likedComment.reply.length; i++) { + likedComment.reply[i] = await addLikeStatus( + likedComment.reply[i], + context.userId, + 'EXP_REPLY' + ) + } return likedComment } await prisma.userVotes.create({ @@ -65,8 +81,23 @@ export const likeExpComment = async (_, payload, context) => { increment: 1, }, }, + include: { + reply: true, + }, }) - likedComment = addUserName(likedComment) + likedComment = await addLikeStatus( + likedComment, + context.userId, + 'EXP_COMMENT' + ) + likedComment = await addUserName(likedComment) + for (let i = 0; i < likedComment.reply.length; i++) { + likedComment.reply[i] = await addLikeStatus( + likedComment.reply[i], + context.userId, + 'EXP_REPLY' + ) + } return likedComment } else { throw new GraphQLError('You are not authorised user!!', { @@ -84,7 +115,7 @@ export const likeExpComment = async (_, payload, context) => { ) { throw error } - console.log("Error while liking comment : ", error) + console.log('Error while liking comment : ', error) throw new GraphQLError('Something went wrong!!') } } diff --git a/backend/src/controllers/Experience/Comment/likeExpCommentReply.js b/backend/src/controllers/Experience/Comment/likeExpCommentReply.js index 106d5fc..e27ac73 100644 --- a/backend/src/controllers/Experience/Comment/likeExpCommentReply.js +++ b/backend/src/controllers/Experience/Comment/likeExpCommentReply.js @@ -1,6 +1,7 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../../prisma/index.js' import { addUserName } from './addUserName.js' +import { addLikeStatus } from '../../../utils/addLikeStatus.js' export const likeExpCommentReply = async (_, payload, context) => { try { @@ -42,7 +43,8 @@ export const likeExpCommentReply = async (_, payload, context) => { }, }, }) - likedReply = addUserName(likedReply) + likedReply = await addLikeStatus(likedReply, context.userId, 'EXP_REPLY') + likedReply = await addUserName(likedReply) return likedReply } @@ -65,7 +67,8 @@ export const likeExpCommentReply = async (_, payload, context) => { }, }, }) - likedReply = addUserName(likedReply) + likedReply = await addLikeStatus(likedReply, context.userId, 'EXP_REPLY') + likedReply = await addUserName(likedReply) return likedReply } else { throw new GraphQLError('You are not authorised user!!', { diff --git a/backend/src/controllers/Experience/downvoteExperience.js b/backend/src/controllers/Experience/downvoteExperience.js index 7fe24d7..e83bda9 100644 --- a/backend/src/controllers/Experience/downvoteExperience.js +++ b/backend/src/controllers/Experience/downvoteExperience.js @@ -1,6 +1,9 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../prisma/index.js' import { addNameExp } from './addnameExp.js' +import { addLikeStatus } from '../../utils/addLikeStatus.js' +import { addUserName } from './Comment/addUserName.js' +import { formatExp } from './formatExp.js' export const downvoteExperience = async (_, payload, context) => { try { @@ -26,10 +29,14 @@ export const downvoteExperience = async (_, payload, context) => { }, }, include: { - comments: true, + comments: { + include: { + reply: true, + }, + }, }, }) - experience = addNameExp(experience) + experience = await formatExp(experience, context) return experience } else { await prisma.userVotes.update({ @@ -53,10 +60,14 @@ export const downvoteExperience = async (_, payload, context) => { }, }, include: { - comments: true, + comments: { + include: { + reply: true, + }, + }, }, }) - experience = addNameExp(experience) + experience = await formatExp(experience, context) return experience } } else { @@ -78,10 +89,14 @@ export const downvoteExperience = async (_, payload, context) => { }, }, include: { - comments: true, + comments: { + include: { + reply: true, + }, + }, }, }) - experience = addNameExp(experience) + experience = await formatExp(experience, context) return experience } } else { diff --git a/backend/src/controllers/Experience/formatExp.js b/backend/src/controllers/Experience/formatExp.js new file mode 100644 index 0000000..c9aa1a9 --- /dev/null +++ b/backend/src/controllers/Experience/formatExp.js @@ -0,0 +1,35 @@ +import { GraphQLError } from "graphql" +import { addLikeStatus } from "../../utils/addLikeStatus.js" +import { addUserName } from "./Comment/addUserName.js" +import { addNameExp } from "./addnameExp.js" + +export const formatExp = async (experience, context) => { + try { + + experience = await addLikeStatus(experience, context.userId, 'EXPERIENCE') + experience = await addNameExp(experience) + console.log('The length of exp.comments : ', experience) + for (let i = 0; i < experience.comments?.length; i++) { + experience.comments[i] = await addLikeStatus( + experience.comments[i], + context.userId, + 'EXP_COMMENT' + ) + experience.comments[i] = await addUserName(experience.comments[i]) + for (let j = 0; j < experience.comments[i]?.reply?.length; j++) { + experience.comments[i].reply[j] = await addLikeStatus( + experience.comments[i].reply[j], + context.userId, + 'EXP_REPLY' + ) + experience.comments[i].reply[j] = await addUserName( + experience.comments[i].reply[j] + ) + } + } + return experience + } catch (error) { + console.log('ERROR WHILE FORMATTING EXP : ', error) + throw new GraphQLError('Something went wrong') + } +} diff --git a/backend/src/controllers/Experience/getExperienceById.js b/backend/src/controllers/Experience/getExperienceById.js index 3a62a55..7b90aee 100644 --- a/backend/src/controllers/Experience/getExperienceById.js +++ b/backend/src/controllers/Experience/getExperienceById.js @@ -2,8 +2,10 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../prisma/index.js' import { addNameExp } from './addnameExp.js' import { addUserName } from './Comment/addUserName.js' +import { addLikeStatus } from '../../utils/addLikeStatus.js' +import { formatExp } from './formatExp.js' -export const getExperienceById = async (_, payload) => { +export const getExperienceById = async (_, payload, context) => { try { /* payload : {experienceId} @@ -14,23 +16,16 @@ export const getExperienceById = async (_, payload) => { }, include: { comments: { - include : { - reply : true - } - } + include: { + reply: true, + }, + }, }, }) - experience = await addNameExp(experience) - console.log('The length of exp.comments : ', experience); - for (let i = 0; i < experience.comments?.length; i++) { - experience.comments[i] = await addUserName(experience.comments[i]) - for(let j = 0; j < experience.comments[i]?.reply?.length; j++) { - experience.comments[i].reply[j] = await addUserName(experience.comments[i].reply[j]) - } - } + experience = await formatExp(experience, context) return experience } catch (error) { - console.log('Error while getting experience by id : ', error); + console.log('Error while getting experience by id : ', error) throw new GraphQLError('Something went wrong!!') } } diff --git a/backend/src/controllers/Experience/updateExperience.js b/backend/src/controllers/Experience/updateExperience.js index d01b6bf..c16332c 100644 --- a/backend/src/controllers/Experience/updateExperience.js +++ b/backend/src/controllers/Experience/updateExperience.js @@ -29,7 +29,8 @@ export const updateExperience = async (_, payload, context) => { comments : true } }) - updatedExp = addNameExp(updatedExp) + updatedExp = await addLikeStatus(updatedExp, context.userId, 'EXPERIENCE') + updatedExp = await addNameExp(updatedExp) return updatedExp } else { throw new GraphQLError( diff --git a/backend/src/controllers/Experience/upvoteExperience.js b/backend/src/controllers/Experience/upvoteExperience.js index e9ff91f..b807cdd 100644 --- a/backend/src/controllers/Experience/upvoteExperience.js +++ b/backend/src/controllers/Experience/upvoteExperience.js @@ -1,6 +1,9 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../prisma/index.js' import { addNameExp } from './addnameExp.js' +import { addLikeStatus } from '../../utils/addLikeStatus.js' +import { addUserName } from './Comment/addUserName.js' +import { formatExp } from './formatExp.js' export const upvoteExperience = async (_, payload, context) => { try { @@ -19,8 +22,15 @@ export const upvoteExperience = async (_, payload, context) => { let experience = await prisma.experience.update({ where: { id: payload.id }, data: { upvotes: { decrement: 1 } }, + include: { + comments: { + include: { + reply: true, + }, + }, + }, }) - experience = addNameExp(experience) + experience = await formatExp(experience, context) return experience } else { await prisma.userVotes.update({ @@ -30,6 +40,7 @@ export const upvoteExperience = async (_, payload, context) => { data: { type: 'LIKE', }, + }) let experience = await prisma.experience.update({ where: { @@ -44,10 +55,14 @@ export const upvoteExperience = async (_, payload, context) => { }, }, include: { - comments: true, + comments: { + include : { + reply : true + } + } }, }) - experience = addNameExp(experience) + experience = await formatExp(experience, context) return experience } } else { @@ -69,10 +84,14 @@ export const upvoteExperience = async (_, payload, context) => { }, }, include: { - comments: true, + comments: { + include : { + reply : true + } + } }, }) - experience = addNameExp(experience) + experience = await formatExp(experience, context) return experience } } else { @@ -91,6 +110,7 @@ export const upvoteExperience = async (_, payload, context) => { ) { throw error } + console.log("ERROR WHILE UPVOTING EXPERIENCE : ", error) throw new GraphQLError('Something went wrong!!') } } diff --git a/backend/src/controllers/Question/downVoteQuestion.js b/backend/src/controllers/Question/downVoteQuestion.js index 76e9ac2..bf182bc 100644 --- a/backend/src/controllers/Question/downVoteQuestion.js +++ b/backend/src/controllers/Question/downVoteQuestion.js @@ -1,6 +1,7 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../prisma/index.js' import { getQuestionByIdHelper } from './getQuestionByIdHelper.js' +import { addLikeStatus } from '../../utils/addLikeStatus.js' export const downVoteQuestion = async (_, payload, context) => { try { @@ -18,7 +19,7 @@ export const downVoteQuestion = async (_, payload, context) => { if (voteEntry.type === 'DISLIKE') { // delete voteEntry and decrement downvotes await prisma.userVotes.delete({ where: { id: voteEntry.id } }) - const updQuestion = await prisma.question.update({ + let updQuestion = await prisma.question.update({ where: { id: payload.QuestionId }, data: { downvotes: { decrement: 1 } }, }) @@ -26,6 +27,8 @@ export const downVoteQuestion = async (_, payload, context) => { 'Successfully Disliked Count Decreased of Question with ID : ', payload.QuestionId ) + + updQuestion = addLikeStatus(updQuestion, context.userId, 'QUESTION') return updQuestion } else { // Update vote entry type to DISLIKE, and increment downvotes and decrement upvotes @@ -33,7 +36,7 @@ export const downVoteQuestion = async (_, payload, context) => { where: { id: voteEntry.id }, data: { type: 'DISLIKE' }, }) - const updQuestion = await prisma.question.update({ + let updQuestion = await prisma.question.update({ where: { id: payload.QuestionId }, data: { downvotes: { increment: 1 }, upvotes: { decrement: 1 } }, }) @@ -41,6 +44,8 @@ export const downVoteQuestion = async (_, payload, context) => { 'Successfully like count decreased and dislike count increased of Question with ID : ', payload.QuestionId ) + + updQuestion = addLikeStatus(updQuestion, context.userId, 'QUESTION') return updQuestion } } else { @@ -52,7 +57,7 @@ export const downVoteQuestion = async (_, payload, context) => { type: 'DISLIKE', }, }) - const updQuestion = await prisma.question.update({ + let updQuestion = await prisma.question.update({ where: { id: payload.QuestionId }, data: { downvotes: { increment: 1 } }, }) @@ -60,6 +65,8 @@ export const downVoteQuestion = async (_, payload, context) => { 'Successfully Disliked Count Increased of Question with ID : ', payload.QuestionId ) + + updQuestion = addLikeStatus(updQuestion, context.userId, 'QUESTION') return updQuestion } } diff --git a/backend/src/controllers/Question/getQuestionById.js b/backend/src/controllers/Question/getQuestionById.js index d095430..b833c57 100644 --- a/backend/src/controllers/Question/getQuestionById.js +++ b/backend/src/controllers/Question/getQuestionById.js @@ -1,8 +1,10 @@ +import { addLikeStatus } from '../../utils/addLikeStatus.js' import { getQuestionByIdHelper } from './getQuestionByIdHelper.js' -export const getQuestionById = async (_, payload, __) => { +export const getQuestionById = async (_, payload, context) => { console.log('GETTING QUESTION BY ID : ', payload.QuestionId) - const question = await getQuestionByIdHelper(payload.QuestionId) + let question = await getQuestionByIdHelper(payload.QuestionId) console.log(`GOT QUESTION WITH ID : ${payload.QuestionId} = ${question}`) + question = addLikeStatus(question, context.userId, 'QUESTION') return question } diff --git a/backend/src/controllers/Question/upVoteQuestion.js b/backend/src/controllers/Question/upVoteQuestion.js index 469ac7d..342f744 100644 --- a/backend/src/controllers/Question/upVoteQuestion.js +++ b/backend/src/controllers/Question/upVoteQuestion.js @@ -1,6 +1,7 @@ import { GraphQLError } from 'graphql' import { prisma } from '../../../prisma/index.js' import { getQuestionByIdHelper } from './getQuestionByIdHelper.js' +import { addLikeStatus } from '../../utils/addLikeStatus.js' export const upVoteQuestion = async (_, payload, context) => { try { @@ -18,7 +19,7 @@ export const upVoteQuestion = async (_, payload, context) => { if (voteEntry.type === 'LIKE') { // Delte voteEntry, and decrement upvotes await prisma.userVotes.delete({ where: { id: voteEntry.id } }) - const updQuestion = await prisma.question.update({ + let updQuestion = await prisma.question.update({ where: { id: payload.QuestionId }, data: { upvotes: { decrement: 1 } }, }) @@ -26,6 +27,8 @@ export const upVoteQuestion = async (_, payload, context) => { 'Successfully Liked Count Decreased of Question with ID : ', payload.QuestionId ) + + updQuestion = addLikeStatus(updQuestion, context.userId, 'QUESTION') return updQuestion } else { // Update vote entry type to LIKE, and increment upvotes and decrement downvotes @@ -33,7 +36,7 @@ export const upVoteQuestion = async (_, payload, context) => { where: { id: voteEntry.id }, data: { type: 'LIKE' }, }) - const updQuestion = await prisma.question.update({ + let updQuestion = await prisma.question.update({ where: { id: payload.QuestionId }, data: { upvotes: { increment: 1 }, downvotes: { decrement: 1 } }, }) @@ -41,6 +44,8 @@ export const upVoteQuestion = async (_, payload, context) => { 'Successfully dislike count decreased and like count increased of Question with ID : ', payload.QuestionId ) + + updQuestion = addLikeStatus(updQuestion, context.userId, 'QUESTION') return updQuestion } } else { @@ -52,7 +57,7 @@ export const upVoteQuestion = async (_, payload, context) => { type: 'LIKE', }, }) - const updQuestion = await prisma.question.update({ + let updQuestion = await prisma.question.update({ where: { id: payload.QuestionId }, data: { upvotes: { increment: 1 } }, }) @@ -60,6 +65,8 @@ export const upVoteQuestion = async (_, payload, context) => { 'Successfully Liked Count Increased of Question with ID : ', payload.QuestionId ) + + updQuestion = addLikeStatus(updQuestion, context.userId, 'QUESTION') return updQuestion } } diff --git a/backend/src/graphql/Experience/resolvers.js b/backend/src/graphql/Experience/resolvers.js index 7157b44..f533a05 100644 --- a/backend/src/graphql/Experience/resolvers.js +++ b/backend/src/graphql/Experience/resolvers.js @@ -18,9 +18,9 @@ const queries = {} const mutations = { - getAllExperience : (_, payload, context) => checkRole({_, payload, context}, getAllExperience), + getAllExperience : (_, payload, context) => getAllExperience(_, payload, context), - getExperienceById : (_, payload, context) => checkRole({_, payload, context}, getExperienceById), + getExperienceById : (_, payload, context) => getExperienceById(_, payload, context), createExperience : (_, payload, context) => checkRole({_, payload, context}, createExperience), diff --git a/backend/src/graphql/Experience/typedefs.js b/backend/src/graphql/Experience/typedefs.js index de67758..20ec447 100644 --- a/backend/src/graphql/Experience/typedefs.js +++ b/backend/src/graphql/Experience/typedefs.js @@ -13,6 +13,8 @@ export const typeDefs = `#graphql comments : [ExpComment] upvotes : Int downvotes : Int + isLiked : Boolean + isDisliked : Boolean } type ExpComment { @@ -23,6 +25,8 @@ export const typeDefs = `#graphql commentorId : Int commentorUserName : String likes : Int + isLiked : Boolean + isDisliked : Boolean } type ExpReply { @@ -31,7 +35,9 @@ export const typeDefs = `#graphql description : String replierId : Int replierUserName : String - likes : Int + likes : Int + isLiked : Boolean + isDisliked : Boolean } input InputExperience { diff --git a/backend/src/graphql/Questions/typedefs.js b/backend/src/graphql/Questions/typedefs.js index a08247e..072ebfe 100644 --- a/backend/src/graphql/Questions/typedefs.js +++ b/backend/src/graphql/Questions/typedefs.js @@ -11,6 +11,8 @@ export const typeDefs = `#graphql isVisible : Boolean upvotes : Int downvotes : Int + isLiked : Boolean + isDisliked : Boolean } type QueAddOnLink { diff --git a/backend/src/utils/addLikeStatus.js b/backend/src/utils/addLikeStatus.js new file mode 100644 index 0000000..f368d86 --- /dev/null +++ b/backend/src/utils/addLikeStatus.js @@ -0,0 +1,27 @@ +import { GraphQLError } from 'graphql' +import { prisma } from '../../prisma/index.js' + +export const addLikeStatus = async (entity, userId, type) => { + try { + console.log("QUERY TO ADD LIKE STATUS : ", JSON.stringify(entity), " -- TYPE : ", type) + const voteEntry = await prisma.userVotes.findFirst({ + where: { + userId, + area: type, + areaId: entity.id, + }, + }) + + entity.isLiked = (voteEntry && voteEntry.type == 'LIKE') ? true : false + entity.isDisliked = (voteEntry && voteEntry.type == 'DISLIKE') ? true : false + + return entity + } catch (error) { + console.log('ERROR : ', error); + throw new GraphQLError("Error while adding like status to entity", { + extensions : { + code : "ERROR_WHILE_ATTACHING_LIKE_STATUS" + } + }) + } +}