Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MyMusicBoxApi/database/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"musicboxapi/models"
)

func (pdb *PostgresDb) FetchPlaylists(ctx context.Context) (playlists []models.Playlist, error error) {
query := "SELECT Id, Name, ThumbnailPath, Description, CreationDate FROM Playlist" // order by?
func (pdb *PostgresDb) FetchPlaylists(ctx context.Context, lastKnowPlaylistId int) (playlists []models.Playlist, error error) {
query := "SELECT Id, Name, ThumbnailPath, Description, CreationDate FROM Playlist WHERE Id > $1 ORDER BY Id" // order by?

rows, err := pdb.connection.QueryContext(ctx, query)
rows, err := pdb.connection.QueryContext(ctx, query, lastKnowPlaylistId)
defer rows.Close()

if err != nil {
Expand Down
16 changes: 10 additions & 6 deletions MyMusicBoxApi/database/playlistsong.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (
"musicboxapi/models"
)

func (pdb *PostgresDb) FetchPlaylistSongs(ctx context.Context, playlistId int) (songs []models.Song, error error) {
func (pdb *PostgresDb) 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, CreatedAt FROM Song s
INNER JOIN PlaylistSong ps ON ps.PlaylistId = $1
WHERE ps.SongId = s.Id
order by ps.Position` // order by playlist position
// query := `SELECT s.Id, s.Name, s.Path, s.ThumbnailPath, s.Duration, s.SourceId, s.UpdatedAt, CreatedAt FROM Song s
// INNER JOIN PlaylistSong ps ON ps.PlaylistId = $1
// WHERE ps.SongId = s.Id
// order by ps.Position` // order by playlist position

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`

statement, err := pdb.connection.Prepare(query)
defer statement.Close()
Expand All @@ -22,7 +26,7 @@ func (pdb *PostgresDb) FetchPlaylistSongs(ctx context.Context, playlistId int) (
return nil, err
}

rows, err := statement.QueryContext(ctx, playlistId)
rows, err := statement.QueryContext(ctx, playlistId, lastKnowPosition)
defer rows.Close()

if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions MyMusicBoxApi/http/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ func FetchPlaylists(ctx *gin.Context) {
db := database.PostgresDb{}
defer db.CloseConnection()

lastKnowPlaylistIdQuery := ctx.Query("lastKnowPlaylistId")

lastKnowPlaylistId := 0

if lastKnowPlaylistIdQuery != "" {
lastKnowPlaylistId, _ = strconv.Atoi(lastKnowPlaylistIdQuery)
}

if db.OpenConnection() {
playlists, err := db.FetchPlaylists(ctx.Request.Context())
playlists, err := db.FetchPlaylists(ctx.Request.Context(), lastKnowPlaylistId)
if err != nil {
ctx.JSON(500, models.ErrorResponse(err))
return
Expand All @@ -31,6 +39,14 @@ func FetchPlaylistSongs(ctx *gin.Context) {

playlistIdParameter := ctx.Param("playlistId")

lastKnowSongPosition := 0

lastKnowSongPositionQuery := ctx.Query("lastKnowSongPosition")

if lastKnowSongPositionQuery != "" {
lastKnowSongPosition, _ = strconv.Atoi(lastKnowSongPositionQuery)
}

if playlistIdParameter == "" {
ctx.JSON(500, models.ErrorResponse("No playlistId inrequest"))
return
Expand All @@ -39,7 +55,7 @@ func FetchPlaylistSongs(ctx *gin.Context) {
playlistId, _ := strconv.Atoi(playlistIdParameter)

if db.OpenConnection() {
songs, err := db.FetchPlaylistSongs(ctx.Request.Context(), playlistId)
songs, err := db.FetchPlaylistSongs(ctx.Request.Context(), playlistId, lastKnowSongPosition)
if err != nil {
ctx.JSON(500, models.ErrorResponse(err))
return
Expand Down
2 changes: 1 addition & 1 deletion MyMusicBoxApi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {

engine := gin.Default()

engine.SetTrustedProxies(nil)
engine.SetTrustedProxies([]string{"127.0.0.1"})

if configuration.Config.UseDevUrl {
engine.Use(cors.Default())
Expand Down
2 changes: 1 addition & 1 deletion MyMusicBoxApi/service/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func downloadPlaylist(
defer db.CloseConnection()

// Check if exists, if not then create
existingPlaylists, _ := db.FetchPlaylists(context.Background())
existingPlaylists, _ := db.FetchPlaylists(context.Background(), 0)

playlistExists := false
playlistId := -1
Expand Down