-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.go
More file actions
123 lines (92 loc) · 3.08 KB
/
playlist.go
File metadata and controls
123 lines (92 loc) · 3.08 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package http
import (
"fmt"
"musicboxapi/configuration"
"musicboxapi/database"
"musicboxapi/logging"
"musicboxapi/models"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type PlaylistHandler struct {
PlaylistTable database.IPlaylistTable
}
// @Produce json
// @Param lastKnowPlaylistId path int false "Last know playlist id by the client, default is 0"
// @Description Returns data for all playlist, if lastKnowPlaylistId then only the playlist after lastKnowPlaylistId
// @Success 200 {object} models.Playlist
// @Failure 500 {object} models.ApiResponseModel
// @Router /api/v1/playlist [get]
func (handler *PlaylistHandler) FetchPlaylists(ctx *gin.Context) {
lastKnowPlaylistIdQuery := ctx.Query("lastKnowPlaylistId")
lastKnowPlaylistId := 0
if lastKnowPlaylistIdQuery != "" {
lastKnowPlaylistId, _ = strconv.Atoi(lastKnowPlaylistIdQuery)
}
playlists, err := handler.PlaylistTable.FetchPlaylists(ctx.Request.Context(), lastKnowPlaylistId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err))
return
}
ctx.JSON(http.StatusOK, models.OkResponse(playlists, fmt.Sprintf("Found %d playlist", len(playlists))))
}
func (hanlder *PlaylistHandler) InsertPlaylist(ctx *gin.Context) {
var playlistModel models.CreatePlaylistModel
err := ctx.ShouldBind(&playlistModel)
if err != nil {
logging.Error("Failed to bind model")
logging.ErrorStackTrace(err)
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err))
return
}
var playlist models.Playlist
var fileName string
hasFormFile := playlistModel.Image.Size > 0
if hasFormFile {
fileName = fmt.Sprintf("%s.jpg", uuid.New().String())
playlist.ThumbnailPath = fileName
} else {
// default_playlist_cover.jpg
playlist.ThumbnailPath = "default_playlist_cover.jpg"
}
playlist.Name = playlistModel.Name
playlist.Description = playlistModel.Description
if strings.Contains(playlistModel.IsPublic, "on") {
playlist.IsPublic = true
}
playlistId, err := hanlder.PlaylistTable.InsertPlaylist(playlist)
if err != nil {
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err))
return
}
if hasFormFile {
path := filepath.Join(configuration.Config.SourceFolder, fmt.Sprintf("images/%s", fileName))
logging.Info(path)
err = ctx.SaveUploadedFile(playlistModel.Image, path)
if err != nil {
logging.ErrorStackTrace(err)
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err))
return
}
}
ctx.JSON(http.StatusOK, models.OkResponse(gin.H{"playlistId": playlistId}, "Created new playlist"))
}
func (handler *PlaylistHandler) DeletePlaylist(ctx *gin.Context) {
playlistIdParameter := ctx.Param("playlistId")
id, err := strconv.Atoi(playlistIdParameter)
if err != nil {
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err))
return
}
err = handler.PlaylistTable.DeletePlaylist(id)
// TODO delete background image if its not the default image for it
if err != nil {
ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err))
return
}
ctx.Status(http.StatusOK)
}