From a1230dddfd668c8169416cf7d4ebda0fc7445dba Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 29 Feb 2016 12:37:23 -0600 Subject: [PATCH] Add the ability to clear a playlist. --- .../com/jdbernard/wdiwtlt/db/ORM.groovy | 32 +++++++++++++------ .../jdbernard/wdiwtlt/db/models/Playlist.java | 2 +- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy index 9def073..b61a36d 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -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 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) { diff --git a/core/src/main/java/com/jdbernard/wdiwtlt/db/models/Playlist.java b/core/src/main/java/com/jdbernard/wdiwtlt/db/models/Playlist.java index f6b8375..326e2b3 100644 --- a/core/src/main/java/com/jdbernard/wdiwtlt/db/models/Playlist.java +++ b/core/src/main/java/com/jdbernard/wdiwtlt/db/models/Playlist.java @@ -11,7 +11,7 @@ public class Playlist extends Model { public int mediaFileCount = 0; public Integer copiedFromId = null; - public String toString() { + public String toString() { if (userCreated) return name; return name + " (auto)"; } }