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
This commit is contained in:
@@ -36,6 +36,7 @@ namespace Icarus.Controllers.Managers
|
|||||||
private string _tempDirectoryRoot;
|
private string _tempDirectoryRoot;
|
||||||
private string _archiveDirectoryRoot;
|
private string _archiveDirectoryRoot;
|
||||||
private string _compressedSongFilename;
|
private string _compressedSongFilename;
|
||||||
|
private string _message;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -56,6 +57,11 @@ namespace Icarus.Controllers.Managers
|
|||||||
get => _compressedSongFilename;
|
get => _compressedSongFilename;
|
||||||
set => _compressedSongFilename = value;
|
set => _compressedSongFilename = value;
|
||||||
}
|
}
|
||||||
|
public string Message
|
||||||
|
{
|
||||||
|
get => _message;
|
||||||
|
set => _message = value;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -108,6 +114,20 @@ namespace Icarus.Controllers.Managers
|
|||||||
|
|
||||||
return successful;
|
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()
|
public void SaveSongDetails()
|
||||||
{
|
{
|
||||||
@@ -171,6 +191,11 @@ namespace Icarus.Controllers.Managers
|
|||||||
Console.WriteLine($"An Error Occurred: {exMsg}");
|
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)
|
public async Task SaveSong(SongData songData)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -87,16 +87,36 @@ namespace Icarus.Controllers
|
|||||||
|
|
||||||
|
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
[Authorize("read:song_details")]
|
[Authorize("update:songs")]
|
||||||
public IActionResult Put(int id, [FromBody] Song song)
|
public IActionResult Put(int id, [FromBody] Song song)
|
||||||
{
|
{
|
||||||
MusicStoreContext context = HttpContext
|
MusicStoreContext context = HttpContext
|
||||||
.RequestServices
|
.RequestServices
|
||||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
.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;
|
song.Id = id;
|
||||||
Console.WriteLine("Retrieving filepath of song");
|
Console.WriteLine("Retrieving filepath of song");
|
||||||
_logger.LogInformation("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);
|
var oldSongRecord = context.GetSong(id);
|
||||||
song.SongPath = oldSongRecord.SongPath;
|
song.SongPath = oldSongRecord.SongPath;
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,12 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
//using NLog;
|
|
||||||
|
|
||||||
namespace Icarus.Models.Context
|
namespace Icarus.Models.Context
|
||||||
{
|
{
|
||||||
public class MusicStoreContext : BaseStoreContext
|
public class MusicStoreContext : BaseStoreContext
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
//private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -64,7 +62,6 @@ namespace Icarus.Models.Context
|
|||||||
_logger.Error(exMsg, "An error occurred");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: Update this method to be compatible with the new AlbumId field
|
|
||||||
public void UpdateSong(Song song)
|
public void UpdateSong(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -74,9 +71,9 @@ namespace Icarus.Models.Context
|
|||||||
conn.Open();
|
conn.Open();
|
||||||
|
|
||||||
string query = "UPDATE Song SET Title=@Title, AlbumTitle=@AlbumTitle, " +
|
string query = "UPDATE Song SET Title=@Title, AlbumTitle=@AlbumTitle, " +
|
||||||
"Artist=@Artist, Year=@Year, Genre=@Genre, " +
|
"Artist=@Artist, Year=@Year, Genre=@Genre, Duration=@Duration, " +
|
||||||
"Duration=@Duration, Filename=@Filename, " +
|
"Filename=@Filename, SongPath=@SongPath, AlbumId=@AlbumId, " +
|
||||||
"SongPath=@SongPath WHERE Id=@Id";
|
"ArtistId=@ArtistId WHERE Id=@Id";
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||||
@@ -88,10 +85,13 @@ namespace Icarus.Models.Context
|
|||||||
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
||||||
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||||
cmd.Parameters.AddWithValue(@"Id", song.Id);
|
cmd.Parameters.AddWithValue(@"Id", song.Id);
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumId", song.AlbumId);
|
||||||
|
cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_logger.Info("Updated song");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -141,23 +141,6 @@ namespace Icarus.Models.Context
|
|||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
{
|
||||||
songs = ParseData(reader);
|
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;
|
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)
|
public Song GetSong(int id)
|
||||||
{
|
{
|
||||||
Song song = new Song();
|
Song song = new Song();
|
||||||
@@ -190,23 +203,6 @@ namespace Icarus.Models.Context
|
|||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
{
|
||||||
song = ParseSingleData(reader);
|
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");
|
_logger.Info("Song found");
|
||||||
@@ -221,6 +217,23 @@ namespace Icarus.Models.Context
|
|||||||
return song;
|
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<Song> ParseData(MySqlDataReader reader)
|
private List<Song> ParseData(MySqlDataReader reader)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -237,7 +250,9 @@ namespace Icarus.Models.Context
|
|||||||
Genre = reader["Genre"].ToString(),
|
Genre = reader["Genre"].ToString(),
|
||||||
Duration = Convert.ToInt32(reader["Duration"]),
|
Duration = Convert.ToInt32(reader["Duration"]),
|
||||||
Filename = reader["Filename"].ToString(),
|
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(),
|
Genre = reader["Genre"].ToString(),
|
||||||
Duration = Convert.ToInt32(reader["Duration"]),
|
Duration = Convert.ToInt32(reader["Duration"]),
|
||||||
Filename = reader["Filename"].ToString(),
|
Filename = reader["Filename"].ToString(),
|
||||||
SongPath = reader["SongPath"].ToString()
|
SongPath = reader["SongPath"].ToString(),
|
||||||
|
AlbumId = Convert.ToInt32(reader["AlbumId"].ToString()),
|
||||||
|
ArtistId = Convert.ToInt32(reader["ArtistId"].ToString())
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ namespace Icarus.Models.Context
|
|||||||
.WithMany(ar => ar.Songs)
|
.WithMany(ar => ar.Songs)
|
||||||
.HasForeignKey(s => s.ArtistId)
|
.HasForeignKey(s => s.ArtistId)
|
||||||
.HasConstraintName("ForeignKey_Song_Artist");
|
.HasConstraintName("ForeignKey_Song_Artist");
|
||||||
|
modelBuilder.Entity<Song>()
|
||||||
|
.Property(s => s.Year)
|
||||||
|
.IsRequired(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-4
@@ -9,9 +9,6 @@ namespace Icarus.Models
|
|||||||
{
|
{
|
||||||
public class Song
|
public class Song
|
||||||
{
|
{
|
||||||
[JsonIgnore]
|
|
||||||
private MusicStoreContext _context;
|
|
||||||
|
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
[JsonProperty("title")]
|
[JsonProperty("title")]
|
||||||
@@ -21,7 +18,7 @@ namespace Icarus.Models
|
|||||||
[JsonProperty("artist")]
|
[JsonProperty("artist")]
|
||||||
public string Artist { get; set; }
|
public string Artist { get; set; }
|
||||||
[JsonProperty("year")]
|
[JsonProperty("year")]
|
||||||
public int Year { get; set; }
|
public int? Year { get; set; }
|
||||||
[JsonProperty("genre")]
|
[JsonProperty("genre")]
|
||||||
public string Genre { get; set; }
|
public string Genre { get; set; }
|
||||||
[JsonProperty("duration")]
|
[JsonProperty("duration")]
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
delete from Song where Id>0;
|
||||||
|
delete from Album where AlbumId>0;
|
||||||
|
delete from Artist where ArtistId>0;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
select * from Song;
|
||||||
Reference in New Issue
Block a user