public PartialViewResult Search(int id, String selected, String value) { if (selected.Equals("local")) { var ret = SearchLocal(value).Select(x => GetTrack(x)); return PartialView("../Track/_TrackList", ret); } else { WebRequest request = WebRequest.Create("http://ws.spotify.com/search/1/" + "track" + "?q=" + value); request.Method = "Get"; request.ContentType = "application/xml"; WebResponse resptrack = null; try { resptrack = request.GetResponse(); } catch (WebException ex) { RedirectToAction("EditTracks"); } if (((HttpWebResponse)resptrack).StatusCode == HttpStatusCode.OK) { Stream streamresp = resptrack.GetResponseStream(); utils = new UtilsSpotify(); List<Track> tracklist = new List<Track>(); // Parse And Save to DB foreach (var source in utils.GetTracks(streamresp)) { double duration = 0; var arr = source.Split('_'); var name = arr[0]; var artist = arr[1]; var album = arr[2]; double.TryParse(arr[3], out duration); if (!_repo.Albums.Select(x => x.Name).Contains(album)) { _repo.Albums.Add(new Album { Name = album, Artist = artist }); _repo.SaveChanges(); } var aa = _repo.Albums.Single(x=>x.Name.Equals(album)); aa.Tracks = aa.Tracks??new List<TrackList>(); if (!_repo.Tracks.Select(x => x.Name).Contains(name)) { var tt = new Track { Artist = artist, Name = name, Duration = duration, Album = aa }; aa.Tracks.Add(new TrackList { Track=tt, Order=aa.Tracks.Count+1}); tracklist.Add(tt); _repo.Tracks.Add(tt); _repo.SaveChanges(); } } var tracks = tracklist.Select(x => GetTrack(x.Id)); return PartialView("../Track/_TrackList", tracks); } else { return new PartialViewResult(); //return new HttpStatusCodeResult(503, "Spotify is offline"); } } }
public ActionResult Search(string username, int id, String selected, String value) { if (selected.Equals("local")) { IEnumerable<TrackModel> a = _t2mMapper.MapAll(_repo.Tracks.Where(x => x.Name.Contains(value))); if (utils != null) utils.Set(null); return View("EditTracks", new PlaylistAddTracksToUserModel() { User = _repo.UsersInfo.Single(u => u.Username.Equals(username)), Playlist = _p2mMapper.Map(_repo.Playlists.Find(id)), Tracks = _t2mMapper.MapAll(_repo.Tracks.Where(x => x.Name.Contains(value))) // Tracks = new List(); }); } else { WebRequest request = WebRequest.Create("http://ws.spotify.com/search/1/" + "track" + "?q=" + value); request.Method = "Get"; request.ContentType = "application/xml"; WebResponse resptrack = null; try { resptrack = request.GetResponse(); } catch (WebException ex) { RedirectToAction("EditTracks"); } if (((HttpWebResponse)resptrack).StatusCode == HttpStatusCode.OK) { Stream streamresp = resptrack.GetResponseStream(); utils = new UtilsSpotify(); IEnumerable<TrackModel> tracks = _s2tMapper.MapAll(utils.GetTracks(streamresp)); var user = _repo.UsersInfo.Single(u => u.Username.Equals(username)); Playlist pl = user.Playlists.Single(p => p.Id == id); //var pl = userdetails.Playlists.First(); return View("EditTracks", new PlaylistAddTracksToUserModel() { User = user ,Playlist = _p2mMapper.Map(pl), Tracks = tracks }); } else { return new HttpStatusCodeResult(503, "Spotify is offline"); } } }