Add the ability to clear a playlist.

This commit is contained in:
Jonathan Bernard 2016-02-29 12:37:23 -06:00
parent 83ffa2aafa
commit a1230dddfd
2 changed files with 24 additions and 10 deletions

View File

@ -116,9 +116,10 @@ public class ORM {
.append(model.id)
.toString()
logger.debug("Updating model.\n\tSQL: {}\n\tPARAMS: {}", query, params)
sql.executeUpdate(query, params)
return model }
return withTransaction {
logger.debug("Updating model.\n\tSQL: {}\n\tPARAMS: {}", query, params)
sql.executeUpdate(query, params)
return refresh(model) } }
public def create(def model) {
def columns = []
@ -164,7 +165,7 @@ public class ORM {
String col1 = nameFromModel(modelClass1.simpleName) + "_id"
String col2 = nameFromModel(modelClass2.simpleName) + "_id"
withTransaction {
return withTransaction {
def query = """\
SELECT * FROM $linkTable
WHERE "${col1}" = ? AND "${col2}" = ?"""
@ -475,11 +476,24 @@ public class ORM {
public Playlist addToPlaylist(int playlistId, int mediaFileId) {
return withTransaction {
associate(Playlist, MediaFile, p, mf)
def p = getById(Playlist, playlistId)
if (!p) return null
associate(Playlist, MediaFile, p, mf)
p.mediaFileCount++
update(p)
return refresh(p) } }
return update(p) } }
public Playlist removeAllFromPlaylist(int playlistId) {
return withTransaction {
def p = getById(Playlist, playlistId)
if (!p) return null
String query =
"DELETE FROM playlists_media_files WHERE playlist_id = ?"
def sqlParams = [playlistId]
logger.debug("Clearing playlist.\n\tSQL: {}\n\tPARAMS: {}",
query, sqlParams)
sql.execute(query, sqlParams)
p.mediaFileCount = 0
return update(p) } }
public List<Playlist> removeEmptyPlaylists() {
throw new UnsupportedOperationException("Not yet implemented.");
@ -525,8 +539,8 @@ public class ORM {
sqlParams << params.name } }
/// ### Utility functions
public void withTransaction(Closure c) {
try { sql.execute("BEGIN TRANSACTION"); c() }
public def withTransaction(Closure c) {
try { sql.execute("BEGIN TRANSACTION"); return c() }
finally { sql.execute("COMMIT") }
}
public static String nameToModel(String name) {