Create a gist now

Instantly share code, notes, and snippets.

Embed
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