| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package daytask
- import (
- "app/commons/model/entity"
- "github.com/gin-gonic/gin"
- )
- // MusicGroups 获取歌单列表(含歌曲)
- func (s *Server) MusicGroups(c *gin.Context) {
- ctx := s.FromContext(c)
- db := s.DB()
- if db == nil {
- ctx.Fail("数据库连接失败")
- return
- }
- type GroupWithSongs struct {
- entity.DtMusicGroup
- Songs []entity.DtMusic `json:"songs"`
- }
- // 获取所有启用的歌单
- groups := make([]*entity.DtMusicGroup, 0)
- db.Model(&entity.DtMusicGroup{}).
- Where("status = ?", 1).
- Order("sort ASC, id ASC").
- Find(&groups)
- // 获取所有启用的歌曲
- songs := make([]*entity.DtMusic, 0)
- db.Model(&entity.DtMusic{}).
- Where("status = ?", 1).
- Order("sort ASC, id ASC").
- Find(&songs)
- // 按歌单分组
- songMap := make(map[int64][]entity.DtMusic)
- for _, song := range songs {
- songMap[song.GroupId] = append(songMap[song.GroupId], *song)
- }
- result := make([]GroupWithSongs, 0)
- for _, group := range groups {
- item := GroupWithSongs{
- DtMusicGroup: *group,
- Songs: songMap[group.Id],
- }
- if item.Songs == nil {
- item.Songs = make([]entity.DtMusic, 0)
- }
- result = append(result, item)
- }
- ctx.OK(result)
- }
|