From 17c04a28bab74dedae583537d42ae91c691eb4b9 Mon Sep 17 00:00:00 2001 From: Silvi Kabra Date: Fri, 2 Dec 2022 20:34:57 -0500 Subject: [PATCH] delete resource route and button --- .../AdminDashboard/DeleteQuestionButton.tsx | 27 ++++++++++++--- client/src/AdminDashboard/QuestionTable.tsx | 2 ++ client/src/AdminDashboard/api.tsx | 14 +++++++- server/src/controllers/admin.controller.ts | 33 ++++++++++++++++++- server/src/routes/admin.route.ts | 9 +++++ server/src/services/question.service.ts | 11 +++++++ 6 files changed, 90 insertions(+), 6 deletions(-) diff --git a/client/src/AdminDashboard/DeleteQuestionButton.tsx b/client/src/AdminDashboard/DeleteQuestionButton.tsx index 04ae4925..289a442e 100644 --- a/client/src/AdminDashboard/DeleteQuestionButton.tsx +++ b/client/src/AdminDashboard/DeleteQuestionButton.tsx @@ -1,11 +1,12 @@ import React, { useState } from 'react'; import Button from '@mui/material/Button'; import { Navigate, useNavigate } from 'react-router-dom'; -import { deleteQuestion } from './api'; // change to deleteQuestion +import { deleteQuestion, deleteResource } from './api'; // change to deleteQuestion import LoadingButton from '../components/buttons/LoadingButton'; import ConfirmationModal from '../components/ConfirmationModal'; interface DeleteQuestionButtonProps { + id: string; isQuestion: boolean; text: string; removeRow: (question: string) => void; @@ -14,12 +15,14 @@ interface DeleteQuestionButtonProps { /** * The button component which, when clicked, will delete the question from the database. * If the user is not a valid question, button will be unclickable //this is kinda unnecessary lowkey + * @param id - id of the question to delete * @param isQuestion - whether the question is valid * @param text - the text of the question to delete * @param removeRow - a function which removes a row from the question table. This * function is called upon successfully deletion of user from the database. */ function DeleteQuestionButton({ + id, isQuestion, text, removeRow, @@ -38,6 +41,18 @@ function DeleteQuestionButton({ setLoading(false); } } + + async function handleDeleteResource() { + console.log('deleting resource'); + console.log(id); + setLoading(true); + if (true) { + await deleteResource(id); + removeRow(text); + } else { + setLoading(false); + } + } if (isLoading) { return ; } @@ -52,10 +67,14 @@ function DeleteQuestionButton({ /> ); } + // resource return ( - + handleDeleteResource()} + /> ); } diff --git a/client/src/AdminDashboard/QuestionTable.tsx b/client/src/AdminDashboard/QuestionTable.tsx index a1fa2bc8..3ea58de4 100644 --- a/client/src/AdminDashboard/QuestionTable.tsx +++ b/client/src/AdminDashboard/QuestionTable.tsx @@ -140,6 +140,8 @@ function QuestionTable() { createAdminDashboardRow( question, removeQuestion(question)} diff --git a/client/src/AdminDashboard/api.tsx b/client/src/AdminDashboard/api.tsx index 5bcbd2ef..eefc34d8 100644 --- a/client/src/AdminDashboard/api.tsx +++ b/client/src/AdminDashboard/api.tsx @@ -20,6 +20,12 @@ async function deleteQuestion(text: string) { return true; } +async function deleteResource(id: string) { + const res = await deleteData(`admin/resource/${id}`); + if (res.error) return false; + return true; +} + // routes! hopefully async function editQuestion( questionVals: { [key: string]: string }, @@ -54,4 +60,10 @@ async function upgradePrivilege(email: string) { return true; } -export { deleteUser, deleteQuestion, editQuestion, upgradePrivilege }; +export { + deleteUser, + deleteQuestion, + deleteResource, + editQuestion, + upgradePrivilege, +}; diff --git a/server/src/controllers/admin.controller.ts b/server/src/controllers/admin.controller.ts index 227b0eba..c34dbb00 100644 --- a/server/src/controllers/admin.controller.ts +++ b/server/src/controllers/admin.controller.ts @@ -17,7 +17,7 @@ import { editQuestion, getAllQuestionsFromDB, getQuestionById, - // deleteQuestionById, + deleteQuestionById, } from '../services/question.service'; /** @@ -167,10 +167,41 @@ const editQuestionText = async ( }); }; +/** + * Delete a resource question from the database. The id of the resource is expected to be in the request parameter (url). Send a 200 OK status code on success. + */ +const deleteResource = async ( + req: express.Request, + res: express.Response, + next: express.NextFunction, +) => { + const { id } = req.params; + if (!id) { + next(ApiError.missingFields(['id'])); + return; + } + + // Check if resource to delete is an admin + const resource: IQuestion | null = await getQuestionById(id); + if (!resource) { + next(ApiError.notFound(`Resource with id ${id} does not exist`)); + return; + } + + // resource is a question + deleteQuestionById(resource._id) + .then(() => res.sendStatus(StatusCode.OK)) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + .catch((e) => { + next(ApiError.internal('Failed to delete question.')); + }); +}; + export { getAllUsers, upgradePrivilege, deleteUser, getAllQuestions, editQuestionText, + deleteResource, }; diff --git a/server/src/routes/admin.route.ts b/server/src/routes/admin.route.ts index 0a574168..72bd6753 100644 --- a/server/src/routes/admin.route.ts +++ b/server/src/routes/admin.route.ts @@ -9,6 +9,7 @@ import { upgradePrivilege, deleteUser, getAllQuestions, + deleteResource, editQuestionText, } from '../controllers/admin.controller'; import { isAuthenticated } from '../controllers/auth.middleware'; @@ -72,4 +73,12 @@ router.get('/allQuestions', getAllQuestions); // router.put('/editQuestion', isAuthenticated, isAdmin, editQuestionText); router.put('/editQuestion', editQuestionText); +/** + * A PUT route to delete a resource. Checks first if the requestor + * is a authenticated and is an admin. + * Expects the following fields in the URL: + * resource id (string) - The id of the resource to be deleted + */ +router.delete('/resource/:id', isAuthenticated, isAdmin, deleteResource); + export default router; diff --git a/server/src/services/question.service.ts b/server/src/services/question.service.ts index 3a68ae2c..6371a6d6 100644 --- a/server/src/services/question.service.ts +++ b/server/src/services/question.service.ts @@ -140,6 +140,16 @@ const editQuestion = async ( // return user; // }; +/** + * A function that deletes a question from the database. + * @param id The id of the question to delete. + * @returns The deleted {@link Question} + */ +const deleteQuestionById = async (id: string) => { + const question = await Question.findByIdAndDelete(id).exec(); + return question; +}; + export { // passwordHashSaltRounds, createQuestion, @@ -151,4 +161,5 @@ export { getAllQuestionsFromDB, editQuestion, // deleteUserById, + deleteQuestionById, };