Beispiel #1
0
func UpdatePlaylist(cmd *m.UpdatePlaylistCommand) error {
	var err error
	playlist := m.Playlist{
		Id:       cmd.Id,
		OrgId:    cmd.OrgId,
		Name:     cmd.Name,
		Interval: cmd.Interval,
	}

	existingPlaylist := x.Where("id = ? AND org_id = ?", cmd.Id, cmd.OrgId).Find(m.Playlist{})

	if existingPlaylist == nil {
		return m.ErrPlaylistNotFound
	}

	cmd.Result = &m.PlaylistDTO{
		Id:       playlist.Id,
		OrgId:    playlist.OrgId,
		Name:     playlist.Name,
		Interval: playlist.Interval,
	}

	_, err = x.Id(cmd.Id).Cols("id", "name", "interval").Update(&playlist)

	if err != nil {
		return err
	}

	rawSql := "DELETE FROM playlist_item WHERE playlist_id = ?"
	_, err = x.Exec(rawSql, cmd.Id)

	if err != nil {
		return err
	}

	playlistItems := make([]m.PlaylistItem, 0)

	for _, item := range cmd.Items {
		playlistItems = append(playlistItems, m.PlaylistItem{
			PlaylistId: playlist.Id,
			Type:       item.Type,
			Value:      item.Value,
			Order:      item.Order,
			Title:      item.Title,
		})
	}

	_, err = x.Insert(&playlistItems)

	return err
}
Beispiel #2
0
func UpdatePlaylist(c *middleware.Context, cmd m.UpdatePlaylistCommand) Response {
	cmd.OrgId = c.OrgId

	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to save playlist", err)
	}

	playlistDTOs, err := LoadPlaylistItemDTOs(cmd.Id)
	if err != nil {
		return ApiError(500, "Failed to save playlist", err)
	}

	cmd.Result.Items = playlistDTOs
	return Json(200, cmd.Result)
}