From 6b4b762207b4d2b040b54fc664b213de15d4903e Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sun, 5 May 2019 21:33:00 -0400 Subject: [PATCH] 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 --- Controllers/AlbumController.cs | 27 ++++++-- Controllers/ArtistController.cs | 26 ++++++-- Controllers/SongController.cs | 26 ++++---- Controllers/Utilities/MetadataRetriever.cs | 30 +++++++++ Models/Context/AlbumContext.cs | 26 ++++++++ Models/Context/AlbumStoreContext.cs | 74 ++++++++++++++++++++++ Models/Context/ArtistContext.cs | 26 ++++++++ Models/Context/ArtistStoreContext.cs | 72 +++++++++++++++++++++ Models/Context/MusicStoreContext.cs | 14 ++++ Models/Context/SongContext.cs | 20 +++--- Startup.cs | 8 +++ 11 files changed, 315 insertions(+), 34 deletions(-) create mode 100644 Models/Context/AlbumContext.cs create mode 100644 Models/Context/AlbumStoreContext.cs create mode 100644 Models/Context/ArtistContext.cs create mode 100644 Models/Context/ArtistStoreContext.cs diff --git a/Controllers/AlbumController.cs b/Controllers/AlbumController.cs index 404ffaf..9413ae5 100644 --- a/Controllers/AlbumController.cs +++ b/Controllers/AlbumController.cs @@ -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 _logger; + #endregion + + + #region Properties + #endregion + + + #region Constructors + public AlbumController(ILogger 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 } } diff --git a/Controllers/ArtistController.cs b/Controllers/ArtistController.cs index 3c3a43f..d7cc2c4 100644 --- a/Controllers/ArtistController.cs +++ b/Controllers/ArtistController.cs @@ -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 _logger; + #endregion + + + #region Properties + #endregion + + + #region Constructors + public ArtistController(ILogger 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 } } diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 1c5bd09..c7ec954 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -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 songs = new List(); 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 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(); } } } diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index 6793889..ab11951 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -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 CheckSongValues(Song song) { var songValues = new SortedDictionary(); 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; diff --git a/Models/Context/AlbumContext.cs b/Models/Context/AlbumContext.cs new file mode 100644 index 0000000..51a449b --- /dev/null +++ b/Models/Context/AlbumContext.cs @@ -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 Albums { get; set; } + + public AlbumContext(DbContextOptions options) + : base(options) + { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + } + } +} diff --git a/Models/Context/AlbumStoreContext.cs b/Models/Context/AlbumStoreContext.cs new file mode 100644 index 0000000..d1b6297 --- /dev/null +++ b/Models/Context/AlbumStoreContext.cs @@ -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 GetAlbums() + { + List albums = new List(); + + 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 + } +} diff --git a/Models/Context/ArtistContext.cs b/Models/Context/ArtistContext.cs new file mode 100644 index 0000000..e680d78 --- /dev/null +++ b/Models/Context/ArtistContext.cs @@ -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 { get; set; } + + public ArtistContext(DbContextOptions options) + : base (options) + { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + } + } +} diff --git a/Models/Context/ArtistStoreContext.cs b/Models/Context/ArtistStoreContext.cs new file mode 100644 index 0000000..0c2f17f --- /dev/null +++ b/Models/Context/ArtistStoreContext.cs @@ -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 GetArtists() + { + List artists = new List(); + 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 + } +} diff --git a/Models/Context/MusicStoreContext.cs b/Models/Context/MusicStoreContext.cs index 9dec0c3..a4c0561 100644 --- a/Models/Context/MusicStoreContext.cs +++ b/Models/Context/MusicStoreContext.cs @@ -2,19 +2,31 @@ using System; using System.Collections.Generic; using MySql.Data.MySqlClient; +using NLog; namespace Icarus.Models.Context { public class MusicStoreContext { + #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 } } diff --git a/Models/Context/SongContext.cs b/Models/Context/SongContext.cs index 8c7fc57..65eccbb 100644 --- a/Models/Context/SongContext.cs +++ b/Models/Context/SongContext.cs @@ -10,18 +10,18 @@ using Icarus.Models; namespace Icarus.Models.Context { - public class SongContext : DbContext - { - public DbSet Songs { get; set; } + public class SongContext : DbContext + { + public DbSet Songs { get; set; } public SongContext(DbContextOptions options) - : base(options) - { } + : base(options) + { } - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity(); - } - } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + } + } } diff --git a/Startup.cs b/Startup.cs index 6bf9c36..e27e6f1 100644 --- a/Startup.cs +++ b/Startup.cs @@ -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"))));