-
Notifications
You must be signed in to change notification settings - Fork 0
/
song.go
79 lines (73 loc) · 1.81 KB
/
song.go
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
package controller
import (
"github.com/96368a/LuoYiMusic-server-api/dto"
services "github.com/96368a/LuoYiMusic-server-api/services"
"github.com/96368a/LuoYiMusic-server-api/utils"
"github.com/96368a/LuoYiMusic-server-api/vo"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
func SongById(c *gin.Context) {
songId := c.Query("id")
id, err := strconv.ParseInt(songId, 10, 64)
if err != nil {
utils.Fail(c, http.StatusInternalServerError, "内部错误", nil)
return
}
song, err := services.SongById(uint64(id))
if err != nil {
utils.Fail(c, http.StatusInternalServerError, "内部错误1", nil)
return
}
c.Header("Content-Type", "audio/mpeg")
c.File("resources/musics/" + song.Hash)
}
func SearchSongs(c *gin.Context) {
var page dto.Page
err := c.ShouldBind(&page)
if err != nil {
utils.Fail(c, http.StatusBadRequest, "参数错误", nil)
return
}
if page.PageSize == 0 {
page.PageSize = 5
}
name := c.Query("name")
//if name == "" {
// utils.Fail(c, 400, "参数错误", nil)
// return
//}
songs, count, err := services.SearchSong(name, page.PageSize, page.Page)
if err != nil {
utils.Fail(c, 500, "内部错误", nil)
return
}
utils.Success(c, gin.H{
"data": vo.ToSongInfoVos(songs),
"currentPage": page.Page,
"pageSize": page.PageSize,
"total": count,
}, "获取成功")
}
func SongNews(c *gin.Context) {
var page dto.Page
err := c.ShouldBind(&page)
if err != nil {
utils.Fail(c, http.StatusBadRequest, "参数错误", nil)
return
}
if page.PageSize == 0 {
page.PageSize = 5
}
songs, err := services.SongNews(page.PageSize, page.Page)
if err != nil {
utils.Fail(c, 500, "内部错误", nil)
return
}
utils.Success(c, gin.H{
"data": vo.ToSongInfoVos(songs),
"currentPage": page.Page,
"pageSize": page.PageSize,
}, "获取成功")
}