|
|
@@ -1,9 +1,23 @@
|
|
|
package repository
|
|
|
|
|
|
-import "github.com/jmoiron/sqlx"
|
|
|
+import (
|
|
|
+ "database/sql"
|
|
|
+
|
|
|
+ "github.com/jmoiron/sqlx"
|
|
|
+)
|
|
|
+
|
|
|
+func getSongIdOrZero(db *sqlx.DB, query string, songId int64) (assocSongId int64, err error) {
|
|
|
+ err = db.QueryRowx(query, songId).Scan(&assocSongId)
|
|
|
+ if err != nil && err == sql.ErrNoRows {
|
|
|
+ err = nil
|
|
|
+ assocSongId = 0
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
func GetNextSongId(db *sqlx.DB, prevSongId int64) (nextSongId int64, err error) {
|
|
|
- err = db.QueryRowx(
|
|
|
+ nextSongId, err = getSongIdOrZero(
|
|
|
+ db,
|
|
|
`
|
|
|
select coalesce(id_next, 0) as id from (
|
|
|
select
|
|
|
@@ -66,13 +80,13 @@ func GetNextSongId(db *sqlx.DB, prevSongId int64) (nextSongId int64, err error)
|
|
|
) result
|
|
|
`,
|
|
|
prevSongId,
|
|
|
- ).Scan(&nextSongId)
|
|
|
-
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func GetPrevSongId(db *sqlx.DB, nextSongId int64) (prevSongId int64, err error) {
|
|
|
- err = db.QueryRowx(
|
|
|
+ prevSongId, err = getSongIdOrZero(
|
|
|
+ db,
|
|
|
`
|
|
|
select coalesce(id_prev, 0) as id from (
|
|
|
select
|
|
|
@@ -135,7 +149,6 @@ func GetPrevSongId(db *sqlx.DB, nextSongId int64) (prevSongId int64, err error)
|
|
|
) result
|
|
|
`,
|
|
|
nextSongId,
|
|
|
- ).Scan(&prevSongId)
|
|
|
-
|
|
|
+ )
|
|
|
return
|
|
|
}
|