Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Created November 22, 2011 06:51
Show Gist options
  • Save fmpwizard/1385079 to your computer and use it in GitHub Desktop.
Save fmpwizard/1385079 to your computer and use it in GitHub Desktop.
class InventorySearch extends Logger with myScalaz.Boxes {
def results = {
val searchBoxText= S.param("p")
val doSearch= new DoSearch
lazy val searchResult= doSearch.queryElasticSearch(searchBoxText) // Returns Elastic Search results in json format
lazy val invItems=
searchResult map { x =>
for {
JField("_id", JString(id)) <- x
} yield Inventory.getItem( Full(id.toInt) )
}
def showResults(searchTerm: Box[String]) = {
debug("Searching for: %s".format(searchBoxText))
val cnt= searchResult map ( x => compact(json.render( x \ "hits" \ "total")).toInt)
cnt match {
case Full(1) => {
val id: Box[Long]= invItems flatMap (_.head map (_.id.is))
S.redirectTo("/inventorydisplay?p=" + ~(id) )
}
case Full(0) => noResult
case _ => displayItems
}
}
def defaultResult = {
"#item_link *" #> "" &
"#part_number *" #> "" &
"#description *" #> "" &
"#category *" #> "" &
"#subcategory *" #> ""
}
def noResult = {
"#item_link *" #> "" &
"#part_number *" #> "" &
"#description *" #> "No Results" &
"#category *" #> "" &
"#subcategory *" #> ""
}
def displayItems ={
".row *" #> ~(invItems).map { _.flatMap { _.map{
x =>
"#part_number *" #> SHtml.link ("/inventorydisplay?p=" + x.id, () => Unit, <span>{x.part_number}</span> ) &
"#description *" #> x.description.filter(_.language.is === "EN").map(_.text) &
"#category *" #> x.category.obj.map(_.category_name) &
"#subcategory *" #> x.subcategory.obj.map(_.subcategory_name)
}}
}
}
(~(searchBoxText) /== "").fold(showResults(searchBoxText), defaultResult)
}
def render ={
var searchBoxText= ""
ClearClearable andThen
"name=searchbox" #> SHtml.text(searchBoxText, searchBoxText= _)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment