From d75d2924a75fd8191c050cb643de7dda812966fd Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sun, 12 May 2019 21:42:25 -0400 Subject: [PATCH] Working on some code cleanup, I thought I was nearing the end. Eventually leading to working on how to stream songs from the server but more work has to be done. I know I have integrated Auth0 but if this project is to be useful to others without having to configure Auth0 then I would like for it to be usable with a couple commands after cloning the repository. More work has to be done but I am close to the releasing the first version. Will update to the project on Github soon. #37 --- Controllers/Managers/SongManager.cs | 25 +++++++ Controllers/SongController.cs | 22 +++++- Models/Context/MusicStoreContext.cs | 103 ++++++++++++++++------------ Models/Context/SongContext.cs | 3 + Models/Song.cs | 5 +- Scripts/MySQL/delete_all.sql | 3 + Scripts/MySQL/display_songs.sql | 1 + 7 files changed, 114 insertions(+), 48 deletions(-) create mode 100644 Scripts/MySQL/delete_all.sql create mode 100644 Scripts/MySQL/display_songs.sql diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 7b3a4b0..5e08197 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -36,6 +36,7 @@ namespace Icarus.Controllers.Managers private string _tempDirectoryRoot; private string _archiveDirectoryRoot; private string _compressedSongFilename; + private string _message; #endregion @@ -56,6 +57,11 @@ namespace Icarus.Controllers.Managers get => _compressedSongFilename; set => _compressedSongFilename = value; } + public string Message + { + get => _message; + set => _message = value; + } #endregion @@ -108,6 +114,20 @@ namespace Icarus.Controllers.Managers return successful; } + // TODO: Implement method + // This method should do the following, with the help of existing methods + // or create helper methods to compelete to intended purpose: + // + // 1. Delete song from the filesystem + // 2. Delete the song record from the database + // 3. Decrement the SongCount value or delete the album record from the Database + // 4. Decrement the SongCount value or delete the artist record from the Database + public bool DeleteSongFromFileSystem(Song song, MusicStoreContext songStore, + AlbumStoreContext albumStore, ArtistStoreContext artistStore) + { + + return false; + } public void SaveSongDetails() { @@ -171,6 +191,11 @@ namespace Icarus.Controllers.Managers Console.WriteLine($"An Error Occurred: {exMsg}"); } } + // TODO: This method should update the Song, Album, and Artist records in the database + public void UpdateSong(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore, + ArtistStoreContext artistStore) + { + } public async Task SaveSong(SongData songData) { diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 6d2b58a..2222828 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -87,16 +87,36 @@ namespace Icarus.Controllers [HttpPut("{id}")] - [Authorize("read:song_details")] + [Authorize("update:songs")] public IActionResult Put(int id, [FromBody] Song song) { MusicStoreContext context = HttpContext .RequestServices .GetService(typeof(MusicStoreContext)) as MusicStoreContext; + ArtistStoreContext artistStore = HttpContext + .RequestServices + .GetService(typeof(ArtistStoreContext)) as ArtistStoreContext; + + AlbumStoreContext albumStore = HttpContext + .RequestServices + .GetService(typeof(AlbumStoreContext)) as AlbumStoreContext; + song.Id = id; Console.WriteLine("Retrieving filepath of song"); _logger.LogInformation("Retrieving filepath of song"); + + if (!context.DoesSongExist(song)) + { + return NotFound(new SongResult + { + Message = "Song does not exist" + }); + } + // TODO: Provide functionality for the UpdateSong(...) method + // before removing the below return statement + return Ok("song exists"); + var oldSongRecord = context.GetSong(id); song.SongPath = oldSongRecord.SongPath; diff --git a/Models/Context/MusicStoreContext.cs b/Models/Context/MusicStoreContext.cs index 4a6c83e..53569b0 100644 --- a/Models/Context/MusicStoreContext.cs +++ b/Models/Context/MusicStoreContext.cs @@ -2,14 +2,12 @@ using System; using System.Collections.Generic; using MySql.Data.MySqlClient; -//using NLog; namespace Icarus.Models.Context { public class MusicStoreContext : BaseStoreContext { #region Fields - //private static Logger _logger = NLog.LogManager.GetCurrentClassLogger(); #endregion @@ -64,7 +62,6 @@ namespace Icarus.Models.Context _logger.Error(exMsg, "An error occurred"); } } - // TODO: Update this method to be compatible with the new AlbumId field public void UpdateSong(Song song) { try @@ -73,10 +70,10 @@ namespace Icarus.Models.Context { conn.Open(); - string query = "UPDATE Song SET Title=@Title, AlbumTitle=@AlbumTitle, " + - "Artist=@Artist, Year=@Year, Genre=@Genre, " + - "Duration=@Duration, Filename=@Filename, " + - "SongPath=@SongPath WHERE Id=@Id"; + string query = "UPDATE Song SET Title=@Title, AlbumTitle=@AlbumTitle, " + + "Artist=@Artist, Year=@Year, Genre=@Genre, Duration=@Duration, " + + "Filename=@Filename, SongPath=@SongPath, AlbumId=@AlbumId, " + + "ArtistId=@ArtistId WHERE Id=@Id"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", song.Title); @@ -88,10 +85,13 @@ namespace Icarus.Models.Context cmd.Parameters.AddWithValue("@Filename", song.Filename); cmd.Parameters.AddWithValue("@SongPath", song.SongPath); cmd.Parameters.AddWithValue(@"Id", song.Id); + cmd.Parameters.AddWithValue("@AlbumId", song.AlbumId); + cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId); cmd.ExecuteNonQuery(); } } + _logger.Info("Updated song"); } catch (Exception ex) { @@ -141,23 +141,6 @@ namespace Icarus.Models.Context using (var reader = cmd.ExecuteReader()) { songs = ParseData(reader); - /** - while (reader.Read()) - { - songs.Add(new Song - { - Id = Convert.ToInt32(reader["Id"]), - Title = reader["Title"].ToString(), - AlbumTitle = reader["AlbumTitle"].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() - }); - } - */ } } } @@ -172,6 +155,36 @@ namespace Icarus.Models.Context return songs; } + public Song GetSong(Song song) + { + try + { + _logger.Info("Retrieving song from database"); + + using (var conn = GetConnection()) + { + conn.Open(); + var query = "SELECT * FROM Song WHERE Id=@Id"; + + using (var cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Id", song.Id); + + using (var reader = cmd.ExecuteReader()) + { + song = ParseSingleData(reader); + } + } + } + } + catch (Exception ex) + { + var msg = ex.Message; + _logger.Error(msg, "An error occurred"); + } + + return song; + } public Song GetSong(int id) { Song song = new Song(); @@ -190,23 +203,6 @@ namespace Icarus.Models.Context using (var reader = cmd.ExecuteReader()) { song = ParseSingleData(reader); - /** - while (reader.Read()) - { - song = new Song - { - Id = Convert.ToInt32(reader["Id"]), - Title = reader["Title"].ToString(), - AlbumTitle = reader["AlbumTitle"].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() - }; - } - */ } } _logger.Info("Song found"); @@ -220,6 +216,23 @@ namespace Icarus.Models.Context return song; } + + public bool DoesSongExist(Song song) + { + _logger.Info("Checking to see if the song exists"); + var songInDatabase = GetSong(song); + var title = songInDatabase.Title; + + if (!string.IsNullOrEmpty(title)) + { + _logger.Info("Song exists"); + return true; + } + + _logger.Info("Song does not exists"); + + return false; + } private List ParseData(MySqlDataReader reader) { @@ -237,7 +250,9 @@ namespace Icarus.Models.Context Genre = reader["Genre"].ToString(), Duration = Convert.ToInt32(reader["Duration"]), Filename = reader["Filename"].ToString(), - SongPath = reader["SongPath"].ToString() + SongPath = reader["SongPath"].ToString(), + AlbumId = Convert.ToInt32(reader["AlbumId"].ToString()), + ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()) }); } @@ -261,7 +276,9 @@ namespace Icarus.Models.Context Genre = reader["Genre"].ToString(), Duration = Convert.ToInt32(reader["Duration"]), Filename = reader["Filename"].ToString(), - SongPath = reader["SongPath"].ToString() + SongPath = reader["SongPath"].ToString(), + AlbumId = Convert.ToInt32(reader["AlbumId"].ToString()), + ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()) }; } diff --git a/Models/Context/SongContext.cs b/Models/Context/SongContext.cs index 6ed8fd8..590bcc4 100644 --- a/Models/Context/SongContext.cs +++ b/Models/Context/SongContext.cs @@ -33,6 +33,9 @@ namespace Icarus.Models.Context .WithMany(ar => ar.Songs) .HasForeignKey(s => s.ArtistId) .HasConstraintName("ForeignKey_Song_Artist"); + modelBuilder.Entity() + .Property(s => s.Year) + .IsRequired(false); } } } diff --git a/Models/Song.cs b/Models/Song.cs index 75cc762..729a919 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -9,9 +9,6 @@ namespace Icarus.Models { public class Song { - [JsonIgnore] - private MusicStoreContext _context; - [JsonProperty("id")] public int Id { get; set; } [JsonProperty("title")] @@ -21,7 +18,7 @@ namespace Icarus.Models [JsonProperty("artist")] public string Artist { get; set; } [JsonProperty("year")] - public int Year { get; set; } + public int? Year { get; set; } [JsonProperty("genre")] public string Genre { get; set; } [JsonProperty("duration")] diff --git a/Scripts/MySQL/delete_all.sql b/Scripts/MySQL/delete_all.sql new file mode 100644 index 0000000..d274c22 --- /dev/null +++ b/Scripts/MySQL/delete_all.sql @@ -0,0 +1,3 @@ +delete from Song where Id>0; +delete from Album where AlbumId>0; +delete from Artist where ArtistId>0; diff --git a/Scripts/MySQL/display_songs.sql b/Scripts/MySQL/display_songs.sql new file mode 100644 index 0000000..e693e0f --- /dev/null +++ b/Scripts/MySQL/display_songs.sql @@ -0,0 +1 @@ +select * from Song;