Created
January 16, 2016 15:34
-
-
Save agnaldo4j/b084b573b1ddeda536ce to your computer and use it in GitHub Desktop.
Exemplo de endpoint acessando usecases
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
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