Skip to content

Commit

Permalink
Implement get and destroy for List.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearsandwich committed Mar 16, 2012
1 parent aa647a7 commit 5a7c39e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/list.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pg = require 'pg'
Task = require './task'

module.exports = class List
constructor: (@name) ->
Expand Down Expand Up @@ -47,4 +48,51 @@ module.exports = class List
callback null, self
return

destroy: (callback) ->
self = this
@client.query {
name: 'delete-list'
text: 'DELETE FROM lists WHERE id = $1;'
values: [@id]
}, (err, result) ->
if err?
callback err, null
return
console.log "returning from destroy with", self
callback null, self
return


@get: (listId, callback) ->
client = new pg.Client @dbConfig
client.on 'drain', client.end.bind client
client.connect()
client.query {
name: 'select-by-id'
text: 'SELECT * FROM lists WHERE id = $1 LIMIT 1;'
values: [listId]
}, (err, result) ->
if err?
callback err, null
return
if result.rows.length is 0
callback null, null
return
listRow = result.rows[0]
list = new List listRow.name
list.id = listRow.id
client.query {
name: 'select-tasks-by-list-id'
text: 'SELECT * FROM tasks WHERE list_id = $1;'
values: [listId]
}, (err, result) ->
callback err, null if err?
result.rows.forEach (taskRow) ->
task = {}
task.name = taskRow.name
task.completed = taskRow.completed
task.listId = taskRow.list_id
list.addTask task
callback null, list

@setDBConfig: (@dbConfig) ->
25 changes: 25 additions & 0 deletions test/list-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,35 @@ vows.describe('List').addBatch(
list = new List "A pretty cool list"
list.addTask task
list.save @callback
return
'Then the tasks are saved and have the proper listId': (err, list) ->
assert.isNull err
assert.isTrue list.tasks[0].isSaved()
assert.equal list.tasks[0].listId, list.id
'When retrieving it with `List.get`':
topic: (err, list) ->
List.get list.id, @callback
return
'Then the retrieved list is equal to the saved list': (err, list) ->
assert.isNull err
assert.equal list.name, 'A persisted list'
assert.lengthOf list.tasks, 1
assert.equal retrievedList, list

'When a list is destroyed':
topic: ->
list = new List 'A deletable list'
list.save (err, l) =>
l.destroy @callback
'When the list is retrieved':
topic: (e, list) ->
console.log "ERROR", e
console.log e is list
console.log "LIST", list
List.get list.id, @callback
'Then null is returned': (err, list) ->
assert.isNull list


).export module

0 comments on commit 5a7c39e

Please sign in to comment.