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:
amazing-username
2019-05-02 22:48:50 -04:00
parent c0d2891d86
commit b0265ebc97
3 changed files with 97 additions and 33 deletions
+24 -12
View File
@@ -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<IEnumerable<Song>> Get()
{
public ActionResult<IEnumerable<Song>> Get()
{
List<Song> songs = new List<Song>();
Console.WriteLine("Attemtping to retrieve songs");
@@ -53,7 +54,7 @@ namespace Icarus.Controllers
songs = context.GetAllSongs();
return songs;
}
}
[HttpGet("{id}")]
public ActionResult<Song> 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)
@@ -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:");