Skip to content
Snippets Groups Projects
Commit fc24d002 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Add endpoint to delete a question

parent cebb00cc
No related branches found
No related tags found
1 merge request!25Add a question management page
......@@ -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.
*
......
......@@ -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)
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment