Skip to content

Commit

Permalink
Search now also looks in emails and hobbies.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianharrington committed Nov 22, 2013
1 parent 6077cd7 commit 1788202
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
36 changes: 27 additions & 9 deletions src/main/scala/dk/itu/ssas/model/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,41 @@ object User extends UserExceptions with DbAccess {
}


/** Searches for other users by name
/** Searches for other users by name, email, or hobby
*
* @param s - The search string
* @param user - This user, if given, won't be included in the results. Useful if this is the user searching.
* @return A list of users with names matching the search string
*/
def search(s: String, user: Option[User] = None): List[User] = Db withSession {
if (validName(s)) {
val users = for {
u <- Users if u.name like s"%$s%"
// If the search string is a valid name, it is also a valid hobby.
val users = if (validHobby(s)) {
val u = for {
u <- Users if ((u.name like s"%$s%") || (u.email like s"%$s%"))
} yield u

user match {
case None => users.list
case Some(user) => users.list filter (u => u.id != user.id)
}
} else List()
val uh= for {
h <- Hobbies if (h.name like s"%$s%")
uh <- UserHobbies if (h.id === uh.hobbyId)
u <- Users if (uh.userId === u.id)
} yield u

(u.list ++ uh.list).distinct
}
// If we get and e-mail, we only look in emails
else if (validEmail(s)) {
val u = for {
u <- Users if (upper(u.email) === s.toUpperCase())
} yield u

u.list
}
else List()

user match {
case None => users
case Some(user) => users filter (u => u.id != user.id)
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/scala/dk/itu/ssas/services/UserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,18 @@ object UserService extends SsasService with UserExceptions {
withSession { s =>
withUser(s) { u =>
withFormKey(s) {
formFields('searchTerm) { name =>
val users = u search name
html(s) { (s, formKey) =>
complete {
SearchPage.render("Search results", formKey, Some(u), SearchPageRequest(users))
formFields('searchTerm) { searchTerm =>
if (validHobby(searchTerm) || validEmail(searchTerm)) {
val users = u search searchTerm
html(s) { (s, formKey) =>
complete {
SearchPage.render("Search results", formKey, Some(u), SearchPageRequest(users))
}
}
}
else {
complete { HttpResponse(StatusCodes.BadRequest, "Search term is improperly formatted") }
}
}
}
}
Expand Down

0 comments on commit 1788202

Please sign in to comment.