From 1354a92204d52de6a8e7d502a925732167c1780e Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 12 Feb 2016 17:20:32 -0600 Subject: [PATCH] ORM returns models from save, create, and update. Also additional lookup functions to ORM. --- .../com/jdbernard/wdiwtlt/db/ORM.groovy | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 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 c699db3..f10526f 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -115,7 +115,8 @@ public class ORM { .toString() logger.debug("Updating model.\n\tSQL: {}\n\tPARAMS: {}", query, params) - return sql.executeUpdate(query, params) } + sql.executeUpdate(query, params) + return model } public def create(def model) { def columns = [] @@ -140,7 +141,7 @@ public class ORM { logger.debug("Creating model.\n\tSQL: {}\n\tPARAMS: {}", query, params) model.id = sql.executeInsert(query, params)[0][0] - return 1 } + return model } public def delete(def model) { def query = new StringBuilder() @@ -181,7 +182,10 @@ public class ORM { /// ### Album-specific methods public Album getAlbumById(int id) { return getById(Album, id) } - public List getAlbumByName(String name) { + public Album[] getAlbumsByNameAndYear(String name, int year) { + return getBy(Album, ['name', 'year'], [name, year]) } + + public List getAlbumsByName(String name) { return getByName(Album, name) } public Album getAlbumByNameAndArtistId(String name, int artistId) { @@ -193,11 +197,23 @@ public class ORM { aa.artist_id = ? WHERE al.name = ?""" def params = [artistId, name] - logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", + logger.debug("Selecting an album.\n\tSQL: {}\n\tPARAMS: {}", query, params) - def albums = sql.rows(query, params) - .collect { recordToModel(Album, it) } - return albums ? albums[0] : null } + return recordToModel(Album, sql.firstRow(query, params)) } + + public Album getAlbumByNameAndYearAndArtistId(String name, int year, + int artistId) { + def query = """\ + SELECT al.* + FROM albums al JOIN + artists_albums aa ON + al.id = aa.album_id AND + aa.artist_id = ? + WHERE al.name = ? AND al.year = ?""" + def params = [artistId, name, year] + logger.debug("Selecting an album. \n\tSQL: {}\n\tPARAMS: {}", + query, params) + return recordToModel(Album, sql.firstRow(query, params)) } public List getAlbumsByArtistId(int artistId) { def query = """\ @@ -221,8 +237,9 @@ public class ORM { /// ### Artist-specific methods public Artist getArtistById(int id) { return getById(Artist, id) } - public List getArtistByName(String name) { - return getByName(Artist, name) } + public Artist getArtistByName(String name) { + def artists = getByName(Artist, name) + if (artists) return artists[0] } public List getArtists() { return getAll(Artist) } public List getArtistsByAlbumId(int albumId) { @@ -301,9 +318,10 @@ public class ORM { public List getPlaylists() { return getAll(Playlist) } - public Playlist addToPlaylist(Playlist p, MediaFile mf) { + public Playlist addToPlaylist(int playlistId, int mediaFileId) { return withTransaction { - associate(p, mf) + associate(Playlist, MediaFile, p, mf) + def p = getById(Playlist, playlistId) p.mediaFileCount++ update(p) return refresh(p) } }