Permalink
Browse files

Facets now have querystrings

  • Loading branch information...
1 parent bcd782f commit bd9a85e04bdabee76079217c5540172aec9f6a42 Michael Hughes committed Feb 17, 2013
Showing with 41 additions and 2 deletions.
  1. +1 −1 src/resources/views/facet.jade
  2. +17 −0 src/server/query.coffee
  3. +2 −1 src/server/webserver.coffee
  4. +21 −0 test/server/query_test.coffee
@@ -4,6 +4,6 @@
- if (facet.length)
each f in facet
li
- a(href="/?#{facetQuery}=#{f._id}")
+ a(href="/#{query.extendQuerystring(facetQuery,f._id)}")
span.facet-name= f._id
span.facet-amount= f.count
View
@@ -0,0 +1,17 @@
+_ = require 'underscore'
+qs = require 'querystring'
+
+exports.createQuery = (previousQuery)->
+
+ extendQuery:(name,value)->
+ if previousQuery.hasOwnProperty name
+ _.omit(previousQuery,name)
+ else
+ q=_.clone(previousQuery)
+ q[name]=value
+ q
+
+
+ extendQuerystring:(name,value)->
+ "?"+qs.stringify(@extendQuery(name,value))
+
@@ -1,5 +1,6 @@
express = require 'express'
mongostore = require './mongostore.coffee'
+query = require './query.coffee'
domain = require './domain.coffee'
mongoPort = process.env.MONGOPORT or 27017
@@ -20,7 +21,7 @@ app.get '/', (req, res)->
search = domain.createSearch mongostore.createMongostore(connectionString)
search.getResults req.query,(err,result)=>
res.send err if err
- res.render('index',{title:"Your Lego Sets",searchResults:result})
+ res.render('index',{title:"Your Lego Sets",searchResults:result,query:query.createQuery(req.query)})
# Start Server
app.listen port, -> console.log "LegoDatabase server is listening on #{port}\nPress CTRL-C to stop server."
@@ -0,0 +1,21 @@
+query = require '../../src/server/query.coffee'
+should = require 'should'
+
+describe 'A query', ->
+ it 'should be able to add values to the query',->
+ q=query.createQuery({})
+ q.extendQuery("a","b").should.eql {"a":"b"}
+ it 'should be able to getQuery with multiple values without them being added',->
+ q=query.createQuery({"1":"2"})
+ q.extendQuery("a","b").should.eql {"1":"2","a":"b"}
+ q.extendQuery("c","d").should.eql {"1":"2","c":"d"}
+ it 'should remove the facet from the query if it already exists',->
+ q=query.createQuery({"a":"2"})
+ q.extendQuery("a","3").should.eql {}
+ it 'should be able to output a querystring',->
+ q=query.createQuery({})
+ q.extendQuerystring("a","b").should.equal "?a=b"
+ it 'should be able to output multiple querystring values',->
+ q=query.createQuery({"1":"2"})
+ q.extendQuerystring("a","b").should.equal "?1=2&a=b"
+

0 comments on commit bd9a85e

Please sign in to comment.