Skip to content

Commit

Permalink
Merge pull request #98 from hmrc/APID-520
Browse files Browse the repository at this point in the history
APID-520 - Migrate to hmrc mongo component
  • Loading branch information
sivaprakashiv authored Oct 17, 2022
2 parents c0f3a1f + 0470754 commit 1a59345
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 500 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package uk.gov.hmrc.apisubscriptionfields

import java.util.UUID

import org.scalatest._
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.Application
Expand All @@ -27,14 +26,14 @@ import play.api.mvc._
import play.api.mvc.request.RequestTarget
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.modules.reactivemongo.ReactiveMongoComponent
import uk.gov.hmrc.apisubscriptionfields.model._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import cats.data.NonEmptyList
import uk.gov.hmrc.apisubscriptionfields.controller.Helper
import uk.gov.hmrc.mongo.MongoComponent

trait AcceptanceTestSpec extends FeatureSpec
with GivenWhenThen
Expand Down Expand Up @@ -97,7 +96,7 @@ trait AcceptanceTestSpec extends FeatureSpec
}

private def dropDatabase(): Unit = {
await( app.injector.instanceOf[ReactiveMongoComponent].mongoConnector.db().drop())
await( app.injector.instanceOf[MongoComponent].database.drop().toFuture())
}

def createRequest(method: String, path: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@

package uk.gov.hmrc.apisubscriptionfields.repository

import javax.inject.{Inject, Singleton}
import akka.stream.Materializer
import com.google.inject.ImplementedBy
import org.bson.codecs.configuration.CodecRegistries.{fromCodecs, fromRegistries}
import org.mongodb.scala.model.Filters.{and, equal}
import org.mongodb.scala.model.Indexes.ascending
import org.mongodb.scala.model.{Filters, IndexModel, IndexOptions}
import org.mongodb.scala.{MongoClient, MongoCollection}
import play.api.libs.json._
import reactivemongo.api.indexes.IndexType
import reactivemongo.bson.BSONObjectID
import reactivemongo.play.json.collection.JSONCollection
import uk.gov.hmrc.mongo.ReactiveRepository
import uk.gov.hmrc.mongo.json.ReactiveMongoFormats
import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters.ApiFieldDefinitionsJF
import uk.gov.hmrc.apisubscriptionfields.model.Types._
import uk.gov.hmrc.apisubscriptionfields.model._
import Types._
import uk.gov.hmrc.apisubscriptionfields.utils.ApplicationLogger
import uk.gov.hmrc.mongo.MongoComponent
import uk.gov.hmrc.mongo.play.json.{Codecs, CollectionFactory, PlayMongoRepository}

import scala.concurrent.Future
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

@ImplementedBy(classOf[ApiFieldDefinitionsMongoRepository])
trait ApiFieldDefinitionsRepository {
Expand All @@ -42,53 +47,71 @@ trait ApiFieldDefinitionsRepository {
}

@Singleton
class ApiFieldDefinitionsMongoRepository @Inject() (mongoDbProvider: MongoDbProvider)
extends ReactiveRepository[ApiFieldDefinitions, BSONObjectID]("fieldsDefinitions", mongoDbProvider.mongo, JsonFormatters.ApiFieldDefinitionsJF, ReactiveMongoFormats.objectIdFormats)
class ApiFieldDefinitionsMongoRepository @Inject() (mongo: MongoComponent)
(implicit ec: ExecutionContext, val mat: Materializer)
extends PlayMongoRepository[ApiFieldDefinitions](
collectionName = "fieldsDefinitions",
mongoComponent = mongo,
domainFormat = JsonFormatters.ApiFieldDefinitionsJF,
indexes = Seq(
IndexModel(ascending(List("apiContext", "apiVersion"): _*),
IndexOptions()
.name("apiContext-apiVersion_index")
.background(true)
.unique(true))
))
with ApiFieldDefinitionsRepository
with MongoCrudHelper[ApiFieldDefinitions] {

override val mongoCollection: JSONCollection = collection

override def indexes = Seq(
createCompoundIndex(
indexFieldMappings = Seq(
"apiContext" -> IndexType.Ascending,
"apiVersion" -> IndexType.Ascending
),
indexName = Some("apiContext-apiVersion_index"),
isUnique = true
)
)

override def save(definitions: ApiFieldDefinitions): Future[(ApiFieldDefinitions, IsInsert)] = {
import JsonFormatters.ApiFieldDefinitionsJF
save(definitions, selectorFor(definitions))
with ApplicationLogger {

override lazy val collection: MongoCollection[ApiFieldDefinitions] =
CollectionFactory
.collection(mongo.database, collectionName, domainFormat)
.withCodecRegistry(
fromRegistries(
fromCodecs(
Codecs.playFormatCodec(domainFormat),
Codecs.playFormatCodec(JsonFormatters.ApiContextJF),
Codecs.playFormatCodec(JsonFormatters.ApiVersionJF),
Codecs.playFormatCodec(JsonFormatters.ApiFieldDefinitionsJF),
Codecs.playFormatCodec(JsonFormatters.ValidationJF)
),
MongoClient.DEFAULT_CODEC_REGISTRY
)
)

def save(definitions: ApiFieldDefinitions): Future[(ApiFieldDefinitions, IsInsert)] = {
val query = and(equal("apiContext", Codecs.toBson(definitions.apiContext.value)),
equal("apiVersion", Codecs.toBson(definitions.apiVersion.value)))

collection.find(query).headOption flatMap {
case Some(_: ApiFieldDefinitions) =>
for {
updatedDefinitions <- collection.replaceOne(
filter = query,
replacement = definitions
).toFuture().map(_ => definitions)
} yield (updatedDefinitions, false)

case None =>
for {
newDefinitions <- collection.insertOne(definitions).toFuture().map(_ => definitions)
} yield (newDefinitions, true)
}
}

override def fetch(apiContext: ApiContext, apiVersion: ApiVersion): Future[Option[ApiFieldDefinitions]] = {
getOne(selectorFor(apiContext, apiVersion))
collection.find(Filters.and(equal("apiContext", Codecs.toBson(apiContext.value)),
equal("apiVersion", Codecs.toBson(apiVersion.value)))).headOption()
}

override def fetchAll(): Future[List[ApiFieldDefinitions]] = {
getMany(Json.obj())
collection.find().toFuture().map(_.toList)
}

override def delete(apiContext: ApiContext, apiVersion: ApiVersion): Future[Boolean] = {
deleteOne(selectorFor(apiContext, apiVersion))
}

private def selectorFor(apiContext: ApiContext, apiVersion: ApiVersion): JsObject = {
selector(apiContext, apiVersion)
}

private def selectorFor(fd: ApiFieldDefinitions): JsObject = {
selector(fd.apiContext, fd.apiVersion)
}

private def selector(apiContext: ApiContext, apiVersion: ApiVersion): JsObject = {
Json.obj(
"apiContext" -> apiContext.value,
"apiVersion" -> apiVersion.value
)
collection.deleteOne(Filters.and(equal("apiContext", Codecs.toBson(apiContext.value)),
equal("apiVersion", Codecs.toBson(apiVersion.value))))
.toFuture()
.map(_.getDeletedCount > 0)
}
}

This file was deleted.

32 changes: 0 additions & 32 deletions app/uk/gov/hmrc/apisubscriptionfields/repository/MongoDb.scala

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1a59345

Please sign in to comment.