diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 3cf130a..9e4fe75 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -18,6 +18,7 @@ namespace Icarus.Controllers { #region Fields private IConfiguration _config; + private MusicStoreContext _context; private SongManager _songMgr; #endregion @@ -39,7 +40,7 @@ namespace Icarus.Controllers public ActionResult> Get() { List songs = new List(); - songs = _songMgr.RetrieveAllSongDetails().Result; + //songs = _songMgr.RetrieveAllSongDetails().Result; Console.WriteLine("Attemtping to retrieve songs"); MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; @@ -74,6 +75,8 @@ namespace Icarus.Controllers [HttpDelete("{id}")] public void Delete(int id) { + MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + context.DeleteSong(id); } } } diff --git a/Models/MusicStoreContext.cs b/Models/MusicStoreContext.cs index 14ec5b7..5487ece 100644 --- a/Models/MusicStoreContext.cs +++ b/Models/MusicStoreContext.cs @@ -49,6 +49,29 @@ namespace Icarus.Models Console.WriteLine($"An error occurred:\n{exMsg}"); } } + public void DeleteSong(int id) + { + try + { + using (MySqlConnection conn = GetConnection()) + { + conn.Open(); + string query = "DELETE FROM Songs WHERE Id=@Id"; + + using (MySqlCommand cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Id", id); + + cmd.ExecuteNonQuery(); + } + } + } + catch (Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine($"An error occurred:\n{exMsg}"); + } + } public List GetAllSongs() {