From fc24d00257099e522bdc748e6151832af5ef7f70 Mon Sep 17 00:00:00 2001 From: Felipe D'Abrantes <felidabrantes@gmail> Date: Fri, 21 Apr 2023 23:45:40 +0100 Subject: [PATCH] Add endpoint to delete a question --- .../app/controllers/QuestionController.scala | 27 ++++++++++++++++++- .../feed-service/app/models/Question.scala | 5 ++++ backend-services/feed-service/conf/routes | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/backend-services/feed-service/app/controllers/QuestionController.scala b/backend-services/feed-service/app/controllers/QuestionController.scala index a812ef30..36c53a66 100644 --- a/backend-services/feed-service/app/controllers/QuestionController.scala +++ b/backend-services/feed-service/app/controllers/QuestionController.scala @@ -2,7 +2,7 @@ package controllers import models.{Question} import models.actions.{AuthenticatedUserAction, AuthenticationRequest} -import models.exceptions.{ConflictException, InvalidRequestBodyException, ForbiddenException} +import models.exceptions.{ConflictException, InvalidRequestBodyException, InvalidQueryParameterException, ForbiddenException} import javax.inject._ import play.api.mvc._ @@ -10,6 +10,7 @@ import play.api.libs.json.{JsValue} import play.api.libs.json.JsLookupResult import scala.concurrent.TimeoutException +import org.bson.types.ObjectId /** @@ -62,6 +63,30 @@ class QuestionController @Inject()(val controllerComponents: ControllerComponent } } + /** + * Create an Action to delete a Question in the DB. + */ + def deleteQuestion(questionId: String) = authenticatedUserAction { implicit request: AuthenticationRequest[AnyContent] => + println("QuestionController:deleteQuestion") + + try { + if (!ObjectId.isValid(questionId)) throw new InvalidQueryParameterException("Invalid query parameter ID format: questionId") + if (!request.isAdmin) throw new ForbiddenException("You must be an admin to delete a question.") + + Question.deleteQuestionAsync(new ObjectId(questionId)) + Ok("Deleted question") + } catch { + case _: TimeoutException => BadRequest("Request timed out") + case ex: InvalidQueryParameterException => BadRequest(ex.getMessage()) + case ex: ConflictException => BadRequest(ex.getMessage()) + case ex: ForbiddenException => Forbidden(ex.getMessage()) + case ex: Throwable => { + println(ex.getMessage()) + BadRequest("Exception raised") + } + } + } + /** * Fetch the needed values from the request body for the liking/unliking a Daily endpoint. * diff --git a/backend-services/feed-service/app/models/Question.scala b/backend-services/feed-service/app/models/Question.scala index 2985269a..58652c45 100644 --- a/backend-services/feed-service/app/models/Question.scala +++ b/backend-services/feed-service/app/models/Question.scala @@ -51,6 +51,11 @@ object Question { Await.result[Question](newQuestion, timeout.seconds) } + def deleteQuestionAsync(questionId: ObjectId, timeout: Int = 4): Unit = { + val delete: Future[Unit] = questionRepo.deleteOne(questionId) + Await.result[Unit](delete, timeout.seconds) + } + def getLeastUsed(): Future[Question] = { questionRepo.getFirst(None, Some(Sorts.ascending("used")), None).map((question: Option[Question]) => { if (question.isEmpty) diff --git a/backend-services/feed-service/conf/routes b/backend-services/feed-service/conf/routes index 10276a28..c4d70c1b 100644 --- a/backend-services/feed-service/conf/routes +++ b/backend-services/feed-service/conf/routes @@ -32,3 +32,5 @@ GET /question controllers.DailyQuestionController.getDailyQues GET /questions controllers.QuestionController.getQuestions() POST /question controllers.QuestionController.insertQuestion() + +DELETE /question controllers.QuestionController.deleteQuestion(questionId: String) -- GitLab