Skip to content

Commit

Permalink
Merge pull request #208 from ChetanSaini12/chetan
Browse files Browse the repository at this point in the history
Chetan
  • Loading branch information
ChetanSaini12 authored Jul 1, 2024
2 parents 53d8f74 + c98e3d2 commit aa4eeff
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 46 deletions.
4 changes: 3 additions & 1 deletion backend/src/controllers/Experience/Comment/addCommentExp.js
Original file line number Diff line number Diff line change
@@ -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 {
/*
Expand All @@ -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', {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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', {
Expand Down
39 changes: 35 additions & 4 deletions backend/src/controllers/Experience/Comment/likeExpComment.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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!!', {
Expand All @@ -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!!')
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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!!', {
Expand Down
27 changes: 21 additions & 6 deletions backend/src/controllers/Experience/downvoteExperience.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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({
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions backend/src/controllers/Experience/formatExp.js
Original file line number Diff line number Diff line change
@@ -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')
}
}
23 changes: 9 additions & 14 deletions backend/src/controllers/Experience/getExperienceById.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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!!')
}
}
3 changes: 2 additions & 1 deletion backend/src/controllers/Experience/updateExperience.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 25 additions & 5 deletions backend/src/controllers/Experience/upvoteExperience.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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({
Expand All @@ -30,6 +40,7 @@ export const upvoteExperience = async (_, payload, context) => {
data: {
type: 'LIKE',
},

})
let experience = await prisma.experience.update({
where: {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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!!')
}
}
Loading

0 comments on commit aa4eeff

Please sign in to comment.