Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exemplo de um usecase
package com.opportunity.objectives.usecases.user
import com.opportunity.objectives.exceptions.UserInvalidException
import com.opportunity.objectives.model.{Friend, User}
import com.opportunity.objectives.persistence.adapter.user.NewUserPersistenceAdapter
class SaveNewFriendsUsecase(persistenceAdapter: NewUserPersistenceAdapter) {
def save(user:User, friends:List[Friend]): User = {
persistenceAdapter.findUserById(user.id) match {
case findedUser:User => prepareFriends(findedUser, friends)
case _ => throw new UserInvalidException(List[Map[String, String]]())
}
}
private def prepareFriends(user:User, friends:List[Friend]): User = {
val emailOfFriendsFriends:List[String] = friends.map { friend => friend.email }
val existentFirends:List[Friend] = persistenceAdapter.findExistentFriends(emailOfFriendsFriends)
val remainingInitialFriends = friends.takeWhile { friend => existentFirends.find{ existent => existent.email.equalsIgnoreCase(friend.email) }.isEmpty }
val finalFriends = existentFirends ++ remainingInitialFriends
persistenceAdapter.saveFriends(user, finalFriends)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment