diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index b806675..f74578d 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -9,15 +9,16 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Icarus.Controllers.Managers; +using Icarus.Controllers.Utilities; using Icarus.Models; using Icarus.Models.Context; namespace Icarus.Controllers { - [Route("api/song")] - [ApiController] - public class SongController : ControllerBase - { + [Route("api/song")] + [ApiController] + public class SongController : ControllerBase + { #region Fields private IConfiguration _config; private MusicStoreContext _context; @@ -38,10 +39,10 @@ namespace Icarus.Controllers #endregion - [HttpGet] + [HttpGet] [Authorize("read:song_details")] - public ActionResult> Get() - { + public ActionResult> Get() + { List songs = new List(); Console.WriteLine("Attemtping to retrieve songs"); @@ -53,7 +54,7 @@ namespace Icarus.Controllers songs = context.GetAllSongs(); return songs; - } + } [HttpGet("{id}")] public ActionResult Get(int id) @@ -69,14 +70,25 @@ namespace Icarus.Controllers } - [HttpPut("{id}")] - public IActionResult Put(int id, [FromBody] Song song) - { + [HttpPut("{id}")] + public IActionResult Put(int id, [FromBody] Song song) + { // TODO: Implement updating of song metadata + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) as MusicStoreContext; + Console.WriteLine("Retrieving filepath of song"); + var songPath = context.GetSong(id).SongPath; + song.SongPath = songPath; + + MetadataRetriever updateMetadata = new MetadataRetriever(); + updateMetadata.UpdateMetadata(song); + + context.UpdateSong(song); + SongResult songResult = new SongResult(); return Ok(songResult); - } + } [HttpDelete("{id}")] public void Delete(int id) diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index 4ce5b5e..c90ce8c 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -58,6 +58,25 @@ namespace Icarus.Controllers.Utilities return song; } + public void UpdateMetadata(Song song) + { + try + { + Console.WriteLine("Updating song metadata"); + var filePath = song.SongPath; + TagLib.File fileTag = TagLib.File.Create(filePath); + fileTag.Tag.Title = song.Title; + fileTag.Tag.Genres = new []{song.Genre}; + fileTag.Save(); + Console.WriteLine("Song metadata updated"); + + } + catch (Exception ex) + { + var msg = ex.Message; + } + } + private void PrintMetadata() { Console.WriteLine("\n\nMetadata of the song:"); diff --git a/Models/Context/MusicStoreContext.cs b/Models/Context/MusicStoreContext.cs index 06a174d..56cfdb1 100644 --- a/Models/Context/MusicStoreContext.cs +++ b/Models/Context/MusicStoreContext.cs @@ -5,30 +5,30 @@ using MySql.Data.MySqlClient; namespace Icarus.Models.Context { - public class MusicStoreContext - { - public string ConnectionString { get; set; } + public class MusicStoreContext + { + public string ConnectionString { get; set; } - public MusicStoreContext(string connectionString) - { - this.ConnectionString = connectionString; - } + public MusicStoreContext(string connectionString) + { + this.ConnectionString = connectionString; + } public void SaveSong(Song song) { - try - { - using (MySqlConnection conn = GetConnection()) + try + { + using (MySqlConnection conn = GetConnection()) { - conn.Open(); - string query = "INSERT INTO Songs(Title, Album, Artist," + + conn.Open(); + string query = "INSERT INTO Songs(Title, Album, Artist," + " Year, Genre, Duration, Filename, SongPath) " + "VALUES(@Title, @Album, @Artist, @Year, @Genre, " + "@Duration, @Filename, @SongPath)"; - using (MySqlCommand cmd = new MySqlCommand(query, conn)) - { - cmd.Parameters.AddWithValue("@Title", song.Title); + using (MySqlCommand cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Title", song.Title); cmd.Parameters.AddWithValue("@Album", song.Album); cmd.Parameters.AddWithValue("@Artist", song.Artist); cmd.Parameters.AddWithValue("@Year", song.Year); @@ -38,14 +38,47 @@ namespace Icarus.Models.Context cmd.Parameters.AddWithValue("@SongPath", song.SongPath); cmd.ExecuteNonQuery(); - } + } } - } - catch(Exception ex) - { - var exMsg = ex.Message; + } + catch(Exception ex) + { + var exMsg = ex.Message; Console.WriteLine($"An error occurred:\n{exMsg}"); - } + } + } + public void UpdateSong(Song song) + { + try + { + using (MySqlConnection conn = GetConnection()) + { + conn.Open(); + + string query = "UPDATE Songs SET Title=@Title, Album=@Album, " + + "Artist=@Artist, Year=@Year, Genre=@Genre, " + + "Duration=@Duration, Filename=@Filename, SongPath=@SongPath"; + using (MySqlCommand cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Title", song.Title); + cmd.Parameters.AddWithValue("@Album", song.Album); + cmd.Parameters.AddWithValue("@Artist", song.Artist); + cmd.Parameters.AddWithValue("@Year", song.Year); + cmd.Parameters.AddWithValue("@Genre", song.Genre); + cmd.Parameters.AddWithValue("@Duration", song.Duration); + cmd.Parameters.AddWithValue("@Filename", song.Filename); + cmd.Parameters.AddWithValue("@SongPath", song.SongPath); + + cmd.ExecuteNonQuery(); + } + } + } + catch (Exception ex) + { + var msg = ex.Message; + Console.WriteLine("An error occurred in MusicStoreContext:"); + Console.WriteLine(msg); + } } public void DeleteSong(int id) {