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

Construct Daily repository with fetch method (#19)

parent bf6517ea
No related branches found
No related tags found
1 merge request!14Add endpoints to manage Dailies
package repositories
import com.typesafe.config.ConfigFactory
import models.{Daily, MongoDBClient}
import org.mongodb.scala.{MongoCollection, Document}
import org.bson.types.ObjectId
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Await}
import scala.concurrent.duration._
class DailyRepository extends MongoDBClient {
// Loads the default configuration
private val config = ConfigFactory.load()
private val databaseName = config.getString("mongo.feedService.db")
private val collectionName = config.getString("mongo.dailies.collection")
/**
* Returns a reference to a MongoDB collection within a database.
* Once Future completes, collection reference is returned.
*
* @return a MongoCollection[Document] object representing the dailies collection.
* @throws TimeoutException if the Future doesn't complete within the 3 second timeout.
*/
private def dailiesCollection: MongoCollection[Document] = {
val futureCollection = for {
database <- getDatabase(databaseName)
collection <- getCollection(database, collectionName)
} yield collection
Await.result(futureCollection, 3.seconds)
}
/**
* Gets all the Daily records.
*
* @return A Future containing a sequence of matching Daily objects.
*/
def getAllDailies(): Future[Seq[Daily]] = {
val documents: Future[Seq[Document]] = find(dailiesCollection)
documents.map(document => {
document.map(doc => Daily(
Some(doc.getObjectId("_id")),
doc.getObjectId("user_id"),
doc.getObjectId("question_id"),
doc.getString("content"),
doc.getInteger("likes")
))
})
}
}
# Default Configuration File
# MongoDB Connection Strings
mongodb.uri = "mongodb://localhost:27017/"
mongodb.uri="mongodb://localhost:27017/"
mongo.feedService.db = "feed-service"
mongo.dailies.collection = "dailies"
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