Created
January 12, 2016 02:20
-
-
Save agnaldo4j/090083698354188c59b3 to your computer and use it in GitHub Desktop.
Exemplo de um usecase
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.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