Skip to content

Commit

Permalink
Able to change states to archived and starred
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hughes authored and Michael Hughes committed Mar 23, 2013
1 parent c8737c9 commit 420bfe9
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 24 deletions.
49 changes: 49 additions & 0 deletions src/client/EntryListController.coffee
@@ -0,0 +1,49 @@
controller = exports ? this

states = {
NEW:"new"
ARCHIVED:"archived"
STARRED:"starred"
}

controller.EntryListCtrl = ($scope,$http)->

$scope.entries = []
if $http
$http.get("/latest/json")
.success (data,status)->
$scope.entries = data
.error (data,status)->

ignoreOutput = (data,status)->

$scope.encode = (url)->
encodeURIComponent url

$scope.plusMinus = (open)->
if open then "minus" else "plus"

$scope.remove = (index)->
entry = $scope.entries[index]
$scope.entries.splice index,1
if $http
$http.get("/changeState?state=" + states.ARCHIVED + "&id=" + $scope.encode(entry.id))
.success(ignoreOutput)
.error(ignoreOutput)

$scope.star = (index)->
entry = $scope.entries[index]
if entry.state==states.STARRED
entry.state=states.NEW
else
entry.state=states.STARRED
if $http
$http.get("/changeState?state="+ entry.state + "&id=" + $scope.encode(entry.id))
.success(ignoreOutput)
.error(ignoreOutput)

$scope.status = (state)->
if state==states.STARRED then "" else "-empty"


scope:$scope
16 changes: 0 additions & 16 deletions src/client/FeedListController.coffee
Expand Up @@ -14,19 +14,3 @@ controller.FeedListCtrl = ($scope)->

scope:$scope

controller.EntryListCtrl = ($scope,$http)->

$scope.entries = []

$http.get("/latest/json")
.success (data,status)->
$scope.entries = data
.error (data,status)->

$scope.plusMinus = (bool)->
if(bool) then "minus" else "plus"

$scope.encode = (url)->
encodeURIComponent url

scope:$scope
10 changes: 5 additions & 5 deletions src/resources/views/index.jade
Expand Up @@ -13,12 +13,12 @@ block content
div(class="btn-group btns-right")
a(class="btn",ng-click="e.open = !e.open",ng-init="e.open=false")
i(class="icon-{{plusMinus(e.open)}}")
a(class="btn")
a(class="btn",ng-click="remove($index)")
i(class="icon-trash")
a(class="btn")
i(class="icon-star-empty")
a(href="redirect?to={{encode(e.link)}}&id={{encode(e.id)}}")
a(class="btn",ng-click="star($index)")
i(class="icon-star{{status(e.state)}}")
a(href="redirect?to={{encode(e.link)}}&id={{encode(e.id)}}",target="_new")
h3 {{e.title}}
span{{e.date}}
span{{e.date | date:'medium'}}
p(class="toggle-{{e.open}}",ng-bind-html-unsafe="e.summary")

2 changes: 1 addition & 1 deletion src/server/MongoStore.coffee
Expand Up @@ -55,7 +55,7 @@ exports.createMongostore = (connectionString)->
@connect (err,collection)->
if err then fun err,null
else
collection.find({state:"new"}).sort({date:-1}).limit(amount).toArray fun
collection.find({$or: [{state:"new"},{state:"starred"}]}).sort({date:-1}).limit(amount).toArray fun

updateEntryState:(id,state,fun)->
@connect (err,collection)->
Expand Down
5 changes: 5 additions & 0 deletions src/server/Webserver.coffee
Expand Up @@ -25,6 +25,11 @@ exports.createWebServer = (port,connectionString)->
res.end(err) if err
res.redirect(req.query.to) unless err

app.get '/changeState', (req,res)->
ms = store.createMongostore(connectionString)
ms.updateEntryState req.query.id,req.query.state,(err,result)->
res.end(err) if err
res.end() unless err

# Start Server
app.listen port, -> console.log "Server is listening on #{port}\nPress CTRL-C to stop server."
18 changes: 18 additions & 0 deletions test/client/unit/EntryListController_test.coffee
@@ -0,0 +1,18 @@
controller = require '../../../src/client/EntryListController.coffee'
expect = (require 'chai').expect

describe 'A EntryListCtrl controller', ->
it 'should be able to remove entries from a list', ()->
c = new controller.EntryListCtrl({})
c.scope.entries = [
{title:"a",id:"aa"},
{title:"b",id:"bb"},
{title:"c",id:"cc"}
]
c.scope.remove(1)
expect(c.scope.entries).to.eql [
{title:"a",id:"aa"},
{title:"c",id:"cc"}
]


12 changes: 10 additions & 2 deletions test/server/integration/MongoStore_test.coffee
Expand Up @@ -7,8 +7,8 @@ after = andAlso = require '../shared/Feed_steps.coffee'
connectionString = "mongodb://localhost:27000/feeds"

feedData = [
{"title":"A news story","link":"http://a.news.story/","tag"}
{"title":"Another news story","link":"http://another.news.story/"}
{"title":"A news story","link":"http://a.news.story/","tag","id":"1"}
{"title":"Another news story","link":"http://another.news.story/","id":"2"}
]

similarData = [
Expand Down Expand Up @@ -94,3 +94,11 @@ describe 'A mongodb store', ->
@database.getLatestNew 10,(err,result)->
expect(result.length).to.equal 10
done()

it 'should return both new and starred entries',(done)->
after.insertingSome(entriesWithDates).intoThe @database,(result)=>
@database.updateEntryState "aNewerEntry","starred",(err,result)=>
@database.getLatestNew 10,(err,result)->
expect(result.length).to.equal 3
expect(result[0].title).to.equal "a newer entry"
done()

0 comments on commit 420bfe9

Please sign in to comment.