Scala check a Sequence of Eithers

Viewed 1372

I want to update a sequence in Scala, I have this code :

def update(userId: Long): Either[String, Int] = {
  Logins.findByUserId(userId) map {
    logins: Login => update(login.id,
      Seq(NamedParameter("random_date", "prefix-" + logins.randomDate)))
  } match {
    case sequence : Seq(Nil, Int) => sequence.foldLeft(Right(_) + Right(_))
    case _ => Left("error.logins.update")
  }
}

Where findByUserId returns a Seq[Logins] and update returns Either[String, Int] where Int is the number of updated rows, and String would be the description of the error.

What I want to achieve is to return an String if while updating the list an error happenes or an Int with the total number of updated rows.

The code is not working, I think I should do something different in the match, I don't know how I can check if every element in the Seq of Eithers is a Right value.

3 Answers
Related