Skip to content

Commit

Permalink
Drop websql and store plain file in dataPath
Browse files Browse the repository at this point in the history
1. Drop websql because it's hard to debug and maintain
2. Use nedb as our db backend (nosql friendly API). Because nedb will
create plain file as db in dataPath, we can debug Atraci more easily
3. fix a bug that when creating playlist, if you type enter (on mac),
then Atraci would jump to white screen without any reason and
  • Loading branch information
EragonJ committed Oct 26, 2014
1 parent 9770f7a commit f831c84
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 190 deletions.
98 changes: 54 additions & 44 deletions coffee/_history.coffee
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
# Init preparation (should be improved later)
db.transaction (tx) ->
tx.executeSql(
'CREATE TABLE IF NOT EXISTS history ' +
'(artist, title, cover_url_medium, cover_url_large, last_played)'
)

class History
@clear: (success) ->
db.transaction (tx) ->
tx.executeSql 'DROP TABLE history'
success?()
db.history.remove({}, { multi: true }, (error) ->
if error
console.log("History.clear remove erorr :")
console.log(error)
success?()
else
success?()
)

@addTrack: (artist, title, cover_url_medium, cover_url_large) ->
unix_timestamp = Math.round((new Date()).getTime() / 1000)
db.transaction (tx) ->
tx.executeSql(
'CREATE TABLE IF NOT EXISTS history ' +
'(artist, title, cover_url_medium, cover_url_large, last_played)'
)

tx.executeSql(
'DELETE FROM history WHERE artist = ? and title = ?', [artist, title]
)

tx.executeSql(
'INSERT INTO history ' +
'(artist, title, cover_url_medium, cover_url_large, last_played) ' +
'VALUES (?, ?, ?, ?, ?)',
[artist, title, cover_url_medium, cover_url_large, unix_timestamp]
)
db.history.remove({
artist: artist,
title: title
}, {}, (error, numRemoved) ->
if error
console.log("History.addTrack remove erorr :")
console.log(error)
else
db.history.insert({
artist: artist,
title: title,
cover_url_medium: cover_url_medium,
cover_url_large: cover_url_large,
last_played: unix_timestamp
}, (error) ->
if error
console.log("History.addTrack insert erorr :")
console.log(error)
)
)

@removeTrack: (artist, title) ->
db.transaction (tx) ->
tx.executeSql(
'DELETE FROM history WHERE artist = ? and title = ?', [artist, title]
)
db.history.remove({
artist: artist,
title: title
}, {}, (error) ->
if error
console.log("History.removeTrack remove erorr :")
console.log(error)
)

@getTracks: (success) ->
tracks = []
db.transaction (tx) ->
tx.executeSql(
'SELECT * FROM history ORDER BY last_played DESC LIMIT 150', [],
(tx, results) ->
i = 0
while i < results.rows.length
tracks.push results.rows.item(i)
i++
success? tracks
)
db.history.find({}).sort({
last_played: -1
}).limit(150).exec((error, foundTracks) ->
if error
console.log("History.getTracks find erorr :")
console.log(error)
success?([])
else
success?(foundTracks)
)

@countTracks: (success) ->
db.transaction (tx) ->
tx.executeSql 'SELECT COUNT(*) AS cnt FROM history', [], (tx, results) ->
success? results.rows.item(0).cnt
db.history.count({}, (error, count) ->
if error
console.log("History.countTracks count erorr :")
console.log(error)
success?(0)
else
success?(count)
)

0 comments on commit f831c84

Please sign in to comment.