Skip to content

Commit ca12d4a

Browse files
authored
Feature/background fetch support (#27)
* trust localhost * background fetch
1 parent 87c23b4 commit ca12d4a

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

MyMusicBoxApi/database/playlist.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"musicboxapi/models"
88
)
99

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

13-
rows, err := pdb.connection.QueryContext(ctx, query)
13+
rows, err := pdb.connection.QueryContext(ctx, query, lastKnowPlaylistId)
1414
defer rows.Close()
1515

1616
if err != nil {

MyMusicBoxApi/database/playlistsong.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import (
77
"musicboxapi/models"
88
)
99

10-
func (pdb *PostgresDb) FetchPlaylistSongs(ctx context.Context, playlistId int) (songs []models.Song, error error) {
10+
func (pdb *PostgresDb) FetchPlaylistSongs(ctx context.Context, playlistId int, lastKnowPosition int) (songs []models.Song, error error) {
1111

12-
query := `SELECT s.Id, s.Name, s.Path, s.ThumbnailPath, s.Duration, s.SourceId, s.UpdatedAt, CreatedAt FROM Song s
13-
INNER JOIN PlaylistSong ps ON ps.PlaylistId = $1
14-
WHERE ps.SongId = s.Id
15-
order by ps.Position` // order by playlist position
12+
// query := `SELECT s.Id, s.Name, s.Path, s.ThumbnailPath, s.Duration, s.SourceId, s.UpdatedAt, CreatedAt FROM Song s
13+
// INNER JOIN PlaylistSong ps ON ps.PlaylistId = $1
14+
// WHERE ps.SongId = s.Id
15+
// order by ps.Position` // order by playlist position
16+
17+
query := `SELECT s.Id, s.Name, s.Path, s.ThumbnailPath, s.Duration, s.SourceId, s.UpdatedAt, s.CreatedAt FROM playlistsong ps
18+
INNER JOIN song s ON s.id = ps.songid
19+
WHERE ps.playlistid = $1 AND ps.position > $2`
1620

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

25-
rows, err := statement.QueryContext(ctx, playlistId)
29+
rows, err := statement.QueryContext(ctx, playlistId, lastKnowPosition)
2630
defer rows.Close()
2731

2832
if err != nil {

MyMusicBoxApi/http/playlist.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ func FetchPlaylists(ctx *gin.Context) {
1313
db := database.PostgresDb{}
1414
defer db.CloseConnection()
1515

16+
lastKnowPlaylistIdQuery := ctx.Query("lastKnowPlaylistId")
17+
18+
lastKnowPlaylistId := 0
19+
20+
if lastKnowPlaylistIdQuery != "" {
21+
lastKnowPlaylistId, _ = strconv.Atoi(lastKnowPlaylistIdQuery)
22+
}
23+
1624
if db.OpenConnection() {
17-
playlists, err := db.FetchPlaylists(ctx.Request.Context())
25+
playlists, err := db.FetchPlaylists(ctx.Request.Context(), lastKnowPlaylistId)
1826
if err != nil {
1927
ctx.JSON(500, models.ErrorResponse(err))
2028
return
@@ -31,6 +39,14 @@ func FetchPlaylistSongs(ctx *gin.Context) {
3139

3240
playlistIdParameter := ctx.Param("playlistId")
3341

42+
lastKnowSongPosition := 0
43+
44+
lastKnowSongPositionQuery := ctx.Query("lastKnowSongPosition")
45+
46+
if lastKnowSongPositionQuery != "" {
47+
lastKnowSongPosition, _ = strconv.Atoi(lastKnowSongPositionQuery)
48+
}
49+
3450
if playlistIdParameter == "" {
3551
ctx.JSON(500, models.ErrorResponse("No playlistId inrequest"))
3652
return
@@ -39,7 +55,7 @@ func FetchPlaylistSongs(ctx *gin.Context) {
3955
playlistId, _ := strconv.Atoi(playlistIdParameter)
4056

4157
if db.OpenConnection() {
42-
songs, err := db.FetchPlaylistSongs(ctx.Request.Context(), playlistId)
58+
songs, err := db.FetchPlaylistSongs(ctx.Request.Context(), playlistId, lastKnowSongPosition)
4359
if err != nil {
4460
ctx.JSON(500, models.ErrorResponse(err))
4561
return

MyMusicBoxApi/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func main() {
2727

2828
engine := gin.Default()
2929

30-
engine.SetTrustedProxies(nil)
30+
engine.SetTrustedProxies([]string{"127.0.0.1"})
3131

3232
if configuration.Config.UseDevUrl {
3333
engine.Use(cors.Default())

MyMusicBoxApi/service/playlist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func downloadPlaylist(
4949
defer db.CloseConnection()
5050

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

5454
playlistExists := false
5555
playlistId := -1

0 commit comments

Comments
 (0)