-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylistsongtable.go
More file actions
83 lines (61 loc) · 2.3 KB
/
playlistsongtable.go
File metadata and controls
83 lines (61 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package database
import (
"context"
"fmt"
"musicboxapi/logging"
"musicboxapi/models"
)
type IPlaylistsongTable interface {
FetchPlaylistSongs(ctx context.Context, playlistId int, lastKnowPosition int) (songs []models.Song, error error)
InsertPlaylistSong(playlistId int, songId int) (lastInsertedId int, error error)
DeleteAllPlaylistSongs(playlistId int) (error error)
DeletePlaylistSong(playlistId int, songId int) (error error)
}
type PlaylistsongTable struct {
BaseTable
}
func NewPlaylistsongTableInstance() IPlaylistsongTable {
return &PlaylistsongTable{
BaseTable: NewBaseTableInstance(),
}
}
func (table *PlaylistsongTable) FetchPlaylistSongs(ctx context.Context, playlistId int, lastKnowPosition int) (songs []models.Song, error error) {
query := `SELECT s.Id, s.Name, s.Path, s.ThumbnailPath, s.Duration, s.SourceId, s.UpdatedAt, s.CreatedAt FROM playlistsong ps
INNER JOIN song s ON s.id = ps.songid
WHERE ps.playlistid = $1 AND ps.position >= $2`
rows, err := table.QueryRowsContex(ctx, query, playlistId, lastKnowPosition)
if err != nil {
logging.Error(fmt.Sprintf("QueryRow error: %s", err.Error()))
return nil, err
}
defer rows.Close()
var song models.Song
songs = make([]models.Song, 0)
for rows.Next() {
scanError := rows.Scan(&song.Id, &song.Name, &song.Path, &song.ThumbnailPath, &song.Duration, &song.SourceId, &song.UpdatedAt, &song.CreatedAt)
if scanError != nil {
logging.Error(fmt.Sprintf("Scan error: %s", scanError.Error()))
continue
}
songs = append(songs, song)
}
return songs, nil
}
func (table *PlaylistsongTable) InsertPlaylistSong(playlistId int, songId int) (lastInsertedId int, error error) {
query := `INSERT INTO PlaylistSong (SongId, PlaylistId) VALUES($1, $2) RETURNING SongId`
lastInsertedId, err := table.InsertWithReturningId(query,
songId,
playlistId,
)
return lastInsertedId, err
}
func (table *PlaylistsongTable) DeleteAllPlaylistSongs(playlistId int) (error error) {
query := `DELETE FROM PlaylistSong WHERE PlaylistId = $1`
err := table.NonScalarQuery(query, playlistId)
return err
}
func (table *PlaylistsongTable) DeletePlaylistSong(playlistId int, songId int) (error error) {
query := `DELETE FROM PlaylistSong WHERE PlaylistId = $1 and SongId = $2`
err := table.NonScalarQuery(query, playlistId, songId)
return err
}