コード例 #1
0
        public ActionResult TopAlbums()
        {
            var albumIDs = new List <string>();

            using (var connection = new SqlConnection(connectionString))
                using (var command = new SqlCommand(null, connection)) {
                    connection.Open();
                    command.CommandText = String.Format("SELECT DISTINCT AlbumID FROM Playlist");
                    using (var reader = command.ExecuteReader()) {
                        while (reader.Read())
                        {
                            var albumID = reader.GetString(reader.GetOrdinal("AlbumID"));
                            albumIDs.Add(albumID);
                        }
                    }
                }

            // now got a list of albumID's we want to call spotify to get info
            var listAlbumDetails = new List <AlbumDetails>();
            var spotifyHelper    = new SpotifyHelper();

            foreach (var albumID in albumIDs)
            {
                var      apiResult    = spotifyHelper.CallSpotifyAPIAlbumDetails(null, id: albumID);
                var      albumDetails = JsonConvert.DeserializeObject <AlbumDetails>(apiResult.Json);
                DateTime d;
                bool     success = DateTime.TryParse(albumDetails.release_date, out d);

                if (!success)
                {
                    // Black crowes was just a year ie 2009
                    int year = Int32.Parse(albumDetails.release_date);
                    d = new DateTime(year, 1, 1);
                }

                albumDetails.releaseDateTime = d;
                listAlbumDetails.Add(albumDetails);
            }

            // Pass current userID to view?
            var a = listAlbumDetails.OrderByDescending(x => x.releaseDateTime).ToList();

            return(View(a));
        }
コード例 #2
0
        private AlbumDetailsViewModel GetAlbumDetailsViewModel(string id)
        {
            var spotifyHelper   = new SpotifyHelper();
            var stopWatchResult = new StopWatchResult();
            var apiResult       = spotifyHelper.CallSpotifyAPIAlbumDetails(stopWatchResult, id);

            ViewBag.Id = id;
            var albumDetails = JsonConvert.DeserializeObject <AlbumDetails>(apiResult.Json);

            // iterate through setting vm checked property
            using (var connection = new SqlConnection(connectionString))
                using (var command = new SqlCommand(null, connection)) {
                    connection.Open();
                    command.CommandText = String.Format("SELECT TrackID FROM Tracks");
                    command.CommandType = CommandType.Text;

                    var trackIDsSelectedInDb = new List <string>();
                    using (var reader = command.ExecuteReader()) {
                        while (reader.Read())
                        {
                            var trackID = reader.GetString(reader.GetOrdinal("TrackID"));
                            trackIDsSelectedInDb.Add(trackID);
                        }
                    }

                    foreach (var trackID in trackIDsSelectedInDb)
                    {
                        var track = albumDetails.tracks.items.FirstOrDefault(x => x.id == trackID);
                        if (track != null)
                        {
                            track.Checked = true;
                        }
                    }
                }

            var vm = new AlbumDetailsViewModel {
                APIDebugList = null,
                AlbumDetails = albumDetails,
            };

            return(vm);
        }