Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now able to add feeds into the DB and get them out again

  • Loading branch information...
commit 1441105877cf953acea7e90f58ead3dce697abad 1 parent 48f1757
Michael Hughes authored
View
58 src/server/FeedStore.coffee
@@ -0,0 +1,58 @@
+mongo = require 'mongodb'
+
+collectionName = "feedData"
+
+exports.createFeedstore = (connectionString)->
+
+ database = null
+
+ connect:(fun)->
+ mongoClient = mongo.MongoClient
+ mongoClient.connect connectionString,(err,db)=>
+ @database = db
+ fun err,null if err
+ fun null,db.collection(collectionName) unless err
+
+ clear:(fun)->
+ @connect (err,collection)->
+ if err then fun err,null
+ else
+ collection.remove {},fun
+
+ close:(fun)->(err,result)=>
+ if @database
+ @database.close()
+ fun(err,result)
+
+ insertFeed:(feed,fun)->
+ @connect (err,collection)=>
+ if err then fun err,null
+ else
+ collection.update {URL:feed.URL}, {$set:feed}, {w:1,upsert:true}, @close(fun)
+
+ insertFeeds:(listOfFeeds,fun,current=0,listOfResults=[])->
+ if listOfFeeds.length <= current then fun(null,listOfResults)
+ else
+ @insertFeed listOfFeeds[current],(err,result)=>
+ if err then fun(err,null)
+ else
+ listOfResults.push result
+ @insertFeeds listOfFeeds,fun,current+1,listOfResults
+
+ getFeeds:(fun)->
+ @connect (err,collection)=>
+ if err then fun err,null
+ else
+ collection.find().toArray @close(fun)
+
+ getTags:(fun)->
+ @connect (err,collection)=>
+ if err then fun err,null
+ else
+ collection.distinct 'tags', @close(fun)
+
+ deleteFeed:(URL,fun)->
+ @connect (err,collection)=>
+ if err then fun err,null
+ else
+ collection.remove {URL:URL}, @close(fun)
View
41 test/server/integration/FeedStore_test.coffee
@@ -0,0 +1,41 @@
+mongostore = require '../../../src/server/FeedStore.coffee'
+
+expect = (require 'chai').expect
+moment = require 'moment'
+
+connectionString = "mongodb://localhost:27000/feeds"
+
+feedData = [
+ {URL:"http://feed/a",tags:["Technology"]}
+ {URL:"http://feed/b",tags:["News"]}
+ ]
+
+describe 'A mongodb store for Feed information', ->
+ beforeEach (done) ->
+ @database = mongostore.createFeedstore(connectionString)
+ @database.clear (err,result)->
+ done()
+
+ it 'should be able to insert data into the datastore', (done)->
+ @database.insertFeed {URL:"http://feed/c",tags:["Sport"]},(err,result)=>
+ throw err if err
+ @database.getFeeds (err,result)->
+ expect(result[0].URL).to.equal "http://feed/c"
+ done()
+
+ it 'should be able to find all the tags in the store', (done)->
+ @database.insertFeeds feedData,(err,result)=>
+ throw err if err
+ @database.getTags (err,result)->
+ expect(result).to.eql ["Technology","News"]
+ done()
+
+ it 'should be able to delete a feed', (done)->
+ @database.insertFeeds feedData,(err,result)=>
+ throw err if err
+ @database.deleteFeed "http://feed/a",(err,result)=>
+ throw err if err
+ @database.getFeeds (err,result)->
+ expect(result.length).to.equal 1
+ expect(result[0].URL).to.equal "http://feed/b"
+ done()
Please sign in to comment.
Something went wrong with that request. Please try again.