コード例 #1
0
ファイル: PlaylistControllerTest.cs プロジェクト: Baptista/PI
        public void Test_Get_Track_With_Playlists_Info()
        {
            // Arrange
            var controller = new Mock<PlaylistController>();
            var repo = new TestDB();
            var t1 = new Track { Id=1, Artist = "Metallica", Name = "Battery", Duration = 5.12 };
            var t2 = new Track { Id=2, Artist = "Nirvana", Name = "Polly", Duration = 2.3 };
            var tl1 = new TrackList{ ID=1, Order=1, Track=t1 };
            var tl2 = new TrackList{ ID=2, Order=2, Track=t2 };
            var tl3 = new TrackList{ ID=3, Order=1, Track=t2 };

            ICollection<TrackList> ts1 = new TrackList[] {tl1,tl2};
            ICollection<TrackList> ts2 = new TrackList[] {tl3};

            var pl1 = new Playlist{ Id=1, Name="pl1", Tracks=ts1};
            var pl2 = new Playlist{ Id=2, Name="pl2", Tracks=ts2};

            repo.Tracks.Add(t1);
            repo.Tracks.Add(t2);

            repo.UsersInfo.Add(new UserInfo { Name="xpto", Username="******", Playlists=new []{pl1,pl2}});
            controller.Object._repo = repo;

            var principal = new Mock<System.Security.Principal.IPrincipal>();
            principal.Setup(x => x.Identity.Name).Returns("xpto");

            var ctx = new Mock<HttpContextBase>();
            ctx.Setup(x => x.User).Returns(principal.Object);

            var controllerContextMock = new Mock<ControllerContext>();
            controllerContextMock.SetupGet(x => x.HttpContext).Returns(ctx.Object);

            controller.Object.ControllerContext = controllerContextMock.Object;
            // TODO: Mock data mappers
            controller.Object._t2tipmMapper = new SpotiChelas.DataMappers.TrackToTrackInPlaylistModelMapper{};
            controller.Object._p2mMapper = new SpotiChelas.DataMappers.PlaylistToPlaylistModelMapper{ trackMapper=new SpotiChelas.DataMappers.TrackToTrackModelMapper()};

            // Act
            var result = controller.Object.GetTrack(1);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Id);
            Assert.AreEqual(1, result.OnPlaylists.Count());
            Assert.AreEqual(1, result.NotOnPlaylists.Count());
            Assert.AreEqual(pl1.Name, result.OnPlaylists.First().Name);
            Assert.AreEqual(pl2.Name, result.NotOnPlaylists.First().Name);
        }
コード例 #2
0
ファイル: PlaylistController.cs プロジェクト: Baptista/PI
        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");
                }
            }
        }
コード例 #3
0
ファイル: TrackController.cs プロジェクト: Baptista/PI
        public TrackInPlaylistModel GetTrack(Track track)
        {
            var pls = _repo.UsersInfo.Single(x => x.Username.Equals(User.Identity.Name)).Playlists;

            // Separate lists containing selected track from the ones that do not
            var onPlaylists = pls.Where(p => p.Tracks.Select(x => x.Track.Id).Contains(track.Id)).ToList();
            var notOnPlaylists = pls.Except(onPlaylists).ToList();

            // Maps to view model entities
            var ret = _t2tipmMapper.Map(track);
            ret.NotOnPlaylists = notOnPlaylists.Select(x => _p2mMapper.Map(x)).ToList();
            ret.OnPlaylists = onPlaylists.Select(x => _p2mMapper.Map(x)).ToList();

            return ret;
        }
