public void SongRootObjectSetterTest()
        {
            SongRoot sr1 = new SongRoot()
            {
                ID      = 1,
                Name    = "name of song root",
                GenreID = 50
            };

            ApiSong api1 = new ApiSong()
            {
                ID     = 5,
                Name   = "name of api song",
                Artist = "artist of api song",
                Album  = "album of api song",
                Genre  = "slow song"
            };

            List <ApiSong> apiList1 = new List <ApiSong>();

            apiList1.Add(api1);
            sr1.Songs = apiList1;

            sr1.ID              = 20;
            sr1.Name            = "new name of api song";
            sr1.GenreID         = 80;
            sr1.Songs[0].Artist = "name of a new artist here";

            Assert.Equal(20, sr1.ID);
            Assert.Equal("new name of api song", sr1.Name);
            Assert.Equal(80, sr1.GenreID);
            Assert.Equal("name of a new artist here", sr1.Songs[0].Artist);
        }
        public void SongRootObjectGetterTest()
        {
            SongRoot sr = new SongRoot()
            {
                ID      = 1,
                Name    = "name of song root",
                GenreID = 50
            };

            ApiSong api = new ApiSong()
            {
                ID     = 5,
                Name   = "name of api song",
                Artist = "artist of api song",
                Album  = "album of api song",
                Genre  = "slow song"
            };

            List <ApiSong> apiList = new List <ApiSong>();

            apiList.Add(api);
            sr.Songs = apiList;

            Assert.Equal(1, sr.ID);
            Assert.Equal("name of song root", sr.Name);
            Assert.Equal(50, sr.GenreID);
            Assert.Equal("name of api song", sr.Songs[0].Name);
        }
Example #3
0
        public async Task <IActionResult> Get(int id)
        {
            var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == id);

            // Redirect if user has no selected playlist
            if (user.GenreID == 0 && user.PlaylistID == 0)
            {
                return(RedirectToAction("Create", new { id = user.Id }));
            }


            if (user.GenreID != 0)
            {
                if (user.PlaylistID != 0)
                {
                    // Condition for coming back default playlist user
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri("http://musicparserapi.azurewebsites.net");

                        var playlistResponse = client.GetAsync("/api/playlist").Result;
                        var songResponse     = client.GetAsync($"/api/playlist/{user.PlaylistID}").Result;

                        if (playlistResponse.EnsureSuccessStatusCode().IsSuccessStatusCode)
                        {
                            var jsonDatapl = await playlistResponse.Content.ReadAsStringAsync();

                            var jsonDataSong = await songResponse.Content.ReadAsStringAsync();

                            List <Playlist> rawAllList  = JsonConvert.DeserializeObject <List <Playlist> >(jsonDatapl);
                            SongRoot        rawAllSongs = JsonConvert.DeserializeObject <SongRoot>(jsonDataSong);

                            var allPlaylists = from a in rawAllList
                                               select a;


                            var allSongs = from s in rawAllSongs.Songs
                                           select s;

                            var myPlaylist = allPlaylists.FirstOrDefault(p => p.Id == user.PlaylistID);

                            myPlaylist.UserID = user.Id;
                            myPlaylist.Id     = null;
                            allSongs.Where(s => s.PlaylistID == user.PlaylistID).ToList();

                            PlaylistViewModel mylistVM = new PlaylistViewModel();
                            mylistVM.ApiSongs  = allSongs.Where(s => s.PlaylistID == user.PlaylistID).ToList();
                            mylistVM.Playlists = allPlaylists.Where(pl => pl.GenreID == user.GenreID).ToList();
                            mylistVM.User      = user;

                            return(View(mylistVM));
                        }
                    }
                }

                // Condition for first time user displaying default playlist
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://musicparserapi.azurewebsites.net");

                    var playlistResponse = client.GetAsync("/api/playlist").Result;

                    if (playlistResponse.EnsureSuccessStatusCode().IsSuccessStatusCode)
                    {
                        var jsonDatapl = await playlistResponse.Content.ReadAsStringAsync();

                        List <Playlist> rawAllList = JsonConvert.DeserializeObject <List <Playlist> >(jsonDatapl);

                        var allPlaylists = from a in rawAllList
                                           select a;

                        var myPlaylist = allPlaylists.FirstOrDefault(p => p.GenreID == user.GenreID);
                        myPlaylist.UserID = user.Id;
                        user.PlaylistID   = myPlaylist.Id.Value;

                        myPlaylist.Id = null;

                        // Once playlist gets assigned to user, request songs from api using playlist id
                        var songResponse = client.GetAsync($"/api/playlist/{user.PlaylistID}").Result;
                        if (!songResponse.EnsureSuccessStatusCode().IsSuccessStatusCode)
                        {
                            return(NotFound());
                        }
                        var jsonDataSong = await songResponse.Content.ReadAsStringAsync();

                        SongRoot rawAllSongs = JsonConvert.DeserializeObject <SongRoot>(jsonDataSong);
                        var      allSongs    = from s in rawAllSongs.Songs
                                               select s;

                        allSongs.Where(s => s.PlaylistID == user.PlaylistID).ToList();

                        PlaylistViewModel mylistVM = new PlaylistViewModel();
                        mylistVM.ApiSongs  = allSongs.Where(s => s.PlaylistID == user.PlaylistID).ToList();
                        mylistVM.Playlists = allPlaylists.Where(pl => pl.GenreID == user.GenreID).ToList();
                        mylistVM.User      = user;

                        return(View(mylistVM));
                    }
                }
            }

            // Condition for custom playlist user
            PlaylistViewModel plVM = new PlaylistViewModel();

            plVM.User      = user;
            plVM.Playlists = _context.Playlists.Where(p => p.UserID == user.Id).ToList();
            plVM.Songs     = _context.Songs.Where(p => p.OurListId == user.PlaylistID).ToList();

            return(View(plVM));
        }