Created artist and album contexts, created artist and album store contexts, and implemented logging to other regions of the codebase beside the APIControllers #16, #21, #22, #25, #26
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Icarus.Models;
|
||||
|
||||
@@ -12,6 +14,24 @@ namespace Icarus.Controllers
|
||||
[ApiController]
|
||||
public class AlbumController : ControllerBase
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<AlbumController> _logger;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public AlbumController(ILogger<AlbumController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP Routes
|
||||
[HttpGet]
|
||||
public IActionResult Get()
|
||||
{
|
||||
@@ -27,11 +47,6 @@ namespace Icarus.Controllers
|
||||
|
||||
return Ok(album);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Icarus.Models;
|
||||
|
||||
@@ -14,6 +15,24 @@ namespace Icarus.Controllers
|
||||
[ApiController]
|
||||
public class ArtistController : ControllerBase
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<ArtistController> _logger;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public ArtistController(ILogger<ArtistController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP Routes
|
||||
[HttpGet]
|
||||
public IActionResult Get()
|
||||
{
|
||||
@@ -29,11 +48,6 @@ namespace Icarus.Controllers
|
||||
|
||||
return Ok(artist);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ namespace Icarus.Controllers
|
||||
_config = config;
|
||||
_logger = logger;
|
||||
_songMgr = new SongManager(config);
|
||||
_logger.LogInformation("Logging is working!");
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -49,11 +48,11 @@ namespace Icarus.Controllers
|
||||
{
|
||||
List<Song> songs = new List<Song>();
|
||||
Console.WriteLine("Attemtping to retrieve songs");
|
||||
_logger.LogInformation("Attempting to retrieve songs");
|
||||
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
||||
|
||||
songs = context.GetAllSongs();
|
||||
|
||||
@@ -64,9 +63,8 @@ namespace Icarus.Controllers
|
||||
public ActionResult<Song> Get(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
||||
|
||||
Song song = context.GetSong(id);
|
||||
|
||||
@@ -77,10 +75,13 @@ namespace Icarus.Controllers
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult Put(int id, [FromBody] Song song)
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
||||
|
||||
song.Id = id;
|
||||
Console.WriteLine("Retrieving filepath of song");
|
||||
_logger.LogInformation("Retrieving filepath of song");
|
||||
var oldSongRecord = context.GetSong(id);
|
||||
song.SongPath = oldSongRecord.SongPath;
|
||||
|
||||
@@ -100,14 +101,15 @@ namespace Icarus.Controllers
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public void Delete(int id)
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
||||
|
||||
context.DeleteSong(id);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog;
|
||||
using TagLib;
|
||||
|
||||
using Icarus.Models;
|
||||
@@ -10,6 +12,7 @@ namespace Icarus.Controllers.Utilities
|
||||
public class MetadataRetriever
|
||||
{
|
||||
#region Fields
|
||||
private static NLog.Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
||||
private Song _updatedSong;
|
||||
private string _message;
|
||||
private string _title;
|
||||
@@ -66,6 +69,7 @@ namespace Icarus.Controllers.Utilities
|
||||
var msg = ex.Message;
|
||||
Console.WriteLine("An error occurred in MetadataRetriever");
|
||||
Console.WriteLine(msg);
|
||||
_logger.Error(msg, "An error occurred in MetadataRetriever");
|
||||
}
|
||||
|
||||
return song;
|
||||
@@ -76,18 +80,21 @@ namespace Icarus.Controllers.Utilities
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Updating song metadata");
|
||||
_logger.Info("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");
|
||||
_logger.Info("Song metadata updated");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
Console.WriteLine($"An error occurred: \n{msg}");
|
||||
_logger.Error(msg, "An error occurred");
|
||||
}
|
||||
}
|
||||
public void UpdateMetadata(Song updatedSong, Song oldSong)
|
||||
@@ -103,6 +110,7 @@ namespace Icarus.Controllers.Utilities
|
||||
{
|
||||
var msg = ex.Message;
|
||||
Console.WriteLine($"An error occurred: {msg}");
|
||||
_logger.Error(msg, "An error occurred");
|
||||
Message = "Failed to update metadata";
|
||||
}
|
||||
}
|
||||
@@ -119,6 +127,7 @@ namespace Icarus.Controllers.Utilities
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Updating metadata of {title}");
|
||||
_logger.Info($"Updating metadata of {title}");
|
||||
foreach (var key in checkedValues.Keys)
|
||||
{
|
||||
bool result = checkedValues[key];
|
||||
@@ -151,11 +160,13 @@ namespace Icarus.Controllers.Utilities
|
||||
}
|
||||
fileTag.Save();
|
||||
Console.WriteLine("Successfully updated metadata");
|
||||
_logger.Info("Successfully updated metadata");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
Console.WriteLine($"An error occurred:\n{msg}");
|
||||
_logger.Error(msg, "An error occurred");
|
||||
}
|
||||
}
|
||||
private void InitializeUpdatedSong(Song song)
|
||||
@@ -182,6 +193,14 @@ namespace Icarus.Controllers.Utilities
|
||||
Console.WriteLine($"Genre: {_genre}");
|
||||
Console.WriteLine($"Year: {_year}");
|
||||
Console.WriteLine($"Duration: {_duration}\n\n");
|
||||
|
||||
_logger.Info("Metadata of the song");
|
||||
_logger.Info($"Title: {_title}");
|
||||
_logger.Info($"Artist: {_artist}");
|
||||
_logger.Info($"Album: {_album}");
|
||||
_logger.Info($"Genre: {_genre}");
|
||||
_logger.Info($"Year: {_year}");
|
||||
_logger.Info($"Duration: {_duration}");
|
||||
}
|
||||
private void PrintMetadata(Song song, string message)
|
||||
{
|
||||
@@ -192,12 +211,21 @@ namespace Icarus.Controllers.Utilities
|
||||
Console.WriteLine($"Genre: {song.Genre}");
|
||||
Console.WriteLine($"Year: {song.Year}");
|
||||
Console.WriteLine($"Duration: {song.Duration}\n\n");
|
||||
|
||||
_logger.Info(message);
|
||||
_logger.Info($"Title: {_title}");
|
||||
_logger.Info($"Artist: {_artist}");
|
||||
_logger.Info($"Album: {_album}");
|
||||
_logger.Info($"Genre: {_genre}");
|
||||
_logger.Info($"Year: {_year}");
|
||||
_logger.Info($"Duration: {_duration}");
|
||||
}
|
||||
|
||||
private SortedDictionary<string, bool> CheckSongValues(Song song)
|
||||
{
|
||||
var songValues = new SortedDictionary<string, bool>();
|
||||
Console.WriteLine("Checking for null data");
|
||||
_logger.Info("Checking for null data");
|
||||
try
|
||||
{
|
||||
songValues["Title"] = String.IsNullOrEmpty(song.Title);
|
||||
@@ -213,11 +241,13 @@ namespace Icarus.Controllers.Utilities
|
||||
songValues["Year"] = false;
|
||||
}
|
||||
Console.WriteLine("Checking for null data completed");
|
||||
_logger.Info("Checking for null data completed");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
Console.WriteLine($"An error occurred: \n{msg}");
|
||||
_logger.Error(msg, "An error occurred");
|
||||
}
|
||||
|
||||
return songValues;
|
||||
|
||||
Reference in New Issue
Block a user