Modified Song model, added more functionality to the Album context and store, and need to work on how album records are handled when uploading songs. I left TODO's on where to pick up. #22, #25, #29

This commit is contained in:
amazing-username
2019-05-06 20:32:34 -04:00
parent 6b4b762207
commit 66f9a05341
8 changed files with 635 additions and 467 deletions
+21 -2
View File
@@ -12,16 +12,19 @@ using Id3;
using Id3.Frames; using Id3.Frames;
using MySql.Data; using MySql.Data;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using NLog;
using TagLib; using TagLib;
using Icarus.Models;
using Icarus.Controllers.Utilities; using Icarus.Controllers.Utilities;
using Icarus.Models;
using Icarus.Models.Context;
namespace Icarus.Controllers.Managers namespace Icarus.Controllers.Managers
{ {
public class SongManager public class SongManager
{ {
#region Fields #region Fields
private static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
private MySqlConnection _conn; private MySqlConnection _conn;
private MySqlCommand _cmd; private MySqlCommand _cmd;
private MySqlDataAdapter _dataDump; private MySqlDataAdapter _dataDump;
@@ -226,6 +229,22 @@ namespace Icarus.Controllers.Managers
Console.WriteLine($"An error occurred: {exMsg}"); Console.WriteLine($"An error occurred: {exMsg}");
} }
} }
public async Task SaveSongToFileSystem(IFormFile song, MusicStoreContext sStoreContext,
AlbumStoreContext alStoreContext,
ArtistStoreContext arStoreContext)
{
try
{
// TODO: Define and implement method that will take care of the
// song and album records, eventually will address the artist
// records. Make use of helper functions
}
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
}
public async Task<List<Song>> RetrieveAllSongDetails() public async Task<List<Song>> RetrieveAllSongDetails()
{ {
@@ -405,7 +424,7 @@ namespace Icarus.Controllers.Managers
Console.WriteLine($"Retrieving metadata of song from filepath {filePath}"); Console.WriteLine($"Retrieving metadata of song from filepath {filePath}");
MetadataRetriever meta = new MetadataRetriever(); MetadataRetriever meta = new MetadataRetriever();
_song = meta.RetrieveMetaData(filePath); _song = meta.RetrieveMetaData(filePath);
//_song = RetrieveMetaData(filePath);
Console.WriteLine("Assigning song filename"); Console.WriteLine("Assigning song filename");
_song.Filename = song.FileName; _song.Filename = song.FileName;
Console.WriteLine($"Song filename retrieved: {song.FileName}"); Console.WriteLine($"Song filename retrieved: {song.FileName}");
+23 -12
View File
@@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Icarus.Controllers.Managers; using Icarus.Controllers.Managers;
using Icarus.Models; using Icarus.Models;
@@ -22,6 +23,7 @@ namespace Icarus.Controllers
{ {
#region Fields #region Fields
private IConfiguration _config; private IConfiguration _config;
private ILogger<SongDataController> _logger;
private SongManager _songMgr; private SongManager _songMgr;
private string _songTempDir; private string _songTempDir;
#endregion #endregion
@@ -32,9 +34,10 @@ namespace Icarus.Controllers
#region Constructor #region Constructor
public SongDataController(IConfiguration config) public SongDataController(IConfiguration config, ILogger<SongDataController> logger)
{ {
_config = config; _config = config;
_logger = logger;
_songTempDir = _config.GetValue<string>("TemporaryMusicPath"); _songTempDir = _config.GetValue<string>("TemporaryMusicPath");
_songMgr = new SongManager(config, _songTempDir); _songMgr = new SongManager(config, _songTempDir);
} }
@@ -48,8 +51,7 @@ namespace Icarus.Controllers
{ {
MusicStoreContext context = HttpContext MusicStoreContext context = HttpContext
.RequestServices .RequestServices
.GetService(typeof(MusicStoreContext)) .GetService(typeof(MusicStoreContext)) as MusicStoreContext;
as MusicStoreContext;
var songMetaData = context.GetSong(id); var songMetaData = context.GetSong(id);
SongData song = await _songMgr.RetrieveSong(songMetaData); SongData song = await _songMgr.RetrieveSong(songMetaData);
@@ -63,29 +65,39 @@ namespace Icarus.Controllers
{ {
try try
{ {
MusicStoreContext context = HttpContext MusicStoreContext songStoreContext = HttpContext
.RequestServices .RequestServices
.GetService(typeof(MusicStoreContext)) .GetService(typeof(MusicStoreContext)) as MusicStoreContext;
as MusicStoreContext; AlbumStoreContext albumStoreContext = HttpContext
.RequestServices
.GetService(typeof(AlbumStoreContext)) as AlbumStoreContext;
ArtistStoreContext artistStoreContext = HttpContext
.RequestServices
.GetService(typeof(ArtistStoreContext)) as ArtistStoreContext;
Console.WriteLine("Uploading song..."); Console.WriteLine("Uploading song...");
_logger.LogInformation("Uploading song...");
var uploads = _songTempDir; var uploads = _songTempDir;
Console.WriteLine($"Song Root Path {uploads}"); Console.WriteLine($"Song Root Path {uploads}");
_logger.LogInformation($"Song root path {uploads}");
foreach (var sng in songData) foreach (var sng in songData)
{ {
if (sng.Length > 0) { if (sng.Length > 0) {
Console.WriteLine($"Song filename {sng.FileName}"); Console.WriteLine($"Song filename {sng.FileName}");
await _songMgr.SaveSongToFileSystem(sng); _logger.LogInformation($"Song filename {sng.FileName}");
// TODO: Add functionality for overloaded method
await _songMgr.SaveSongToFileSystem(sng, songStoreContext,
albumStoreContext, artistStoreContext);
var song = _songMgr.SongDetails; var song = _songMgr.SongDetails;
context.SaveSong(song); songStoreContext.SaveSong(song);
Console.WriteLine("Song successfully saved");
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"An error occurred: {ex.Message}"); var msg = ex.Message;
_logger.LogError(msg, "An error occurred");
} }
} }
@@ -95,8 +107,7 @@ namespace Icarus.Controllers
{ {
MusicStoreContext context = HttpContext MusicStoreContext context = HttpContext
.RequestServices .RequestServices
.GetService(typeof(MusicStoreContext)) .GetService(typeof(MusicStoreContext)) as MusicStoreContext;
as MusicStoreContext;
var songMetaData = context.GetSong(id); var songMetaData = context.GetSong(id);
+4
View File
@@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -14,5 +16,7 @@ namespace Icarus.Models
public string AlbumArtist { get; set; } public string AlbumArtist { get; set; }
[JsonProperty("song_count")] [JsonProperty("song_count")]
public int SongCount { get; set; } public int SongCount { get; set; }
[JsonIgnore]
public List<Song> Songs { get; set; }
} }
} }
+81
View File
@@ -69,6 +69,87 @@ namespace Icarus.Models.Context
return albums; return albums;
} }
public bool DoesAlbumExist(Album album)
{
try
{
}
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
return false;
}
public void SaveAlbum(Album album)
{
try
{
using (MySqlConnection conn = GetConnection())
{
conn.Open();
var query = "INSERT INTO Album(Title, AlbumArtist, " +
"SongCount) VALUES (@Title, @AlbumArtist, " +
"SongCount)";
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Title", album.Title);
cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist);
cmd.Parameters.AddWithValue("@SongCount", album.SongCount);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
}
private List<Album> ParseData(MySqlDataReader reader)
{
List<Album> albums = new List<Album>();
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
});
}
return albums;
}
private Album ParseSingleData(MySqlDataReader reader)
{
Album album = new Album();
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"]);
album = new Album
{
Id = id,
Title = title,
AlbumArtist = albumArtist,
SongCount = songCount
};
}
return album;
}
#endregion #endregion
} }
} }
+47 -5
View File
@@ -6,7 +6,7 @@ using NLog;
namespace Icarus.Models.Context namespace Icarus.Models.Context
{ {
public class MusicStoreContext public class MusicStoreContext : BaseStoreContext
{ {
#region Fields #region Fields
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger(); private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
@@ -14,14 +14,13 @@ namespace Icarus.Models.Context
#region Properties #region Properties
public string ConnectionString { get; set; }
#endregion #endregion
#region Constructors #region Constructors
public MusicStoreContext(string connectionString) public MusicStoreContext(string connectionString)
{ {
this.ConnectionString = connectionString; _connectionString = connectionString;
} }
#endregion #endregion
@@ -202,10 +201,53 @@ namespace Icarus.Models.Context
return song; return song;
} }
private MySqlConnection GetConnection() private List<Song> ParseData(MySqlDataReader reader)
{ {
return new MySqlConnection(ConnectionString);
List<Song> songs = new List<Song>();
while (reader.Read())
{
songs.Add(new Song
{
Id = Convert.ToInt32(reader["Id"]),
Title = reader["Title"].ToString(),
Album = reader["Album"].ToString(),
Artist = reader["Artist"].ToString(),
Year = Convert.ToInt32(reader["Year"]),
Genre = reader["Genre"].ToString(),
Duration = Convert.ToInt32(reader["Duration"]),
Filename = reader["Filename"].ToString(),
SongPath = reader["SongPath"].ToString()
});
} }
return songs;
}
private Song ParseSingleData(MySqlDataReader reader)
{
Song song = new Song();
while (reader.Read())
{
song = new Song
{
Id = Convert.ToInt32(reader["Id"]),
Title = reader["Title"].ToString(),
Album = reader["Album"].ToString(),
Artist = reader["Artist"].ToString(),
Year = Convert.ToInt32(reader["Year"]),
Genre = reader["Genre"].ToString(),
Duration = Convert.ToInt32(reader["Duration"]),
Filename = reader["Filename"].ToString(),
SongPath = reader["SongPath"].ToString()
};
}
return song;
}
#endregion #endregion
} }
} }
+5 -1
View File
@@ -21,7 +21,11 @@ namespace Icarus.Models.Context
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<Song>(); modelBuilder.Entity<Song>()
.HasOne(s => s.AlbumObject)
.WithMany(a => a.Songs)
.HasForeignKey(s => s.AlbumId)
.HasConstraintName("ForeignKey_Song_Album");
} }
} }
} }
+5
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -29,5 +30,9 @@ namespace Icarus.Models
public string Filename { get; set; } public string Filename { get; set; }
[JsonProperty("song_path")] [JsonProperty("song_path")]
public string SongPath { get; set; } public string SongPath { get; set; }
[JsonIgnore]
public Album AlbumObject { get; set; }
[JsonIgnore]
public int AlbumId { get; set; }
} }
} }
+2
View File
@@ -100,6 +100,8 @@ namespace Icarus
.GetConnectionString("DefaultConnection")))); .GetConnectionString("DefaultConnection"))));
services.AddDbContext<SongContext>(options => options.UseMySQL(connString)); services.AddDbContext<SongContext>(options => options.UseMySQL(connString));
services.AddDbContext<AlbumContext>(options => options.UseMySQL(connString));
services.AddDbContext<ArtistContext>(options => options.UseMySQL(connString));
services.AddDbContext<UserContext>(options => options.UseMySQL(connString)); services.AddDbContext<UserContext>(options => options.UseMySQL(connString));
} }