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 1fd7eff..e8eaa9e 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -512,11 +512,78 @@ public class ORM { return sql.rows(query, sqlParams) .collect { recordToModel(Playlist, it) } } - public Playlist addToPlaylist(int playlistId, int mediaFileId) { + public int getNextPlaylistPosition(int playlistId) { + String query = """\ + SELECT COALESCE(MAX(position), 0) + 1 + FROM playlists_media_files + WHERE playlist_id = ?""" + + logger.debug('Finding next playlist position.\n\tSQL: {}\n\tPARAMS: {}', + query, playlistId) + + return sql.firstRow(query, playlistId)[0] } + + private int incrementPlaylistPositions(int playlistId, int startPosition, + int incrementAmount) { + String query = """\ + UPDATE playlists_media_files + SET position = position + ? + WHERE playlist_id = ? AND position > ?""" + def params = [incrementAmount, playlistId, startPosition] + + logger.debug('Incrementing playlist positions.\n\tSQL: {}\n\tPARAMS: {}', + query, params) + return sql.executeUpdate(query, params) } + + public Playlist addToPlaylist(int playlistId, + List mediaFileIds, int startPosition = -1) { + + String query + def params + return withTransaction { def p = getById(Playlist, playlistId) if (!p) return null - associate(Playlist, MediaFile, p, mf) + + if (startPosition < 0) + startPosition = getNextPlaylistPosition(playlistId) + else + incrementPlaylistPositions(playlistId, startPosition, + mediaFileIds.size()) + + def position = startPosition + mediaFileIds.each { mediaFileId -> + query = 'INSERT INTO playlists_media_files VALUES (?, ?, ?)' + params = [playlistId, mediaFileId, position++] + logger.debug( + 'Adding a new playlist/media file relation.\n\tSQL: {}\n\tPARAMS: {}', + query, params) + sql.executeInsert(query, params) } + + p.mediaFileCount += mediaFileIds.size() + return update(p) } } + + public Playlist addToPlaylist(int playlistId, int mediaFileId, + int position = -1) { + + String query + def params + + return withTransaction { + def p = getById(Playlist, playlistId) + if (!p) return null + + if (position < 0) position = getNextPlaylistPosition(playlistId) + else incrementPlaylistPositions(playlistId, position, 1) + + query = 'INSERT INTO playlists_media_files VALUES (?, ?, ?)' + params = [playlistId, mediaFileId, position] + + logger.debug( + 'Adding a new playlist/media file relation.\n\tSQL: {}\n\tPARAMS: {}', + query, params) + sql.executeInsert(query, params) + p.mediaFileCount++ return update(p) } } @@ -540,6 +607,8 @@ public class ORM { /// ### Tag-specific methods public Tag getTagById(int id) { return getById(Tag, id) } public Tag getTagByName(String name) { return getByName(Tag, name)[0] } + public List getTags() { return getAll(Tag, 'name ASC') } + public List getTagsWhere(Map params) { def query = new StringBuilder() def sqlParams = [] @@ -576,6 +645,27 @@ public class ORM { query.append(' WHERE t.name = ? ') sqlParams << params.name } } + public def tagMediaFiles(List mediaFileIds, List tagNames) { + String insertQuery = 'INSERT INTO media_files_tags VALUES (?, ?)' + String checkQuery = 'SELECT * FROM media_files_tags WHERE media_file_id = ? AND tag_id = ?' + def params + + withTransaction { + List tags = tagNames.collect { name -> + getTagByName(name) ?: create(new Tag(name: name)) } + + tags.each { tag -> + params = [null, tag.id] + mediaFileIds.each { mediaFileId -> + params[0] = mediaFileId + logger.debug('Checking to see if a media file is ' + + 'already tagged.\n\tSQL: {}\n\tPARAMS: {}', params) + if (sql.rows(checkQuery, params).size() == 0) { + logger.debug( + 'Tagging a media file.\n\tSQL: {}\n\tPARAMS: {}', + params) + sql.executeInsert(insertQuery, params) } } } } } + /// ### Utility functions public def withTransaction(Closure c) { try { sql.execute('BEGIN TRANSACTION'); return c() }