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:
amazing-username
2019-05-05 21:33:00 -04:00
parent 4a385af3b5
commit 6b4b762207
11 changed files with 315 additions and 34 deletions
+26
View File
@@ -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>();
}
}
}
+74
View File
@@ -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
}
}
+26
View File
@@ -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>();
}
}
}
+72
View File
@@ -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
}
}
+14
View File
@@ -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
}
}
+10 -10
View File
@@ -10,18 +10,18 @@ using Icarus.Models;
namespace Icarus.Models.Context
{
public class SongContext : DbContext
{
public DbSet<Song> Songs { get; set; }
public class SongContext : DbContext
{
public DbSet<Song> Songs { get; set; }
public SongContext(DbContextOptions<SongContext> options)
: base(options)
{ }
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Song>();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Song>();
}
}
}