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

Create a Question repository

parent 481e5d14
No related branches found
No related tags found
1 merge request!17Implement Question Service
package models
import org.bson.types.ObjectId
import java.util.Date
case class Question (
id: Option[ObjectId],
content: String,
used: Integer,
createdAt: Date,
updatedAt: Date
)
package repositories
import models.Question
import utils.MongoConnection
import com.typesafe.config.ConfigFactory
import org.bson.types.ObjectId
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class QuestionRepository extends Repository[Question] (
ConfigFactory.load().getString("mongo.questionService.db"),
ConfigFactory.load().getString("mongo.questions.collection")
) {
/**
* Inserts a Question record into the database.
*
* @return A Future containing the inserted Question object with the generated ID.
*/
def insertQuestion(question: Question): Future[Question] = {
val result: Future[String] = MongoConnection.insertOne[Question](collection, question)
// Return a Question entity with the generated ID
result.flatMap[Question](id => {
val updatedQuestion: Question = question.copy(id = Some(new ObjectId(id)))
Future.successful(updatedQuestion)
})
}
}
package utils
import models.Daily
import models.{Daily, Question}
import org.bson.codecs.Codec
import org.bson.codecs.configuration.{CodecProvider, CodecRegistry, CodecRegistries}
......@@ -14,6 +14,10 @@ object MongoCodecs {
// If the class is the Daily case class, return the Daily codec
Daily.codec.asInstanceOf[Codec[T]]
}
else if (clazz == classOf[Question]) {
// If the class is the Question case class, return the Question codec
Question.codec.asInstanceOf[Codec[T]]
}
else {
// If the class is not the User case class, return null
null
......
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