Skip to content

Commit

Permalink
[LA] Basic CRUD functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Leigh Anderson committed Sep 21, 2016
1 parent 0ce0e91 commit af3592a
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 45 deletions.
25 changes: 25 additions & 0 deletions app/uk/gov/hmrc/agentclientrelationships/binders/PathBinders.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2016 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.agentclientrelationships.binders

import uk.gov.hmrc.domain.{AgentCode, SaUtr}
import uk.gov.hmrc.play.binders.SimpleObjectBinder

object PathBinders {
implicit object AgentCodeBinder extends SimpleObjectBinder[AgentCode](AgentCode.apply, _.value)
implicit object SaUtrBinder extends SimpleObjectBinder[SaUtr](SaUtr.apply, _.value)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2016 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.agentclientrelationships.controllers

import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.Json
import play.api.mvc.Action
import uk.gov.hmrc.agentclientrelationships.model.Relationship
import uk.gov.hmrc.agentclientrelationships.repositories.RelationshipRepository
import uk.gov.hmrc.domain.{AgentCode, SaUtr}
import uk.gov.hmrc.play.microservice.controller.BaseController

import scala.concurrent.Future

class Relationships(relationshipRepository: RelationshipRepository) extends BaseController {

def create(saUtr: SaUtr, agentCode: AgentCode) = Action.async {
findNonRemoved(saUtr, agentCode).flatMap {
case Nil => relationshipRepository.create(saUtr.toString(), "sa", agentCode).map(Json.toJson(_)).map(Ok(_))
case r :: _ => Future successful Ok(Json.toJson(r))
}
}

def getRelationship(saUtr: SaUtr, agentCode: AgentCode) = Action.async {
findNonRemoved(saUtr, agentCode).map {
case Nil => NotFound
case r :: _ => Ok(Json.toJson(r))
}
}

def removeRelationship(saUtr: SaUtr, agentCode: AgentCode) = Action.async {
findNonRemoved(saUtr, agentCode).flatMap {
case Nil => Future successful NoContent
case r :: _ => relationshipRepository.removeRelationship(saUtr.toString(), "sa", agentCode).map(_ => NoContent)
}
}

def findNonRemoved(saUtr: SaUtr, agentCode: AgentCode): Future[List[Relationship]] = {
relationshipRepository.list(saUtr.toString(), "sa", agentCode).map(
_.filter(r => !r.isRemoved))
}

def getAgentRelationships(agentCode: AgentCode) = Action.async {
relationshipRepository.list(agentCode).map(Json.toJson(_)).map(Ok(_))
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2016 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.agentclientrelationships

import com.typesafe.config.Config
Expand Down Expand Up @@ -34,7 +50,7 @@ object MicroserviceAuthFilter extends AuthorisationFilter {
override def controllerNeedsAuth(controllerName: String): Boolean = ControllerConfiguration.paramsForController(controllerName).needsAuth
}

object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode {
object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode with ControllerRegistry with ServiceRegistry {
override val auditConnector = MicroserviceAuditConnector

override def microserviceMetricsConfig(implicit app: Application): Option[Configuration] = app.configuration.getConfig(s"microservice.metrics")
Expand All @@ -44,4 +60,8 @@ object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode {
override val microserviceAuditFilter = MicroserviceAuditFilter

override val authFilter = Some(MicroserviceAuthFilter)

override def getControllerInstance[A](controllerClass: Class[A]): A = {
getController(controllerClass)
}
}
43 changes: 43 additions & 0 deletions app/uk/gov/hmrc/agentclientrelationships/microserviceWiring.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/*
* Copyright 2016 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.agentclientrelationships

import play.api.mvc.Controller
import play.modules.reactivemongo.ReactiveMongoPlugin
import uk.gov.hmrc.agentclientrelationships.controllers.Relationships
import uk.gov.hmrc.agentclientrelationships.repositories.{RelationshipMongoRepository, RelationshipRepository}
import uk.gov.hmrc.mongo.MongoConnector
import uk.gov.hmrc.play.audit.http.config.LoadAuditingConfig
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.play.auth.microservice.connectors.AuthConnector
Expand All @@ -18,3 +39,25 @@ object MicroserviceAuditConnector extends AuditConnector with RunMode {
object MicroserviceAuthConnector extends AuthConnector with ServicesConfig {
override val authBaseUrl = baseUrl("auth")
}

trait LazyMongoDbConnection {
import play.api.Play.current

lazy val mongoConnector: MongoConnector = ReactiveMongoPlugin.mongoConnector

implicit lazy val db = mongoConnector.db
}

trait ServiceRegistry extends LazyMongoDbConnection {
lazy val relationshipRepository: RelationshipRepository = new RelationshipMongoRepository()
}

trait ControllerRegistry {
registry: ServiceRegistry =>

private lazy val controllers = Map[Class[_], Controller](
classOf[Relationships] -> new Relationships(relationshipRepository)
)

def getController[A](controllerClass: Class[A]) : A = controllers(controllerClass).asInstanceOf[A]
}
38 changes: 38 additions & 0 deletions app/uk/gov/hmrc/agentclientrelationships/model/Relationship.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.agentclientrelationships.model

import org.joda.time.DateTime
import play.api.libs.json.Json
import reactivemongo.bson.BSONObjectID
import uk.gov.hmrc.domain.AgentCode
import uk.gov.hmrc.mongo.json.ReactiveMongoFormats

object Relationship {
implicit val oidFormats = ReactiveMongoFormats.objectIdFormats
implicit val jsonFormats = Json.format[Relationship]
val mongoFormats = ReactiveMongoFormats.mongoEntity(jsonFormats)
}

case class Relationship (id: BSONObjectID,
agentCode: AgentCode,
regime: String,
clientRegimeId: String,
created: DateTime,
removed: Option[DateTime] = None) {
val isRemoved = removed.isDefined
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2016 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.agentclientrelationships.repositories

import org.joda.time.DateTime
import reactivemongo.api.DB
import reactivemongo.bson.BSONObjectID
import uk.gov.hmrc.agentclientrelationships.model.Relationship
import uk.gov.hmrc.domain.AgentCode
import uk.gov.hmrc.mongo.json.ReactiveMongoFormats
import uk.gov.hmrc.mongo.{AtomicUpdate, ReactiveRepository, Repository}
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import scala.concurrent.Future

trait RelationshipRepository extends Repository[Relationship, BSONObjectID] {

def create(clientRegimeId: String, regime: String, agentCode: AgentCode): Future[Relationship]
def removeRelationship(clientRegimeId: String, regime: String, agentCode: AgentCode): Future[_]
def list(clientRegimeId: String, regime: String, agentCode: AgentCode): Future[List[Relationship]]
def list(agentCode: AgentCode): Future[Seq[Relationship]]
}

class RelationshipMongoRepository(implicit mongo: () => DB) extends ReactiveRepository[Relationship, BSONObjectID]("agentClientRelationships", mongo, Relationship.mongoFormats, ReactiveMongoFormats.objectIdFormats)
with RelationshipRepository with AtomicUpdate[Relationship] {
override def create(clientRegimeId: String, regime: String, agentCode: AgentCode): Future[Relationship] = {
val request = Relationship(
id = BSONObjectID.generate,
agentCode = agentCode,
regime = regime,
clientRegimeId = clientRegimeId,
created = new DateTime()
)

insert(request).map(_ => request)
}

override def list(clientRegimeId: String, regime: String, agentCode: AgentCode): Future[List[Relationship]] =
find("clientRegimeId" -> clientRegimeId, "regime" -> regime, "agentCode" -> agentCode)

override def removeRelationship(clientRegimeId: String, regime: String, agentCode: AgentCode) = {
remove(query = "clientRegimeId" -> clientRegimeId, "regime" -> regime, "agentCode" -> agentCode)
}


override def list(agentCode: AgentCode): Future[Seq[Relationship]] =
find("agentCode" -> agentCode)

override def isInsertion(newRecordId: BSONObjectID, oldRecord: Relationship): Boolean =
newRecordId != oldRecord.id
}
5 changes: 4 additions & 1 deletion conf/app.routes
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# microservice specific routes

GET /hello-world uk.gov.hmrc.agentclientrelationships.controllers.MicroserviceHelloWorld.hello
PUT /relationships/sa/:saUtr/:agentCode @uk.gov.hmrc.agentclientrelationships.controllers.Relationships.create(saUtr: uk.gov.hmrc.domain.SaUtr, agentCode: uk.gov.hmrc.domain.AgentCode)
GET /relationships/sa/:saUtr/:agentCode @uk.gov.hmrc.agentclientrelationships.controllers.Relationships.getRelationship(saUtr: uk.gov.hmrc.domain.SaUtr, agentCode: uk.gov.hmrc.domain.AgentCode)
DELETE /relationships/sa/:saUtr/:agentCode @uk.gov.hmrc.agentclientrelationships.controllers.Relationships.removeRelationship(saUtr: uk.gov.hmrc.domain.SaUtr, agentCode: uk.gov.hmrc.domain.AgentCode)
GET /relationships/agent/:agentCode @uk.gov.hmrc.agentclientrelationships.controllers.Relationships.getAgentRelationships(agentCode: uk.gov.hmrc.domain.AgentCode)
17 changes: 14 additions & 3 deletions conf/application.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Copyright 2016 HM Revenue & Customs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is the main configuration file for the application.
# ~~~~~
Expand Down Expand Up @@ -62,7 +75,7 @@ controllers {
needsAuditing = false
}

uk.gov.hmrc.agentclientrelationships.controllers.MicroserviceHelloWorld = {
uk.gov.hmrc.agentclientrelationships.controllers.Relationships = {
needsAuth = false
needsLogging = false
needsAuditing = false
Expand Down Expand Up @@ -138,5 +151,3 @@ microservice {

}
}


2 changes: 2 additions & 0 deletions project/MicroService.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sbt.Keys._
import play.PlayImport.PlayKeys._
import sbt.Tests.{SubProcess, Group}
import sbt._
import uk.gov.hmrc.sbtdistributables.SbtDistributablesPlugin._
Expand Down Expand Up @@ -28,6 +29,7 @@ trait MicroService {
.settings(scalaSettings: _*)
.settings(publishingSettings: _*)
.settings(defaultSettings(): _*)
.settings(routesImport ++= Seq("uk.gov.hmrc.agentclientrelationships.binders.PathBinders._"))
.settings(
libraryDependencies ++= appDependencies,
retrieveManaged := true,
Expand Down
2 changes: 2 additions & 0 deletions project/MicroServiceBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ private object AppDependencies {
def apply() = new TestDependencies {
override lazy val test = Seq(
"uk.gov.hmrc" %% "hmrctest" % hmrcTestVersion % scope,
"uk.gov.hmrc" %% "reactivemongo-test" % "1.6.0" % scope,
"org.scalatest" %% "scalatest" % scalaTestVersion % scope,
"org.scalatestplus" %% "play" % "1.1.0" % scope,
"org.pegdown" % "pegdown" % pegdownVersion % scope,
"com.typesafe.play" %% "play-test" % PlayVersion.current % scope
)
Expand Down

This file was deleted.

Loading

0 comments on commit af3592a

Please sign in to comment.