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
+81
View File
@@ -69,6 +69,87 @@ namespace Icarus.Models.Context
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
}
}
+49 -7
View File
@@ -6,7 +6,7 @@ using NLog;
namespace Icarus.Models.Context
{
public class MusicStoreContext
public class MusicStoreContext : BaseStoreContext
{
#region Fields
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
@@ -14,14 +14,13 @@ namespace Icarus.Models.Context
#region Properties
public string ConnectionString { get; set; }
#endregion
#region Constructors
public MusicStoreContext(string connectionString)
{
this.ConnectionString = connectionString;
_connectionString = connectionString;
}
#endregion
@@ -202,10 +201,53 @@ namespace Icarus.Models.Context
return song;
}
private MySqlConnection GetConnection()
{
return new MySqlConnection(ConnectionString);
}
private List<Song> ParseData(MySqlDataReader reader)
{
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
}
}
+5 -1
View File
@@ -21,7 +21,11 @@ namespace Icarus.Models.Context
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");
}
}
}