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

View File

@ -11,7 +11,7 @@ public class Playlist extends Model {
public int mediaFileCount = 0; public int mediaFileCount = 0;
public Integer copiedFromId = null; public Integer copiedFromId = null;
public String toString() { public String toString() {
if (userCreated) return name; if (userCreated) return name;
return name + " (auto)"; } return name + " (auto)"; }
} }