diff --git a/backend-services/feed-service/app/controllers/QuestionController.scala b/backend-services/feed-service/app/controllers/QuestionController.scala index a812ef309a59da16fb5a6bc3bd4acd622e5740c9..36c53a668b81229e7609a1fd5dd0bcb4e26f9ffa 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 2985269a5944fb38c19735dd386a73f6c18acbcb..58652c4531b63ea9bab27746e680a384c6806583 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 10276a280227d589276c60563d573fcad14764cc..c4d70c1b77ae33a4544848ef4dc890d73abea5fe 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)