Skip to content

Instantly share code, notes, and snippets.

@agnaldo4j
Created January 16, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agnaldo4j/b084b573b1ddeda536ce to your computer and use it in GitHub Desktop.
Save agnaldo4j/b084b573b1ddeda536ce to your computer and use it in GitHub Desktop.
Exemplo de endpoint acessando usecases
package com.opportunity.simple.restapi
import javax.persistence.EntityManagerFactory
import com.opportunity.objectives.model.User
import com.opportunity.objectives.usecases.user.{ListUsersUsecase, LoginByFacebookUserUsecase, SaveNewUserUsecase}
import com.opportunity.simple.restapi.response.ProtocolResponse
import com.typesafe.config.Config
class UserAPI(val apiConfig: Config, val entityManagerFactory: EntityManagerFactory) extends Respondable with Authenticable with PersistibleUser {
post("/list") {
contentType = "application/json"
authenticated { jsonObject =>
withPersistenceAdapter { userPersistenceAdapter =>
writeOkResponse(
ProtocolResponse.prepare(
ListUsersUsecase.newWith( userPersistenceAdapter ).execute().map { user => User.toMap(user) }
)
)
}
}
}
post("/save") {
contentType = "application/json"
authenticated { jsonObject =>
withPersistenceAdapter { userPersistenceAdapter =>
writeOkResponse(
ProtocolResponse.prepare(
User.toMap(
SaveNewUserUsecase.newWith( userPersistenceAdapter ).execute( User.fromMap(jsonObject) )
)
)
)
}
}
}
post("/facebookLogin") {
contentType = "application/json"
authenticated { jsonObject =>
withPersistenceAdapter { userPersistenceAdapter =>
LoginByFacebookUserUsecase.newWith( userPersistenceAdapter ).execute( User.fromMap(jsonObject) ) match {
case Some(user) => writeOkResponse(ProtocolResponse.prepare(User.toMap(user)))
case None => writeNotFoundResponse(ProtocolResponse.prepareUserNotFound())
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment