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,6 +9,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
using Icarus.Controllers.Managers;
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Models.Context;
|
||||
|
||||
@@ -73,6 +74,17 @@ namespace Icarus.Controllers
|
||||
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);
|
||||
|
||||
@@ -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:");
|
||||
|
||||
@@ -47,6 +47,39 @@ namespace Icarus.Models.Context
|
||||
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)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user