public IHttpActionResult getLivePlaylist(long id)
{
var userIdentity = RequestContext.Principal.Identity as ClaimsIdentity;
if (userIdentity == null) return NotFound();
var email = userIdentity.Claims.First(c => c.Type == "sub").Value;
if (email == null) return NotFound();
var playlist = playlistManager.ReadPlaylist(id);
if (playlist == null) return NotFound();
List<LivePlaylistTrackViewModel> livePlaylistTracks = new List<LivePlaylistTrackViewModel>();
foreach (var playlistTrack in playlist.PlaylistTracks.Where(t => t.PlayedAt == null))
{
var score = playlistTrack.Votes.FirstOrDefault(v => v.User.Email == email)?.Score;
livePlaylistTracks.Add(new LivePlaylistTrackViewModel()
{
Id = playlistTrack.Id,
Track = playlistTrack.Track,
Score = playlistTrack.Votes.Sum(v => v.Score),
PersonalScore = score ?? 0
});
}
livePlaylistTracks.Sort((t1, t2) => t2.Score - t1.Score);
var livePlaylist = 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
};
return Ok(livePlaylist);
}