Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
quiz-management-service/spray-crud/src/main/scala/com/danielasfregola/quiz/management/services/QuestionService.scala
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (37 sloc)
1.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danielasfregola.quiz.management.services | |
import com.danielasfregola.quiz.management.entities.{Question, QuestionUpdate} | |
import scala.concurrent.{ExecutionContext, Future} | |
class QuestionService(implicit val executionContext: ExecutionContext) { | |
var questions = Vector.empty[Question] | |
def createQuestion(question: Question): Future[Option[String]] = Future { | |
questions.find(_.id == question.id) match { | |
case Some(q) => None // Conflict! id is already taken | |
case None => | |
questions = questions :+ question | |
Some(question.id) | |
} | |
} | |
def getQuestion(id: String): Future[Option[Question]] = Future { | |
questions.find(_.id == id) | |
} | |
def updateQuestion(id: String, update: QuestionUpdate): Future[Option[Question]] = { | |
def updateEntity(question: Question): Question = { | |
val title = update.title.getOrElse(question.title) | |
val text = update.text.getOrElse(question.text) | |
Question(id, title, text) | |
} | |
getQuestion(id).flatMap { maybeQuestion => | |
maybeQuestion match { | |
case None => Future { None } // No question found, nothing to update | |
case Some(question) => | |
val updatedQuestion = updateEntity(question) | |
deleteQuestion(id).flatMap { _ => | |
createQuestion(updatedQuestion).map(_ => Some(updatedQuestion)) | |
} | |
} | |
} | |
} | |
def deleteQuestion(id: String): Future[Unit] = Future { | |
questions = questions.filterNot(_.id == id) | |
} | |
} | |