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); }
public ActionResult Details(AlbumDetailsViewModel vm, string id) { // id is AlbumID? using (var connection = new SqlConnection(connectionString)) using (var command = new SqlCommand(null, connection)) { connection.Open(); // Tracks foreach (var t in vm.AlbumDetails.tracks.items) { if (t.Checked) { // Is it already there in the database? command.CommandText = String.Format("SELECT COUNT(*) FROM Tracks WHERE TrackID = '{0}'", t.id); command.CommandType = CommandType.Text; var result = command.ExecuteScalar().ToString(); if (result == "0") { // Add track to db command.CommandText = "INSERT INTO Tracks (TrackID, TrackName, ArtistName, ArtistID, TrackPreviewURL, AlbumName," + "AlbumID, AlbumImageURL, AlbumDate) " + "VALUES (@TrackID, @TrackName,@ArtistName,@ArtistID,@TrackPreviewURL, @AlbumName, @AlbumID," + "@AlbumImageURL, @AlbumDate)"; command.Parameters.Clear(); command.Parameters.AddWithValue("@TrackID", t.id); command.Parameters.AddWithValue("@TrackName", t.name); //command.Parameters.AddWithValue("@ArtistName", vm.ArtistDetails.Name); command.Parameters.AddWithValue("@ArtistName", vm.AlbumDetails.artists[0].name); command.Parameters.AddWithValue("@ArtistID", vm.AlbumDetails.artists[0].id); command.Parameters.AddWithValue("@TrackPreviewURL", t.preview_url); //command.Parameters.AddWithValue("@AlbumName", t.album.name); command.Parameters.AddWithValue("@AlbumName", vm.AlbumDetails.name); //command.Parameters.AddWithValue("@AlbumID", t.album.id); command.Parameters.AddWithValue("@AlbumID", vm.AlbumDetails.id); //command.Parameters.AddWithValue("@AlbumImageURL", t.album.images[2].url); command.Parameters.AddWithValue("@AlbumImageURL", vm.AlbumDetails.images[2].url); //string dateOfAlbum = t.album.DateOfAlbumRelease; string dateOfAlbum = vm.AlbumDetails.release_date; DateTime d; // Maybe its in 2000/01/01 bool r = DateTime.TryParse(dateOfAlbum, out d); if (!r) { // Maybe its in the format 2000 int year; if (Int32.TryParse(dateOfAlbum, out year)) { if (DateTime.TryParse(year + "/1/1", out d)) { } else { d = new DateTime(1900, 1, 1); } } } command.Parameters.AddWithValue("@AlbumDate", d); command.CommandType = CommandType.Text; command.ExecuteNonQuery(); } } else { // If its been unchecked and is there in the database? command.CommandText = String.Format("SELECT COUNT(*) FROM Tracks WHERE TrackID = '{0}'", t.id); command.CommandType = CommandType.Text; var result = command.ExecuteScalar().ToString(); if (result != "0") { command.CommandText = String.Format("DELETE FROM Tracks WHERE TrackID = '{0}'", t.id); command.CommandType = CommandType.Text; command.ExecuteNonQuery(); } } } } // Get data again as not saved, including Checked status var vm2 = GetAlbumDetailsViewModel(id); return(View(vm2)); }