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;
|
||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
||||
|
||||
songs = context.GetAllSongs();
|
||||
|
||||
@@ -65,8 +64,7 @@ namespace Icarus.Controllers
|
||||
{
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
.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;
|
||||
.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;
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MySql.Data;
|
||||
using MySql.Data.EntityFrameworkCore.Extensions;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
using Icarus.Models;
|
||||
|
||||
namespace Icarus.Models.Context
|
||||
{
|
||||
public class AlbumContext : DbContext
|
||||
{
|
||||
public DbSet<Album> Albums { get; set; }
|
||||
|
||||
public AlbumContext(DbContextOptions<AlbumContext> options)
|
||||
: base(options)
|
||||
{ }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Album>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
using MySql.Data.MySqlClient;
|
||||
using NLog;
|
||||
|
||||
using Icarus.Models;
|
||||
|
||||
namespace Icarus.Models.Context
|
||||
{
|
||||
public class AlbumStoreContext : BaseStoreContext
|
||||
{
|
||||
#region Fields
|
||||
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public AlbumStoreContext(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
public List<Album> GetAlbums()
|
||||
{
|
||||
List<Album> albums = new List<Album>();
|
||||
|
||||
try
|
||||
{
|
||||
using (MySqlConnection conn = GetConnection())
|
||||
{
|
||||
conn.Open();
|
||||
var query = "SELECT * FROM Album";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var id = Convert.ToInt32(reader["Id"]);
|
||||
var title = reader["Title"].ToString();
|
||||
var albumArtist = reader["AlbumArtist"].ToString();
|
||||
var songCount = Convert.ToInt32(reader["SongCount"]);
|
||||
albums.Add(new Album
|
||||
{
|
||||
Id = id,
|
||||
Title = title,
|
||||
AlbumArtist = albumArtist,
|
||||
SongCount = songCount
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
_logger.Error(msg, "An error occurred");
|
||||
}
|
||||
|
||||
return albums;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MySql.Data;
|
||||
using MySql.Data.EntityFrameworkCore;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
using Icarus.Models;
|
||||
|
||||
namespace Icarus.Models.Context
|
||||
{
|
||||
public class ArtistContext : DbContext
|
||||
{
|
||||
public DbSet<Artist> Artist { get; set; }
|
||||
|
||||
public ArtistContext(DbContextOptions<ArtistContext> options)
|
||||
: base (options)
|
||||
{ }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Artist>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MySql.Data.MySqlClient;
|
||||
using NLog;
|
||||
|
||||
using Icarus.Models;
|
||||
|
||||
namespace Icarus.Models.Context
|
||||
{
|
||||
public class ArtistStoreContext : BaseStoreContext
|
||||
{
|
||||
#region Fields
|
||||
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public ArtistStoreContext(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
public List<Artist> GetArtists()
|
||||
{
|
||||
List<Artist> artists = new List<Artist>();
|
||||
try
|
||||
{
|
||||
using (MySqlConnection conn = GetConnection())
|
||||
{
|
||||
conn.Open();
|
||||
var query = "SELECT * FROM Artist";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var id = Convert.ToInt32(reader["Id"].ToString());
|
||||
var name = reader["Name"].ToString();
|
||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||
artists.Add(new Artist
|
||||
{
|
||||
Id = id,
|
||||
Name = name,
|
||||
SongCount = songCount
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
_logger.Error(msg, "An error occurred");
|
||||
}
|
||||
|
||||
return artists;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,31 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using MySql.Data.MySqlClient;
|
||||
using NLog;
|
||||
|
||||
namespace Icarus.Models.Context
|
||||
{
|
||||
public class MusicStoreContext
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
#region Fields
|
||||
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
public string ConnectionString { get; set; }
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public MusicStoreContext(string connectionString)
|
||||
{
|
||||
this.ConnectionString = connectionString;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
public void SaveSong(Song song)
|
||||
{
|
||||
try
|
||||
@@ -179,6 +191,7 @@ namespace Icarus.Models.Context
|
||||
}
|
||||
}
|
||||
}
|
||||
_logger.Info("Song found");
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
@@ -193,5 +206,6 @@ namespace Icarus.Models.Context
|
||||
{
|
||||
return new MySqlConnection(ConnectionString);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +87,14 @@ namespace Icarus
|
||||
new MusicStoreContext(Configuration
|
||||
.GetConnectionString("DefaultConnection"))));
|
||||
|
||||
services.Add(new ServiceDescriptor(typeof(AlbumStoreContext),
|
||||
new AlbumStoreContext(Configuration
|
||||
.GetConnectionString("DefaultConnection"))));
|
||||
|
||||
services.Add(new ServiceDescriptor(typeof(ArtistStoreContext),
|
||||
new ArtistStoreContext(Configuration
|
||||
.GetConnectionString("DefaultConnection"))));
|
||||
|
||||
services.Add(new ServiceDescriptor(typeof(UserStoreContext),
|
||||
new UserStoreContext(Configuration
|
||||
.GetConnectionString("DefaultConnection"))));
|
||||
|
||||
Reference in New Issue
Block a user