public HttpResponseMessage AddSong(int id, PlaylistSongDto playlistSongDto) { try { using (var unitOfWork = new UnitOfWork()) { var playlist = unitOfWork.PlaylistRepository.Get(id); var song = unitOfWork.SongRepository.Get(playlistSongDto.Song.Id); var playlistSong = new PlaylistSong { Playlist = playlist, Song = song, Position = playlistSongDto.Position }; unitOfWork.PlaylistSongRepository.Save(playlistSong); unitOfWork.Commit(); playlistSongDto.Id = playlistSong.Id; return(Request.CreateResponse(HttpStatusCode.Created, playlistSongDto)); } } catch (Exception ex) { IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public async Task <IActionResult> AddSong(int pPlaylistId, string pSongGuid) { if (pPlaylistId <= 0) { return(BadRequest()); } if (String.IsNullOrEmpty(pSongGuid)) { return(BadRequest()); } AbstractSongDto video; video = await _youtubeSearch.GetByYoutubeId(pSongGuid); AbstractSongDto song = new PlaylistSongDto(); if (video != null) { song.YouTubeId = pSongGuid; song.PlaylistId = pPlaylistId; song.Artist = video.Artist; song.Name = video.Name; song.UserId = SessionState.GetCurrUserID(User); song.Id = await _playlistSongEntity.Add(song); } return(Ok(song)); }
private async Task AddSong(object item, SongDto selectedSong) { var playlistSongDto = new PlaylistSongDto { Song = selectedSong, Position = _playlist.Count + 1 }; using (var client = new HttpClient()) { InitializeClient(client); var response = await client.PostAsJsonAsync <PlaylistSongDto>(string.Format("playlists/{0}/songs", _selectedPlaylist.Id), playlistSongDto).ConfigureAwait(false); if (response.IsSuccessStatusCode) { lstPlaylist.Items.Add(item); _playlist.Add(playlistSongDto); lblPlaylistSongs.Text = string.Format("Total {0} songs", _playlist.Count); } else { ShowErrorMessage(GetErrorMessageFromResponse(response)); } } }
public HttpResponseMessage NextShuffleSong(int id, int songId) { try { using (var unitOfWork = new UnitOfWork()) { var playlist = unitOfWork.PlaylistRepository.Get(id); var songCount = unitOfWork.PlaylistSongRepository.Count(); PlaylistSong nextSong = null; while (nextSong == null) { int nextSongPosition = new Random().Next(0, songCount - 1); nextSong = playlist.PlaylistSongs.ElementAt(nextSongPosition); if (nextSong.Song.Id == songId) { nextSong = null; } } PlaylistSongDto playlistSongDto = null; if (nextSong != null) { playlistSongDto = new PlaylistSongDto { Id = nextSong.Id, Song = new SongDto { Id = nextSong.Song.Id, Album = nextSong.Song.Album, Artist = nextSong.Song.Artist, Bitrate = nextSong.Song.Bitrate, Duration = nextSong.Song.Duration, Genre = nextSong.Song.Genre, Title = nextSong.Song.Title, Year = nextSong.Song.Year ?? 0 }, Position = nextSong.Position }; } return(Request.CreateResponse(HttpStatusCode.OK, playlistSongDto)); } } catch (Exception ex) { IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public async Task <string> SearchFirstByKeyword(string p_searchKeyword) { string returnVideoID = ""; var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = _ApiKey, ApplicationName = _ApplicationName }); var searchListRequest = youtubeService.Search.List("snippet"); searchListRequest.Q = p_searchKeyword; searchListRequest.MaxResults = MaxResults; var searchListResponse = await searchListRequest.ExecuteAsync(); List <string> videos = new List <string>(); List <AbstractSongDto> videosList = new List <AbstractSongDto>(); // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach (var searchResult in searchListResponse.Items) { switch (searchResult.Id.Kind) { case "youtube#video": videos.Add(searchResult.Id.VideoId); AbstractSongDto foundSong = new PlaylistSongDto(); foundSong.YouTubeId = searchResult.Id.VideoId; videosList.Add(foundSong); break; } } if (videos.Count > 0) { returnVideoID = videos[0]; } return(returnVideoID); }
public async Task <IHttpActionResult> PostPlaylistSongDto(object playlistSongDto) { var action = (playlistSongDto as Newtonsoft.Json.Linq.JObject).SelectToken("Action").ToString(); if (action == "add") { var newPlaylistSongDto = new PlaylistSongDto(playlistSongDto); newPlaylistSongDto = new PlaylistSongDto(Operations.AddSongToPlaylist(newPlaylistSongDto.UserId, newPlaylistSongDto.PlaylistId, newPlaylistSongDto.SongId)); return(CreatedAtRoute("DefaultApi", new { id = newPlaylistSongDto.Id }, newPlaylistSongDto)); } if (action == "play") { var playlistSongId = int.Parse((playlistSongDto as Newtonsoft.Json.Linq.JObject).SelectToken("SongId").ToString()); var playlistSong = new PlaylistSongDto(Operations.PlayPlaylistSong(playlistSongId)); return(CreatedAtRoute("DefaultApi", new { id = playlistSong.Id }, playlistSong)); } return(CreatedAtRoute("DefaultApi", null, new PlaylistSongDto(null))); }
public HttpResponseMessage NextSong(int id, int songId) { try { PlaylistSongDto playlistSongDto = null; using (var unitOfWork = new UnitOfWork()) { var playlistSong = unitOfWork.PlaylistSongRepository.Get(filter: p => p.SongId == songId && p.PlaylistId == id).FirstOrDefault(); var nextSong = unitOfWork.PlaylistSongRepository.Get(filter: p => p.PlaylistId == id && p.Position == playlistSong.Position + 1).FirstOrDefault(); if (nextSong != null) { playlistSongDto = new PlaylistSongDto { Id = nextSong.Id, Song = new SongDto { Id = nextSong.Song.Id, Album = nextSong.Song.Album, Artist = nextSong.Song.Artist, Bitrate = nextSong.Song.Bitrate, Duration = nextSong.Song.Duration, Genre = nextSong.Song.Genre, Title = nextSong.Song.Title, Year = nextSong.Song.Year ?? 0 }, Position = nextSong.Position }; } } return(Request.CreateResponse(HttpStatusCode.OK, playlistSongDto)); } catch (Exception ex) { IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
/* * // need to finish this * public async Task<List<AbstractSongDto>> GetByYoutubeId(string p_youtubeId) * { * List<AbstractSongDto> videosList = new List<AbstractSongDto>(); * * try * { * string returnVideoID = ""; * * var youtubeService = new YouTubeService(new BaseClientService.Initializer() * { * ApiKey = _ApiKey, * ApplicationName = _ApplicationName * }); * * var searchListRequest = youtubeService.Videos.List("snippet"); //.Search. .List("snippet"); * searchListRequest.Id = p_youtubeId; * searchListRequest.MaxResults = 10; * * // Call the search.list method to retrieve results matching the specified query term. * var searchListResponse = await searchListRequest.ExecuteAsync(); * * List<string> videos = new List<string>(); * * * // Add each result to the appropriate list, and then display the lists of * // matching videos, channels, and playlists. * foreach (var searchResult in searchListResponse.Items) * { * AbstractSongDto foundSong = new PlaylistSongDto(); * foundSong.YouTubeId = searchResult.Id; * * char[] spliOption = { '-' }; * string[] songInfoArr = searchResult.Snippet.Title.Split(spliOption); * string songArtist = ""; * string songName = ""; * * if(songInfoArr.Length == 2) * { * songArtist = songInfoArr[0]; * songName = songInfoArr[1]; * } * else * { * songArtist = searchResult.Snippet.Title; * } * * //will need to strip title to song artist and name * foundSong.Artist = songArtist; * foundSong.Name = songName; * * videosList.Add(foundSong); * } * * * if (videos.Count > 0) * { * returnVideoID = videos[0]; * } * * } * catch (Exception ex) * { * //TODO: log exception * } * * * return videosList; * } * * * * public async Task<AbstractSongDto> SearchSong(string pSongId) * { * * }*/ public async Task <AbstractSongDto> GetByYoutubeId(string pYoutubeId) { if (string.IsNullOrEmpty(pYoutubeId)) { throw new ArgumentOutOfRangeException(); } var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = _ApiKey, ApplicationName = _ApplicationName }); AbstractSongDto foundSong = new PlaylistSongDto(); foundSong.YouTubeId = pYoutubeId; //------------------------------------------------------------ var videoRequest = youtubeService.Videos.List("snippet,statistics,contentDetails"); videoRequest.Id = pYoutubeId; videoRequest.MaxResults = 1; var videoItemRequestResponse = await videoRequest.ExecuteAsync(); // Get the videoID of the first video in the list var video = videoItemRequestResponse.Items[0]; //will need to strip title to song artist and name foundSong.Artist = video.Snippet.Title; foundSong.Duration = video.ContentDetails.Duration.Replace("PT", "").Replace("M", ":").Replace("S", ""); foundSong.ViewsNum = video.Statistics.ViewCount == null ? "" : MusicHelper.ConvertViews((ulong)video.Statistics.ViewCount); foundSong.LikesNum = video.Statistics.LikeCount == null ? "" : MusicHelper.ConvertLikes((ulong)video.Statistics.LikeCount); //------------------------------------------------------------ return(foundSong); }
/* * public async Task<AbstractSearchResultDto> SearchListByKeyword(string p_searchKeyword) * { * AbstractSearchResultDto videosList = new Playlist.Zone.Dto.Music.SearchResult.SearchResultDto(); * * try * { * var youtubeService = new YouTubeService(new BaseClientService.Initializer() * { * ApiKey = _ApiKey, * ApplicationName = _ApplicationName * }); * * var searchListRequest = youtubeService.Search.List("snippet"); //("snippet"); * searchListRequest.Q = p_searchKeyword; * searchListRequest.MaxResults = 10; * * var searchListResponse = await searchListRequest.ExecuteAsync(); * * videosList.NextPageToken = searchListResponse.NextPageToken; * * foreach (var searchResult in searchListResponse.Items) * { * switch (searchResult.Id.Kind) * { * case "youtube#video": * * AbstractSongDto foundSong = new PlaylistSongDto(); * foundSong.YouTubeId = searchResult.Id.VideoId; * * //will need to strip title to song artist and name * foundSong.Artist = searchResult.Snippet.Title; * * * //------------------------------------------------------------ * var videoRequest = youtubeService.Videos.List("snippet,statistics,contentDetails"); * videoRequest.Id = searchResult.Id.VideoId; * videoRequest.MaxResults = 1; * var videoItemRequestResponse = await videoRequest.ExecuteAsync(); * * // Get the videoID of the first video in the list * var video = videoItemRequestResponse.Items[0]; * //foundSong.Duration = video.ContentDetails.Duration; * foundSong.Duration = video.ContentDetails.Duration.Replace("PT", "").Replace("M", ":").Replace("S", ""); * foundSong.ViewsNum = video.Statistics.ViewCount == null ? 0 : (ulong)video.Statistics.ViewCount; * foundSong.LikesNum = video.Statistics.LikeCount == null ? 0 : (ulong)video.Statistics.ViewCount; * //------------------------------------------------------------ * * * videosList.SongsList.Add(foundSong); * break; * } * } * * * } * catch (Exception ex) * { * throw new Exception("Search Exception", ex); * } * * * return videosList; * }*/ public async Task <AbstractSearchResultDto> SearchListByKeyword(string pSearchKeyword, string pPageToken = "") { AbstractSearchResultDto videosList = new Playlist.Zone.Dto.Music.SearchResult.SearchResultDto(); try { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = _ApiKey, ApplicationName = _ApplicationName }); var searchListRequest = youtubeService.Search.List("id"); searchListRequest.Q = pSearchKeyword; if (pPageToken != string.Empty) { searchListRequest.PageToken = pPageToken; } searchListRequest.MaxResults = MaxResults; // Call the search.list method to retrieve results matching the specified query term. var searchListResponse = await searchListRequest.ExecuteAsync(); videosList.NextPageToken = searchListResponse.NextPageToken; // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach (var searchResult in searchListResponse.Items) { switch (searchResult.Id.Kind) { case "youtube#video": AbstractSongDto foundSong = new PlaylistSongDto(); var videoRequest = youtubeService.Videos.List("snippet,statistics,contentDetails"); videoRequest.Id = searchResult.Id.VideoId; videoRequest.MaxResults = 1; var videoItemRequestResponse = await videoRequest.ExecuteAsync(); // Get the videoID of the first video in the list var video = videoItemRequestResponse.Items[0]; //will need to strip title to song artist and name foundSong.YouTubeId = searchResult.Id.VideoId; foundSong.Artist = video.Snippet.Title; foundSong.Duration = video.ContentDetails.Duration.Replace("PT", "").Replace("M", ":").Replace("S", ""); foundSong.ViewsNum = video.Statistics.ViewCount == null ? "" : MusicHelper.ConvertViews((ulong)video.Statistics.ViewCount); foundSong.LikesNum = video.Statistics.LikeCount == null ? "" : MusicHelper.ConvertLikes((ulong)video.Statistics.LikeCount); videosList.SongsList.Add(foundSong); break; } } } catch (Exception ex) { throw new Exception("Search Exception", ex); } return(videosList); }