Skip to content

Commit

Permalink
new schema for answer votes
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielzxccc committed Dec 26, 2023
1 parent 032efc5 commit d0d3177
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/modules/Forums/ForumsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,15 @@ export async function voteAnswer(req: SessionRequest, res: Response) {
errorHandler(res, error)
}
}

export async function deleteVoteAnswer(req: SessionRequest, res: Response) {
try {
const { id } = req.params
const userid = req.session.userid
await Interactor.deleteAnswerVote(id, userid)
res.status(200).json({ message: 'deleted Successfully' })
} catch (error) {
console.log(error.stack)
errorHandler(res, error)
}
}
10 changes: 10 additions & 0 deletions src/modules/Forums/ForumsInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,13 @@ export async function voteAnswer(
const votedQuestion = await Service.voteAnswer(data)
return votedQuestion
}

export async function deleteAnswerVote(id: string, userid: string) {
if (!userid) throw new HttpError('Unauthorized', 401)
const vote = await Service.findAnswerVote(id)
if (!vote) throw new HttpError('Already Deleted', 400)

if (vote.userid !== userid) throw new HttpError('Unauthorized', 401)

await Service.deleteAnswerVote(id)
}
69 changes: 69 additions & 0 deletions src/modules/Forums/ForumsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ ForumsRouter.get('/:id', ForumsController.viewQuestion)
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "401":
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "500":
* description: Server Error
* content:
Expand Down Expand Up @@ -272,6 +278,12 @@ ForumsRouter.post(
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "401":
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "429":
* description: Too much request
* content:
Expand Down Expand Up @@ -332,6 +344,12 @@ ForumsRouter.post(
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "401":
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "429":
* description: Too much request
* content:
Expand Down Expand Up @@ -391,6 +409,12 @@ ForumsRouter.post(
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "401":
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "429":
* description: Too much request
* content:
Expand All @@ -417,3 +441,48 @@ ForumsRouter.post(
]),
ForumsController.voteAnswer
)

/**
* @openapi
* /api/forums/delete/vote-answer/{id}:
* delete:
* summary: Delete a vote for an answer
* tags:
* - Forums
* parameters:
* - name: id
* in: path
* required: true
* schema:
* type: string
* description: The ID of the vote for an answer
* responses:
* "200":
* description: Success. Indicates that the vote for the answer has been deleted.
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/DeleteVoteAnswerResponse"
* "401":
* description: Validation Error
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "429":
* description: Too much request
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorResponse"
* "500":
* description: Server Error
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ServerError"
*/
ForumsRouter.delete(
'/delete/vote-answer/:id',
ForumsController.deleteVoteAnswer
)
21 changes: 20 additions & 1 deletion src/modules/Forums/ForumsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ export async function viewQuestion(
])
.whereRef('forums_answers.userid', '=', 'users.id')
).as('user'),
jsonObjectFrom(
eb
.selectFrom('answer_votes')
.select([sql<string>`CAST(id AS TEXT)`.as('id'), 'type'])
.where('answer_votes.userid', '=', userid)
.whereRef('answer_votes.answerid', '=', 'forums_answers.id')
).as('vote'),
fn
.count<number>('answer_votes.id')
.filterWhere('type', '=', 'upvote')
Expand Down Expand Up @@ -198,7 +205,7 @@ export async function viewQuestion(
jsonObjectFrom(
eb
.selectFrom('forums_ratings')
.select(['type'])
.select([sql<string>`CAST(id AS TEXT)`.as('id'), 'type'])
.where('forums_ratings.userid', '=', userid)
.whereRef('forums.id', '=', 'forums_ratings.questionid')
).as('vote'),
Expand Down Expand Up @@ -335,6 +342,18 @@ export async function voteAnswer(vote: NewVoteQuestion) {
.executeTakeFirst()
}

export async function deleteAnswerVote(id: string) {
return await db.deleteFrom('answer_votes').where('id', '=', id).execute()
}

export async function findAnswerVote(id: string) {
return await db
.selectFrom('answer_votes')
.selectAll()
.where('id', '=', id)
.executeTakeFirst()
}

// export async function findVoteByUserId(userid: string) {
// return await db
// .selectFrom('forums_ratings')
Expand Down
21 changes: 21 additions & 0 deletions src/schema/ForumsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ import { z } from 'zod'
* vote:
* type: object
* properties:
* id:
* type: string
* type:
* type: string
* description: The vote information (null in the provided example)
Expand Down Expand Up @@ -228,6 +230,13 @@ import { z } from 'zod'
* $ref: "#/components/schemas/UserObject"
* createdat:
* type: string
* vote:
* type: object
* properties:
* id:
* type: string
* type:
* type: string
*
* Comment:
* type: object
Expand Down Expand Up @@ -492,3 +501,15 @@ export const VoteAnswerSchema = z.object({
})

export type ForumsContent = z.infer<typeof ForumsSchema>

/**
* @openapi
* components:
* schemas:
* DeleteVoteAnswerResponse:
* type: object
* properties:
* message:
* type: string
* description: The success message indicating that the vote has been deleted
*/

0 comments on commit d0d3177

Please sign in to comment.