Implemented Song attribute deletion functionality #17

This commit is contained in:
amazing-username
2019-04-14 19:09:29 +00:00
parent c9b1abdbdb
commit 9f0d23f420
2 changed files with 27 additions and 1 deletions
+4 -1
View File
@@ -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<IEnumerable<Song>> Get()
{
List<Song> songs = new List<Song>();
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);
}
}
}
+23
View File
@@ -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<Song> GetAllSongs()
{