@@ -18,55 +18,59 @@ package uk.gov.hmrc.apiscope.controllers
1818
1919import javax .inject .{Inject , Singleton }
2020import scala .concurrent .{ExecutionContext , Future }
21+
2122import util .ApplicationLogger
23+
2224import play .api .libs .json ._
2325import play .api .mvc ._
26+ import uk .gov .hmrc .play .bootstrap .backend .controller .BackendController
27+
2428import uk .gov .hmrc .apiscope .models .ErrorCode ._
29+ import uk .gov .hmrc .apiscope .models .ResponseFormatters ._
2530import uk .gov .hmrc .apiscope .models .{Scope , ScopeData }
2631import uk .gov .hmrc .apiscope .services .ScopeService
27- import uk .gov .hmrc .play .bootstrap .backend .controller .BackendController
28- import uk .gov .hmrc .apiscope .models .ResponseFormatters ._
2932
3033@ Singleton
31- class ScopeController @ Inject ()(scopeService : ScopeService , cc : ControllerComponents , playBodyParsers : PlayBodyParsers )(implicit val ec : ExecutionContext )
32- extends BackendController (cc) with ApplicationLogger {
34+ class ScopeController @ Inject () (scopeService : ScopeService , cc : ControllerComponents , playBodyParsers : PlayBodyParsers )(implicit val ec : ExecutionContext )
35+ extends BackendController (cc) with ApplicationLogger {
3336
3437 def createOrUpdateScope (): Action [JsValue ] = Action .async(playBodyParsers.json) { implicit request =>
3538 handleRequest[Seq [ScopeData ]](request) {
36- scopeRequest => {
37- logger.info(s " Creating api-scope from request: $request" )
38- val scopes = scopeRequest.map {
39- scopeData => Scope (scopeData.key, scopeData.name, scopeData.description, scopeData.confidenceLevel)
39+ scopeRequest =>
40+ {
41+ logger.info(s " Creating api-scope from request: $request" )
42+ val scopes = scopeRequest.map {
43+ scopeData => Scope (scopeData.key, scopeData.name, scopeData.description, scopeData.confidenceLevel)
44+ }
45+ scopeService.saveScopes(scopes).map {
46+ scopes =>
47+ logger.info(s " api-scope successfully created: $scopes" )
48+ Ok (Json .toJson(scopes))
49+ } recover recovery
4050 }
41- scopeService.saveScopes(scopes).map {
42- scopes =>
43- logger.info(s " api-scope successfully created: $scopes" )
44- Ok (Json .toJson(scopes))
45- } recover recovery
46- }
4751 }
4852 }
4953
5054 def fetchScope (key : String ): Action [AnyContent ] = Action .async {
5155 scopeService.fetchScope(key).map {
5256 case Some (scope) => Ok (Json .toJson(scope))
53- case None => NotFound (error(SCOPE_NOT_FOUND , s " Scope not found with key: $key" ))
57+ case None => NotFound (error(SCOPE_NOT_FOUND , s " Scope not found with key: $key" ))
5458 } recover recovery
5559 }
5660
5761 def fetchScopes (scopes : String ): Action [AnyContent ] = Action .async {
5862 logger.info(s " Fetching scopes: $scopes" )
5963 val future : Future [Seq [Scope ]] = scopes match {
60- case " *" => scopeService.fetchAll
64+ case " *" => scopeService.fetchAll
6165 case spaceSeparatedScopes =>
6266 scopeService.fetchScopes(spaceSeparatedScopes.split(" \\ s+" ).toSet)
6367 }
6468
65- future map(scopes => Ok (Json .toJson(scopes))) recover recovery
69+ future map (scopes => Ok (Json .toJson(scopes))) recover recovery
6670 }
6771
6872 def validate : Action [JsValue ] = Action .async(playBodyParsers.json) { implicit request =>
69- handleRequest[Seq [ScopeData ]](request) (_ => Future .successful(NoContent ))
73+ handleRequest[Seq [ScopeData ]](request)(_ => Future .successful(NoContent ))
7074 }
7175
7276 private def recovery : PartialFunction [Throwable , Result ] = {
@@ -75,4 +79,4 @@ class ScopeController @Inject()(scopeService: ScopeService, cc: ControllerCompon
7579 InternalServerError (error(UNKNOWN_ERROR , " An unexpected error occurred" ))
7680 }
7781
78- }
82+ }
0 commit comments