From 178820240be7791bcedb10542f780bcf88cb2872 Mon Sep 17 00:00:00 2001 From: Christian Harrington Date: Fri, 22 Nov 2013 14:03:25 +0100 Subject: [PATCH] Search now also looks in emails and hobbies. --- src/main/scala/dk/itu/ssas/model/User.scala | 36 ++++++++++++++----- .../dk/itu/ssas/services/UserService.scala | 15 +++++--- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/main/scala/dk/itu/ssas/model/User.scala b/src/main/scala/dk/itu/ssas/model/User.scala index 979146f..e42aa66 100644 --- a/src/main/scala/dk/itu/ssas/model/User.scala +++ b/src/main/scala/dk/itu/ssas/model/User.scala @@ -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) + } } } diff --git a/src/main/scala/dk/itu/ssas/services/UserService.scala b/src/main/scala/dk/itu/ssas/services/UserService.scala index 7dcccfa..8c3b498 100644 --- a/src/main/scala/dk/itu/ssas/services/UserService.scala +++ b/src/main/scala/dk/itu/ssas/services/UserService.scala @@ -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") } + } } } }