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 _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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<Song> 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())
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,9 @@ namespace Icarus.Models.Context
|
||||
.WithMany(ar => ar.Songs)
|
||||
.HasForeignKey(s => s.ArtistId)
|
||||
.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
|
||||
{
|
||||
[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")]
|
||||
|
||||
@@ -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