<commit_before>package com.blinkbox.books.reading.admin

import akka.actor.ActorRefFactory
import com.blinkbox.books.auth.Constraints.hasAnyRole
import com.blinkbox.books.auth.Elevation
import com.blinkbox.books.auth.UserRole._
import com.blinkbox.books.clients.catalogue.CatalogueInfoMissingException
import com.blinkbox.books.config.ApiConfig
import com.blinkbox.books.spray.AuthDirectives.authenticateAndAuthorize
import com.blinkbox.books.spray.Directives.rootPath
import com.blinkbox.books.spray.MonitoringDirectives.monitor
import com.blinkbox.books.spray.v2.Implicits.throwableMarshaller
import com.blinkbox.books.spray.{JsonFormats, BearerTokenAuthenticator, url2uri, v2}
import com.typesafe.scalalogging.StrictLogging
import org.json4s._
import org.json4s.JsonAST.JString
import spray.http.{IllegalRequestException, HttpRequest, StatusCodes}
import spray.routing.{ValidationRejection, ExceptionHandler, HttpService}

import scala.concurrent.ExecutionContext.Implicits.global

case class LibraryItemReq(isbn: String, ownership: Ownership)

class ReadingAdminApi(apiConfig: ApiConfig,
  authenticator: BearerTokenAuthenticator,
  libraryAdminService: LibraryAdminService)(implicit val actorRefFactory: ActorRefFactory) extends HttpService with v2.JsonSupport with StrictLogging {

  import ReadingAdminApi._

  implicit override val jsonFormats = JsonFormats.blinkboxFormat() + OwnershipSerializer

  val addFullBook = post {
    path("admin" / "users" / IntNumber / "library") { userId =>
      authenticateAndAuthorize(authenticator.withElevation(Elevation.Critical), hasAnyRole(CustomerServicesRep, CustomerServicesManager)) { repUser =>
        entity(as[LibraryItemReq]) { item =>
          onSuccess(libraryAdminService.addBook(item.isbn, userId, item.ownership)) { res =>
            complete(StatusCodes.NoContent)
          }
        }
      }
    }
  }

  val exceptionHandler = ExceptionHandler {
    case e: CatalogueInfoMissingException => reject(ValidationRejection(e.getMessage))
    case e: LibraryItemConflict => failWith(new IllegalRequestException(StatusCodes.Conflict, e.getMessage))<commit_msg>Organised imports<commit_after>package com.blinkbox.books.reading.admin

import akka.actor.ActorRefFactory
import com.blinkbox.books.auth.Constraints.hasAnyRole
import com.blinkbox.books.auth.Elevation
import com.blinkbox.books.auth.UserRole._
import com.blinkbox.books.clients.catalogue.CatalogueInfoMissingException
import com.blinkbox.books.config.ApiConfig
import com.blinkbox.books.spray.AuthDirectives.authenticateAndAuthorize
import com.blinkbox.books.spray.Directives.rootPath
import com.blinkbox.books.spray.MonitoringDirectives.monitor
import com.blinkbox.books.spray.v2.Implicits.throwableMarshaller
import com.blinkbox.books.spray.{BearerTokenAuthenticator, JsonFormats, url2uri, v2}
import com.typesafe.scalalogging.StrictLogging
import org.json4s.JsonAST.JString
import org.json4s._
import spray.http.{IllegalRequestException, StatusCodes}
import spray.routing.{ExceptionHandler, HttpService, ValidationRejection}

import scala.concurrent.ExecutionContext.Implicits.global

case class LibraryItemReq(isbn: String, ownership: Ownership)

class ReadingAdminApi(apiConfig: ApiConfig,
  authenticator: BearerTokenAuthenticator,
  libraryAdminService: LibraryAdminService)(implicit val actorRefFactory: ActorRefFactory) extends HttpService with v2.JsonSupport with StrictLogging {

  import ReadingAdminApi._

  implicit override val jsonFormats = JsonFormats.blinkboxFormat() + OwnershipSerializer

  val addFullBook = post {
    path("admin" / "users" / IntNumber / "library") { userId =>
      authenticateAndAuthorize(authenticator.withElevation(Elevation.Critical), hasAnyRole(CustomerServicesRep, CustomerServicesManager)) { repUser =>
        entity(as[LibraryItemReq]) { item =>
          onSuccess(libraryAdminService.addBook(item.isbn, userId, item.ownership)) { res =>
            complete(StatusCodes.NoContent)
          }
        }
      }
    }
  }

  val exceptionHandler = ExceptionHandler {
    case e: CatalogueInfoMissingException => reject(ValidationRejection(e.getMessage))
    case e: LibraryItemConflict => failWith(new IllegalRequestException(StatusCodes.Conflict, e.getMessage))
{'fraction_non_alphanumeric': 0.0871310507674144, 'fraction_numerical': 0.002833530106257379, 'mean_word_length': 5.7776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5538262', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1229, 'n_words': 209}