コード例 #4
0
ファイル: Global.asax.cs プロジェクト: Baptista/PI
        private void InitDB(IUnityContainer container)
        {
            var repo = container.Resolve<IDbContext>();

            if (repo.Albums.Count()==0)
            {
                var album0 = new Album { Name = "...And Justice for All", Artist = "Metallica", Year = 1988 };
                var track0_1 = new Track { Artist = "Metallica", Name = "Blackened", Duration = 6.41 };
                var track0_2 = new Track { Artist = "Metallica", Name = "...And Justice for All", Duration = 9.46 };
                var track0_3 = new Track { Artist = "Metallica", Name = "Eye of the Beholder", Duration = 6.30 };
                var track0_4 = new Track { Artist = "Metallica", Name = "One", Duration = 7.27 };
                var track0_5 = new Track { Artist = "Metallica", Name = "The Shortest Straw", Duration = 6.35 };
                var track0_6 = new Track { Artist = "Metallica", Name = "Harvester of Sorrow", Duration = 5.45 };
                var track0_7 = new Track { Artist = "Metallica", Name = "The Frayed Ends of Sanity", Duration = 7.44 };
                var track0_8 = new Track { Artist = "Metallica", Name = "To Live Is to Die", Duration = 9.48 };
                var track0_9 = new Track { Artist = "Metallica", Name = "Dyers Eve", Duration = 5.13 };

                album0.Tracks = new List<TrackList>();
                album0.Tracks.Add(new TrackList { Order = 1, Track = track0_1 });
                album0.Tracks.Add(new TrackList { Order = 2, Track = track0_2 });
                album0.Tracks.Add(new TrackList { Order = 3, Track = track0_3 });
                album0.Tracks.Add(new TrackList { Order = 4, Track = track0_4 });
                album0.Tracks.Add(new TrackList { Order = 5, Track = track0_5 });
                album0.Tracks.Add(new TrackList { Order = 6, Track = track0_6 });
                album0.Tracks.Add(new TrackList { Order = 7, Track = track0_7 });
                album0.Tracks.Add(new TrackList { Order = 8, Track = track0_8 });
                album0.Tracks.Add(new TrackList { Order = 9, Track = track0_9 });

                var album1 = new Album { Name = "Nevermind", Artist = "Nirvana", Year = 1991 };
                var track1_1 = new Track { Artist = "Nirvana", Duration = 5.02, Name = "Smells Like Teen Spirit" };
                var track1_2 = new Track { Artist = "Nirvana", Duration = 4.15, Name = "In Bloom" };
                var track1_3 = new Track { Artist = "Nirvana", Duration = 3.39, Name = "Come as You Are" };
                var track1_4 = new Track { Artist = "Nirvana", Duration = 3.04, Name = "Breed" };
                var track1_5 = new Track { Artist = "Nirvana", Duration = 4.17, Name = "Lithium" };
                var track1_6 = new Track { Artist = "Nirvana", Duration = 2.57, Name = "Polly" };
                var track1_7 = new Track { Artist = "Nirvana", Duration = 2.23, Name = "Territorial Pissings" };
                var track1_8 = new Track { Artist = "Nirvana", Duration = 3.44, Name = "Drain You" };
                var track1_9 = new Track { Artist = "Nirvana", Duration = 2.37, Name = "Lounge Act" };
                var track1_10 = new Track { Artist = "Nirvana", Duration = 3.32, Name = "Stay Away" };
                var track1_11 = new Track { Artist = "Nirvana", Duration = 3.16, Name = "On a Plain" };
                var track1_12 = new Track { Artist = "Nirvana", Duration = 3.48, Name = "Something in the Way" };
                var track1_13 = new Track { Artist = "Nirvana", Duration = 6.44, Name = "Endless, Nameless" };

                album1.Tracks = new List<TrackList>();
                album1.Tracks.Add(new TrackList { Order = 1, Track = track1_1 });
                album1.Tracks.Add(new TrackList { Order = 2, Track = track1_2 });
                album1.Tracks.Add(new TrackList { Order = 3, Track = track1_3 });
                album1.Tracks.Add(new TrackList { Order = 4, Track = track1_4 });
                album1.Tracks.Add(new TrackList { Order = 5, Track = track1_5 });
                album1.Tracks.Add(new TrackList { Order = 6, Track = track1_6 });
                album1.Tracks.Add(new TrackList { Order = 7, Track = track1_7 });
                album1.Tracks.Add(new TrackList { Order = 8, Track = track1_8 });
                album1.Tracks.Add(new TrackList { Order = 9, Track = track1_9 });
                album1.Tracks.Add(new TrackList { Order = 10, Track = track1_10 });
                album1.Tracks.Add(new TrackList { Order = 11, Track = track1_11 });
                album1.Tracks.Add(new TrackList { Order = 12, Track = track1_12 });
                album1.Tracks.Add(new TrackList { Order = 13, Track = track1_13 });

                var album2 = new Album { Name = "Now", Artist = "The Rolling Stones", Year = 1965 };
                var track2_1 = new Track { Artist = "The Rolling Stones", Duration = 3.01, Name = "Everybody Needs Somebody to Love" };
                var track2_2 = new Track { Artist = "The Rolling Stones", Duration = 4.10, Name = "Down Home Girl" };
                var track2_3 = new Track { Artist = "The Rolling Stones", Duration = 3.35, Name = "You Can't Catch Me" };
                var track2_4 = new Track { Artist = "The Rolling Stones", Duration = 2.43, Name = "Heart of Stone" };
                var track2_5 = new Track { Artist = "The Rolling Stones", Duration = 3.09, Name = "What a Shame" };
                var track2_6 = new Track { Artist = "The Rolling Stones", Duration = 3.37, Name = "Mona (I Need You Baby)" };
                var track2_7 = new Track { Artist = "The Rolling Stones", Duration = 3.02, Name = "Down the Road Apiece" };
                var track2_8 = new Track { Artist = "The Rolling Stones", Duration = 2.34, Name = "Off the Hook" };
                var track2_9 = new Track { Artist = "The Rolling Stones", Duration = 2.14, Name = "Pain in My Hear" };
                var track2_10 = new Track { Artist = "The Rolling Stones", Duration = 2.10, Name = "Oh Baby (We Got a Good Thing Goin')" };
                var track2_11 = new Track { Artist = "The Rolling Stones", Duration = 3.04, Name = "Little Red Rooster" };
                var track2_12 = new Track { Artist = "The Rolling Stones", Duration = 2.30, Name = "Surprise, Surprise" };

                album2.Tracks = new List<TrackList>();
                album2.Tracks.Add(new TrackList { Order = 1, Track = track2_1 });
                album2.Tracks.Add(new TrackList { Order = 2, Track = track2_2 });
                album2.Tracks.Add(new TrackList { Order = 3, Track = track2_3 });
                album2.Tracks.Add(new TrackList { Order = 4, Track = track2_4 });
                album2.Tracks.Add(new TrackList { Order = 5, Track = track2_5 });
                album2.Tracks.Add(new TrackList { Order = 6, Track = track2_6 });
                album2.Tracks.Add(new TrackList { Order = 7, Track = track2_7 });
                album2.Tracks.Add(new TrackList { Order = 8, Track = track2_8 });
                album2.Tracks.Add(new TrackList { Order = 9, Track = track2_9 });
                album2.Tracks.Add(new TrackList { Order = 10, Track = track2_10 });
                album2.Tracks.Add(new TrackList { Order = 11, Track = track2_11 });
                album2.Tracks.Add(new TrackList { Order = 12, Track = track2_12 });

                repo.Albums.Add(album0);
                repo.Albums.Add(album1);
                repo.Albums.Add(album2);

                repo.SaveChanges();
            }
        }