Implemented Song attribute deletion functionality #17
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user