Skip to content

Commit 034dff1

Browse files
committed
Merge branch 'feature/delete-songs' of https://github.com/DutchJavaDev/MyMusicBox into feature/delete-songs
2 parents f82a731 + 21f9c52 commit 034dff1

7 files changed

Lines changed: 16 additions & 9 deletions

File tree

MyMusicBoxApi/database/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func CreateDatabasConnectionPool() error {
4545
// Should create test for these?
4646
var _ ISongTable = (*SongTable)(nil)
4747
var _ IPlaylistTable = (*PlaylistTable)(nil)
48-
var _ IPlaylistsongTable = (*PlaylistsongTable)(nil)
48+
var _ IPlaylistSongTable = (*PlaylistsongTable)(nil)
4949
var _ ITasklogTable = (*TasklogTable)(nil)
5050
var _ IMigrationTable = (*MigrationTable)(nil)
5151

MyMusicBoxApi/database/playlistsongtable.go

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

10-
type IPlaylistsongTable interface {
10+
type IPlaylistSongTable interface {
1111
FetchPlaylistSongs(ctx context.Context, playlistId int, lastKnowPosition int) (songs []models.Song, error error)
1212
InsertPlaylistSong(playlistId int, songId int) (lastInsertedId int, error error)
1313
DeleteAllPlaylistSongs(playlistId int) (error error)
@@ -18,7 +18,7 @@ type PlaylistsongTable struct {
1818
BaseTable
1919
}
2020

21-
func NewPlaylistsongTableInstance() IPlaylistsongTable {
21+
func NewPlaylistsongTableInstance() IPlaylistSongTable {
2222
return &PlaylistsongTable{
2323
BaseTable: NewBaseTableInstance(),
2424
}

MyMusicBoxApi/database/playlisttable.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewPlaylistTableInstance() IPlaylistTable {
2424
}
2525

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

2929
rows, err := table.QueryRowsContex(ctx, query, lastKnowPlaylistId)
3030

@@ -41,7 +41,7 @@ func (table *PlaylistTable) FetchPlaylists(ctx context.Context, lastKnowPlaylist
4141
playlists = make([]models.Playlist, 0)
4242

4343
for rows.Next() {
44-
scanError := rows.Scan(&playlist.Id, &playlist.Name, &playlist.ThumbnailPath, &playlist.Description, &playlist.CreationDate)
44+
scanError := rows.Scan(&playlist.Id, &playlist.Name, &playlist.ThumbnailPath, &playlist.Description, &playlist.CreationDate, &playlist.IsPublic)
4545

4646
if scanError != nil {
4747
logging.Error(fmt.Sprintf("Scan error: %s", scanError.Error()))
@@ -68,9 +68,9 @@ func (table *PlaylistTable) InsertPlaylist(playlist models.Playlist) (lastInsert
6868
}
6969

7070
func (table *PlaylistTable) DeletePlaylist(playlistId int) (error error) {
71-
query := `DELETE FROM Playlist WHERE Id = $1`
71+
query := `DELETE FROM Playlist WHERE Id = $1 AND IsPublic = $2` // Prevemts private playlists (like the default one) from being deleted for real
7272

73-
err := table.NonScalarQuery(query, playlistId)
73+
err := table.NonScalarQuery(query, playlistId, true)
7474

7575
if err != nil {
7676
logging.Error(fmt.Sprintf("Failed to delete playlist: %s", err.Error()))

MyMusicBoxApi/http/playlist.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func (handler *PlaylistHandler) DeletePlaylist(ctx *gin.Context) {
110110
return
111111
}
112112

113+
if DefaultPlaylistId == id {
114+
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse("Funky music... "))
115+
return
116+
}
117+
113118
err = handler.PlaylistTable.DeletePlaylist(id)
114119

115120
// TODO delete background image if its not the default image for it

MyMusicBoxApi/http/playlistsong.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
type PlaylistSongHandler struct {
18-
PlaylistsongTable database.IPlaylistsongTable
18+
PlaylistsongTable database.IPlaylistSongTable
1919
}
2020

2121
const DefaultPlaylistId = 1
@@ -106,6 +106,7 @@ func (handler *PlaylistSongHandler) DeletePlaylistSong(ctx *gin.Context) {
106106
return
107107
}
108108

109+
// Thumbnail and .opus file will be deleted only if you delete a song via the main playlist containing all the songs
109110
if playlistId == DefaultPlaylistId {
110111

111112
songTable := database.NewSongTableInstance()

MyMusicBoxApi/http/playlistsong_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type mockPlaylistSongTable struct {
16-
database.IPlaylistsongTable
16+
database.IPlaylistSongTable
1717
fetchPlaylistSongs func(ctx context.Context, playlistId int, lastKnowPosition int) (songs []models.Song, error error)
1818
insertPlaylistSong func(playlistId int, songId int) (lastInsertedId int, error error)
1919
deleteAllPlaylistSongs func(playlistId int) (error error) // TODO

MyMusicBoxApi/models/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ApiResponseModel struct {
1919
}
2020

2121
func ErrorResponse(data any) ApiResponseModel {
22+
2223
return ApiResponseModel{
2324
Data: data,
2425
Message: "An error occurred",

0 commit comments

Comments
 (0)