public IHttpActionResult getNextTrack(long playlistId)
        {
            var userIdentity = RequestContext.Principal.Identity as ClaimsIdentity;

            if (userIdentity == null)
            {
                return(InternalServerError());
            }

            var email = userIdentity.Claims.First(c => c.Type == "sub").Value;

            if (email == null)
            {
                return(InternalServerError());
            }

            var user = userManager.ReadUser(email);

            if (user == null)
            {
                return(InternalServerError());
            }

            if (AssignPlaylistMaster(playlistId, user.Id))
            {
                var playlistTracks = playlistManager.ReadPlaylist(playlistId).PlaylistTracks
                                     .Where(t => t.PlayedAt == null);

                if (!playlistTracks.Any())
                {
                    return(NotFound());
                }

                var sortedByVotesPlaylistTracks = playlistTracks.ToList();
                sortedByVotesPlaylistTracks.Sort((t1, t2) => t2.Votes.Sum(v => v.Score) - t1.Votes.Sum(v => v.Score));


                var originalPlayListTrack = sortedByVotesPlaylistTracks.First(t => t.PlayedAt == null);
                var newTrack = GetTrackWithFreshYoutubeUrl(originalPlayListTrack.Track);

                var success = playlistManager.MarkTrackAsPlayed(originalPlayListTrack.Id, playlistId);
                if (success)
                {
                    return(Ok(newTrack));
                }
                else
                {
                    return(InternalServerError(new Exception("Could not mark track as played.")));
                }
            }
            else
            {
                return(BadRequest("Playlistmaster already set or you are not the organiser or co-organiser."));
            }
        }
        public void GetLivePlaylistWithOneTrackPlayed()
        {
            var playedTrack = playlist.PlaylistTracks.First();

            playlistManager.MarkTrackAsPlayed(playedTrack.Id, playlist.Id);
            var notPlayedTrack = playlist.PlaylistTracks.Last();

            List <LivePlaylistTrackViewModel> livePlaylistTracks = new List <LivePlaylistTrackViewModel>();

            livePlaylistTracks.Add(
                new LivePlaylistTrackViewModel()
            {
                Id            = notPlayedTrack.Id,
                Track         = notPlayedTrack.Track,
                Score         = 0,
                PersonalScore = 0
            });

            playlistControllerWithAuthenticatedUser
            .Calling(p => p.getLivePlaylist(playlist.Id))
            .ShouldReturn()
            .Ok()
            .WithResponseModel(new LivePlaylistViewModel()
            {
                Id                  = playlist.Id,
                PlaylistTracks      = livePlaylistTracks,
                Active              = playlist.Active,
                ChatComments        = playlist.ChatComments,
                Comments            = playlist.Comments,
                CreatedById         = playlist.CreatedById,
                Description         = playlist.Description,
                ImageUrl            = playlist.ImageUrl,
                Key                 = playlist.Key,
                MaximumVotesPerUser = playlist.MaximumVotesPerUser,
                PlaylistMasterId    = playlist.PlaylistMasterId,
                Name                = playlist.Name
            });
        }
        public ActionResult MoveTrackToHistory(long id)
        {
            var tracks = playlistManager.ReadPlaylist(id).PlaylistTracks;

            if (tracks.Count == 0)
            {
                return(new HttpStatusCodeResult(400));
            }
            var track = tracks.OrderByDescending(p => p.Votes.Sum(v => v.Score)).First(t => t.PlayedAt == null);

            playlistManager.MarkTrackAsPlayed(track.Id, id);

            return(new HttpStatusCodeResult(200));
        }