From d0d31776ee0ed721d1be198d6c3263a888194985 Mon Sep 17 00:00:00 2001 From: Danielzxccc Date: Tue, 26 Dec 2023 21:13:31 +0800 Subject: [PATCH] new schema for answer votes --- src/modules/Forums/ForumsController.ts | 12 +++++ src/modules/Forums/ForumsInteractor.ts | 10 ++++ src/modules/Forums/ForumsRouter.ts | 69 ++++++++++++++++++++++++++ src/modules/Forums/ForumsService.ts | 21 +++++++- src/schema/ForumsSchema.ts | 21 ++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) diff --git a/src/modules/Forums/ForumsController.ts b/src/modules/Forums/ForumsController.ts index f6faaea..9cc1632 100644 --- a/src/modules/Forums/ForumsController.ts +++ b/src/modules/Forums/ForumsController.ts @@ -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) + } +} diff --git a/src/modules/Forums/ForumsInteractor.ts b/src/modules/Forums/ForumsInteractor.ts index 7e097a9..a25ce95 100644 --- a/src/modules/Forums/ForumsInteractor.ts +++ b/src/modules/Forums/ForumsInteractor.ts @@ -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) +} diff --git a/src/modules/Forums/ForumsRouter.ts b/src/modules/Forums/ForumsRouter.ts index 835b438..4a44815 100644 --- a/src/modules/Forums/ForumsRouter.ts +++ b/src/modules/Forums/ForumsRouter.ts @@ -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: @@ -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: @@ -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: @@ -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: @@ -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 +) diff --git a/src/modules/Forums/ForumsService.ts b/src/modules/Forums/ForumsService.ts index dd0726a..47d4470 100644 --- a/src/modules/Forums/ForumsService.ts +++ b/src/modules/Forums/ForumsService.ts @@ -153,6 +153,13 @@ export async function viewQuestion( ]) .whereRef('forums_answers.userid', '=', 'users.id') ).as('user'), + jsonObjectFrom( + eb + .selectFrom('answer_votes') + .select([sql`CAST(id AS TEXT)`.as('id'), 'type']) + .where('answer_votes.userid', '=', userid) + .whereRef('answer_votes.answerid', '=', 'forums_answers.id') + ).as('vote'), fn .count('answer_votes.id') .filterWhere('type', '=', 'upvote') @@ -198,7 +205,7 @@ export async function viewQuestion( jsonObjectFrom( eb .selectFrom('forums_ratings') - .select(['type']) + .select([sql`CAST(id AS TEXT)`.as('id'), 'type']) .where('forums_ratings.userid', '=', userid) .whereRef('forums.id', '=', 'forums_ratings.questionid') ).as('vote'), @@ -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') diff --git a/src/schema/ForumsSchema.ts b/src/schema/ForumsSchema.ts index e089d3b..9abd431 100644 --- a/src/schema/ForumsSchema.ts +++ b/src/schema/ForumsSchema.ts @@ -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) @@ -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 @@ -492,3 +501,15 @@ export const VoteAnswerSchema = z.object({ }) export type ForumsContent = z.infer + +/** + * @openapi + * components: + * schemas: + * DeleteVoteAnswerResponse: + * type: object + * properties: + * message: + * type: string + * description: The success message indicating that the vote has been deleted + */