Partially implemented updating song metadata. Need to make it more dynamic, meaning only update attributes that have values sent with the request. #36
This commit is contained in:
@@ -9,15 +9,16 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
using Icarus.Controllers.Managers;
|
using Icarus.Controllers.Managers;
|
||||||
|
using Icarus.Controllers.Utilities;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
using Icarus.Models.Context;
|
using Icarus.Models.Context;
|
||||||
|
|
||||||
namespace Icarus.Controllers
|
namespace Icarus.Controllers
|
||||||
{
|
{
|
||||||
[Route("api/song")]
|
[Route("api/song")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class SongController : ControllerBase
|
public class SongController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private MusicStoreContext _context;
|
private MusicStoreContext _context;
|
||||||
@@ -38,10 +39,10 @@ namespace Icarus.Controllers
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:song_details")]
|
[Authorize("read:song_details")]
|
||||||
public ActionResult<IEnumerable<Song>> Get()
|
public ActionResult<IEnumerable<Song>> Get()
|
||||||
{
|
{
|
||||||
List<Song> songs = new List<Song>();
|
List<Song> songs = new List<Song>();
|
||||||
Console.WriteLine("Attemtping to retrieve songs");
|
Console.WriteLine("Attemtping to retrieve songs");
|
||||||
|
|
||||||
@@ -53,7 +54,7 @@ namespace Icarus.Controllers
|
|||||||
songs = context.GetAllSongs();
|
songs = context.GetAllSongs();
|
||||||
|
|
||||||
return songs;
|
return songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public ActionResult<Song> Get(int id)
|
public ActionResult<Song> Get(int id)
|
||||||
@@ -69,14 +70,25 @@ namespace Icarus.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
public IActionResult Put(int id, [FromBody] Song song)
|
public IActionResult Put(int id, [FromBody] Song song)
|
||||||
{
|
{
|
||||||
// TODO: Implement updating of song metadata
|
// 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();
|
SongResult songResult = new SongResult();
|
||||||
|
|
||||||
return Ok(songResult);
|
return Ok(songResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public void Delete(int id)
|
public void Delete(int id)
|
||||||
|
|||||||
@@ -58,6 +58,25 @@ namespace Icarus.Controllers.Utilities
|
|||||||
return song;
|
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()
|
private void PrintMetadata()
|
||||||
{
|
{
|
||||||
Console.WriteLine("\n\nMetadata of the song:");
|
Console.WriteLine("\n\nMetadata of the song:");
|
||||||
|
|||||||
@@ -5,30 +5,30 @@ using MySql.Data.MySqlClient;
|
|||||||
|
|
||||||
namespace Icarus.Models.Context
|
namespace Icarus.Models.Context
|
||||||
{
|
{
|
||||||
public class MusicStoreContext
|
public class MusicStoreContext
|
||||||
{
|
{
|
||||||
public string ConnectionString { get; set; }
|
public string ConnectionString { get; set; }
|
||||||
|
|
||||||
public MusicStoreContext(string connectionString)
|
public MusicStoreContext(string connectionString)
|
||||||
{
|
{
|
||||||
this.ConnectionString = connectionString;
|
this.ConnectionString = connectionString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void SaveSong(Song song)
|
public void SaveSong(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
string query = "INSERT INTO Songs(Title, Album, Artist," +
|
string query = "INSERT INTO Songs(Title, Album, Artist," +
|
||||||
" Year, Genre, Duration, Filename, SongPath) " +
|
" Year, Genre, Duration, Filename, SongPath) " +
|
||||||
"VALUES(@Title, @Album, @Artist, @Year, @Genre, " +
|
"VALUES(@Title, @Album, @Artist, @Year, @Genre, " +
|
||||||
"@Duration, @Filename, @SongPath)";
|
"@Duration, @Filename, @SongPath)";
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||||
cmd.Parameters.AddWithValue("@Album", song.Album);
|
cmd.Parameters.AddWithValue("@Album", song.Album);
|
||||||
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
||||||
cmd.Parameters.AddWithValue("@Year", song.Year);
|
cmd.Parameters.AddWithValue("@Year", song.Year);
|
||||||
@@ -38,14 +38,47 @@ namespace Icarus.Models.Context
|
|||||||
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
var exMsg = ex.Message;
|
var exMsg = ex.Message;
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
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)
|
public void DeleteSong(int id)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user