|
16 | 16 |
|
17 | 17 | package uk.gov.hmrc.apiscope.repository |
18 | 18 |
|
19 | | -import javax.inject.{Inject, Singleton} |
| 19 | +import org.bson.codecs.configuration.CodecRegistries.{fromCodecs, fromRegistries} |
| 20 | +import org.mongodb.scala.model.Filters.equal |
| 21 | +import org.mongodb.scala.model.Indexes.ascending |
| 22 | +import org.mongodb.scala.model.Updates.{combine, set} |
| 23 | +import org.mongodb.scala.model.{FindOneAndUpdateOptions, IndexModel, IndexOptions, ReturnDocument} |
| 24 | +import org.mongodb.scala.{MongoClient, MongoCollection} |
| 25 | +import play.api.Logger |
| 26 | +import play.api.libs.json.{Json, OFormat} |
| 27 | +import uk.gov.hmrc.apiscope.models.Scope |
| 28 | +import uk.gov.hmrc.mongo.MongoComponent |
| 29 | +import uk.gov.hmrc.mongo.play.json.{Codecs, CollectionFactory, PlayMongoRepository} |
20 | 30 |
|
| 31 | +import javax.inject.{Inject, Singleton} |
21 | 32 | import scala.collection.Seq |
22 | 33 | import scala.concurrent.{ExecutionContext, Future} |
23 | | -import reactivemongo.api.Cursor |
24 | | -import reactivemongo.api.indexes.{Index, IndexType} |
25 | | -import reactivemongo.bson.BSONObjectID |
26 | | -import reactivemongo.play.json.ImplicitBSONHandlers._ |
27 | | -import play.api.libs.json.{JsObject, Json} |
28 | | -import play.api.libs.json.Json._ |
29 | | -import play.modules.reactivemongo.ReactiveMongoComponent |
30 | | -import uk.gov.hmrc.mongo.ReactiveRepository |
31 | | -import uk.gov.hmrc.mongo.json.ReactiveMongoFormats |
32 | | -import uk.gov.hmrc.apiscope.models.Scope |
| 34 | + |
33 | 35 |
|
34 | 36 | private object ScopeFormats { |
35 | | - implicit val objectIdFormats = ReactiveMongoFormats.objectIdFormats |
36 | | - implicit val scopeFormat = Json.format[Scope] |
| 37 | + implicit val scopeFormat:OFormat[Scope] = Json.format[Scope] |
37 | 38 | } |
38 | 39 |
|
39 | 40 | @Singleton |
40 | | - class ScopeRepository @Inject()(mongo: ReactiveMongoComponent)(implicit val ec: ExecutionContext) |
41 | | - extends ReactiveRepository[Scope, BSONObjectID]("scope", mongo.mongoConnector.db, |
42 | | - ScopeFormats.scopeFormat, ReactiveMongoFormats.objectIdFormats) { |
43 | | - |
| 41 | + class ScopeRepository @Inject()(mongoComponent: MongoComponent)(implicit val ec: ExecutionContext) |
| 42 | + extends PlayMongoRepository[Scope]( |
| 43 | + mongoComponent = mongoComponent, |
| 44 | + collectionName = "scope", |
| 45 | + domainFormat = ScopeFormats.scopeFormat, |
| 46 | + indexes = Seq(IndexModel(ascending("key"), |
| 47 | + IndexOptions() |
| 48 | + .name("keyIndex") |
| 49 | + .background(true) |
| 50 | + .unique(true))), |
| 51 | + replaceIndexes = true |
| 52 | + ) { |
| 53 | + private val logger = Logger(this.getClass) |
44 | 54 |
|
45 | | - ensureIndex("key", "keyIndex") |
| 55 | + override lazy val collection: MongoCollection[Scope] = |
| 56 | + CollectionFactory |
| 57 | + .collection(mongoComponent.database, collectionName, domainFormat) |
| 58 | + .withCodecRegistry( |
| 59 | + fromRegistries( |
| 60 | + fromCodecs( |
| 61 | + Codecs.playFormatCodec(domainFormat) |
| 62 | + ), |
| 63 | + MongoClient.DEFAULT_CODEC_REGISTRY |
| 64 | + ) |
| 65 | + ) |
46 | 66 |
|
47 | 67 | def save(scope: Scope) : Future[Scope] = { |
48 | | - collection |
49 | | - .update(false).one(Json.obj("key" -> scope.key), scope, upsert = true) |
50 | | - .map(_ => scope) |
51 | | - } |
52 | | - |
53 | | - private def ensureIndex(field: String, indexName: String, isUnique: Boolean = true, isBackground: Boolean = true): Future[Boolean] = { |
54 | | - def createIndex = Index( |
55 | | - key = Seq(field -> IndexType.Ascending), |
56 | | - name = Some(indexName), |
57 | | - unique = isUnique, |
58 | | - background = isBackground |
59 | | - ) |
60 | | - |
61 | | - collection.indexesManager.ensure(createIndex) |
| 68 | + var updateSeq = Seq( |
| 69 | + set("key", Codecs.toBson(scope.key)), |
| 70 | + set("name", Codecs.toBson(scope.name)), |
| 71 | + set("description", Codecs.toBson(scope.description))) |
| 72 | + scope.confidenceLevel match { |
| 73 | + case Some(value) => |
| 74 | + logger.info(s"confidenceLevel value id ${value} and value enumeration ${value.value}") |
| 75 | + updateSeq = updateSeq :+ set("confidenceLevel", Codecs.toBson(value)) |
| 76 | + case None => Future.successful(None) |
| 77 | + } |
| 78 | + logger.info(s"updateSeq: $updateSeq") |
| 79 | + collection.findOneAndUpdate(equal("key", Codecs.toBson(scope.key)), |
| 80 | + update = combine(updateSeq: _*), |
| 81 | + options = FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER) |
| 82 | + ).map(_.asInstanceOf[Scope]).head() |
62 | 83 | } |
63 | 84 |
|
64 | 85 | def fetch(key: String): Future[Option[Scope]] = { |
65 | | - collection.find[JsObject, Scope](Json.obj("key" -> key), None).one[Scope]. map { |
66 | | - case Some(s) => Some(s) |
67 | | - case None => |
68 | | - logger.info(s"The scope $key doesn't exist") |
69 | | - None |
70 | | - } |
| 86 | + collection.find(equal("key", key)).headOption() |
| 87 | + .flatMap { |
| 88 | + case Some(scope) => Future.successful(Some(scope)) |
| 89 | + case None => |
| 90 | + logger.info(s"The scope $key doesn't exist") |
| 91 | + Future.successful(None) |
| 92 | + } |
71 | 93 | } |
72 | 94 |
|
73 | 95 | def fetchAll(): Future[Seq[Scope]] = { |
74 | | - collection.find[JsObject, Scope](Json.obj(), None).cursor[Scope]().collect[Seq](Int.MaxValue,Cursor.FailOnError[Seq[Scope]]()) |
| 96 | + collection.find().toFuture() |
75 | 97 | } |
76 | 98 |
|
77 | 99 | } |
0 commit comments