-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Leigh Anderson
committed
Sep 21, 2016
1 parent
0ce0e91
commit af3592a
Showing
14 changed files
with
430 additions
and
45 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
app/uk/gov/hmrc/agentclientrelationships/binders/PathBinders.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
15 changes: 0 additions & 15 deletions
15
app/uk/gov/hmrc/agentclientrelationships/controllers/MicroserviceHelloWorld.scala
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
app/uk/gov/hmrc/agentclientrelationships/controllers/Relationships.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/uk/gov/hmrc/agentclientrelationships/model/Relationship.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
65 changes: 65 additions & 0 deletions
65
app/uk/gov/hmrc/agentclientrelationships/repositories/RelationshipRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
test/uk/gov/hmrc/agentclientrelationships/MicroserviceHelloWorldControllerSpec.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.