Skip to content

Commit

Permalink
Merge pull request #235 from hmrc/ITSASU-1373
Browse files Browse the repository at this point in the history
ITSASU-1373 - Remove simple-reactivemongo dependency
  • Loading branch information
aug24 authored Jul 22, 2022
2 parents 0899eef + d206813 commit 75fd9f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 70 deletions.
46 changes: 15 additions & 31 deletions app/repositories/LockoutMongoRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@

package repositories

import com.mongodb.client.model.{IndexModel, IndexOptions}
import com.mongodb.client.model.IndexOptions
import config.AppConfig
import models.lockout.CheckLockout
import models.matching.LockoutResponse
import org.bson.Document
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.model.IndexModel
import org.mongodb.scala.result.InsertOneResult
import org.mongodb.scala.{Observable, SingleObservable}
import play.api.libs.json.{Format, JsObject, JsValue, Json}
import reactivemongo.bson.BSONDocument
import repositories.LockoutMongoRepository.toIndexModel
import uk.gov.hmrc.mongo.MongoComponent
import uk.gov.hmrc.mongo.play.json.PlayMongoRepository

import java.time._
import java.util.concurrent.TimeUnit
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
import scala.language.implicitConversions

@Singleton
class LockoutMongoRepositoryConfig @Inject()(
val appConfig: AppConfig) {
class LockoutMongoRepositoryConfig @Inject()(val appConfig: AppConfig) {

def mongoComponent: MongoComponent = MongoComponent(appConfig.mongoUri)

import repositories.LockoutMongoRepository.lockIndex

def indexes: Seq[IndexModel] = Seq(toIndexModel(lockIndex))
def indexes: Seq[IndexModel] = Seq(lockIndex)

}

@Singleton
Expand All @@ -52,7 +52,8 @@ class LockoutMongoRepository @Inject()(val config: LockoutMongoRepositoryConfig)
collectionName = "selfEmploymentsData",
mongoComponent = config.mongoComponent,
domainFormat = implicitly[Format[JsObject]],
indexes = config.indexes
indexes = config.indexes,
replaceIndexes = true
) {

def insert(document: JsObject): Future[InsertOneResult] = collection.insertOne(document).toFuture
Expand Down Expand Up @@ -100,15 +101,13 @@ class LockoutMongoRepository @Inject()(val config: LockoutMongoRepositoryConfig)

object LockoutMongoRepository {

val lockIndex: Index = Index(
Seq((CheckLockout.expiry, IndexType.ascending)),
name = Some("lockExpires"),
unique = false,
// background = false,
dropDups = false,
sparse = false,
version = None,
options = BSONDocument("expireAfterSeconds" -> 0)
val lockIndex: IndexModel = IndexModel(
Json.obj(CheckLockout.expiry -> IndexType.ascending),
new IndexOptions()
.name("lockExpires")
.unique(false)
.sparse(false)
.expireAfter(0, TimeUnit.SECONDS)
)

object IndexType {
Expand All @@ -117,15 +116,6 @@ object LockoutMongoRepository {
def descending: Int = -1
}

case class Index(
key: Seq[(String, Json.JsValueWrapper)],
name: Option[String],
unique: Boolean,
dropDups: Boolean,
sparse: Boolean,
version: Option[Any],
options: BSONDocument)

implicit def asOption(o: JsObject): Option[JsValue] = o.result.toOption.flatMap(Option(_))

implicit def toBson(doc: JsObject): Bson = Document.parse(doc.toString())
Expand All @@ -134,11 +124,5 @@ object LockoutMongoRepository {

implicit def toFuture[T](observable: Observable[T]): Future[Seq[T]] = observable.toFuture()

implicit def toIndexModel(index: Index): IndexModel = new IndexModel(
Json.obj(index.key: _*),
new IndexOptions()
.name(index.name.get)
.unique(index.unique)
.sparse(index.sparse))

}
50 changes: 15 additions & 35 deletions app/repositories/ThrottlingRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import org.mongodb.scala.model.IndexModel
import org.mongodb.scala.result.InsertOneResult
import org.mongodb.scala.{Observable, SingleObservable}
import play.api.libs.json.{Format, JsObject, JsValue, Json}
import reactivemongo.bson.BSONDocument
import reactivemongo.play.json.ImplicitBSONHandlers._
import repositories.ThrottlingRepository._
import uk.gov.hmrc.mongo.MongoComponent
import uk.gov.hmrc.mongo.play.json.PlayMongoRepository
Expand All @@ -50,9 +48,11 @@ class ThrottlingRepositoryConfig @Inject()(val appConfig: AppConfig) {

def mongoComponent: MongoComponent = MongoComponent(appConfig.mongoUri)

def indexes: Seq[IndexModel] =
Seq(ttlIndex(ttlLengthSeconds)) ++
Seq(idTimecodeIndex).map(toIndexModel)
def indexes: Seq[IndexModel] = Seq(
ttlIndex(ttlLengthSeconds),
idTimecodeIndex
)

}

@Singleton
Expand Down Expand Up @@ -129,16 +129,6 @@ class ThrottlingRepository @Inject()(config: ThrottlingRepositoryConfig, instant

object ThrottlingRepository {

case class Index(
key: Seq[(String, Json.JsValueWrapper)],
name: Option[String],
unique: Boolean,
dropDups: Boolean,
sparse: Boolean,
version: Option[Any],
options: BSONDocument
)

object IndexType {
def ascending: Int = 1

Expand All @@ -153,32 +143,22 @@ object ThrottlingRepository {

implicit def toFuture[T](observable: Observable[T]): Future[Seq[T]] = observable.toFuture()

implicit def toIndexModel(index: Index): IndexModel = new IndexModel(
Json.obj(index.key: _*),
new IndexOptions()
.name(index.name.get)
.unique(index.unique)
.sparse(index.sparse))

val throttleIdKey = "throttleId"
val timecodeKey = "timecode"
val countKey = "count"
val lastUpdatedTimestampKey = "lastUpdatedTimestamp"


val idTimecodeIndex: Index =
Index(
key = Seq(
throttleIdKey -> IndexType.ascending,
timecodeKey -> IndexType.ascending
),
name = Some("idTimecodeIndex"),
unique = true,
dropDups = false,
sparse = true,
version = None,
options = BSONDocument()
)
val idTimecodeIndex: IndexModel = IndexModel(
Json.obj(
throttleIdKey -> IndexType.ascending,
timecodeKey -> IndexType.ascending
),
new IndexOptions()
.name("idTimecodeIndex")
.unique(true)
.sparse(true)
)

def ttlIndex(ttlLengthSeconds: Long): IndexModel = new IndexModel(
Json.obj(lastUpdatedTimestampKey -> IndexType.ascending),
Expand Down
1 change: 0 additions & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ play.application.loader = "uk.gov.hmrc.play.bootstrap.ApplicationLoader"
play.http.requestHandler = "uk.gov.hmrc.play.bootstrap.http.RequestHandler"

# Define any modules used here
play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoHmrcModule"
play.modules.enabled += "uk.gov.hmrc.play.bootstrap.HttpClientModule"
play.modules.enabled += "uk.gov.hmrc.play.bootstrap.AuthModule"

Expand Down
3 changes: 0 additions & 3 deletions project/AppDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ object AppDependencies {

private val scalaJVersion = "2.4.2"

private val reactiveMongoVersion = "8.0.0-play-28"

private val hmrcMongoVersion = "0.64.0"

private val wiremockVersion = "2.32.0"
Expand All @@ -43,7 +41,6 @@ object AppDependencies {
ws,
"uk.gov.hmrc" %% "bootstrap-backend-play-28" % bootstrapBackendVersion,
"uk.gov.hmrc" %% "domain" % domainVersion,
"uk.gov.hmrc" %% "simple-reactivemongo" % reactiveMongoVersion,
"uk.gov.hmrc.mongo" %% "hmrc-mongo-play-28" % hmrcMongoVersion
)

Expand Down

0 comments on commit 75fd9f6

Please sign in to comment.