Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3948e25fc3 | |||
| a39c232c34 | |||
| 8ec7092272 | |||
| 84615472ef | |||
| e4be56101a | |||
| 59895af3b4 | |||
| 1004f6c93d | |||
| 026a528a81 | |||
| 226ca44206 | |||
| 4940768d22 | |||
| f7963237fd | |||
| 0ee1473639 | |||
| 2b59bfac32 | |||
| 0932be2f2b | |||
| 82686f5c33 | |||
| b5501e323f | |||
| 089bd1191c | |||
| 56fe805f80 | |||
| ada8fa79d3 | |||
| bc209c2363 |
@@ -8,25 +8,24 @@ using Icarus.Authorization;
|
|||||||
|
|
||||||
namespace Icarus.Authorization.Handlers
|
namespace Icarus.Authorization.Handlers
|
||||||
{
|
{
|
||||||
public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
|
public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
|
||||||
|
{
|
||||||
|
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement)
|
||||||
{
|
{
|
||||||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
|
if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer))
|
||||||
HasScopeRequirement requirement)
|
{
|
||||||
{
|
return Task.CompletedTask;
|
||||||
if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer))
|
}
|
||||||
{
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
var scopes = context.User.FindFirst(c => c.Type == "scope" &&
|
var scopes = context.User.FindFirst(c =>
|
||||||
c.Issuer == requirement.Issuer).Value.Split(' ');
|
c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' ');
|
||||||
|
|
||||||
if (scopes.Any(s => s == requirement.Scope))
|
if (scopes.Any(s => s == requirement.Scope))
|
||||||
{
|
{
|
||||||
context.Succeed(requirement);
|
context.Succeed(requirement);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,16 @@ using Microsoft.AspNetCore.Authorization;
|
|||||||
|
|
||||||
namespace Icarus.Authorization
|
namespace Icarus.Authorization
|
||||||
{
|
{
|
||||||
public class HasScopeRequirement : IAuthorizationRequirement
|
public class HasScopeRequirement : IAuthorizationRequirement
|
||||||
|
{
|
||||||
|
public string Issuer { get; }
|
||||||
|
public string Scope { get; }
|
||||||
|
|
||||||
|
|
||||||
|
public HasScopeRequirement(string scope, string issuer)
|
||||||
{
|
{
|
||||||
public string Issuer { get; }
|
Scope = scope ?? throw new ArgumentNullException(nameof(scope));
|
||||||
public string Scope { get; }
|
Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer));
|
||||||
|
|
||||||
|
|
||||||
public HasScopeRequirement(string scope, string issuer)
|
|
||||||
{
|
|
||||||
Scope = scope ?? throw new ArgumentNullException(nameof(scope));
|
|
||||||
Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Icarus.Constants
|
||||||
|
{
|
||||||
|
public class DirectoryPaths
|
||||||
|
{
|
||||||
|
public static string CoverArtPath =>
|
||||||
|
Directory.GetCurrentDirectory() + "/Images/Stock/CoverArt.png";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,21 +8,21 @@ using Icarus.Database.Contexts;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class AlbumManager : BaseManager
|
public class AlbumManager : BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,21 +8,21 @@ using Icarus.Database.Contexts;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class ArtistManager : BaseManager
|
public class ArtistManager : BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ using NLog;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class BaseManager
|
public class BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
protected static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
protected static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,127 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using Icarus.Constants;
|
||||||
|
using Icarus.Controllers.Utilities;
|
||||||
|
using Icarus.Database.Repositories;
|
||||||
|
using Icarus.Models;
|
||||||
|
using Icarus.Types;
|
||||||
|
|
||||||
|
namespace Icarus.Controllers.Managers
|
||||||
|
{
|
||||||
|
public class CoverArtManager : BaseManager
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private string _rootCoverArtPath;
|
||||||
|
private byte[] _stockCoverArt = null;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public CoverArtManager(string rootPath)
|
||||||
|
{
|
||||||
|
_rootCoverArtPath = rootPath;
|
||||||
|
Initialize();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
public void SaveCoverArtToDatabase(ref Song song, ref CoverArt coverArt,
|
||||||
|
CoverArtRepository coverArtRepository)
|
||||||
|
{
|
||||||
|
_logger.Info("Saving cover art record to the database");
|
||||||
|
coverArtRepository.SaveCoverArt(coverArt);
|
||||||
|
|
||||||
|
coverArt = coverArtRepository.GetCoverArt(CoverArtField.SongTitle,
|
||||||
|
coverArt);
|
||||||
|
|
||||||
|
song.CoverArtId = coverArt.CoverArtId;
|
||||||
|
}
|
||||||
|
public void DeleteCoverArtFromDatabase(CoverArt coverArt,
|
||||||
|
CoverArtRepository coverArtRepository)
|
||||||
|
{
|
||||||
|
_logger.Info("Attempting to delete cover art from the database");
|
||||||
|
coverArtRepository.DeleteCoverArt(coverArt);
|
||||||
|
}
|
||||||
|
public void DeleteCoverArt(CoverArt coverArt)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var stockCoverArtPath = _rootCoverArtPath + "CoverArt.png";
|
||||||
|
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath,
|
||||||
|
StringComparison.CurrentCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
_logger.Info("Song does not contain the stock cover art");
|
||||||
|
File.Delete(coverArt.ImagePath);
|
||||||
|
_logger.Info("Cover art deleted from the filesystem");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Info("Song contains the stock cover art");
|
||||||
|
_logger.Info("Will not delete from from the filesystem");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public CoverArt SaveCoverArt(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var dirMgr = new DirectoryManager(_rootCoverArtPath);
|
||||||
|
dirMgr.CreateDirectory(song);
|
||||||
|
var imagePath = dirMgr.SongDirectory + song.Title + ".png";
|
||||||
|
var coverArt = new CoverArt
|
||||||
|
{
|
||||||
|
SongTitle = song.Title,
|
||||||
|
ImagePath = imagePath
|
||||||
|
};
|
||||||
|
|
||||||
|
var metaData = new MetadataRetriever();
|
||||||
|
var imgBytes = metaData.RetrieveCoverArtBytes(song);
|
||||||
|
|
||||||
|
if (imgBytes != null)
|
||||||
|
{
|
||||||
|
_logger.Info("Saving cover art to the filesystem");
|
||||||
|
File.WriteAllBytes(coverArt.ImagePath, imgBytes);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Info("Song has no cover art, applying stock cover art");
|
||||||
|
coverArt.ImagePath = _rootCoverArtPath + "CoverArt.png";
|
||||||
|
metaData.UpdateCoverArt(song, coverArt);
|
||||||
|
File.WriteAllBytes(coverArt.ImagePath, _stockCoverArt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return coverArt;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Initialize()
|
||||||
|
{
|
||||||
|
if (System.IO.File.Exists(DirectoryPaths.CoverArtPath))
|
||||||
|
_stockCoverArt = File.ReadAllBytes(DirectoryPaths.CoverArtPath);
|
||||||
|
|
||||||
|
if (!File.Exists(_rootCoverArtPath + "CoverArt.png"))
|
||||||
|
{
|
||||||
|
File.WriteAllBytes(_rootCoverArtPath + "CoverArt.png",
|
||||||
|
_stockCoverArt);
|
||||||
|
Console.WriteLine("Copied Stock Cover Art");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,201 +5,237 @@ using System.Linq;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
using Icarus.Types;
|
||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class DirectoryManager : BaseManager
|
public class DirectoryManager : BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private Song _song;
|
private Song _song;
|
||||||
private string _rootSongDirectory;
|
private string _rootSongDirectory;
|
||||||
private string _songDirectory;
|
private string _songDirectory;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
public string SongDirectory
|
public string SongDirectory
|
||||||
{
|
{
|
||||||
get => _songDirectory;
|
get => _songDirectory;
|
||||||
set => _songDirectory = value;
|
set => _songDirectory = value;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public DirectoryManager(IConfiguration config, Song song)
|
public DirectoryManager(IConfiguration config, Song song)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_song = song;
|
_song = song;
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
public DirectoryManager(IConfiguration config)
|
public DirectoryManager(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
#endregion
|
public DirectoryManager(string rootDirectory)
|
||||||
|
{
|
||||||
|
_rootSongDirectory = rootDirectory;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public void CreateDirectory()
|
public void CreateDirectory()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_songDirectory = AlbumDirectory();
|
_songDirectory = AlbumDirectory();
|
||||||
|
|
||||||
if (!Directory.Exists(_songDirectory))
|
if (!Directory.Exists(_songDirectory))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(_songDirectory);
|
Directory.CreateDirectory(_songDirectory);
|
||||||
Console.WriteLine("The directory has been created");
|
Console.WriteLine("The directory has been created");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine($"The song will be saved in the following" +
|
Console.WriteLine($"The song will be saved in the following" +
|
||||||
$" directory: {_songDirectory}");
|
$" directory: {_songDirectory}");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"An error occurred {ex.Message}");
|
Console.WriteLine($"An error occurred {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void DeleteEmptyDirectories()
|
public void CreateDirectory(Song song)
|
||||||
{
|
{
|
||||||
try
|
_song = song;
|
||||||
{
|
|
||||||
var albumDirectory = AlbumDirectory();
|
|
||||||
var artistDirectory = ArtistDirectory();
|
|
||||||
if (IsDirectoryEmpty(albumDirectory))
|
|
||||||
{
|
|
||||||
Directory.Delete(albumDirectory);
|
|
||||||
Console.WriteLine($"directory {albumDirectory} deleted");
|
|
||||||
}
|
|
||||||
if (IsDirectoryEmpty(artistDirectory))
|
|
||||||
{
|
|
||||||
Directory.Delete(artistDirectory);
|
|
||||||
Console.WriteLine($"directory {artistDirectory} deleted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var exMsg = ex.Message;
|
|
||||||
Console.WriteLine($"An error occurred {exMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void DeleteEmptyDirectories(Song song)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var albumDirectory = AlbumDirectory(song);
|
|
||||||
var artistDirectory = ArtistDirectory(song);
|
|
||||||
|
|
||||||
if (IsDirectoryEmpty(albumDirectory))
|
try
|
||||||
{
|
{
|
||||||
Directory.Delete(albumDirectory);
|
_songDirectory = AlbumDirectory();
|
||||||
_logger.Info("Album directory deleted");
|
|
||||||
}
|
|
||||||
if (IsDirectoryEmpty(artistDirectory))
|
|
||||||
{
|
|
||||||
Directory.Delete(artistDirectory);
|
|
||||||
_logger.Info("Artist directory deleted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string RetrieveAlbumPath(Song song)
|
if (!Directory.Exists(_songDirectory))
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving album song path");
|
Directory.CreateDirectory(_songDirectory);
|
||||||
|
Console.WriteLine($"The directory has been created");
|
||||||
|
}
|
||||||
|
|
||||||
var albumPath = string.Empty;
|
Console.WriteLine($"The song will be saved in the following" +
|
||||||
albumPath = AlbumDirectory(song);
|
$" directory: {_songDirectory}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteEmptyDirectories()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var albumDirectory = AlbumDirectory();
|
||||||
|
var artistDirectory = ArtistDirectory();
|
||||||
|
if (IsDirectoryEmpty(albumDirectory))
|
||||||
|
{
|
||||||
|
Directory.Delete(albumDirectory);
|
||||||
|
Console.WriteLine($"directory {albumDirectory} deleted");
|
||||||
|
}
|
||||||
|
if (IsDirectoryEmpty(artistDirectory))
|
||||||
|
{
|
||||||
|
Directory.Delete(artistDirectory);
|
||||||
|
Console.WriteLine($"directory {artistDirectory} deleted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var exMsg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred {exMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteEmptyDirectories(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var albumDirectory = AlbumDirectory(song);
|
||||||
|
var artistDirectory = ArtistDirectory(song);
|
||||||
|
|
||||||
return albumPath;
|
if (IsDirectoryEmpty(albumDirectory))
|
||||||
}
|
{
|
||||||
public string RetrieveArtistPath(Song song)
|
Directory.Delete(albumDirectory);
|
||||||
{
|
_logger.Info("Album directory deleted");
|
||||||
_logger.Info("Retrieving artist path");
|
}
|
||||||
|
if (IsDirectoryEmpty(artistDirectory))
|
||||||
|
{
|
||||||
|
Directory.Delete(artistDirectory);
|
||||||
|
_logger.Info("Artist directory deleted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var artistPath = string.Empty;
|
public string RetrieveAlbumPath(Song song)
|
||||||
artistPath = ArtistDirectory(song);
|
{
|
||||||
|
_logger.Info("Retrieving album song path");
|
||||||
|
|
||||||
return artistPath;
|
var albumPath = string.Empty;
|
||||||
}
|
albumPath = AlbumDirectory(song);
|
||||||
public string GenerateSongPath(Song song)
|
|
||||||
{
|
|
||||||
_logger.Info("Generating song path");
|
|
||||||
|
|
||||||
var songPath = string.Empty;
|
return albumPath;
|
||||||
var artistPath = ArtistDirectory(song);
|
}
|
||||||
var albumPath = AlbumDirectory(song);
|
public string RetrieveArtistPath(Song song)
|
||||||
|
{
|
||||||
|
_logger.Info("Retrieving artist path");
|
||||||
|
|
||||||
if (!Directory.Exists(artistPath))
|
var artistPath = string.Empty;
|
||||||
{
|
artistPath = ArtistDirectory(song);
|
||||||
_logger.Info("Artist path does not exist");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(artistPath);
|
return artistPath;
|
||||||
|
}
|
||||||
|
public string GenerateSongPath(Song song)
|
||||||
|
{
|
||||||
|
_logger.Info("Generating song path");
|
||||||
|
|
||||||
_logger.Info("Creating artist path");
|
var songPath = string.Empty;
|
||||||
}
|
var artistPath = ArtistDirectory(song);
|
||||||
if (!Directory.Exists(albumPath))
|
var albumPath = AlbumDirectory(song);
|
||||||
{
|
|
||||||
_logger.Info("Album path does not exist");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(albumPath);
|
if (!Directory.Exists(artistPath))
|
||||||
|
{
|
||||||
|
_logger.Info("Artist path does not exist");
|
||||||
|
|
||||||
_logger.Info("Created album path");
|
Directory.CreateDirectory(artistPath);
|
||||||
}
|
|
||||||
|
|
||||||
songPath = albumPath;
|
_logger.Info("Creating artist path");
|
||||||
|
}
|
||||||
|
if (!Directory.Exists(albumPath))
|
||||||
|
{
|
||||||
|
_logger.Info("Album path does not exist");
|
||||||
|
|
||||||
return songPath;
|
Directory.CreateDirectory(albumPath);
|
||||||
}
|
|
||||||
|
|
||||||
private void Initialize()
|
_logger.Info("Created album path");
|
||||||
{
|
}
|
||||||
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsDirectoryEmpty(string path)
|
songPath = albumPath;
|
||||||
{
|
|
||||||
return !Directory.EnumerateFileSystemEntries(path).Any();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string AlbumDirectory()
|
return songPath;
|
||||||
{
|
}
|
||||||
string directory = _rootSongDirectory;
|
|
||||||
directory += $@"{_song.Artist}/{_song.AlbumTitle}/";
|
|
||||||
|
|
||||||
return directory;
|
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
|
||||||
}
|
{
|
||||||
private string AlbumDirectory(Song song)
|
switch (dirTypes)
|
||||||
{
|
{
|
||||||
var directory = _rootSongDirectory;
|
case DirectoryType.Music:
|
||||||
directory += $@"{song.Artist}/{song.AlbumTitle}/";
|
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
|
||||||
Console.WriteLine($"Album directory {directory}");
|
break;
|
||||||
|
case DirectoryType.CoverArt:
|
||||||
|
_rootSongDirectory = _config.GetValue<string>("CoverArtPath");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return directory;
|
private bool IsDirectoryEmpty(string path)
|
||||||
}
|
{
|
||||||
private string ArtistDirectory()
|
return !Directory.EnumerateFileSystemEntries(path).Any();
|
||||||
{
|
}
|
||||||
var directory = _rootSongDirectory;
|
|
||||||
directory += $@"{_song.Artist}/";
|
|
||||||
|
|
||||||
return directory;
|
private string AlbumDirectory()
|
||||||
}
|
{
|
||||||
private string ArtistDirectory(Song song)
|
string directory = _rootSongDirectory;
|
||||||
{
|
directory += $@"{_song.Artist}/{_song.AlbumTitle}/";
|
||||||
var directory = _rootSongDirectory;
|
|
||||||
directory += $@"{song.Artist}/";
|
|
||||||
Console.WriteLine($"Artist directory {directory}");
|
|
||||||
|
|
||||||
return directory;
|
return directory;
|
||||||
}
|
}
|
||||||
#endregion
|
private string AlbumDirectory(Song song)
|
||||||
}
|
{
|
||||||
|
var directory = _rootSongDirectory;
|
||||||
|
directory += $@"{song.Artist}/{song.AlbumTitle}/";
|
||||||
|
Console.WriteLine($"Album directory {directory}");
|
||||||
|
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
private string ArtistDirectory()
|
||||||
|
{
|
||||||
|
var directory = _rootSongDirectory;
|
||||||
|
directory += $@"{_song.Artist}/";
|
||||||
|
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
private string ArtistDirectory(Song song)
|
||||||
|
{
|
||||||
|
var directory = _rootSongDirectory;
|
||||||
|
directory += $@"{song.Artist}/";
|
||||||
|
Console.WriteLine($"Artist directory {directory}");
|
||||||
|
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,21 +8,21 @@ using Icarus.Database.Contexts;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class GenreManager : BaseManager
|
public class GenreManager : BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1198
-1207
File diff suppressed because it is too large
Load Diff
@@ -10,132 +10,125 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class TokenManager : BaseManager
|
public class TokenManager : BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private string _clientId;
|
private string _clientId;
|
||||||
private string _clientSecret;
|
private string _clientSecret;
|
||||||
private string _audience;
|
private string _audience;
|
||||||
private string _grantType;
|
private string _grantType;
|
||||||
private string _url;
|
private string _url;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public TokenManager(IConfiguration config)
|
public TokenManager(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
InitializeValues();
|
InitializeValues();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public LoginResult RetrieveLoginResult(User user)
|
public LoginResult RetrieveLoginResult(User user)
|
||||||
{
|
{
|
||||||
_logger.Info("Preparing Auth0 API request");
|
_logger.Info("Preparing Auth0 API request");
|
||||||
|
|
||||||
var client = new RestClient(_url);
|
var client = new RestClient(_url);
|
||||||
var request = new RestRequest("oauth/token", Method.POST);
|
var request = new RestRequest("oauth/token", Method.POST);
|
||||||
var tokenRequest = RetrieveTokenRequest();
|
var tokenRequest = RetrieveTokenRequest();
|
||||||
|
|
||||||
_logger.Info("Serializing token object into JSON");
|
_logger.Info("Serializing token object into JSON");
|
||||||
var tokenObject = JsonConvert.SerializeObject(tokenRequest);
|
var tokenObject = JsonConvert.SerializeObject(tokenRequest);
|
||||||
Console.WriteLine(tokenObject);
|
|
||||||
request.AddParameter("application/json; charset=utf-8",
|
|
||||||
tokenObject, ParameterType.RequestBody);
|
|
||||||
|
|
||||||
request.RequestFormat = DataFormat.Json;
|
request.AddParameter("application/json; charset=utf-8",
|
||||||
|
tokenObject, ParameterType.RequestBody);
|
||||||
|
|
||||||
_logger.Info("Sending request");
|
request.RequestFormat = DataFormat.Json;
|
||||||
IRestResponse response = client.Execute(request);
|
|
||||||
_logger.Info("Response received");
|
_logger.Info("Sending request");
|
||||||
|
IRestResponse response = client.Execute(request);
|
||||||
|
_logger.Info("Response received");
|
||||||
|
|
||||||
|
|
||||||
_logger.Info("Deserializing response");
|
_logger.Info("Deserializing response");
|
||||||
var tokenResult = JsonConvert
|
var tokenResult = JsonConvert
|
||||||
.DeserializeObject<Token>(response.Content);
|
.DeserializeObject<Token>(response.Content);
|
||||||
_logger.Info("Response deserialized");
|
_logger.Info("Response deserialized");
|
||||||
Console.WriteLine(response.Content);
|
|
||||||
|
|
||||||
return new LoginResult
|
return new LoginResult
|
||||||
{
|
{
|
||||||
UserId = user.Id,
|
UserId = user.Id, Username = user.Username, Token = tokenResult.AccessToken,
|
||||||
Username = user.Username,
|
TokenType = tokenResult.TokenType, Expiration = tokenResult.Expiration,
|
||||||
Token = tokenResult.AccessToken,
|
Message = "Successfully retrieved token"
|
||||||
TokenType = tokenResult.TokenType,
|
};
|
||||||
Expiration = tokenResult.Expiration,
|
}
|
||||||
Message = "Successfully retrieved token"
|
|
||||||
};
|
private TokenRequest RetrieveTokenRequest()
|
||||||
}
|
{
|
||||||
|
_logger.Info("Retrieving token object");
|
||||||
private TokenRequest RetrieveTokenRequest()
|
|
||||||
{
|
|
||||||
_logger.Info("Retrieving token object");
|
|
||||||
|
|
||||||
return new TokenRequest
|
return new TokenRequest
|
||||||
{
|
{
|
||||||
ClientId = _clientId,
|
ClientId = _clientId, ClientSecret = _clientSecret,
|
||||||
ClientSecret = _clientSecret,
|
Audience = _audience, GrantType = _grantType
|
||||||
Audience = _audience,
|
};
|
||||||
GrantType = _grantType
|
}
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeValues()
|
private void InitializeValues()
|
||||||
{
|
{
|
||||||
_logger.Info("Analyzing Auth0 information");
|
_logger.Info("Analyzing Auth0 information");
|
||||||
|
|
||||||
_clientId = _config["Auth0:ClientId"];
|
_clientId = _config["Auth0:ClientId"];
|
||||||
_clientSecret = _config["Auth0:ClientSecret"];
|
_clientSecret = _config["Auth0:ClientSecret"];
|
||||||
_audience = _config["Auth0:ApiIdentifier"];
|
_audience = _config["Auth0:ApiIdentifier"];
|
||||||
_grantType = "client_credentials";
|
_grantType = "client_credentials";
|
||||||
_url = $"https://{_config["Auth0:Domain"]}";
|
_url = $"https://{_config["Auth0:Domain"]}";
|
||||||
|
|
||||||
PrintCredentials();
|
PrintCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
#region Testing Methods
|
||||||
|
// For testing purposes
|
||||||
#region Testing Methods
|
private void PrintCredentials()
|
||||||
// For testing purposes
|
{
|
||||||
private void PrintCredentials()
|
Console.WriteLine("Auth0 credentials:");
|
||||||
{
|
Console.WriteLine($"Client Id: {_clientId}");
|
||||||
Console.WriteLine("Auth0 credentials:");
|
Console.WriteLine($"Client Secret: {_clientSecret}");
|
||||||
Console.WriteLine($"Client Id: {_clientId}");
|
Console.WriteLine($"Audience: {_audience}");
|
||||||
Console.WriteLine($"Client Secret: {_clientSecret}");
|
Console.WriteLine($"Url: {_url}");
|
||||||
Console.WriteLine($"Audience: {_audience}");
|
}
|
||||||
Console.WriteLine($"Url: {_url}");
|
#endregion
|
||||||
}
|
#endregion
|
||||||
#endregion
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Classes
|
#region Classes
|
||||||
private class TokenRequest
|
private class TokenRequest
|
||||||
{
|
{
|
||||||
[JsonProperty("client_id")]
|
[JsonProperty("client_id")]
|
||||||
public string ClientId { get; set; }
|
public string ClientId { get; set; }
|
||||||
[JsonProperty("client_secret")]
|
[JsonProperty("client_secret")]
|
||||||
public string ClientSecret { get; set; }
|
public string ClientSecret { get; set; }
|
||||||
[JsonProperty("audience")]
|
[JsonProperty("audience")]
|
||||||
public string Audience { get; set; }
|
public string Audience { get; set; }
|
||||||
[JsonProperty("grant_type")]
|
[JsonProperty("grant_type")]
|
||||||
public string GrantType { get; set; }
|
public string GrantType { get; set; }
|
||||||
}
|
}
|
||||||
private class Token
|
private class Token
|
||||||
{
|
{
|
||||||
[JsonProperty("access_token")]
|
[JsonProperty("access_token")]
|
||||||
public string AccessToken { get; set; }
|
public string AccessToken { get; set; }
|
||||||
[JsonProperty("expires_in")]
|
[JsonProperty("expires_in")]
|
||||||
public int Expiration { get; set; }
|
public int Expiration { get; set; }
|
||||||
[JsonProperty("token_type")]
|
[JsonProperty("token_type")]
|
||||||
public string TokenType { get; set; }
|
public string TokenType { get; set; }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,21 +8,21 @@ using Icarus.Database.Contexts;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
public class YearManager : BaseManager
|
public class YearManager : BaseManager
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,284 +9,313 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Utilities
|
namespace Icarus.Controllers.Utilities
|
||||||
{
|
{
|
||||||
public class MetadataRetriever
|
public class MetadataRetriever
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private static NLog.Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
private static NLog.Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
||||||
private Song _updatedSong;
|
private Song _updatedSong;
|
||||||
private string _message;
|
private string _message;
|
||||||
private string _title;
|
private string _title;
|
||||||
private string _artist;
|
private string _artist;
|
||||||
private string _album;
|
private string _album;
|
||||||
private string _genre;
|
private string _genre;
|
||||||
private int _year;
|
private int _year;
|
||||||
private int _duration;
|
private int _duration;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
public Song UpdatedSongRecord
|
public Song UpdatedSongRecord
|
||||||
{
|
{
|
||||||
get => _updatedSong;
|
get => _updatedSong;
|
||||||
set => _updatedSong = value;
|
set => _updatedSong = value;
|
||||||
}
|
}
|
||||||
public string Message
|
public string Message
|
||||||
{
|
{
|
||||||
get => _message;
|
get => _message;
|
||||||
set => _message = value;
|
set => _message = value;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public static void PrintMetadata(Song song)
|
public static void PrintMetadata(Song song)
|
||||||
{
|
{
|
||||||
Console.WriteLine("\n\nMetadata of the song:");
|
Console.WriteLine("\n\nMetadata of the song:");
|
||||||
Console.WriteLine($"Id: {song.Id}");
|
Console.WriteLine($"Id: {song.Id}");
|
||||||
Console.WriteLine($"Title: {song.Title}");
|
Console.WriteLine($"Title: {song.Title}");
|
||||||
Console.WriteLine($"Artist: {song.Artist}");
|
Console.WriteLine($"Artist: {song.Artist}");
|
||||||
Console.WriteLine($"Album: {song.AlbumTitle}");
|
Console.WriteLine($"Album: {song.AlbumTitle}");
|
||||||
Console.WriteLine($"Genre: {song.Genre}");
|
Console.WriteLine($"Genre: {song.Genre}");
|
||||||
Console.WriteLine($"Year: {song.Year}");
|
Console.WriteLine($"Year: {song.Year}");
|
||||||
Console.WriteLine($"Duration: {song.Duration}");
|
Console.WriteLine($"Duration: {song.Duration}");
|
||||||
Console.WriteLine($"AlbumId: {song.AlbumId}");
|
Console.WriteLine($"AlbumId: {song.AlbumId}");
|
||||||
Console.WriteLine($"ArtistId: {song.ArtistId}");
|
Console.WriteLine($"ArtistId: {song.ArtistId}");
|
||||||
Console.WriteLine($"GenreId: {song.GenreId}");
|
Console.WriteLine($"GenreId: {song.GenreId}");
|
||||||
Console.WriteLine($"YearId: {song.YearId}");
|
Console.WriteLine($"YearId: {song.YearId}");
|
||||||
Console.WriteLine($"Song Path: {song.SongPath}");
|
Console.WriteLine($"Song Path: {song.SongPath}");
|
||||||
Console.WriteLine($"Filename: {song.Filename}");
|
Console.WriteLine($"Filename: {song.Filename}");
|
||||||
Console.WriteLine("\n");
|
Console.WriteLine("\n");
|
||||||
|
|
||||||
_logger.Info("Metadata of the song");
|
_logger.Info("Metadata of the song");
|
||||||
_logger.Info($"Title: {song.Title}");
|
_logger.Info($"Title: {song.Title}");
|
||||||
_logger.Info($"Artist: {song.Artist}");
|
_logger.Info($"Artist: {song.Artist}");
|
||||||
_logger.Info($"Album: {song.AlbumTitle}");
|
_logger.Info($"Album: {song.AlbumTitle}");
|
||||||
_logger.Info($"Genre: {song.Genre}");
|
_logger.Info($"Genre: {song.Genre}");
|
||||||
_logger.Info($"Year: {song.Year}");
|
_logger.Info($"Year: {song.Year}");
|
||||||
_logger.Info($"Duration: {song.Duration}");
|
_logger.Info($"Duration: {song.Duration}");
|
||||||
}
|
}
|
||||||
public Song RetrieveMetaData(string filePath)
|
public Song RetrieveMetaData(string filePath)
|
||||||
{
|
{
|
||||||
Song song = new Song();
|
Song song = new Song();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TagLib.File fileTag = TagLib.File.Create(filePath);
|
TagLib.File fileTag = TagLib.File.Create(filePath);
|
||||||
_title = fileTag.Tag.Title;
|
_title = fileTag.Tag.Title;
|
||||||
_artist = string.Join("", fileTag.Tag.Performers);
|
_artist = string.Join("", fileTag.Tag.Performers);
|
||||||
_album = fileTag.Tag.Album;
|
_album = fileTag.Tag.Album;
|
||||||
_genre = string.Join("", fileTag.Tag.Genres);
|
_genre = string.Join("", fileTag.Tag.Genres);
|
||||||
_year = (int)fileTag.Tag.Year;
|
_year = (int)fileTag.Tag.Year;
|
||||||
_duration = (int)fileTag.Properties.Duration.TotalSeconds;
|
_duration = (int)fileTag.Properties.Duration.TotalSeconds;
|
||||||
|
|
||||||
song.Title = _title;
|
song.Title = _title;
|
||||||
song.Artist = _artist;
|
song.Artist = _artist;
|
||||||
song.AlbumTitle = _album;
|
song.AlbumTitle = _album;
|
||||||
song.Genre = _genre;
|
song.Genre = _genre;
|
||||||
song.Year = _year;
|
song.Year = _year;
|
||||||
song.Duration = _duration;
|
song.Duration = _duration;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
Console.WriteLine("An error occurred in MetadataRetriever");
|
Console.WriteLine("An error occurred in MetadataRetriever");
|
||||||
Console.WriteLine(msg);
|
Console.WriteLine(msg);
|
||||||
_logger.Error(msg, "An error occurred in MetadataRetriever");
|
_logger.Error(msg, "An error occurred in MetadataRetriever");
|
||||||
}
|
}
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateMetadata(Song song)
|
public byte[] RetrieveCoverArtBytes(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine("Updating song metadata");
|
Console.WriteLine("Fetching image");
|
||||||
_logger.Info("Updating song metadata");
|
var tag = TagLib.File.Create(song.SongPath);
|
||||||
var filePath = song.SongPath;
|
byte[] imgBytes = tag.Tag.Pictures[0].Data.Data;
|
||||||
TagLib.File fileTag = TagLib.File.Create(filePath);
|
|
||||||
fileTag.Tag.Title = song.Title;
|
return imgBytes;
|
||||||
fileTag.Tag.Genres = new []{song.Genre};
|
}
|
||||||
fileTag.Save();
|
catch (Exception ex)
|
||||||
Console.WriteLine("Song metadata updated");
|
{
|
||||||
_logger.Info("Song metadata updated");
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred in MetadataRetriever");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
return null;
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
Console.WriteLine($"An error occurred: \n{msg}");
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void UpdateMetadata(Song updatedSong, Song oldSong)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
InitializeUpdatedSong(oldSong);
|
|
||||||
var songValues = CheckSongValues(updatedSong);
|
|
||||||
PerformUpdate(updatedSong, songValues);
|
|
||||||
Message = "Successfully updated metadata";
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
Console.WriteLine($"An error occurred: {msg}");
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
Message = "Failed to update metadata";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void PerformUpdate(Song updatedSong, SortedDictionary<string, bool> checkedValues)
|
public void UpdateMetadata(Song song)
|
||||||
{
|
{
|
||||||
var filePath = updatedSong.SongPath;
|
try
|
||||||
var title = updatedSong.Title;
|
{
|
||||||
var artist = updatedSong.Artist;
|
Console.WriteLine("Updating song metadata");
|
||||||
var album = updatedSong.AlbumTitle;
|
_logger.Info("Updating song metadata");
|
||||||
var genre = updatedSong.Genre;
|
var filePath = song.SongPath;
|
||||||
var year = updatedSong.Year;
|
TagLib.File fileTag = TagLib.File.Create(filePath);
|
||||||
TagLib.File fileTag = TagLib.File.Create(filePath);
|
fileTag.Tag.Title = song.Title;
|
||||||
try
|
fileTag.Tag.Genres = new []{song.Genre};
|
||||||
{
|
fileTag.Save();
|
||||||
Console.WriteLine($"Updating metadata of {title}");
|
Console.WriteLine("Song metadata updated");
|
||||||
_logger.Info($"Updating metadata of {title}");
|
_logger.Info("Song metadata updated");
|
||||||
|
|
||||||
foreach (var key in checkedValues.Keys)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
bool result = checkedValues[key];
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: \n{msg}");
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateMetadata(Song updatedSong, Song oldSong)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
InitializeUpdatedSong(oldSong);
|
||||||
|
var songValues = CheckSongValues(updatedSong);
|
||||||
|
PerformUpdate(updatedSong, songValues);
|
||||||
|
Message = "Successfully updated metadata";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: {msg}");
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
Message = "Failed to update metadata";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateCoverArt(Song song, CoverArt coverArt)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Updating song's cover art");
|
||||||
|
|
||||||
|
var tag = TagLib.File.Create(song.SongPath);
|
||||||
|
var pics = tag.Tag.Pictures;
|
||||||
|
Array.Resize(ref pics, 1);
|
||||||
|
|
||||||
|
pics[0] = new Picture(coverArt.ImagePath)
|
||||||
|
{
|
||||||
|
Description = "Cover Art"
|
||||||
|
};
|
||||||
|
|
||||||
|
tag.Tag.Pictures = pics;
|
||||||
|
tag.Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PerformUpdate(Song updatedSong, SortedDictionary<string, bool> checkedValues)
|
||||||
|
{
|
||||||
|
var filePath = updatedSong.SongPath;
|
||||||
|
var title = updatedSong.Title;
|
||||||
|
var artist = updatedSong.Artist;
|
||||||
|
var album = updatedSong.AlbumTitle;
|
||||||
|
var genre = updatedSong.Genre;
|
||||||
|
var year = updatedSong.Year;
|
||||||
|
TagLib.File fileTag = TagLib.File.Create(filePath);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Updating metadata of {title}");
|
||||||
|
_logger.Info($"Updating metadata of {title}");
|
||||||
|
|
||||||
|
foreach (var key in checkedValues.Keys)
|
||||||
|
{
|
||||||
|
bool result = checkedValues[key];
|
||||||
|
|
||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
switch (key.ToLower())
|
||||||
switch (key.ToLower())
|
{
|
||||||
{
|
case "title":
|
||||||
case "title":
|
_updatedSong.Title = title;
|
||||||
_updatedSong.Title = title;
|
fileTag.Tag.Title = title;
|
||||||
fileTag.Tag.Title = title;
|
break;
|
||||||
break;
|
case "artists":
|
||||||
case "artists":
|
_updatedSong.Artist = artist;
|
||||||
_updatedSong.Artist = artist;
|
fileTag.Tag.Performers = new []{artist};
|
||||||
fileTag.Tag.Performers = new []{artist};
|
break;
|
||||||
break;
|
case "album":
|
||||||
case "album":
|
_updatedSong.AlbumTitle = album;
|
||||||
_updatedSong.AlbumTitle = album;
|
fileTag.Tag.Album = album;
|
||||||
fileTag.Tag.Album = album;
|
break;
|
||||||
break;
|
case "genre":
|
||||||
case "genre":
|
_updatedSong.Genre = genre;
|
||||||
_updatedSong.Genre = genre;
|
fileTag.Tag.Genres = new []{genre};
|
||||||
fileTag.Tag.Genres = new []{genre};
|
break;
|
||||||
break;
|
case "year":
|
||||||
case "year":
|
_updatedSong.Year = year;
|
||||||
_updatedSong.Year = year;
|
fileTag.Tag.Year = (uint)year;
|
||||||
fileTag.Tag.Year = (uint)year;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fileTag.Save();
|
fileTag.Save();
|
||||||
|
|
||||||
Console.WriteLine("Successfully updated metadata");
|
Console.WriteLine("Successfully updated metadata");
|
||||||
_logger.Info("Successfully updated metadata");
|
_logger.Info("Successfully updated metadata");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
Console.WriteLine($"An error occurred:\n{msg}");
|
Console.WriteLine($"An error occurred:\n{msg}");
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void InitializeUpdatedSong(Song song)
|
private void InitializeUpdatedSong(Song song)
|
||||||
{
|
{
|
||||||
_updatedSong = new Song
|
_updatedSong = new Song
|
||||||
{
|
{
|
||||||
Id = song.Id,
|
Id = song.Id,
|
||||||
Title = song.Title,
|
Title = song.Title,
|
||||||
AlbumTitle = song.AlbumTitle,
|
AlbumTitle = song.AlbumTitle,
|
||||||
Artist = song.Artist,
|
Artist = song.Artist,
|
||||||
Genre = song.Genre,
|
Genre = song.Genre,
|
||||||
Year = song.Year,
|
Year = song.Year,
|
||||||
Duration = song.Duration,
|
Duration = song.Duration,
|
||||||
Filename = song.Filename,
|
Filename = song.Filename,
|
||||||
SongPath = song.SongPath
|
SongPath = song.SongPath
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
private void PrintMetadata()
|
private void PrintMetadata()
|
||||||
{
|
{
|
||||||
Console.WriteLine("\n\nMetadata of the song:");
|
Console.WriteLine("\n\nMetadata of the song:");
|
||||||
Console.WriteLine($"Title: {_title}");
|
Console.WriteLine($"Title: {_title}");
|
||||||
Console.WriteLine($"Artist: {_artist}");
|
Console.WriteLine($"Artist: {_artist}");
|
||||||
Console.WriteLine($"Album: {_album}");
|
Console.WriteLine($"Album: {_album}");
|
||||||
Console.WriteLine($"Genre: {_genre}");
|
Console.WriteLine($"Genre: {_genre}");
|
||||||
Console.WriteLine($"Year: {_year}");
|
Console.WriteLine($"Year: {_year}");
|
||||||
Console.WriteLine($"Duration: {_duration}\n\n");
|
Console.WriteLine($"Duration: {_duration}\n\n");
|
||||||
|
|
||||||
_logger.Info("Metadata of the song");
|
_logger.Info("Metadata of the song");
|
||||||
_logger.Info($"Title: {_title}");
|
_logger.Info($"Title: {_title}");
|
||||||
_logger.Info($"Artist: {_artist}");
|
_logger.Info($"Artist: {_artist}");
|
||||||
_logger.Info($"Album: {_album}");
|
_logger.Info($"Album: {_album}");
|
||||||
_logger.Info($"Genre: {_genre}");
|
_logger.Info($"Genre: {_genre}");
|
||||||
_logger.Info($"Year: {_year}");
|
_logger.Info($"Year: {_year}");
|
||||||
_logger.Info($"Duration: {_duration}");
|
_logger.Info($"Duration: {_duration}");
|
||||||
}
|
}
|
||||||
private void PrintMetadata(Song song, string message)
|
private void PrintMetadata(Song song, string message)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"\n\n{message}");
|
Console.WriteLine($"\n\n{message}");
|
||||||
Console.WriteLine($"Title: {song.Title}");
|
Console.WriteLine($"Title: {song.Title}");
|
||||||
Console.WriteLine($"Artist: {song.Artist}");
|
Console.WriteLine($"Artist: {song.Artist}");
|
||||||
Console.WriteLine($"Album: {song.Album}");
|
Console.WriteLine($"Album: {song.Album}");
|
||||||
Console.WriteLine($"Genre: {song.Genre}");
|
Console.WriteLine($"Genre: {song.Genre}");
|
||||||
Console.WriteLine($"Year: {song.Year}");
|
Console.WriteLine($"Year: {song.Year}");
|
||||||
Console.WriteLine($"Duration: {song.Duration}\n\n");
|
Console.WriteLine($"Duration: {song.Duration}\n\n");
|
||||||
|
|
||||||
_logger.Info(message);
|
_logger.Info(message);
|
||||||
_logger.Info($"Title: {_title}");
|
_logger.Info($"Title: {_title}");
|
||||||
_logger.Info($"Artist: {_artist}");
|
_logger.Info($"Artist: {_artist}");
|
||||||
_logger.Info($"Album: {_album}");
|
_logger.Info($"Album: {_album}");
|
||||||
_logger.Info($"Genre: {_genre}");
|
_logger.Info($"Genre: {_genre}");
|
||||||
_logger.Info($"Year: {_year}");
|
_logger.Info($"Year: {_year}");
|
||||||
_logger.Info($"Duration: {_duration}");
|
_logger.Info($"Duration: {_duration}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private SortedDictionary<string, bool> CheckSongValues(Song song)
|
private SortedDictionary<string, bool> CheckSongValues(Song song)
|
||||||
{
|
{
|
||||||
var songValues = new SortedDictionary<string, bool>();
|
var songValues = new SortedDictionary<string, bool>();
|
||||||
Console.WriteLine("Checking for null data");
|
Console.WriteLine("Checking for null data");
|
||||||
_logger.Info("Checking for null data");
|
_logger.Info("Checking for null data");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
songValues["Title"] = String.IsNullOrEmpty(song.Title);
|
songValues["Title"] = String.IsNullOrEmpty(song.Title);
|
||||||
songValues["Artists"] = String.IsNullOrEmpty(song.Artist);
|
songValues["Artists"] = String.IsNullOrEmpty(song.Artist);
|
||||||
songValues["Album"] = String.IsNullOrEmpty(song.AlbumTitle);
|
songValues["Album"] = String.IsNullOrEmpty(song.AlbumTitle);
|
||||||
songValues["Genre"] = String.IsNullOrEmpty(song.Genre);
|
songValues["Genre"] = String.IsNullOrEmpty(song.Genre);
|
||||||
if (song.Year == null)
|
|
||||||
{
|
|
||||||
songValues["Year"] = true;
|
|
||||||
}
|
|
||||||
else if (song.Year==0)
|
|
||||||
{
|
|
||||||
songValues["Year"] = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
songValues["Year"] = false;
|
|
||||||
}
|
|
||||||
Console.WriteLine("Checking for null data completed");
|
|
||||||
_logger.Info("Checking for null data completed");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
Console.WriteLine($"An error occurred: \n{msg}");
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return songValues;
|
if (song.Year == null)
|
||||||
}
|
songValues["Year"] = true;
|
||||||
#endregion
|
else if (song.Year==0)
|
||||||
}
|
songValues["Year"] = true;
|
||||||
|
else
|
||||||
|
songValues["Year"] = false;
|
||||||
|
|
||||||
|
Console.WriteLine("Checking for null data completed");
|
||||||
|
_logger.Info("Checking for null data completed");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: \n{msg}");
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return songValues;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,85 +9,82 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.Utilities
|
namespace Icarus.Controllers.Utilities
|
||||||
{
|
{
|
||||||
public class PasswordEncryption
|
public class PasswordEncryption
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public bool VerifyPassword(User user, string password)
|
public bool VerifyPassword(User user, string password)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = BCrypt.Net.BCrypt.Verify(password, user.Password);
|
var result = BCrypt.Net.BCrypt.Verify(password, user.Password);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string HashPassword(User user)
|
public string HashPassword(User user)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string hashedPassword = string.Empty;
|
string hashedPassword = string.Empty;
|
||||||
hashedPassword = BCrypt.Net.BCrypt.HashPassword(user.Password);
|
hashedPassword = BCrypt.Net.BCrypt.HashPassword(user.Password);
|
||||||
|
|
||||||
_logger.Info("Successfully hashed password");
|
_logger.Info("Successfully hashed password");
|
||||||
|
|
||||||
return hashedPassword;
|
return hashedPassword;
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var exMsg = ex.Message;
|
var exMsg = ex.Message;
|
||||||
_logger.Error(exMsg, "An error occurred");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
string GenerateHash(string password, byte[] salt)
|
string GenerateHash(string password, byte[] salt)
|
||||||
{
|
{
|
||||||
|
|
||||||
string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
|
string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
|
||||||
password: password,
|
password: password, salt: salt,
|
||||||
salt: salt,
|
prf: KeyDerivationPrf.HMACSHA1,
|
||||||
prf: KeyDerivationPrf.HMACSHA1,
|
iterationCount: 10000,
|
||||||
iterationCount: 10000,
|
numBytesRequested: 256/8));
|
||||||
numBytesRequested: 256/8));
|
|
||||||
|
|
||||||
return hashed;
|
return hashed;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] GenerateSalt()
|
byte[] GenerateSalt()
|
||||||
{
|
{
|
||||||
byte[] salt = new byte[128/8];
|
byte[] salt = new byte[128/8];
|
||||||
|
|
||||||
using (var rng = RandomNumberGenerator.Create())
|
using (var rng = RandomNumberGenerator.Create())
|
||||||
{
|
rng.GetBytes(salt);
|
||||||
rng.GetBytes(salt);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return salt;
|
return salt;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,74 +12,66 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/album")]
|
[Route("api/v1/album")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class AlbumController : ControllerBase
|
public class AlbumController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private readonly ILogger<AlbumController> _logger;
|
private readonly ILogger<AlbumController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public AlbumController(ILogger<AlbumController> logger)
|
public AlbumController(ILogger<AlbumController> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region HTTP Routes
|
#region HTTP Routes
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:albums")]
|
[Authorize("read:albums")]
|
||||||
public IActionResult Get()
|
public IActionResult Get()
|
||||||
{
|
{
|
||||||
List<Album> albums = new List<Album>();
|
List<Album> albums = new List<Album>();
|
||||||
|
|
||||||
AlbumRepository albumStoreContext = HttpContext
|
AlbumRepository albumStoreContext = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
||||||
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
|
||||||
|
|
||||||
albums = albumStoreContext.GetAlbums();
|
albums = albumStoreContext.GetAlbums();
|
||||||
|
|
||||||
if (albums.Count > 0)
|
if (albums.Count > 0)
|
||||||
{
|
return Ok(albums);
|
||||||
return Ok(albums);
|
else
|
||||||
}
|
return NotFound();
|
||||||
else
|
}
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("read:albums")]
|
[Authorize("read:albums")]
|
||||||
public IActionResult Get(int id)
|
public IActionResult Get(int id)
|
||||||
{
|
{
|
||||||
Album album = new Album
|
Album album = new Album
|
||||||
{
|
{
|
||||||
AlbumId = id
|
AlbumId = id
|
||||||
};
|
};
|
||||||
|
|
||||||
AlbumRepository albumStoreContext = HttpContext
|
AlbumRepository albumStoreContext = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
||||||
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
|
||||||
|
|
||||||
if (albumStoreContext.DoesAlbumExist(album))
|
if (albumStoreContext.DoesAlbumExist(album))
|
||||||
{
|
{
|
||||||
album = albumStoreContext.GetAlbum(album);
|
album = albumStoreContext.GetAlbum(album);
|
||||||
|
|
||||||
return Ok(album);
|
return Ok(album);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
return NotFound();
|
||||||
return NotFound();
|
}
|
||||||
}
|
#endregion
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,73 +13,64 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/artist")]
|
[Route("api/v1/artist")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class ArtistController : ControllerBase
|
public class ArtistController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private readonly ILogger<ArtistController> _logger;
|
private readonly ILogger<ArtistController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public ArtistController(ILogger<ArtistController> logger)
|
public ArtistController(ILogger<ArtistController> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region HTTP Routes
|
#region HTTP Routes
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:artists")]
|
[Authorize("read:artists")]
|
||||||
public IActionResult Get()
|
public IActionResult Get()
|
||||||
{
|
{
|
||||||
ArtistRepository artistStoreContext = HttpContext
|
ArtistRepository artistStoreContext = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
||||||
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
|
||||||
|
|
||||||
var artists = artistStoreContext.GetArtists();
|
var artists = artistStoreContext.GetArtists();
|
||||||
|
|
||||||
if (artists.Count > 0)
|
if (artists.Count > 0)
|
||||||
{
|
return Ok(artists);
|
||||||
return Ok(artists);
|
else
|
||||||
}
|
return NotFound();
|
||||||
else
|
}
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("read:artists")]
|
[Authorize("read:artists")]
|
||||||
public IActionResult Get(int id)
|
public IActionResult Get(int id)
|
||||||
{
|
{
|
||||||
Artist artist = new Artist
|
Artist artist = new Artist
|
||||||
{
|
{
|
||||||
ArtistId = id
|
ArtistId = id
|
||||||
};
|
};
|
||||||
|
|
||||||
ArtistRepository artistStoreContext = HttpContext
|
ArtistRepository artistStoreContext = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
||||||
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
|
||||||
|
|
||||||
if (artistStoreContext.DoesArtistExist(artist))
|
if (artistStoreContext.DoesArtistExist(artist))
|
||||||
{
|
{
|
||||||
artist = artistStoreContext.GetArtist(artist);
|
artist = artistStoreContext.GetArtist(artist);
|
||||||
|
|
||||||
|
return Ok(artist);
|
||||||
return Ok(artist);
|
}
|
||||||
}
|
else
|
||||||
else
|
return NotFound();
|
||||||
{
|
}
|
||||||
return NotFound();
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
using Icarus.Controllers.Managers;
|
||||||
|
using Icarus.Database.Repositories;
|
||||||
|
using Icarus.Models;
|
||||||
|
|
||||||
|
namespace Icarus.Controllers.V1
|
||||||
|
{
|
||||||
|
[Route("api/v1/coverart")]
|
||||||
|
[ApiController]
|
||||||
|
public class CoverArtController : ControllerBase
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private readonly ILogger<CoverArtController> _logger;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public CoverArtController(ILogger<CoverArtController> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region HTTP Routes
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[Authorize("download:cover_art")]
|
||||||
|
public IActionResult Get(int id)
|
||||||
|
{
|
||||||
|
var coverArt = new CoverArt { CoverArtId = id };
|
||||||
|
|
||||||
|
var coverArtRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(
|
||||||
|
typeof(CoverArtRepository)) as CoverArtRepository;
|
||||||
|
|
||||||
|
coverArt = coverArtRepository.GetCoverArt(coverArt);
|
||||||
|
|
||||||
|
if (coverArt != null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Found cover art record");
|
||||||
|
var coverArtBytes = System.IO.File.ReadAllBytes(
|
||||||
|
coverArt.ImagePath);
|
||||||
|
|
||||||
|
return File(coverArtBytes, "application/x-msdownload",
|
||||||
|
coverArt.SongTitle);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Cover art not found");
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,74 +11,66 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/genre")]
|
[Route("api/v1/genre")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class GenreController : ControllerBase
|
public class GenreController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private readonly ILogger<GenreController> _logger;
|
private readonly ILogger<GenreController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public GenreController(ILogger<GenreController> logger)
|
public GenreController(ILogger<GenreController> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region HTTP Routes
|
#region HTTP Routes
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:genre")]
|
[Authorize("read:genre")]
|
||||||
public IActionResult Get()
|
public IActionResult Get()
|
||||||
{
|
{
|
||||||
var genres = new List<Genre>();
|
var genres = new List<Genre>();
|
||||||
|
|
||||||
var genreStore = HttpContext
|
var genreStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(GenreRepository)) as GenreRepository;
|
||||||
.GetService(typeof(GenreRepository)) as GenreRepository;
|
|
||||||
|
|
||||||
genres = genreStore.GetGenres();
|
genres = genreStore.GetGenres();
|
||||||
|
|
||||||
if (genres.Count > 0)
|
if (genres.Count > 0)
|
||||||
{
|
return Ok(genres);
|
||||||
return Ok(genres);
|
else
|
||||||
}
|
return NotFound(new List<Genre>());
|
||||||
else
|
}
|
||||||
{
|
|
||||||
return NotFound(new List<Genre>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("read:genre")]
|
[Authorize("read:genre")]
|
||||||
public IActionResult Get(int id)
|
public IActionResult Get(int id)
|
||||||
{
|
{
|
||||||
var genre = new Genre
|
var genre = new Genre
|
||||||
{
|
{
|
||||||
GenreId = id
|
GenreId = id
|
||||||
};
|
};
|
||||||
|
|
||||||
var genreStore = HttpContext
|
var genreStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(GenreRepository)) as GenreRepository;
|
||||||
.GetService(typeof(GenreRepository)) as GenreRepository;
|
|
||||||
|
|
||||||
if (genreStore.DoesGenreExist(genre))
|
if (genreStore.DoesGenreExist(genre))
|
||||||
{
|
{
|
||||||
genre = genreStore.GetGenre(genre);
|
genre = genreStore.GetGenre(genre);
|
||||||
|
|
||||||
return Ok(genre);
|
return Ok(genre);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
return NotFound(new Genre());
|
||||||
return NotFound(new Genre());
|
}
|
||||||
}
|
#endregion
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,75 +15,74 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/login")]
|
[Route("api/v1/login")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class LoginController : ControllerBase
|
public class LoginController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private ILogger<LoginController> _logger;
|
private ILogger<LoginController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Contructors
|
#region Contructors
|
||||||
public LoginController(IConfiguration config, ILogger<LoginController> logger)
|
public LoginController(IConfiguration config, ILogger<LoginController> logger)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region HTTP endpoints
|
#region HTTP endpoints
|
||||||
public IActionResult Post([FromBody] User user)
|
public IActionResult Post([FromBody] User user)
|
||||||
{
|
{
|
||||||
UserRepository context = HttpContext
|
UserRepository context = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(UserRepository)) as UserRepository;
|
||||||
.GetService(typeof(UserRepository)) as UserRepository;
|
|
||||||
|
|
||||||
_logger.LogInformation("Starting process of validating credentials");
|
_logger.LogInformation("Starting process of validating credentials");
|
||||||
|
|
||||||
var message = "Invalid credentials";
|
var message = "Invalid credentials";
|
||||||
var password = user.Password;
|
var password = user.Password;
|
||||||
|
|
||||||
var loginRes = new LoginResult
|
var loginRes = new LoginResult
|
||||||
{
|
{
|
||||||
Username = user.Username
|
Username = user.Username
|
||||||
};
|
};
|
||||||
|
|
||||||
if (context.DoesUserExist(user))
|
if (context.DoesUserExist(user))
|
||||||
{
|
{
|
||||||
user = context.RetrieveUser(user);
|
user = context.RetrieveUser(user);
|
||||||
|
|
||||||
var validatePass = new PasswordEncryption();
|
var validatePass = new PasswordEncryption();
|
||||||
var validated = validatePass.VerifyPassword(user, password);
|
var validated = validatePass.VerifyPassword(user, password);
|
||||||
if (!validated)
|
if (!validated)
|
||||||
{
|
{
|
||||||
loginRes.Message = message;
|
loginRes.Message = message;
|
||||||
_logger.LogInformation(message);
|
_logger.LogInformation(message);
|
||||||
|
|
||||||
return Ok(loginRes);
|
return Ok(loginRes);
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Successfully validated user credentials");
|
_logger.LogInformation("Successfully validated user credentials");
|
||||||
|
|
||||||
TokenManager tk = new TokenManager(_config);
|
TokenManager tk = new TokenManager(_config);
|
||||||
|
|
||||||
loginRes = tk.RetrieveLoginResult(user);
|
loginRes = tk.RetrieveLoginResult(user);
|
||||||
|
|
||||||
return Ok(loginRes);
|
return Ok(loginRes);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loginRes.Message = message;
|
loginRes.Message = message;
|
||||||
|
|
||||||
return NotFound(loginRes);
|
return NotFound(loginRes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,58 +14,57 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/register")]
|
[Route("api/v1/register")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class RegisterController : ControllerBase
|
public class RegisterController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public RegisterController(IConfiguration config)
|
public RegisterController(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public IActionResult Post([FromBody] User user)
|
public IActionResult Post([FromBody] User user)
|
||||||
{
|
{
|
||||||
PasswordEncryption pe = new PasswordEncryption();
|
PasswordEncryption pe = new PasswordEncryption();
|
||||||
user.Password = pe.HashPassword(user);
|
user.Password = pe.HashPassword(user);
|
||||||
user.EmailVerified = false;
|
user.EmailVerified = false;
|
||||||
|
|
||||||
UserRepository context = HttpContext
|
UserRepository context = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(UserRepository)) as UserRepository;
|
||||||
.GetService(typeof(UserRepository)) as UserRepository;
|
|
||||||
|
|
||||||
context.SaveUser(user);
|
context.SaveUser(user);
|
||||||
|
|
||||||
var registerResult = new RegisterResult
|
var registerResult = new RegisterResult
|
||||||
{
|
{
|
||||||
Username = user.Username
|
Username = user.Username
|
||||||
};
|
};
|
||||||
|
|
||||||
if (context.DoesUserExist(user))
|
if (context.DoesUserExist(user))
|
||||||
{
|
{
|
||||||
registerResult.Message = "Successful registration";
|
registerResult.Message = "Successful registration";
|
||||||
registerResult.SuccessfullyRegistered = true;
|
registerResult.SuccessfullyRegistered = true;
|
||||||
|
|
||||||
return Ok(registerResult);
|
return Ok(registerResult);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
registerResult.Message = "Registration failed";
|
registerResult.Message = "Registration failed";
|
||||||
registerResult.SuccessfullyRegistered = false;
|
registerResult.SuccessfullyRegistered = false;
|
||||||
|
|
||||||
return Ok(registerResult);
|
return Ok(registerResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,49 +17,48 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/song/compressed/data")]
|
[Route("api/v1/song/compressed/data")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class SongCompressedDataController : ControllerBase
|
public class SongCompressedDataController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private string _songTempDir;
|
private string _songTempDir;
|
||||||
private string _archiveDir;
|
private string _archiveDir;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public SongCompressedDataController(IConfiguration config)
|
public SongCompressedDataController(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||||
_archiveDir = _config.GetValue<string>("ArchivePath");
|
_archiveDir = _config.GetValue<string>("ArchivePath");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region API Routes
|
#region API Routes
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("download:songs")]
|
[Authorize("download:songs")]
|
||||||
public async Task<IActionResult> Get(int id)
|
public async Task<IActionResult> Get(int id)
|
||||||
{
|
{
|
||||||
SongRepository context = HttpContext
|
SongRepository context = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(SongRepository)) as SongRepository;
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
|
|
||||||
SongCompression cmp = new SongCompression(_archiveDir);
|
SongCompression cmp = new SongCompression(_archiveDir);
|
||||||
|
|
||||||
Console.WriteLine($"Archive directory root: {_archiveDir}");
|
Console.WriteLine($"Archive directory root: {_archiveDir}");
|
||||||
|
|
||||||
Console.WriteLine("Starting process of retrieving comrpessed song");
|
Console.WriteLine("Starting process of retrieving comrpessed song");
|
||||||
SongData song = await cmp.RetrieveCompressedSong(context.GetSong(id));
|
SongData song = await cmp.RetrieveCompressedSong(context.GetSong(id));
|
||||||
|
|
||||||
return File(song.Data, "application/x-msdownload", cmp.CompressedSongFilename);
|
return File(song.Data, "application/x-msdownload", cmp.CompressedSongFilename);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,118 +16,101 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/song")]
|
[Route("api/v1/song")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class SongController : ControllerBase
|
public class SongController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private readonly ILogger<SongController> _logger;
|
private readonly ILogger<SongController> _logger;
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private SongManager _songMgr;
|
private SongManager _songMgr;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public SongController(IConfiguration config, ILogger<SongController> logger)
|
public SongController(IConfiguration config, ILogger<SongController> logger)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_songMgr = new SongManager(config);
|
_songMgr = new SongManager(config);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:song_details")]
|
[Authorize("read:song_details")]
|
||||||
public IActionResult Get()
|
public IActionResult Get()
|
||||||
{
|
{
|
||||||
List<Song> songs = new List<Song>();
|
List<Song> songs = new List<Song>();
|
||||||
Console.WriteLine("Attemtping to retrieve songs");
|
Console.WriteLine("Attemtping to retrieve songs");
|
||||||
_logger.LogInformation("Attempting to retrieve songs");
|
_logger.LogInformation("Attempting to retrieve songs");
|
||||||
|
|
||||||
SongRepository context = HttpContext
|
SongRepository context = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(SongRepository)) as SongRepository;
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
|
|
||||||
songs = context.GetAllSongs();
|
songs = context.GetAllSongs();
|
||||||
|
|
||||||
if (songs.Count > 0)
|
if (songs.Count > 0)
|
||||||
{
|
return Ok(songs);
|
||||||
return Ok(songs);
|
else
|
||||||
}
|
return NotFound();
|
||||||
else
|
}
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("read:song_details")]
|
[Authorize("read:song_details")]
|
||||||
public IActionResult Get(int id)
|
public IActionResult Get(int id)
|
||||||
{
|
{
|
||||||
SongRepository context = HttpContext
|
SongRepository context = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(SongRepository)) as SongRepository;
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
|
Song song = new Song { Id = id };
|
||||||
Song song = new Song { Id = id };
|
song = context.GetSong(song);
|
||||||
song = context.GetSong(song);
|
|
||||||
|
|
||||||
Console.WriteLine("Here");
|
Console.WriteLine("Here");
|
||||||
|
|
||||||
if (song.Id != 0)
|
if (song.Id != 0)
|
||||||
{
|
return Ok(song);
|
||||||
return Ok(song);
|
else
|
||||||
}
|
return NotFound();
|
||||||
else
|
}
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Authorize("update:songs")]
|
[Authorize("update:songs")]
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
public IActionResult Put(int id, [FromBody] Song song)
|
public IActionResult Put(int id, [FromBody] Song song)
|
||||||
{
|
{
|
||||||
SongRepository context = HttpContext
|
SongRepository context = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(SongRepository)) as SongRepository;
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
|
|
||||||
ArtistRepository artistStore = HttpContext
|
ArtistRepository artistStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
||||||
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
|
||||||
|
|
||||||
AlbumRepository albumStore = HttpContext
|
AlbumRepository albumStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
||||||
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
|
||||||
|
GenreRepository genreStore = HttpContext.RequestServices
|
||||||
GenreRepository genreStore = HttpContext
|
.GetService(typeof(GenreRepository)) as GenreRepository;
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(GenreRepository)) as GenreRepository;
|
|
||||||
|
|
||||||
YearRepository yearStore = HttpContext
|
YearRepository yearStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(YearRepository)) as YearRepository;
|
||||||
.GetService(typeof(YearRepository)) as YearRepository;
|
|
||||||
|
|
||||||
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))
|
if (!context.DoesSongExist(song))
|
||||||
{
|
return NotFound(new SongResult
|
||||||
return NotFound(new SongResult
|
{
|
||||||
{
|
Message = "Song does not exist"
|
||||||
Message = "Song does not exist"
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var songRes = _songMgr.UpdateSong(song, context, albumStore, artistStore, genreStore,
|
var songRes = _songMgr.UpdateSong(song, context, albumStore, artistStore, genreStore,
|
||||||
yearStore);
|
yearStore);
|
||||||
|
|
||||||
return Ok(songRes);
|
return Ok(songRes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,137 +13,142 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
using Icarus.Controllers.Managers;
|
using Icarus.Controllers.Managers;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
using Icarus.Database.Contexts;
|
||||||
using Icarus.Database.Repositories;
|
using Icarus.Database.Repositories;
|
||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/song/data")]
|
[Route("api/v1/song/data")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class SongDataController : ControllerBase
|
public class SongDataController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private SongRepository _songRepository;
|
||||||
private ILogger<SongDataController> _logger;
|
private AlbumRepository _albumRepository;
|
||||||
private SongManager _songMgr;
|
private ArtistRepository _artistRepository;
|
||||||
private string _songTempDir;
|
private GenreRepository _genreRepository;
|
||||||
#endregion
|
private YearRepository _yearRepository;
|
||||||
|
private CoverArtRepository _coverArtRepository;
|
||||||
|
private IConfiguration _config;
|
||||||
|
private ILogger<SongDataController> _logger;
|
||||||
|
private SongManager _songMgr;
|
||||||
|
private string _songTempDir;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public SongDataController(IConfiguration config, ILogger<SongDataController> logger)
|
public SongDataController(IConfiguration config, ILogger<SongDataController> logger)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||||
_songMgr = new SongManager(config, _songTempDir);
|
_songMgr = new SongManager(config, _songTempDir);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
private void Initialize()
|
||||||
|
{
|
||||||
|
_songRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService
|
||||||
|
(typeof(SongRepository)) as SongRepository;
|
||||||
|
|
||||||
|
_albumRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
||||||
|
|
||||||
|
_artistRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
||||||
|
|
||||||
|
_genreRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(GenreRepository)) as GenreRepository;
|
||||||
|
|
||||||
|
_yearRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(YearRepository)) as YearRepository;
|
||||||
|
|
||||||
|
_coverArtRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(CoverArtRepository)) as CoverArtRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Route("private-scoped")]
|
[Route("private-scoped")]
|
||||||
[Authorize("download:songs")]
|
[Authorize("download:songs")]
|
||||||
public async Task<IActionResult> Get(int id)
|
public async Task<IActionResult> Get(int id)
|
||||||
{
|
{
|
||||||
SongRepository context = HttpContext
|
Initialize();
|
||||||
.RequestServices
|
var songMetaData = _songRepository.GetSong(id);
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
var songMetaData = context.GetSong(id);
|
SongData song = await _songMgr.RetrieveSong(songMetaData);
|
||||||
|
|
||||||
SongData song = await _songMgr.RetrieveSong(songMetaData);
|
return File(song.Data, "application/x-msdownload", songMetaData.Filename);
|
||||||
|
}
|
||||||
return File(song.Data, "application/x-msdownload", songMetaData.Filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Authorize("upload:songs")]
|
[Authorize("upload:songs")]
|
||||||
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
|
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SongRepository songRepository = HttpContext
|
Initialize();
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
AlbumRepository albumStoreContext = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
|
||||||
ArtistRepository artistStoreContext = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
|
||||||
GenreRepository genreStore = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(GenreRepository)) as GenreRepository;
|
|
||||||
YearRepository yearStore = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(YearRepository)) as YearRepository;
|
|
||||||
|
|
||||||
Console.WriteLine("Uploading song...");
|
Console.WriteLine("Uploading song...");
|
||||||
_logger.LogInformation("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}");
|
_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}");
|
_logger.LogInformation($"Song filename {sng.FileName}");
|
||||||
_logger.LogInformation($"Song filename {sng.FileName}");
|
|
||||||
|
|
||||||
await _songMgr.SaveSongToFileSystem(sng, songRepository,
|
await _songMgr.SaveSongToFileSystem(sng, _songRepository,
|
||||||
albumStoreContext, artistStoreContext,
|
_albumRepository, _artistRepository,
|
||||||
genreStore, yearStore);
|
_genreRepository, _yearRepository, _coverArtRepository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
var msg = ex.Message;
|
||||||
var msg = ex.Message;
|
_logger.LogError(msg, "An error occurred");
|
||||||
_logger.LogError(msg, "An error occurred");
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
[Authorize("delete:songs")]
|
[Authorize("delete:songs")]
|
||||||
public IActionResult Delete(int id)
|
public IActionResult Delete(int id)
|
||||||
{
|
{
|
||||||
SongRepository context = HttpContext
|
Initialize();
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
AlbumRepository albumStore = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(AlbumRepository)) as AlbumRepository;
|
|
||||||
ArtistRepository artistStore = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(ArtistRepository)) as ArtistRepository;
|
|
||||||
GenreRepository genreStore = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(GenreRepository)) as GenreRepository;
|
|
||||||
YearRepository yearStore = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService(typeof(YearRepository)) as YearRepository;
|
|
||||||
|
|
||||||
var songMetaData = new Song{ Id = id };
|
|
||||||
Console.WriteLine($"Id {songMetaData.Id}");
|
|
||||||
songMetaData = context.GetSong(songMetaData);
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(songMetaData.Title))
|
var songMetaData = new Song{ Id = id };
|
||||||
{
|
Console.WriteLine($"Id {songMetaData.Id}");
|
||||||
_logger.LogInformation("Song does not exist");
|
songMetaData = _songRepository.GetSong(songMetaData);
|
||||||
return NotFound("Song does not exist");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Starting process of deleting song from the filesystem and database");
|
|
||||||
|
|
||||||
_songMgr.DeleteSong(songMetaData, context, albumStore,
|
if (string.IsNullOrEmpty(songMetaData.Title))
|
||||||
artistStore, genreStore, yearStore);
|
{
|
||||||
|
_logger.LogInformation("Song does not exist");
|
||||||
|
return NotFound("Song does not exist");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Starting process of deleting song from the filesystem and database");
|
||||||
|
|
||||||
return Ok();
|
_songMgr.DeleteSong(songMetaData, _songRepository,
|
||||||
}
|
_albumRepository, _artistRepository,
|
||||||
}
|
_genreRepository, _yearRepository,
|
||||||
}
|
_coverArtRepository);
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,52 +16,49 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controllers.V1
|
namespace Icarus.Controllers.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/song/stream")]
|
[Route("api/v1/song/stream")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class SongStreamController : ControllerBase
|
public class SongStreamController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private ILogger<SongStreamController> _logger;
|
private ILogger<SongStreamController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public SongStreamController(ILogger<SongStreamController> logger)
|
public SongStreamController(ILogger<SongStreamController> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region HTTP endpoints
|
#region HTTP endpoints
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("stream:songs")]
|
[Authorize("stream:songs")]
|
||||||
public async Task<IActionResult> Get(int id)
|
public async Task<IActionResult> Get(int id)
|
||||||
{
|
{
|
||||||
var songStore= HttpContext
|
var songStore= HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(SongRepository)) as SongRepository;
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
|
||||||
|
|
||||||
var song = songStore.GetSong(new Song { Id = id });
|
var song = songStore.GetSong(new Song { Id = id });
|
||||||
|
|
||||||
var mem = new MemoryStream();
|
var mem = new MemoryStream();
|
||||||
|
|
||||||
using (var stream = new FileStream(song.SongPath, FileMode.Open, FileAccess.Read))
|
using (var stream = new FileStream(song.SongPath, FileMode.Open, FileAccess.Read))
|
||||||
{
|
await stream.CopyToAsync(mem);
|
||||||
await stream.CopyToAsync(mem);
|
|
||||||
}
|
|
||||||
|
|
||||||
mem.Position = 0;
|
mem.Position = 0;
|
||||||
|
|
||||||
_logger.LogInformation("Starting to stream song...>");
|
_logger.LogInformation("Starting to stream song...>");
|
||||||
Console.WriteLine("Starting to streamsong...");
|
Console.WriteLine("Starting to streamsong...");
|
||||||
|
|
||||||
return File(mem, "application/octet-stream", Path.GetFileName(song.SongPath));
|
return File(mem, "application/octet-stream", Path.GetFileName(song.SongPath));
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,74 +11,66 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus.Controller.V1
|
namespace Icarus.Controller.V1
|
||||||
{
|
{
|
||||||
[Route("api/v1/year")]
|
[Route("api/v1/year")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class YearController : ControllerBase
|
public class YearController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private readonly ILogger<YearController> _logger;
|
private readonly ILogger<YearController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public YearController(ILogger<YearController> logger)
|
public YearController(ILogger<YearController> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region HTTP Routes
|
#region HTTP Routes
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:year")]
|
[Authorize("read:year")]
|
||||||
public IActionResult Get()
|
public IActionResult Get()
|
||||||
{
|
{
|
||||||
var yearValues = new List<Year>();
|
var yearValues = new List<Year>();
|
||||||
|
|
||||||
var yearStore = HttpContext
|
var yearStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(YearRepository)) as YearRepository;
|
||||||
.GetService(typeof(YearRepository)) as YearRepository;
|
|
||||||
|
|
||||||
yearValues = yearStore.GetSongYears();
|
yearValues = yearStore.GetSongYears();
|
||||||
|
|
||||||
if (yearValues.Count > 0)
|
if (yearValues.Count > 0)
|
||||||
{
|
return Ok(yearValues);
|
||||||
return Ok(yearValues);
|
else
|
||||||
}
|
return NotFound(new List<Year>());
|
||||||
else
|
}
|
||||||
{
|
|
||||||
return NotFound(new List<Year>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[Authorize("read:year")]
|
[Authorize("read:year")]
|
||||||
public IActionResult Get(int id)
|
public IActionResult Get(int id)
|
||||||
{
|
{
|
||||||
var year = new Year
|
var year = new Year
|
||||||
{
|
{
|
||||||
YearId = id
|
YearId = id
|
||||||
};
|
};
|
||||||
|
|
||||||
var yearStore = HttpContext
|
var yearStore = HttpContext.RequestServices
|
||||||
.RequestServices
|
.GetService(typeof(YearRepository)) as YearRepository;
|
||||||
.GetService(typeof(YearRepository)) as YearRepository;
|
|
||||||
|
|
||||||
if (yearStore.DoesYearExist(year))
|
if (yearStore.DoesYearExist(year))
|
||||||
{
|
{
|
||||||
year = yearStore.GetSongYear(year);
|
year = yearStore.GetSongYear(year);
|
||||||
|
|
||||||
return Ok(year);
|
return Ok(year);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
return NotFound(new Year());
|
||||||
return NotFound(new Year());
|
}
|
||||||
}
|
#endregion
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,16 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Contexts
|
namespace Icarus.Database.Contexts
|
||||||
{
|
{
|
||||||
public class AlbumContext : DbContext
|
public class AlbumContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<Album> Albums { get; set; }
|
||||||
|
|
||||||
|
public AlbumContext(DbContextOptions<AlbumContext> options) : base(options) { }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
public DbSet<Album> Albums { get; set; }
|
modelBuilder.Entity<Album>()
|
||||||
|
.ToTable("Album");
|
||||||
public AlbumContext(DbContextOptions<AlbumContext> options)
|
|
||||||
: base(options)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
modelBuilder.Entity<Album>()
|
|
||||||
.ToTable("Album");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,16 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Contexts
|
namespace Icarus.Database.Contexts
|
||||||
{
|
{
|
||||||
public class ArtistContext : DbContext
|
public class ArtistContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<Artist> Artists { get; set; }
|
||||||
|
|
||||||
|
public ArtistContext(DbContextOptions<ArtistContext> options) : base (options) { }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
public DbSet<Artist> Artists { get; set; }
|
modelBuilder.Entity<Artist>()
|
||||||
|
.ToTable("Artist");
|
||||||
public ArtistContext(DbContextOptions<ArtistContext> options)
|
|
||||||
: base (options)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
modelBuilder.Entity<Artist>()
|
|
||||||
.ToTable("Artist");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MySql.Data;
|
||||||
|
using MySql.Data.EntityFrameworkCore.Extensions;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
using Icarus.Models;
|
||||||
|
|
||||||
|
namespace Icarus.Database.Contexts
|
||||||
|
{
|
||||||
|
public class CoverArtContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<CoverArt> CoverArtImages { get; set; }
|
||||||
|
|
||||||
|
public CoverArtContext(DbContextOptions<CoverArtContext> options) : base(options) { }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<CoverArt>()
|
||||||
|
.ToTable("CoverArt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,18 +10,15 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Contexts
|
namespace Icarus.Database.Contexts
|
||||||
{
|
{
|
||||||
public class GenreContext : DbContext
|
public class GenreContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<Genre> Genres { get; set; }
|
||||||
|
public GenreContext(DbContextOptions<GenreContext> options) : base(options) { }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
public DbSet<Genre> Genres { get; set; }
|
modelBuilder.Entity<Genre>()
|
||||||
|
.ToTable("Genre");
|
||||||
public GenreContext(DbContextOptions<GenreContext> options)
|
|
||||||
: base(options)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
modelBuilder.Entity<Genre>()
|
|
||||||
.ToTable("Genre");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,59 +10,66 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Contexts
|
namespace Icarus.Database.Contexts
|
||||||
{
|
{
|
||||||
public class SongContext : DbContext
|
public class SongContext : DbContext
|
||||||
{
|
{
|
||||||
public DbSet<Song> Songs { get; set; }
|
public DbSet<Song> Songs { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public SongContext(DbContextOptions<SongContext> options)
|
public SongContext(DbContextOptions<SongContext> options) : base(options) { }
|
||||||
: base(options)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.ToTable("Song");
|
.ToTable("Song");
|
||||||
|
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.HasOne(s => s.Album)
|
.HasOne(s => s.Album)
|
||||||
.WithMany(al => al.Songs)
|
.WithMany(al => al.Songs)
|
||||||
.HasForeignKey(s => s.AlbumId)
|
.HasForeignKey(s => s.AlbumId)
|
||||||
.OnDelete(DeleteBehavior.SetNull);
|
.OnDelete(DeleteBehavior.SetNull);
|
||||||
|
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.HasOne(sa => sa.SongArtist)
|
.HasOne(sa => sa.SongArtist)
|
||||||
.WithMany(ar => ar.Songs)
|
.WithMany(ar => ar.Songs)
|
||||||
.HasForeignKey(s => s.ArtistId)
|
.HasForeignKey(s => s.ArtistId)
|
||||||
.OnDelete(DeleteBehavior.SetNull);
|
.OnDelete(DeleteBehavior.SetNull);
|
||||||
|
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.HasOne(s => s.SongGenre)
|
.HasOne(s => s.SongGenre)
|
||||||
.WithMany(gnr => gnr.Songs)
|
.WithMany(gnr => gnr.Songs)
|
||||||
.HasForeignKey(s => s.GenreId)
|
.HasForeignKey(s => s.GenreId)
|
||||||
.OnDelete(DeleteBehavior.SetNull);
|
.OnDelete(DeleteBehavior.SetNull);
|
||||||
|
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.HasOne(s => s.SongYear)
|
.HasOne(s => s.SongYear)
|
||||||
.WithMany(yr => yr.Songs)
|
.WithMany(yr => yr.Songs)
|
||||||
.HasForeignKey(s => s.YearId)
|
.HasForeignKey(s => s.YearId)
|
||||||
.OnDelete(DeleteBehavior.SetNull);
|
.OnDelete(DeleteBehavior.SetNull);
|
||||||
|
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.Property(s => s.Year)
|
.HasOne(s => s.SongCoverArt)
|
||||||
.IsRequired(false);
|
.WithMany(ca => ca.Songs)
|
||||||
modelBuilder.Entity<Song>()
|
.HasForeignKey(s => s.CoverArtId)
|
||||||
.Property(s => s.YearId)
|
.OnDelete(DeleteBehavior.SetNull);
|
||||||
.IsRequired(false);
|
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.Property(s => s.GenreId)
|
.Property(s => s.Year)
|
||||||
.IsRequired(false);
|
.IsRequired(false);
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.Property(s => s.ArtistId)
|
.Property(s => s.YearId)
|
||||||
.IsRequired(false);
|
.IsRequired(false);
|
||||||
modelBuilder.Entity<Song>()
|
modelBuilder.Entity<Song>()
|
||||||
.Property(s => s.AlbumId)
|
.Property(s => s.GenreId)
|
||||||
.IsRequired(false);
|
.IsRequired(false);
|
||||||
}
|
modelBuilder.Entity<Song>()
|
||||||
}
|
.Property(s => s.ArtistId)
|
||||||
|
.IsRequired(false);
|
||||||
|
modelBuilder.Entity<Song>()
|
||||||
|
.Property(s => s.AlbumId)
|
||||||
|
.IsRequired(false);
|
||||||
|
modelBuilder.Entity<Song>()
|
||||||
|
.Property(s => s.CoverArtId)
|
||||||
|
.IsRequired(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,23 +10,21 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Contexts
|
namespace Icarus.Database.Contexts
|
||||||
{
|
{
|
||||||
public class UserContext : DbContext
|
public class UserContext : DbContext
|
||||||
{
|
{
|
||||||
public DbSet<User> Users { get; set; }
|
public DbSet<User> Users { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public UserContext(DbContextOptions<UserContext> options)
|
public UserContext(DbContextOptions<UserContext> options) : base(options) { }
|
||||||
: base(options)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
modelBuilder.Entity<User>()
|
modelBuilder.Entity<User>()
|
||||||
.ToTable("User");
|
.ToTable("User");
|
||||||
modelBuilder.Entity<User>()
|
modelBuilder.Entity<User>()
|
||||||
.Property(u => u.LastLogin).IsRequired(false);
|
.Property(u => u.LastLogin).IsRequired(false);
|
||||||
modelBuilder.Entity<User>()
|
modelBuilder.Entity<User>()
|
||||||
.Property(u => u.DateCreated).HasDefaultValue(DateTime.Now);
|
.Property(u => u.DateCreated).HasDefaultValue(DateTime.Now);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,16 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Contexts
|
namespace Icarus.Database.Contexts
|
||||||
{
|
{
|
||||||
public class YearContext : DbContext
|
public class YearContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<Year> YearValues { get; set; }
|
||||||
|
|
||||||
|
public YearContext(DbContextOptions<YearContext> options) : base(options) { }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
public DbSet<Year> YearValues { get; set; }
|
modelBuilder.Entity<Year>()
|
||||||
|
.ToTable("Year");
|
||||||
public YearContext(DbContextOptions<YearContext> options)
|
|
||||||
: base(options)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
modelBuilder.Entity<Year>()
|
|
||||||
.ToTable("Year");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,404 +8,378 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class AlbumRepository : BaseRepository
|
public class AlbumRepository : BaseRepository
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public AlbumRepository(string connectionString)
|
||||||
{
|
{
|
||||||
#region Fields
|
_connectionString = connectionString;
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
public AlbumRepository(string connectionString)
|
|
||||||
{
|
|
||||||
_connectionString = connectionString;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
|
||||||
public List<Album> GetAlbumWithoutCount()
|
|
||||||
{
|
|
||||||
var albums = new List<Album>();
|
|
||||||
|
|
||||||
if (AnyAlbums())
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "SELECT * FROM Album";
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
albums = ParseData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return albums;
|
|
||||||
}
|
|
||||||
public List<Album> GetAlbums()
|
|
||||||
{
|
|
||||||
List<Album> albums = new List<Album>();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "SELECT alb.*,COUNT(*) AS SongCount FROM Album alb " +
|
|
||||||
"LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE " +
|
|
||||||
"alb.AlbumId=sng.AlbumId GROUP BY alb.AlbumId";
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
albums = ParseData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return albums;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Album GetAlbum(Album album)
|
|
||||||
{
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "SELECT alb.*, COUNT(*) AS SongCount FROM Album alb " +
|
|
||||||
"LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE "+
|
|
||||||
"alb.AlbumId=@AlbumId LIMIT 1";
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
album = ParseSingleData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return album;
|
|
||||||
}
|
|
||||||
public Album GetAlbum(Song song)
|
|
||||||
{
|
|
||||||
var album = new Album();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "SELECT alb.*, 0 AS SongCount FROM Album alb " +
|
|
||||||
"WHERE alb.Title=@Title LIMIT 1";
|
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Album title {song.AlbumTitle}");
|
|
||||||
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
|
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
album = ParseSingleData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return album;
|
|
||||||
}
|
|
||||||
public Album GetAlbum(Song song, bool retrieveCount)
|
|
||||||
{
|
|
||||||
var album = new Album();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = string.Empty;
|
|
||||||
if (retrieveCount)
|
|
||||||
{
|
|
||||||
query = "SELECT alb.*, COUNT(*) AS SongCount FROM Album alb " +
|
|
||||||
"LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE " +
|
|
||||||
"alb.Title=@Title GROUP BY alb.AlbumId LIMIT 1";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
query = "SELECT alb.*, 0 AS SongCount FROM Album alb WHERE " +
|
|
||||||
"alb.Title=@Title LIMIT 1";
|
|
||||||
}
|
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
|
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
album = ParseSingleData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return album;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool DoesAlbumExist(Album album)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT alb.*, 0 AS SongCount FROM Album alb WHERE alb.AlbumId=@AlbumId";
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
album = ParseSingleData(reader);
|
|
||||||
|
|
||||||
if (album.Title != null)
|
|
||||||
{
|
|
||||||
_logger.Info($"Album {album.Title} exists");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
public bool DoesAlbumExist(Song song)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT alb.*, 0 AS SongCount FROM Album alb WHERE alb.Title=@Title";
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
|
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
var album = ParseSingleData(reader);
|
|
||||||
|
|
||||||
if (!String.IsNullOrEmpty(album.Title))
|
|
||||||
{
|
|
||||||
_logger.Info($"Album {album.Title} exists");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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)" +
|
|
||||||
" VALUES (@Title, @AlbumArtist)";
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@Title", album.Title);
|
|
||||||
cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist);
|
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void UpdateAlbum(Album album)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "UPDATE Album SET Title=@Title, AlbumArtist=" +
|
|
||||||
"@AlbumArtist WHERE AlbumId=@AlbumId";
|
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@Title", album.Title);
|
|
||||||
cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist);
|
|
||||||
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void DeleteAlbum(Album album)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (var conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "DELETE FROM Album WHERE AlbumId=@AlbumId";
|
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
|
||||||
|
|
||||||
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>();
|
|
||||||
_logger.Info("Retrieving album records");
|
|
||||||
|
|
||||||
while (reader.Read())
|
|
||||||
{
|
|
||||||
var id = Convert.ToInt32(reader["AlbumId"]);
|
|
||||||
var title = reader["Title"].ToString();
|
|
||||||
var albumArtist = reader["AlbumArtist"].ToString();
|
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"]);
|
|
||||||
albums.Add(new Album
|
|
||||||
{
|
|
||||||
AlbumId = id,
|
|
||||||
Title = title,
|
|
||||||
AlbumArtist = albumArtist,
|
|
||||||
SongCount = songCount
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Info("Album records retrieved");
|
|
||||||
|
|
||||||
return albums;
|
|
||||||
}
|
|
||||||
private Album ParseSingleData(MySqlDataReader reader)
|
|
||||||
{
|
|
||||||
Album album = new Album();
|
|
||||||
_logger.Info("Retrieving single album record");
|
|
||||||
|
|
||||||
while (reader.Read())
|
|
||||||
{
|
|
||||||
var id = Convert.ToInt32(reader["AlbumId"]);
|
|
||||||
var title = reader["Title"].ToString();
|
|
||||||
var albumArtist = reader["AlbumArtist"].ToString();
|
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"]);
|
|
||||||
album = new Album
|
|
||||||
{
|
|
||||||
AlbumId = id,
|
|
||||||
Title = title,
|
|
||||||
AlbumArtist = albumArtist,
|
|
||||||
SongCount = songCount
|
|
||||||
};
|
|
||||||
}
|
|
||||||
_logger.Info("Single ablum retreived");
|
|
||||||
|
|
||||||
return album;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool AnyAlbums()
|
|
||||||
{
|
|
||||||
var albums = new List<Album>();
|
|
||||||
|
|
||||||
using (var conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT * FROM Album";
|
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
albums = ParseData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (albums.Count > 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
public List<Album> GetAlbumWithoutCount()
|
||||||
|
{
|
||||||
|
var albums = new List<Album>();
|
||||||
|
|
||||||
|
if (AnyAlbums())
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "SELECT * FROM Album";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
albums = ParseData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return albums;
|
||||||
|
}
|
||||||
|
public List<Album> GetAlbums()
|
||||||
|
{
|
||||||
|
List<Album> albums = new List<Album>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "SELECT alb.*,COUNT(*) AS SongCount FROM Album alb " +
|
||||||
|
"LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE " +
|
||||||
|
"alb.AlbumId=sng.AlbumId GROUP BY alb.AlbumId";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
albums = ParseData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return albums;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Album GetAlbum(Album album)
|
||||||
|
{
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "SELECT alb.*, COUNT(*) AS SongCount FROM Album alb " +
|
||||||
|
"LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE "+
|
||||||
|
"alb.AlbumId=@AlbumId LIMIT 1";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
album = ParseSingleData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return album;
|
||||||
|
}
|
||||||
|
public Album GetAlbum(Song song)
|
||||||
|
{
|
||||||
|
var album = new Album();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "SELECT alb.*, 0 AS SongCount FROM Album alb " +
|
||||||
|
"WHERE alb.Title=@Title LIMIT 1";
|
||||||
|
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Album title {song.AlbumTitle}");
|
||||||
|
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
album = ParseSingleData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return album;
|
||||||
|
}
|
||||||
|
public Album GetAlbum(Song song, bool retrieveCount)
|
||||||
|
{
|
||||||
|
var album = new Album();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = string.Empty;
|
||||||
|
if (retrieveCount)
|
||||||
|
query = "SELECT alb.*, COUNT(*) AS SongCount FROM Album alb " +
|
||||||
|
"LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE " +
|
||||||
|
"alb.Title=@Title GROUP BY alb.AlbumId LIMIT 1";
|
||||||
|
else
|
||||||
|
query = "SELECT alb.*, 0 AS SongCount FROM Album alb WHERE " +
|
||||||
|
"alb.Title=@Title LIMIT 1";
|
||||||
|
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
album = ParseSingleData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return album;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesAlbumExist(Album album)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
var query = "SELECT alb.*, 0 AS SongCount FROM Album alb WHERE alb.AlbumId=@AlbumId";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
album = ParseSingleData(reader);
|
||||||
|
|
||||||
|
if (album.Title != null)
|
||||||
|
{
|
||||||
|
_logger.Info($"Album {album.Title} exists");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public bool DoesAlbumExist(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
var query = "SELECT alb.*, 0 AS SongCount FROM Album alb WHERE alb.Title=@Title";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
var album = ParseSingleData(reader);
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(album.Title))
|
||||||
|
{
|
||||||
|
_logger.Info($"Album {album.Title} exists");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)" +
|
||||||
|
" VALUES (@Title, @AlbumArtist)";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Title", album.Title);
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist);
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateAlbum(Album album)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "UPDATE Album SET Title=@Title, AlbumArtist=" +
|
||||||
|
"@AlbumArtist WHERE AlbumId=@AlbumId";
|
||||||
|
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Title", album.Title);
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist);
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteAlbum(Album album)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "DELETE FROM Album WHERE AlbumId=@AlbumId";
|
||||||
|
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId);
|
||||||
|
|
||||||
|
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>();
|
||||||
|
_logger.Info("Retrieving album records");
|
||||||
|
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
var id = Convert.ToInt32(reader["AlbumId"]);
|
||||||
|
var title = reader["Title"].ToString();
|
||||||
|
var albumArtist = reader["AlbumArtist"].ToString();
|
||||||
|
var songCount = Convert.ToInt32(reader["SongCount"]);
|
||||||
|
albums.Add(new Album
|
||||||
|
{
|
||||||
|
AlbumId = id,
|
||||||
|
Title = title,
|
||||||
|
AlbumArtist = albumArtist,
|
||||||
|
SongCount = songCount
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Info("Album records retrieved");
|
||||||
|
|
||||||
|
return albums;
|
||||||
|
}
|
||||||
|
private Album ParseSingleData(MySqlDataReader reader)
|
||||||
|
{
|
||||||
|
Album album = new Album();
|
||||||
|
_logger.Info("Retrieving single album record");
|
||||||
|
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
var id = Convert.ToInt32(reader["AlbumId"]);
|
||||||
|
var title = reader["Title"].ToString();
|
||||||
|
var albumArtist = reader["AlbumArtist"].ToString();
|
||||||
|
var songCount = Convert.ToInt32(reader["SongCount"]);
|
||||||
|
album = new Album
|
||||||
|
{
|
||||||
|
AlbumId = id,
|
||||||
|
Title = title,
|
||||||
|
AlbumArtist = albumArtist,
|
||||||
|
SongCount = songCount
|
||||||
|
};
|
||||||
|
}
|
||||||
|
_logger.Info("Single ablum retreived");
|
||||||
|
|
||||||
|
return album;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool AnyAlbums()
|
||||||
|
{
|
||||||
|
var albums = new List<Album>();
|
||||||
|
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
var query = "SELECT * FROM Album";
|
||||||
|
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
albums = ParseData(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (albums.Count > 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,361 +10,346 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class ArtistRepository : BaseRepository
|
public class ArtistRepository : BaseRepository
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public ArtistRepository(string connectionString)
|
public ArtistRepository(string connectionString)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public List<Artist> GetArtists()
|
public List<Artist> GetArtists()
|
||||||
{
|
{
|
||||||
List<Artist> artists = new List<Artist>();
|
List<Artist> artists = new List<Artist>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "SELECT art.*, COUNT(*) AS SongCount FROM Artist " +
|
var query = "SELECT art.*, COUNT(*) AS SongCount FROM Artist " +
|
||||||
"art LEFT JOIN Song sng ON art.ArtistId=sng.ArtistId " +
|
"art LEFT JOIN Song sng ON art.ArtistId=sng.ArtistId " +
|
||||||
"GROUP BY art.ArtistId";
|
"GROUP BY art.ArtistId";
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
using (var reader = cmd.ExecuteReader())
|
||||||
using (var reader = cmd.ExecuteReader())
|
artists = ParseData(reader);
|
||||||
{
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
artists = ParseData(reader);
|
return artists;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return artists;
|
public Artist GetArtist(Artist artist)
|
||||||
}
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.Info("Retrieving artist record from the database");
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
public Artist GetArtist(Artist artist)
|
var query = "SELECT art.*, COUNT(*) AS SongCount FROM Artist " +
|
||||||
{
|
"art LEFT JOIN Song sng ON art.ArtistId=sng.ArtistId " +
|
||||||
try
|
"WHERE art.ArtistId=@ArtistId";
|
||||||
{
|
|
||||||
_logger.Info("Retrieving artist record from the database");
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT art.*, COUNT(*) AS SongCount FROM Artist " +
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
"art LEFT JOIN Song sng ON art.ArtistId=sng.ArtistId " +
|
{
|
||||||
"WHERE art.ArtistId=@ArtistId";
|
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
artist = ParseSingleData(reader);
|
||||||
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
return artist;
|
||||||
{
|
}
|
||||||
artist = ParseSingleData(reader);
|
public Artist GetArtist(Song song)
|
||||||
}
|
{
|
||||||
}
|
Artist artist = new Artist();
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
_logger.Info("Retrieving artist record from the database");
|
||||||
{
|
using (MySqlConnection conn = GetConnection())
|
||||||
var msg = ex.Message;
|
{
|
||||||
_logger.Error(msg, "An error occurred");
|
conn.Open();
|
||||||
}
|
|
||||||
|
|
||||||
return artist;
|
var query = "SELECT art.*, 0 AS SongCount FROM Artist " +
|
||||||
}
|
"art WHERE art.Name=@Name";
|
||||||
public Artist GetArtist(Song song)
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
Artist artist = new Artist();
|
cmd.Parameters.AddWithValue("@Name", song.Artist);
|
||||||
try
|
|
||||||
{
|
|
||||||
_logger.Info("Retrieving artist record from the database");
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT art.*, 0 AS SongCount FROM Artist " +
|
using (var reader = cmd.ExecuteReader())
|
||||||
"art WHERE art.Name=@Name";
|
artist = ParseSingleData(reader);
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
}
|
||||||
cmd.Parameters.AddWithValue("@Name", song.Artist);
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
return artist;
|
||||||
{
|
}
|
||||||
artist = ParseSingleData(reader);
|
public Artist GetArtist(Song song, bool retrieveCount)
|
||||||
}
|
{
|
||||||
}
|
var artist = new Artist();
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return artist;
|
try
|
||||||
}
|
{
|
||||||
public Artist GetArtist(Song song, bool retrieveCount)
|
_logger.Info("Retrieving artist record from the database");
|
||||||
{
|
using (MySqlConnection conn = GetConnection())
|
||||||
var artist = new Artist();
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
try
|
var query = string.Empty;
|
||||||
{
|
|
||||||
_logger.Info("Retrieving artist record from the database");
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = string.Empty;
|
if (retrieveCount)
|
||||||
|
query = "SELECT art.*, COUNT(*) AS SongCount FROM Artist " +
|
||||||
|
"art LEFT JOIN Song sng ON art.ArtistId=sng.ArtistId " +
|
||||||
|
"WHERE art.Name=@Name GROUP BY art.ArtistId LIMIT 1";
|
||||||
|
else
|
||||||
|
query = "SELECT art.*, 0 AS SongCount FROM Artist art " +
|
||||||
|
"WHERE art.Name=@Name LIMIT 1";
|
||||||
|
|
||||||
if (retrieveCount)
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
query = "SELECT art.*, COUNT(*) AS SongCount FROM Artist " +
|
cmd.Parameters.AddWithValue("@Name", song.Artist);
|
||||||
"art LEFT JOIN Song sng ON art.ArtistId=sng.ArtistId " +
|
|
||||||
"WHERE art.Name=@Name GROUP BY art.ArtistId LIMIT 1";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
query = "SELECT art.*, 0 AS SongCount FROM Artist art " +
|
|
||||||
"WHERE art.Name=@Name LIMIT 1";
|
|
||||||
}
|
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
artist = ParseSingleData(reader);
|
||||||
cmd.Parameters.AddWithValue("@Name", song.Artist);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
return artist;
|
||||||
{
|
}
|
||||||
artist = ParseSingleData(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return artist;
|
public bool DoesArtistExist(Artist artist)
|
||||||
}
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.Info("Checking to see if Artist exists");
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
public bool DoesArtistExist(Artist artist)
|
var query = "SELECT art.*, 0 AS SongCount FROM Artist art WHERE art.ArtistId=@ArtistId";
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_logger.Info("Checking to see if Artist exists");
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT art.*, 0 AS SongCount FROM Artist art WHERE art.ArtistId=@ArtistId";
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
artist = ParseSingleData(reader);
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
if (artist.Name != null)
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
_logger.Info($"Artist {artist.Name} exists");
|
||||||
|
return true;
|
||||||
using (var reader = cmd.ExecuteReader())
|
}
|
||||||
{
|
}
|
||||||
artist = ParseSingleData(reader);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Info("Could not successfully retrieve Artist");
|
||||||
|
|
||||||
if (artist.Name != null)
|
return false;
|
||||||
{
|
}
|
||||||
_logger.Info($"Artist {artist.Name} exists");
|
public bool DoesArtistExist(Song song)
|
||||||
return true;
|
{
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
}
|
_logger.Info("Checking to see if Artist exists");
|
||||||
}
|
using (MySqlConnection conn = GetConnection())
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
conn.Open();
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Info("Could not successfully retrieve Artist");
|
|
||||||
|
|
||||||
return false;
|
var query = "SELECT art.*, 0 AS SongCount FROM Artist art WHERE art.Name=@Name";
|
||||||
}
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
public bool DoesArtistExist(Song song)
|
{
|
||||||
{
|
cmd.Parameters.AddWithValue("@Name", song.Artist);
|
||||||
try
|
|
||||||
{
|
using (var reader = cmd.ExecuteReader())
|
||||||
_logger.Info("Checking to see if Artist exists");
|
{
|
||||||
using (MySqlConnection conn = GetConnection())
|
var artist = ParseSingleData(reader);
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
var query = "SELECT art.*, 0 AS SongCount FROM Artist art WHERE art.Name=@Name";
|
if (!String.IsNullOrEmpty(artist.Name))
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
{
|
||||||
{
|
_logger.Info($"Artist {artist.Name} exists");
|
||||||
cmd.Parameters.AddWithValue("@Name", song.Artist);
|
return true;
|
||||||
|
}
|
||||||
using (var reader = cmd.ExecuteReader())
|
}
|
||||||
{
|
}
|
||||||
var artist = ParseSingleData(reader);
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
if (!String.IsNullOrEmpty(artist.Name))
|
_logger.Info("Could not successfully retrieve Artist");
|
||||||
{
|
|
||||||
_logger.Info($"Artist {artist.Name} exists");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Info("Could not successfully retrieve Artist");
|
return false;
|
||||||
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void SaveArtist(Artist artist)
|
public void SaveArtist(Artist artist)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Saving artist record");
|
_logger.Info("Saving artist record");
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "INSERT INTO Artist(Name) VALUES(@Name)";
|
var query = "INSERT INTO Artist(Name) VALUES(@Name)";
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Name", artist.Name);
|
cmd.Parameters.AddWithValue("@Name", artist.Name);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info("Artist record has successfully been saved");
|
_logger.Info("Artist record has successfully been saved");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void UpdateArtist(Artist artist)
|
public void UpdateArtist(Artist artist)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Updating artist record");
|
_logger.Info("Updating artist record");
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "UPDATE Artist SET Name=@Name WHERE ArtistId=@ArtistId";
|
var query = "UPDATE Artist SET Name=@Name WHERE ArtistId=@ArtistId";
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Name", artist.Name);
|
cmd.Parameters.AddWithValue("@Name", artist.Name);
|
||||||
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info("Artist record has successfully been saved");
|
_logger.Info("Artist record has successfully been saved");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void DeleteArtist(Artist artist)
|
public void DeleteArtist(Artist artist)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Deleting artist record");
|
_logger.Info("Deleting artist record");
|
||||||
|
|
||||||
using (var conn = GetConnection())
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
|
||||||
var query = "DELETE FROM Artist WHERE ArtistId=@ArtistId";
|
var query = "DELETE FROM Artist WHERE ArtistId=@ArtistId";
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Artist> ParseData(MySqlDataReader reader)
|
private List<Artist> ParseData(MySqlDataReader reader)
|
||||||
{
|
{
|
||||||
List<Artist> artists = new List<Artist>();
|
List<Artist> artists = new List<Artist>();
|
||||||
|
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
var id = Convert.ToInt32(reader["ArtistId"]);
|
var id = Convert.ToInt32(reader["ArtistId"]);
|
||||||
var name = reader["Name"].ToString();
|
var name = reader["Name"].ToString();
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||||
artists.Add(new Artist
|
artists.Add(new Artist
|
||||||
{
|
{
|
||||||
ArtistId = id,
|
ArtistId = id,
|
||||||
Name = name,
|
Name = name,
|
||||||
SongCount = songCount
|
SongCount = songCount
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info("Artist records retrieved");
|
_logger.Info("Artist records retrieved");
|
||||||
|
|
||||||
return artists;
|
return artists;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Artist ParseSingleData(MySqlDataReader reader)
|
private Artist ParseSingleData(MySqlDataReader reader)
|
||||||
{
|
{
|
||||||
Artist artist = new Artist();
|
Artist artist = new Artist();
|
||||||
|
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
var id = Convert.ToInt32(reader["ArtistId"]);
|
var id = Convert.ToInt32(reader["ArtistId"]);
|
||||||
var name = reader["Name"].ToString();
|
var name = reader["Name"].ToString();
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||||
artist.ArtistId = id;
|
artist.ArtistId = id;
|
||||||
artist.Name = name;
|
artist.Name = name;
|
||||||
artist.SongCount = songCount;
|
artist.SongCount = songCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info("Single artist record retrieved");
|
_logger.Info("Single artist record retrieved");
|
||||||
|
|
||||||
return artist;
|
return artist;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,19 +5,19 @@ using NLog;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class BaseRepository
|
public class BaseRepository
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
protected string _connectionString;
|
protected string _connectionString;
|
||||||
protected static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
protected static Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
protected MySqlConnection GetConnection()
|
protected MySqlConnection GetConnection()
|
||||||
{
|
{
|
||||||
return new MySqlConnection(_connectionString);
|
return new MySqlConnection(_connectionString);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,255 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
using Icarus.Models;
|
||||||
|
using Icarus.Types;
|
||||||
|
|
||||||
|
namespace Icarus.Database.Repositories
|
||||||
|
{
|
||||||
|
public class CoverArtRepository : BaseRepository
|
||||||
|
{
|
||||||
|
#region Constructors
|
||||||
|
public CoverArtRepository(string connectionString)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
public CoverArt GetCoverArt(CoverArt cover)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
_logger.Info("Querying cover art record");
|
||||||
|
|
||||||
|
var query = "SELECT * FROM CoverArt WHERE " +
|
||||||
|
"CoverArtId=@CoverArtId";
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters
|
||||||
|
.AddWithValue("@CoverArtId", cover.CoverArtId);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
return ParseSingleData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public CoverArt GetCoverArt(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
_logger.Info("Querying cover art record");
|
||||||
|
|
||||||
|
var query = "SELECT cov.* FROM CoverArt cov LEFT JOIN " +
|
||||||
|
"Song sng ON cov.CoverArtId=sng.CoverArtId WHERE " +
|
||||||
|
"sng.Id=@SongId";
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@SongId", song.Id);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
return ParseSingleData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public CoverArt GetCoverArt(CoverArtField field, CoverArt cover)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
_logger.Info("Querying cover art record");
|
||||||
|
|
||||||
|
using (var cmd = new MySqlCommand(BuildQuery(field), conn))
|
||||||
|
{
|
||||||
|
switch (field)
|
||||||
|
{
|
||||||
|
case CoverArtField.SongTitle:
|
||||||
|
cmd.Parameters.AddWithValue("@SongTitle",
|
||||||
|
cover.SongTitle);
|
||||||
|
break;
|
||||||
|
case CoverArtField.ImagePath:
|
||||||
|
cmd.Parameters.AddWithValue("@ImagePath",
|
||||||
|
cover.ImagePath);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
return ParseSingleData(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesCoverArtExist(CoverArt cover)
|
||||||
|
{
|
||||||
|
return GetCoverArt(cover) != null ? true : false;
|
||||||
|
}
|
||||||
|
public bool DoesCoverArtExist(Song song)
|
||||||
|
{
|
||||||
|
return GetCoverArt(song) != null ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SaveCoverArt(CoverArt coverArt)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
_logger.Info("Saving cover art record");
|
||||||
|
|
||||||
|
var query = "INSERT INTO CoverArt(SongTitle, ImagePath) " +
|
||||||
|
"VALUES(@SongTitle, @ImagePath)";
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters
|
||||||
|
.AddWithValue("@SongTitle", coverArt.SongTitle);
|
||||||
|
cmd.Parameters
|
||||||
|
.AddWithValue("@ImagePath", coverArt.ImagePath);
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteCoverArt(CoverArt cover)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
_logger.Info("Deleting cover art record");
|
||||||
|
|
||||||
|
var query = "DELETE FROM CoverArt WHERE " +
|
||||||
|
"CoverArtId=@CoverArtId";
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters
|
||||||
|
.AddWithValue("@CoverArtId", cover.CoverArtId);
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<CoverArt> ParseData(MySqlDataReader reader)
|
||||||
|
{
|
||||||
|
if (reader.HasRows)
|
||||||
|
{
|
||||||
|
var coverArtList = new List<CoverArt>();
|
||||||
|
_logger.Info("Parsing cover art records");
|
||||||
|
while (reader.Read())
|
||||||
|
coverArtList.Add(new CoverArt
|
||||||
|
{
|
||||||
|
CoverArtId = Convert.ToInt32(reader["CoverArtId"]),
|
||||||
|
SongTitle = reader["SongTitle"].ToString(),
|
||||||
|
ImagePath = reader["ImagePath"].ToString()
|
||||||
|
});
|
||||||
|
|
||||||
|
return coverArtList;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private CoverArt ParseSingleData(MySqlDataReader reader)
|
||||||
|
{
|
||||||
|
if (reader.HasRows)
|
||||||
|
{
|
||||||
|
_logger.Info("Parsing single cover art record");
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
return new CoverArt
|
||||||
|
{
|
||||||
|
CoverArtId = Convert.ToInt32(reader["CoverArtId"]),
|
||||||
|
SongTitle = reader["SongTitle"].ToString(),
|
||||||
|
ImagePath = reader["ImagePath"].ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string BuildQuery(CoverArtField field)
|
||||||
|
{
|
||||||
|
switch (field)
|
||||||
|
{
|
||||||
|
case CoverArtField.SongTitle:
|
||||||
|
return "SELECT * FROM CoverArt WHERE SongTitle=@SongTitle";
|
||||||
|
case CoverArtField.ImagePath:
|
||||||
|
return "SELECT * FROM CoverArt WHERE ImagePath=" +
|
||||||
|
"@ImagePath";
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool? AnyCoverArt()
|
||||||
|
{
|
||||||
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
_logger.Info("Checking to see if there are any cover art " +
|
||||||
|
"records");
|
||||||
|
|
||||||
|
var query = "SELECT * FROM CoverArt";
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
return reader.HasRows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,462 +8,440 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class GenreRepository : BaseRepository
|
public class GenreRepository : BaseRepository
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public GenreRepository(string connectionString)
|
public GenreRepository(string connectionString)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public List<Genre> GetGenresWithoutCount()
|
public List<Genre> GetGenresWithoutCount()
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving all genre records without song counts");
|
_logger.Info("Retrieving all genre records without song counts");
|
||||||
|
|
||||||
var genres = new List<Genre>();
|
var genres = new List<Genre>();
|
||||||
|
|
||||||
if (AnyGenre())
|
if (AnyGenre())
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var conn = GetConnection())
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
|
||||||
var query = "SELECT * FROM Genre";
|
var query = "SELECT * FROM Genre";
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
using (var reader = cmd.ExecuteReader())
|
||||||
using (var reader = cmd.ExecuteReader())
|
genres = ParseData(reader);
|
||||||
{
|
}
|
||||||
genres = ParseData(reader);
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
}
|
var msg = ex.Message;
|
||||||
}
|
_logger.Error(msg, "An error occurred");
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
}
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
return genres;
|
||||||
}
|
}
|
||||||
}
|
public List<Genre> GetGenres()
|
||||||
|
{
|
||||||
return genres;
|
_logger.Info("Retrieving Genre records");
|
||||||
}
|
|
||||||
public List<Genre> GetGenres()
|
var genres = new List<Genre>();
|
||||||
{
|
|
||||||
_logger.Info("Retrieving Genre records");
|
if (AnyGenre())
|
||||||
|
{
|
||||||
var genres = new List<Genre>();
|
|
||||||
|
try
|
||||||
if (AnyGenre())
|
{
|
||||||
{
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
try
|
conn.Open();
|
||||||
{
|
|
||||||
using (var conn = GetConnection())
|
var query = "SELECT gnr.*, COUNT(*) AS SongCount FROM Genre " +
|
||||||
{
|
"gnr LEFT JOIN Song sng ON gnr.GenreId=sng.GenreId " +
|
||||||
conn.Open();
|
"GROUP BY gnr.GenreId";
|
||||||
|
|
||||||
var query = "SELECT gnr.*, COUNT(*) AS SongCount FROM Genre " +
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
"gnr LEFT JOIN Song sng ON gnr.GenreId=sng.GenreId " +
|
using (var reader = cmd.ExecuteReader())
|
||||||
"GROUP BY gnr.GenreId";
|
genres = ParseData(reader);
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
using (var reader = cmd.ExecuteReader())
|
{
|
||||||
{
|
var msg = ex.Message;
|
||||||
genres = ParseData(reader);
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
return genres;
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
public Genre GetGenre(Genre genre)
|
||||||
_logger.Error(msg, "An error occurred");
|
{
|
||||||
}
|
_logger.Info("Retrieving Genre record");
|
||||||
}
|
|
||||||
|
if (DoesGenreExist(genre))
|
||||||
return genres;
|
{
|
||||||
}
|
try
|
||||||
|
{
|
||||||
public Genre GetGenre(Genre genre)
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving Genre record");
|
conn.Open();
|
||||||
|
|
||||||
if (DoesGenreExist(genre))
|
var query = "SELECT gnr.*, COUNT(*) AS SongCount FROM Genre " +
|
||||||
{
|
"gnr LEFT JOIN Song sng ON gnr.GenreId=sng.GenreId " +
|
||||||
try
|
"WHERE gnr.GenreId=@GenreId";
|
||||||
{
|
|
||||||
using (var conn = GetConnection())
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
conn.Open();
|
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
||||||
|
|
||||||
var query = "SELECT gnr.*, COUNT(*) AS SongCount FROM Genre " +
|
using (var reader = cmd.ExecuteReader())
|
||||||
"gnr LEFT JOIN Song sng ON gnr.GenreId=sng.GenreId " +
|
genre = ParseSingleData(reader);
|
||||||
"WHERE gnr.GenreId=@GenreId";
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
using (var reader = cmd.ExecuteReader())
|
_logger.Error(msg, "An error occurred");
|
||||||
{
|
}
|
||||||
genre = ParseSingleData(reader);
|
}
|
||||||
}
|
|
||||||
}
|
return genre;
|
||||||
}
|
}
|
||||||
}
|
public Genre GetGenre(Song song)
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
_logger.Info("Retrieving Genre record");
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
var genre = new Genre();
|
||||||
}
|
|
||||||
}
|
if (DoesGenreExist(song))
|
||||||
|
{
|
||||||
return genre;
|
try
|
||||||
}
|
{
|
||||||
public Genre GetGenre(Song song)
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving Genre record");
|
conn.Open();
|
||||||
|
|
||||||
var genre = new Genre();
|
var query = "SELECT gnr.*, 0 AS SongCount FROM Genre " +
|
||||||
|
"gnr WHERE gnr.GenreName=@GenreName";
|
||||||
if (DoesGenreExist(song))
|
|
||||||
{
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
try
|
{
|
||||||
{
|
cmd.Parameters.AddWithValue("@GenreName", song.Genre);
|
||||||
using (var conn = GetConnection())
|
_logger.Info($"Song genre:\n\n\n {song.Genre}");
|
||||||
{
|
|
||||||
conn.Open();
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
genre = ParseSingleData(reader);
|
||||||
var query = "SELECT gnr.*, 0 AS SongCount FROM Genre " +
|
}
|
||||||
"gnr WHERE gnr.GenreName=@GenreName";
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@GenreName", song.Genre);
|
var msg = ex.Message;
|
||||||
_logger.Info($"Song genre:\n\n\n {song.Genre}");
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
using (var reader = cmd.ExecuteReader())
|
}
|
||||||
{
|
|
||||||
genre = ParseSingleData(reader);
|
return genre;
|
||||||
}
|
}
|
||||||
}
|
public Genre GetGenre(Song song, bool retrieveCount)
|
||||||
}
|
{
|
||||||
}
|
_logger.Info("Retrieving Genre record");
|
||||||
catch (Exception ex)
|
|
||||||
{
|
var genre = new Genre();
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
if (DoesGenreExist(song))
|
||||||
}
|
{
|
||||||
}
|
try
|
||||||
|
{
|
||||||
return genre;
|
using (var conn = GetConnection())
|
||||||
}
|
{
|
||||||
public Genre GetGenre(Song song, bool retrieveCount)
|
conn.Open();
|
||||||
{
|
|
||||||
_logger.Info("Retrieving Genre record");
|
var query = string.Empty;
|
||||||
|
|
||||||
var genre = new Genre();
|
if (retrieveCount)
|
||||||
|
query = "SELECT gnr.*, COUNT(*) AS SongCount FROM Genre gnr " +
|
||||||
if (DoesGenreExist(song))
|
"LEFT JOIN Song sng ON gnr.GenreId=sng.GenreId " +
|
||||||
{
|
"WHERE gnr.GenreName=@GenreName GROUP BY gnr.GenreId " +
|
||||||
try
|
"LIMIT 1";
|
||||||
{
|
else
|
||||||
using (var conn = GetConnection())
|
query = "SELECT gnr.*, 0 AS SongCount FROM Genre gnr " +
|
||||||
{
|
"WHERE gnr.GenreName=@GenreName LIMIT 1";
|
||||||
conn.Open();
|
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
var query = string.Empty;
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@GenreName", song.Genre);
|
||||||
if (retrieveCount)
|
_logger.Info($"Song genre:\n\n\n {song.Genre}");
|
||||||
{
|
|
||||||
query = "SELECT gnr.*, COUNT(*) AS SongCount FROM Genre gnr " +
|
using (var reader = cmd.ExecuteReader())
|
||||||
"LEFT JOIN Song sng ON gnr.GenreId=sng.GenreId " +
|
genre = ParseSingleData(reader);
|
||||||
"WHERE gnr.GenreName=@GenreName GROUP BY gnr.GenreId " +
|
}
|
||||||
"LIMIT 1";
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
query = "SELECT gnr.*, 0 AS SongCount FROM Genre gnr " +
|
var msg = ex.Message;
|
||||||
"WHERE gnr.GenreName=@GenreName LIMIT 1";
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
return genre;
|
||||||
cmd.Parameters.AddWithValue("@GenreName", song.Genre);
|
}
|
||||||
_logger.Info($"Song genre:\n\n\n {song.Genre}");
|
|
||||||
|
public bool DoesGenreExist(Genre genre)
|
||||||
using (var reader = cmd.ExecuteReader())
|
{
|
||||||
{
|
_logger.Info("Checking to see if Genre record exists");
|
||||||
genre = ParseSingleData(reader);
|
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
}
|
using (var conn = GetConnection())
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
conn.Open();
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
var query = "SELECT gnr.*, 0 AS SongCount FROM Genre gnr WHERE " +
|
||||||
_logger.Error(msg, "An error occurred");
|
"gnr.GenreId=@GenreId";
|
||||||
}
|
|
||||||
}
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
return genre;
|
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
||||||
}
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
public bool DoesGenreExist(Genre genre)
|
{
|
||||||
{
|
genre = ParseSingleData(reader);
|
||||||
_logger.Info("Checking to see if Genre record exists");
|
var genreName = genre.GenreName;
|
||||||
|
|
||||||
try
|
if (!string.IsNullOrEmpty(genreName))
|
||||||
{
|
{
|
||||||
using (var conn = GetConnection())
|
_logger.Info("Genre exists");
|
||||||
{
|
|
||||||
conn.Open();
|
return true;
|
||||||
|
}
|
||||||
var query = "SELECT gnr.*, 0 AS SongCount FROM Genre gnr WHERE " +
|
}
|
||||||
"gnr.GenreId=@GenreId";
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
using (var reader = cmd.ExecuteReader())
|
_logger.Error(msg, "An error occurred");
|
||||||
{
|
}
|
||||||
genre = ParseSingleData(reader);
|
|
||||||
var genreName = genre.GenreName;
|
_logger.Info("Genre does not exist");
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(genreName))
|
return false;
|
||||||
{
|
}
|
||||||
_logger.Info("Genre exists");
|
public bool DoesGenreExist(Song song)
|
||||||
|
{
|
||||||
return true;
|
_logger.Info("Checking to see if Genre record exists");
|
||||||
}
|
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
}
|
using (var conn = GetConnection())
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
conn.Open();
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
var query = "SELECT gnr.*, 0 AS SongCount FROM Genre gnr WHERE " +
|
||||||
_logger.Error(msg, "An error occurred");
|
"gnr.GenreName=@GenreName";
|
||||||
}
|
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
_logger.Info("Genre does not exist");
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@GenreName", song.Genre);
|
||||||
return false;
|
|
||||||
}
|
using (var reader = cmd.ExecuteReader())
|
||||||
public bool DoesGenreExist(Song song)
|
{
|
||||||
{
|
var genre = ParseSingleData(reader);
|
||||||
_logger.Info("Checking to see if Genre record exists");
|
var genreName = genre.GenreName;
|
||||||
|
|
||||||
try
|
if (!string.IsNullOrEmpty(genreName))
|
||||||
{
|
{
|
||||||
using (var conn = GetConnection())
|
_logger.Info("Genre exists");
|
||||||
{
|
|
||||||
conn.Open();
|
return true;
|
||||||
|
}
|
||||||
var query = "SELECT gnr.*, 0 AS SongCount FROM Genre gnr WHERE " +
|
}
|
||||||
"gnr.GenreName=@GenreName";
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
cmd.Parameters.AddWithValue("@GenreName", song.Genre);
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
using (var reader = cmd.ExecuteReader())
|
_logger.Error(msg, "An error occurred");
|
||||||
{
|
}
|
||||||
var genre = ParseSingleData(reader);
|
|
||||||
var genreName = genre.GenreName;
|
_logger.Info("Genre does not exist");
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(genreName))
|
return false;
|
||||||
{
|
}
|
||||||
_logger.Info("Genre exists");
|
|
||||||
|
public void SaveGenre(Genre genre)
|
||||||
return true;
|
{
|
||||||
}
|
_logger.Info("Saving Genre record");
|
||||||
}
|
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
}
|
using (var conn = GetConnection())
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
conn.Open();
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
var query = "INSERT INTO Genre(GenreName) VALUES(@GenreName)";
|
||||||
}
|
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
_logger.Info("Genre does not exist");
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@GenreName", genre.GenreName);
|
||||||
return false;
|
|
||||||
}
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
public void SaveGenre(Genre genre)
|
}
|
||||||
{
|
}
|
||||||
_logger.Info("Saving Genre record");
|
catch (Exception ex)
|
||||||
|
{
|
||||||
try
|
var msg = ex.Message;
|
||||||
{
|
_logger.Error(msg, "An error occurred");
|
||||||
using (var conn = GetConnection())
|
}
|
||||||
{
|
}
|
||||||
conn.Open();
|
public void UpdateGenre(Genre genre)
|
||||||
|
{
|
||||||
var query = "INSERT INTO Genre(GenreName) VALUES(@GenreName)";
|
_logger.Info("Updating Genre record");
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
try
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@GenreName", genre.GenreName);
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
cmd.ExecuteNonQuery();
|
conn.Open();
|
||||||
}
|
|
||||||
}
|
var query = "UPDATE Genre SET GenreName=@GenreName " +
|
||||||
}
|
"WHERE GenreId=@GenreId";
|
||||||
catch (Exception ex)
|
|
||||||
{
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
var msg = ex.Message;
|
{
|
||||||
_logger.Error(msg, "An error occurred");
|
cmd.Parameters.AddWithValue("@GenreName", genre.GenreName);
|
||||||
}
|
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
||||||
}
|
|
||||||
public void UpdateGenre(Genre genre)
|
cmd.ExecuteNonQuery();
|
||||||
{
|
}
|
||||||
_logger.Info("Updating Genre record");
|
}
|
||||||
|
}
|
||||||
try
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
using (var conn = GetConnection())
|
var msg = ex.Message;
|
||||||
{
|
_logger.Error(msg, "An error occurred");
|
||||||
conn.Open();
|
}
|
||||||
|
}
|
||||||
var query = "UPDATE Genre SET GenreName=@GenreName " +
|
public void DeleteGenre(Genre genre)
|
||||||
"WHERE GenreId=@GenreId";
|
{
|
||||||
|
_logger.Info("Deleting Genre record");
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
try
|
||||||
cmd.Parameters.AddWithValue("@GenreName", genre.GenreName);
|
{
|
||||||
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
cmd.ExecuteNonQuery();
|
conn.Open();
|
||||||
}
|
|
||||||
}
|
var query = "DELETE FROM Genre WHERE GenreId=@GenreId";
|
||||||
}
|
|
||||||
catch (Exception ex)
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
public void DeleteGenre(Genre genre)
|
}
|
||||||
{
|
}
|
||||||
_logger.Info("Deleting Genre record");
|
catch (Exception ex)
|
||||||
|
{
|
||||||
try
|
var msg = ex.Message;
|
||||||
{
|
_logger.Error(msg, "An error occurred");
|
||||||
using (var conn = GetConnection())
|
}
|
||||||
{
|
}
|
||||||
conn.Open();
|
|
||||||
|
private List<Genre> ParseData(MySqlDataReader reader)
|
||||||
var query = "DELETE FROM Genre WHERE GenreId=@GenreId";
|
{
|
||||||
|
var genres = new List<Genre>();
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
_logger.Info("Retrieving genre records");
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@GenreId", genre.GenreId);
|
while (reader.Read())
|
||||||
|
{
|
||||||
cmd.ExecuteNonQuery();
|
var id = Convert.ToInt32(reader["GenreId"].ToString());
|
||||||
}
|
var genreName = reader["GenreName"].ToString();
|
||||||
}
|
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||||
}
|
|
||||||
catch (Exception ex)
|
genres.Add(new Genre
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
GenreId = id,
|
||||||
_logger.Error(msg, "An error occurred");
|
GenreName = genreName,
|
||||||
}
|
SongCount = songCount
|
||||||
}
|
});
|
||||||
|
}
|
||||||
private List<Genre> ParseData(MySqlDataReader reader)
|
|
||||||
{
|
_logger.Info("Genre records retrieved");
|
||||||
var genres = new List<Genre>();
|
|
||||||
_logger.Info("Retrieving genre records");
|
return genres;
|
||||||
|
}
|
||||||
while (reader.Read())
|
|
||||||
{
|
private Genre ParseSingleData(MySqlDataReader reader)
|
||||||
var id = Convert.ToInt32(reader["GenreId"].ToString());
|
{
|
||||||
var genreName = reader["GenreName"].ToString();
|
var genre = new Genre();
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
_logger.Info("Retrieving single genre record");
|
||||||
|
|
||||||
genres.Add(new Genre
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
GenreId = id,
|
var id = Convert.ToInt32(reader["GenreId"].ToString());
|
||||||
GenreName = genreName,
|
var genreName = reader["GenreName"].ToString();
|
||||||
SongCount = songCount
|
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||||
});
|
genre.GenreId = id;
|
||||||
}
|
genre.GenreName = genreName;
|
||||||
|
genre.SongCount = songCount;
|
||||||
_logger.Info("Genre records retrieved");
|
}
|
||||||
|
|
||||||
return genres;
|
_logger.Info("Single genre record retrieved");
|
||||||
}
|
|
||||||
|
return genre;
|
||||||
private Genre ParseSingleData(MySqlDataReader reader)
|
}
|
||||||
{
|
|
||||||
var genre = new Genre();
|
private bool AnyGenre()
|
||||||
_logger.Info("Retrieving single genre record");
|
{
|
||||||
|
try
|
||||||
while (reader.Read())
|
{
|
||||||
{
|
using (var conn = GetConnection())
|
||||||
var id = Convert.ToInt32(reader["GenreId"].ToString());
|
{
|
||||||
var genreName = reader["GenreName"].ToString();
|
conn.Open();
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
|
||||||
genre.GenreId = id;
|
var query = "SELECT * FROM Genre";
|
||||||
genre.GenreName = genreName;
|
|
||||||
genre.SongCount = songCount;
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
}
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
return reader.HasRows;
|
||||||
_logger.Info("Single genre record retrieved");
|
}
|
||||||
|
}
|
||||||
return genre;
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
private bool AnyGenre()
|
_logger.Error(msg, "An error occurred");
|
||||||
{
|
}
|
||||||
try
|
|
||||||
{
|
_logger.Info("No genre records found");
|
||||||
using (var conn = GetConnection())
|
|
||||||
{
|
return false;
|
||||||
conn.Open();
|
}
|
||||||
|
#endregion
|
||||||
var query = "SELECT * FROM Genre";
|
}
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
return reader.HasRows;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Info("No genre records found");
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,320 +7,318 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class SongRepository : BaseRepository
|
public class SongRepository : BaseRepository
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public SongRepository(string connectionString)
|
public SongRepository(string connectionString)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public void SaveSong(Song song)
|
public void SaveSong(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Saving song to the database");
|
_logger.Info("Saving song to the database");
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
string query = "INSERT INTO Song(Title, AlbumTitle, Artist," +
|
string query = "INSERT INTO Song(Title, AlbumTitle, Artist," +
|
||||||
" Year, Genre, Duration, Filename, SongPath, AlbumId, " +
|
" Year, Genre, Duration, Filename, SongPath, AlbumId, " +
|
||||||
"ArtistId, GenreId, YearId) VALUES(@Title, @AlbumTitle, " +
|
"ArtistId, GenreId, YearId, CoverArtId) VALUES(@Title," +
|
||||||
"@Artist, @Year, @Genre, @Duration, @Filename, @SongPath, " +
|
" @AlbumTitle, @Artist, @Year, @Genre, @Duration, " +
|
||||||
"@AlbumId, @ArtistId, @GenreId, @YearId)";
|
"@Filename, @SongPath, @AlbumId, @ArtistId, @GenreId," +
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
" @YearId, @CoverArtId)";
|
||||||
{
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
{
|
||||||
cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle);
|
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||||
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle);
|
||||||
cmd.Parameters.AddWithValue("@Year", song.Year);
|
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
||||||
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
cmd.Parameters.AddWithValue("@Year", song.Year);
|
||||||
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
||||||
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
||||||
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
||||||
cmd.Parameters.AddWithValue("@AlbumId", song.AlbumId);
|
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||||
cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId);
|
cmd.Parameters.AddWithValue("@AlbumId", song.AlbumId);
|
||||||
cmd.Parameters.AddWithValue("@GenreId", song.GenreId);
|
cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId);
|
||||||
cmd.Parameters.AddWithValue("@YearId", song.YearId);
|
cmd.Parameters.AddWithValue("@GenreId", song.GenreId);
|
||||||
|
cmd.Parameters.AddWithValue("@YearId", song.YearId);
|
||||||
|
cmd.Parameters.AddWithValue("@CoverArtId", song.CoverArtId);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_logger.Info("Successfully saved song to the database");
|
_logger.Info("Successfully saved song to the database");
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
var exMsg = ex.Message;
|
var exMsg = ex.Message;
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
Console.WriteLine($"An error occurred:\n{exMsg}");
|
||||||
_logger.Error(exMsg, "An error occurred");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void UpdateSong(Song song)
|
public void UpdateSong(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
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, Duration=@Duration, " +
|
"Artist=@Artist, Year=@Year, Genre=@Genre, Duration=@Duration, " +
|
||||||
"Filename=@Filename, SongPath=@SongPath, AlbumId=@AlbumId, " +
|
"Filename=@Filename, SongPath=@SongPath, AlbumId=@AlbumId, " +
|
||||||
"ArtistId=@ArtistId, GenreId=@GenreId, YearId=@YearId WHERE Id=@Id";
|
"ArtistId=@ArtistId, GenreId=@GenreId, YearId=@YearId 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);
|
||||||
cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle);
|
cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle);
|
||||||
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
||||||
cmd.Parameters.AddWithValue("@Year", song.Year);
|
cmd.Parameters.AddWithValue("@Year", song.Year);
|
||||||
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
||||||
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
||||||
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("@AlbumId", song.AlbumId);
|
||||||
cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId);
|
cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId);
|
||||||
cmd.Parameters.AddWithValue("@GenreId", song.GenreId);
|
cmd.Parameters.AddWithValue("@GenreId", song.GenreId);
|
||||||
cmd.Parameters.AddWithValue("@YearId", song.YearId);
|
cmd.Parameters.AddWithValue("@YearId", song.YearId);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_logger.Info("Updated song");
|
_logger.Info("Updated song");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
Console.WriteLine("An error occurred in SongRepository:");
|
Console.WriteLine("An error occurred in SongRepository:");
|
||||||
Console.WriteLine(msg);
|
Console.WriteLine(msg);
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void DeleteSong(Song song)
|
public void DeleteSong(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Deleting song record");
|
_logger.Info("Deleting song record");
|
||||||
|
|
||||||
using (var conn = GetConnection())
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
|
||||||
var query = "Delete FROM Song WHERE Id=@Id";
|
var query = "Delete FROM Song WHERE Id=@Id";
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Id", song.Id);
|
cmd.Parameters.AddWithValue("@Id", song.Id);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void DeleteSong(int id)
|
public void DeleteSong(int id)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Deleting song record");
|
_logger.Info("Deleting song record");
|
||||||
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
string query = "DELETE FROM Song WHERE Id=@Id";
|
string query = "DELETE FROM Song WHERE Id=@Id";
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Id", id);
|
cmd.Parameters.AddWithValue("@Id", id);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var exMsg = ex.Message;
|
var exMsg = ex.Message;
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
Console.WriteLine($"An error occurred:\n{exMsg}");
|
||||||
_logger.Error(exMsg, "An error occurred");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Song> GetAllSongs()
|
public List<Song> GetAllSongs()
|
||||||
{
|
{
|
||||||
List<Song> songs = new List<Song>();
|
List<Song> songs = new List<Song>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving songs from the database");
|
_logger.Info("Retrieving songs from the database");
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "SELECT * FROM Song";
|
var query = "SELECT * FROM Song";
|
||||||
Console.WriteLine("ffff");
|
Console.WriteLine("ffff");
|
||||||
MySqlCommand cmd = new MySqlCommand(query, conn);
|
MySqlCommand cmd = new MySqlCommand(query, conn);
|
||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
songs = ParseData(reader);
|
||||||
songs = ParseData(reader);
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
var exMsg = ex.Message;
|
||||||
{
|
Console.WriteLine($"An error ocurred:\n{exMsg}");
|
||||||
var exMsg = ex.Message;
|
songs.Clear();
|
||||||
Console.WriteLine($"An error ocurred:\n{exMsg}");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
songs.Clear();
|
}
|
||||||
_logger.Error(exMsg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return songs;
|
return songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Song GetSong(Song song)
|
public Song GetSong(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving song from database");
|
_logger.Info("Retrieving song from database");
|
||||||
|
|
||||||
using (var conn = GetConnection())
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "SELECT * FROM Song WHERE Id=@Id";
|
var query = "SELECT * FROM Song WHERE Id=@Id";
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Id", song.Id);
|
cmd.Parameters.AddWithValue("@Id", song.Id);
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
song = ParseSingleData(reader);
|
||||||
song = ParseSingleData(reader);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
var msg = ex.Message;
|
||||||
{
|
_logger.Error(msg, "An error occurred");
|
||||||
var msg = ex.Message;
|
}
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
public Song GetSong(int id)
|
public Song GetSong(int id)
|
||||||
{
|
{
|
||||||
Song song = new Song();
|
Song song = new Song();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving song from database");
|
_logger.Info("Retrieving song from database");
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "SELECT * FROM Song WHERE Id=@Id";
|
var query = "SELECT * FROM Song WHERE Id=@Id";
|
||||||
|
|
||||||
MySqlCommand cmd = new MySqlCommand(query, conn);
|
MySqlCommand cmd = new MySqlCommand(query, conn);
|
||||||
cmd.Parameters.AddWithValue("@Id", id);
|
cmd.Parameters.AddWithValue("@Id", id);
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
song = ParseSingleData(reader);
|
||||||
song = ParseSingleData(reader);
|
}
|
||||||
}
|
_logger.Info("Song found");
|
||||||
}
|
}
|
||||||
_logger.Info("Song found");
|
catch(Exception ex)
|
||||||
}
|
{
|
||||||
catch(Exception ex)
|
var exMsg = ex.Message;
|
||||||
{
|
Console.WriteLine($"An error ocurred: {exMsg}");
|
||||||
var exMsg = ex.Message;
|
_logger.Error(exMsg, "An error occurred");
|
||||||
Console.WriteLine($"An error ocurred: {exMsg}");
|
}
|
||||||
_logger.Error(exMsg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool DoesSongExist(Song song)
|
public bool DoesSongExist(Song song)
|
||||||
{
|
{
|
||||||
_logger.Info("Checking to see if the song exists");
|
_logger.Info("Checking to see if the song exists");
|
||||||
var songInDatabase = GetSong(song);
|
var songInDatabase = GetSong(song);
|
||||||
var title = songInDatabase.Title;
|
var title = songInDatabase.Title;
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(title))
|
if (!string.IsNullOrEmpty(title))
|
||||||
{
|
{
|
||||||
_logger.Info("Song exists");
|
_logger.Info("Song exists");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info("Song does not exists");
|
_logger.Info("Song does not exists");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Song> ParseData(MySqlDataReader reader)
|
private List<Song> ParseData(MySqlDataReader reader)
|
||||||
{
|
{
|
||||||
|
|
||||||
List<Song> songs = new List<Song>();
|
List<Song> songs = new List<Song>();
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
songs.Add(new Song
|
songs.Add(new Song
|
||||||
{
|
{
|
||||||
Id = Convert.ToInt32(reader["Id"]),
|
Id = Convert.ToInt32(reader["Id"]),
|
||||||
Title = reader["Title"].ToString(),
|
Title = reader["Title"].ToString(),
|
||||||
AlbumTitle = reader["AlbumTitle"].ToString(),
|
AlbumTitle = reader["AlbumTitle"].ToString(),
|
||||||
Artist = reader["Artist"].ToString(),
|
Artist = reader["Artist"].ToString(),
|
||||||
Year = Convert.ToInt32(reader["Year"]),
|
Year = Convert.ToInt32(reader["Year"]),
|
||||||
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()),
|
AlbumId = Convert.ToInt32(reader["AlbumId"].ToString()),
|
||||||
ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()),
|
ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()),
|
||||||
GenreId = Convert.ToInt32(reader["GenreId"].ToString()),
|
GenreId = Convert.ToInt32(reader["GenreId"].ToString()),
|
||||||
YearId = Convert.ToInt32(reader["YearId"].ToString())
|
YearId = Convert.ToInt32(reader["YearId"].ToString()),
|
||||||
});
|
CoverArtId = Convert.ToInt32(reader["CoverArtId"].ToString())
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return songs;
|
return songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Song ParseSingleData(MySqlDataReader reader)
|
private Song ParseSingleData(MySqlDataReader reader)
|
||||||
{
|
{
|
||||||
Song song = new Song();
|
Song song = new Song();
|
||||||
|
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
song.Id = Convert.ToInt32(reader["Id"]);
|
song.Id = Convert.ToInt32(reader["Id"]);
|
||||||
song.Title = reader["Title"].ToString();
|
song.Title = reader["Title"].ToString();
|
||||||
song.AlbumTitle = reader["AlbumTitle"].ToString();
|
song.AlbumTitle = reader["AlbumTitle"].ToString();
|
||||||
song.Artist = reader["Artist"].ToString();
|
song.Artist = reader["Artist"].ToString();
|
||||||
song.Year = Convert.ToInt32(reader["Year"].ToString());
|
song.Year = Convert.ToInt32(reader["Year"].ToString());
|
||||||
song.Genre = reader["Genre"].ToString();
|
song.Genre = reader["Genre"].ToString();
|
||||||
song.Duration = Convert.ToInt32(reader["Duration"]);
|
song.Duration = Convert.ToInt32(reader["Duration"]);
|
||||||
song.Filename = reader["Filename"].ToString();
|
song.Filename = reader["Filename"].ToString();
|
||||||
song.SongPath = reader["SongPath"].ToString();
|
song.SongPath = reader["SongPath"].ToString();
|
||||||
song.AlbumId = Convert.ToInt32(reader["AlbumId"].ToString());
|
song.AlbumId = Convert.ToInt32(reader["AlbumId"].ToString());
|
||||||
song.ArtistId = Convert.ToInt32(reader["ArtistId"].ToString());
|
song.ArtistId = Convert.ToInt32(reader["ArtistId"].ToString());
|
||||||
song.GenreId = Convert.ToInt32(reader["GenreId"].ToString());
|
song.GenreId = Convert.ToInt32(reader["GenreId"].ToString());
|
||||||
song.YearId = Convert.ToInt32(reader["YearId"].ToString());
|
song.YearId = Convert.ToInt32(reader["YearId"].ToString());
|
||||||
}
|
song.CoverArtId = Convert.ToInt32(reader["CoverArtId"].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,224 +8,222 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class UserRepository: BaseRepository
|
public class UserRepository: BaseRepository
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public UserRepository(string connectionString)
|
public UserRepository(string connectionString)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public void SaveUser(User user)
|
public void SaveUser(User user)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Saving user");
|
_logger.Info("Saving user");
|
||||||
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
string query = "INSERT INTO User(Username, Password, Nickname, Email" +
|
string query = "INSERT INTO User(Username, Password, Nickname, Email" +
|
||||||
", PhoneNumber, Firstname, Lastname, EmailVerified) " +
|
", PhoneNumber, Firstname, Lastname, EmailVerified) " +
|
||||||
"VALUES(@Username, @Password, @Nickname, @Email, @PhoneNumber," +
|
"VALUES(@Username, @Password, @Nickname, @Email, @PhoneNumber," +
|
||||||
" @Firstname, @Lastname, @EmailVerified)";
|
" @Firstname, @Lastname, @EmailVerified)";
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Username", user.Username);
|
cmd.Parameters.AddWithValue("@Username", user.Username);
|
||||||
cmd.Parameters.AddWithValue("@Password", user.Password);
|
cmd.Parameters.AddWithValue("@Password", user.Password);
|
||||||
cmd.Parameters.AddWithValue("@Nickname", user.Nickname);
|
cmd.Parameters.AddWithValue("@Nickname", user.Nickname);
|
||||||
cmd.Parameters.AddWithValue("@Email", user.Email);
|
cmd.Parameters.AddWithValue("@Email", user.Email);
|
||||||
cmd.Parameters.AddWithValue("@PhoneNumber", user.PhoneNumber);
|
cmd.Parameters.AddWithValue("@PhoneNumber", user.PhoneNumber);
|
||||||
cmd.Parameters.AddWithValue("@Firstname", user.Firstname);
|
cmd.Parameters.AddWithValue("@Firstname", user.Firstname);
|
||||||
cmd.Parameters.AddWithValue("@Lastname", user.Lastname);
|
cmd.Parameters.AddWithValue("@Lastname", user.Lastname);
|
||||||
cmd.Parameters.AddWithValue("@EmailVerified", user.EmailVerified);
|
cmd.Parameters.AddWithValue("@EmailVerified", user.EmailVerified);
|
||||||
|
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info("Successfully saved user");
|
_logger.Info("Successfully saved user");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var exMsg = ex.Message;
|
var exMsg = ex.Message;
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
Console.WriteLine($"An error occurred:\n{exMsg}");
|
||||||
_logger.Error(exMsg, "An error occurred");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public User RetrieveUser(User user)
|
public User RetrieveUser(User user)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving user");
|
_logger.Info("Retrieving user");
|
||||||
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
using (MySqlConnection conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "SELECT * FROM User WHERE Username=@Username";
|
var query = "SELECT * FROM User WHERE Username=@Username";
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Username", user.Username);
|
cmd.Parameters.AddWithValue("@Username", user.Username);
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
user = ParseSingleData(reader);
|
||||||
user = ParseSingleData(reader);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Info("Successfully retrieved user");
|
_logger.Info("Successfully retrieved user");
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var exMsg = ex.Message;
|
var exMsg = ex.Message;
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
Console.WriteLine($"An error occurred:\n{exMsg}");
|
||||||
_logger.Error(exMsg, "An error occurred");
|
_logger.Error(exMsg, "An error occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool DoesUserExist(User user)
|
public bool DoesUserExist(User user)
|
||||||
{
|
{
|
||||||
var username = user.Username;
|
var username = user.Username;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Info($"Checking to see if {user.Username} exists");
|
_logger.Info($"Checking to see if {user.Username} exists");
|
||||||
|
|
||||||
using (var conn = GetConnection())
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var query = "SELECT * FROM User WHERE Username=@Username";
|
var query = "SELECT * FROM User WHERE Username=@Username";
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Username", user.Username);
|
cmd.Parameters.AddWithValue("@Username", user.Username);
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
{
|
||||||
user = ParseSingleData(reader, true);
|
user = ParseSingleData(reader, true);
|
||||||
username = user.Username;
|
username = user.Username;
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(username))
|
if (!string.IsNullOrEmpty(username))
|
||||||
{
|
{
|
||||||
_logger.Info($"The user {username} exists");
|
_logger.Info($"The user {username} exists");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Info($"The user {username} does not exists");
|
_logger.Info($"The user {username} does not exists");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private User ParseSingleData(MySqlDataReader reader)
|
private User ParseSingleData(MySqlDataReader reader)
|
||||||
{
|
{
|
||||||
var user = new User();
|
var user = new User();
|
||||||
|
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
var id = Convert.ToInt32(reader["Id"].ToString());
|
var id = Convert.ToInt32(reader["Id"].ToString());
|
||||||
var username = reader["Username"].ToString();
|
var username = reader["Username"].ToString();
|
||||||
var password = reader["Password"].ToString();
|
var password = reader["Password"].ToString();
|
||||||
var nickname = reader["Nickname"].ToString();
|
var nickname = reader["Nickname"].ToString();
|
||||||
var email = reader["Email"].ToString();
|
var email = reader["Email"].ToString();
|
||||||
var phoneNumber = reader["PhoneNumber"].ToString();
|
var phoneNumber = reader["PhoneNumber"].ToString();
|
||||||
var emailVerified = reader["EmailVerified"].ToString() == "1";
|
var emailVerified = reader["EmailVerified"].ToString() == "1";
|
||||||
var firstname = reader["Firstname"].ToString();
|
var firstname = reader["Firstname"].ToString();
|
||||||
var lastname = reader["Lastname"].ToString();
|
var lastname = reader["Lastname"].ToString();
|
||||||
var rawLastLogin = reader["LastLogin"].ToString();
|
var rawLastLogin = reader["LastLogin"].ToString();
|
||||||
|
|
||||||
var parsedDateCreated = DateTime.Parse(reader["DateCreated"].ToString());
|
var parsedDateCreated = DateTime.Parse(reader["DateCreated"].ToString());
|
||||||
|
|
||||||
var dateCreated = DateTime.Parse(parsedDateCreated.ToString("yyyy-MM-dd HH:mm:ss"));
|
var dateCreated = DateTime.Parse(parsedDateCreated.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(rawLastLogin))
|
if (!string.IsNullOrEmpty(rawLastLogin))
|
||||||
{
|
{
|
||||||
var parsedLastLogin = DateTime.Parse(rawLastLogin);
|
var parsedLastLogin = DateTime.Parse(rawLastLogin);
|
||||||
var lastLogin = DateTime.Parse(parsedLastLogin.ToString("yyyy-MM-dd HH:mm:ss"));
|
var lastLogin = DateTime.Parse(parsedLastLogin.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||||
user.LastLogin = lastLogin;
|
user.LastLogin = lastLogin;
|
||||||
}
|
}
|
||||||
|
|
||||||
user.Id = id;
|
user.Id = id;
|
||||||
user.Username = username;
|
user.Username = username;
|
||||||
user.Password = password;
|
user.Password = password;
|
||||||
user.Nickname = nickname;
|
user.Nickname = nickname;
|
||||||
user.Email = email;
|
user.Email = email;
|
||||||
user.PhoneNumber = phoneNumber;
|
user.PhoneNumber = phoneNumber;
|
||||||
user.EmailVerified = emailVerified;
|
user.EmailVerified = emailVerified;
|
||||||
user.Firstname = firstname;
|
user.Firstname = firstname;
|
||||||
user.Lastname = lastname;
|
user.Lastname = lastname;
|
||||||
user.DateCreated = dateCreated;
|
user.DateCreated = dateCreated;
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
private User ParseSingleData(MySqlDataReader reader, bool ignoreLastLogin)
|
private User ParseSingleData(MySqlDataReader reader, bool ignoreLastLogin)
|
||||||
{
|
{
|
||||||
var user = new User();
|
var user = new User();
|
||||||
|
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
var id = Convert.ToInt32(reader["Id"].ToString());
|
var id = Convert.ToInt32(reader["Id"].ToString());
|
||||||
var username = reader["Username"].ToString();
|
var username = reader["Username"].ToString();
|
||||||
var nickname = reader["Nickname"].ToString();
|
var nickname = reader["Nickname"].ToString();
|
||||||
var email = reader["Email"].ToString();
|
var email = reader["Email"].ToString();
|
||||||
var phoneNumber = reader["PhoneNumber"].ToString();
|
var phoneNumber = reader["PhoneNumber"].ToString();
|
||||||
var emailVerified = reader["EmailVerified"].ToString() == "1";
|
var emailVerified = reader["EmailVerified"].ToString() == "1";
|
||||||
var firstname = reader["Firstname"].ToString();
|
var firstname = reader["Firstname"].ToString();
|
||||||
var lastname = reader["Lastname"].ToString();
|
var lastname = reader["Lastname"].ToString();
|
||||||
|
|
||||||
var parsedDateCreated = DateTime.Parse(reader["DateCreated"].ToString());
|
var parsedDateCreated = DateTime.Parse(reader["DateCreated"].ToString());
|
||||||
|
|
||||||
var dateCreated = DateTime.Parse(parsedDateCreated.ToString("yyyy-MM-dd HH:mm:ss"));
|
var dateCreated = DateTime.Parse(parsedDateCreated.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
|
||||||
if (!ignoreLastLogin)
|
if (!ignoreLastLogin)
|
||||||
{
|
{
|
||||||
var parsedLastLogin = DateTime.Parse(reader["LastLogin"].ToString());
|
var parsedLastLogin = DateTime.Parse(reader["LastLogin"].ToString());
|
||||||
var lastLogin = DateTime.Parse(parsedLastLogin.ToString("yyyy-MM-dd HH:mm:ss"));
|
var lastLogin = DateTime.Parse(parsedLastLogin.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||||
user.LastLogin = lastLogin;
|
user.LastLogin = lastLogin;
|
||||||
}
|
}
|
||||||
|
|
||||||
user.Id = id;
|
user.Id = id;
|
||||||
user.Username = username;
|
user.Username = username;
|
||||||
user.Nickname = nickname;
|
user.Nickname = nickname;
|
||||||
user.Email = email;
|
user.Email = email;
|
||||||
user.PhoneNumber = phoneNumber;
|
user.PhoneNumber = phoneNumber;
|
||||||
user.EmailVerified = emailVerified;
|
user.EmailVerified = emailVerified;
|
||||||
user.Firstname = firstname;
|
user.Firstname = firstname;
|
||||||
user.Lastname = lastname;
|
user.Lastname = lastname;
|
||||||
user.DateCreated = dateCreated;
|
user.DateCreated = dateCreated;
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,374 +8,360 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Database.Repositories
|
namespace Icarus.Database.Repositories
|
||||||
{
|
{
|
||||||
public class YearRepository : BaseRepository
|
public class YearRepository : BaseRepository
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public YearRepository(string connectionString)
|
public YearRepository(string connectionString)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public List<Year> GetSongYears()
|
public List<Year> GetSongYears()
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving Year records");
|
_logger.Info("Retrieving Year records");
|
||||||
|
|
||||||
var yearValues = new List<Year>();
|
var yearValues = new List<Year>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var conn = GetConnection())
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
|
||||||
var query = "SELECT yr.*, COUNT(*) AS SongCount FROM Year " +
|
var query = "SELECT yr.*, COUNT(*) AS SongCount FROM Year " +
|
||||||
"yr LEFT JOIN Song sng ON yr.YearId=sng.YearId " +
|
"yr LEFT JOIN Song sng ON yr.YearId=sng.YearId " +
|
||||||
"GROUP BY yr.YearId";
|
"GROUP BY yr.YearId";
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
using (var reader = cmd.ExecuteReader())
|
||||||
using (var reader = cmd.ExecuteReader())
|
yearValues = ParseData(reader);
|
||||||
{
|
}
|
||||||
yearValues = ParseData(reader);
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
}
|
var msg = ex.Message;
|
||||||
}
|
_logger.Error(msg, "An error occurred");
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
return yearValues;
|
||||||
_logger.Error(msg, "An error occurred");
|
}
|
||||||
}
|
|
||||||
|
public Year GetSongYear(Year year)
|
||||||
return yearValues;
|
{
|
||||||
}
|
_logger.Info("Retrieving Year record");
|
||||||
|
|
||||||
public Year GetSongYear(Year year)
|
try
|
||||||
{
|
{
|
||||||
_logger.Info("Retrieving Year record");
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
try
|
conn.Open();
|
||||||
{
|
|
||||||
using (var conn = GetConnection())
|
var query = "SELECT yr.*, COUNT(*) AS SongCount FROM Year " +
|
||||||
{
|
"yr LEFT JOIN Song sng ON yr.YearId=sng.YearId WHERE " +
|
||||||
conn.Open();
|
"YearId=@YearId GROUP BY yr.YearId";
|
||||||
|
|
||||||
var query = "SELECT yr.*, COUNT(*) AS SongCount FROM Year " +
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
"yr LEFT JOIN Song sng ON yr.YearId=sng.YearId WHERE " +
|
{
|
||||||
"YearId=@YearId GROUP BY yr.YearId";
|
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
||||||
|
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
using (var reader = cmd.ExecuteReader())
|
||||||
{
|
year = ParseSingleData(reader);
|
||||||
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
}
|
||||||
|
}
|
||||||
using (var reader = cmd.ExecuteReader())
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
year = ParseSingleData(reader);
|
{
|
||||||
}
|
var msg = ex.Message;
|
||||||
}
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception ex)
|
return year;
|
||||||
{
|
}
|
||||||
var msg = ex.Message;
|
public Year GetSongYear(Song song)
|
||||||
_logger.Error(msg, "An error occurred");
|
{
|
||||||
}
|
var year = new Year();
|
||||||
|
|
||||||
return year;
|
_logger.Info("Retrieving Year record");
|
||||||
}
|
|
||||||
public Year GetSongYear(Song song)
|
try
|
||||||
{
|
{
|
||||||
var year = new Year();
|
using (var conn = GetConnection())
|
||||||
|
{
|
||||||
_logger.Info("Retrieving Year record");
|
conn.Open();
|
||||||
|
|
||||||
try
|
var query = "SELECT yr.*, 0 AS SongCount FROM Year " +
|
||||||
{
|
"yr WHERE yr.YearValue=@YearValue";
|
||||||
using (var conn = GetConnection())
|
|
||||||
{
|
using(var cmd = new MySqlCommand(query, conn))
|
||||||
conn.Open();
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@YearValue", song.Year);
|
||||||
var query = "SELECT yr.*, 0 AS SongCount FROM Year " +
|
|
||||||
"yr WHERE yr.YearValue=@YearValue";
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
year = ParseSingleData(reader);
|
||||||
using(var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
}
|
||||||
cmd.Parameters.AddWithValue("@YearValue", song.Year);
|
}
|
||||||
|
catch (Exception ex)
|
||||||
using (var reader = cmd.ExecuteReader())
|
{
|
||||||
{
|
var msg = ex.Message;
|
||||||
year = ParseSingleData(reader);
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
return year;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
public Year GetSongYear(Song song, bool retrieveCount)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var year = new Year();
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
_logger.Info("Retrieving year record");
|
||||||
|
|
||||||
return year;
|
try
|
||||||
}
|
{
|
||||||
public Year GetSongYear(Song song, bool retrieveCount)
|
using (var conn = GetConnection())
|
||||||
{
|
{
|
||||||
var year = new Year();
|
conn.Open();
|
||||||
|
|
||||||
_logger.Info("Retrieving year record");
|
var query = string.Empty;
|
||||||
|
|
||||||
try
|
if (retrieveCount)
|
||||||
{
|
query = "SELECT yr.*, COUNT(*) AS SongCount FROM Year yr " +
|
||||||
using (var conn = GetConnection())
|
"LEFT JOIN Song sng ON yr.YearValue=sng.Year WHERE " +
|
||||||
{
|
"yr.YearValue=@YearValue GROUP BY yr.YearId LIMIT 1";
|
||||||
conn.Open();
|
else
|
||||||
|
query = "SELECT yr.*, 0 AS SongCount FROM Year yr " +
|
||||||
var query = string.Empty;
|
"WHERE yr.YearValue=@YearValue LIMIT 1";
|
||||||
|
|
||||||
if (retrieveCount)
|
using(var cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
query = "SELECT yr.*, COUNT(*) AS SongCount FROM Year yr " +
|
cmd.Parameters.AddWithValue("@YearValue", song.Year);
|
||||||
"LEFT JOIN Song sng ON yr.YearValue=sng.Year WHERE " +
|
|
||||||
"yr.YearValue=@YearValue GROUP BY yr.YearId LIMIT 1";
|
using (var reader = cmd.ExecuteReader())
|
||||||
}
|
year = ParseSingleData(reader);
|
||||||
else
|
}
|
||||||
{
|
}
|
||||||
query = "SELECT yr.*, 0 AS SongCount FROM Year yr " +
|
}
|
||||||
"WHERE yr.YearValue=@YearValue LIMIT 1";
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
using(var cmd = new MySqlCommand(query, conn))
|
_logger.Error(msg, "An error occurred");
|
||||||
{
|
}
|
||||||
cmd.Parameters.AddWithValue("@YearValue", song.Year);
|
|
||||||
|
return year;
|
||||||
using (var reader = cmd.ExecuteReader())
|
}
|
||||||
{
|
|
||||||
year = ParseSingleData(reader);
|
public bool DoesYearExist(Year year)
|
||||||
}
|
{
|
||||||
}
|
_logger.Info("Checking to see if Year record exists");
|
||||||
}
|
|
||||||
}
|
try
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
using (var conn = GetConnection())
|
||||||
var msg = ex.Message;
|
{
|
||||||
_logger.Error(msg, "An error occurred");
|
conn.Open();
|
||||||
}
|
|
||||||
|
var query = "SELECT yr.*, 0 AS SongCount FROM Year yr WHERE " +
|
||||||
return year;
|
"yr.YearId=@YearId";
|
||||||
}
|
|
||||||
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
public bool DoesYearExist(Year year)
|
{
|
||||||
{
|
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
||||||
_logger.Info("Checking to see if Year record exists");
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
try
|
{
|
||||||
{
|
year = ParseSingleData(reader);
|
||||||
using (var conn = GetConnection())
|
|
||||||
{
|
if (year.YearValue > 0)
|
||||||
conn.Open();
|
{
|
||||||
|
_logger.Info("Year record exists");
|
||||||
var query = "SELECT yr.*, 0 AS SongCount FROM Year yr WHERE " +
|
|
||||||
"yr.YearId=@YearId";
|
return true;
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
}
|
||||||
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
}
|
||||||
|
}
|
||||||
using (var reader = cmd.ExecuteReader())
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
year = ParseSingleData(reader);
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
if (year.YearValue > 0)
|
}
|
||||||
{
|
|
||||||
_logger.Info("Year record exists");
|
_logger.Info("Year record does not exist");
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
public bool DoesYearExist(Song song)
|
||||||
}
|
{
|
||||||
}
|
_logger.Info("Checking to see if Year record exists");
|
||||||
}
|
|
||||||
catch (Exception ex)
|
try
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
using (var conn = GetConnection())
|
||||||
_logger.Error(msg, "An error occurred");
|
{
|
||||||
}
|
conn.Open();
|
||||||
|
|
||||||
_logger.Info("Year record does not exist");
|
var query = "SELECT yr.*, 0 AS SongCount FROM Year yr WHERE " +
|
||||||
|
"yr.YearValue=@YearValue";
|
||||||
return false;
|
|
||||||
}
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
public bool DoesYearExist(Song song)
|
{
|
||||||
{
|
cmd.Parameters.AddWithValue("@YearValue", song.Year);
|
||||||
_logger.Info("Checking to see if Year record exists");
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
try
|
{
|
||||||
{
|
var year = ParseSingleData(reader);
|
||||||
using (var conn = GetConnection())
|
|
||||||
{
|
if (year.YearValue > 0)
|
||||||
conn.Open();
|
{
|
||||||
|
_logger.Info("Year record exists");
|
||||||
var query = "SELECT yr.*, 0 AS SongCount FROM Year yr WHERE " +
|
|
||||||
"yr.YearValue=@YearValue";
|
return true;
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
}
|
||||||
cmd.Parameters.AddWithValue("@YearValue", song.Year);
|
}
|
||||||
|
}
|
||||||
using (var reader = cmd.ExecuteReader())
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var year = ParseSingleData(reader);
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
if (year.YearValue > 0)
|
}
|
||||||
{
|
|
||||||
_logger.Info("Year record exists");
|
_logger.Info("Year record does not exist");
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
public void SaveYear(Year year)
|
||||||
}
|
{
|
||||||
}
|
_logger.Info("Saving Year record");
|
||||||
catch (Exception ex)
|
|
||||||
{
|
try
|
||||||
var msg = ex.Message;
|
{
|
||||||
_logger.Error(msg, "An error occurred");
|
using (var conn = GetConnection())
|
||||||
}
|
{
|
||||||
|
conn.Open();
|
||||||
_logger.Info("Year record does not exist");
|
|
||||||
|
var query = "INSERT INTO Year(YearValue) VALUES(@YearValue)";
|
||||||
return false;
|
|
||||||
}
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
public void SaveYear(Year year)
|
cmd.Parameters.AddWithValue("@YearValue", year.YearValue);
|
||||||
{
|
|
||||||
_logger.Info("Saving Year record");
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
try
|
}
|
||||||
{
|
}
|
||||||
using (var conn = GetConnection())
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
conn.Open();
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
var query = "INSERT INTO Year(YearValue) VALUES(@YearValue)";
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
public void UpdateYear(Year year)
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@YearValue", year.YearValue);
|
_logger.Info("Deleting Year record");
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
try
|
||||||
}
|
{
|
||||||
}
|
using (var conn = GetConnection())
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
conn.Open();
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
var query = "UPDATE Year SET YearValue=@YearValue WHERE YearId=@YearId";
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
}
|
{
|
||||||
public void UpdateYear(Year year)
|
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
||||||
{
|
cmd.Parameters.AddWithValue("@YearValue", year.YearValue);
|
||||||
_logger.Info("Deleting Year record");
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
try
|
}
|
||||||
{
|
}
|
||||||
using (var conn = GetConnection())
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
conn.Open();
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
var query = "UPDATE Year SET YearValue=@YearValue WHERE YearId=@YearId";
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
}
|
||||||
{
|
public void DeleteYear(Year year)
|
||||||
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
{
|
||||||
cmd.Parameters.AddWithValue("@YearValue", year.YearValue);
|
_logger.Info("Deleting Year record");
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
try
|
||||||
}
|
{
|
||||||
}
|
using (var conn = GetConnection())
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
conn.Open();
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
var query = "DELETE FROM Year WHERE YearId=@YearId";
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
using (var cmd = new MySqlCommand(query, conn))
|
||||||
}
|
{
|
||||||
public void DeleteYear(Year year)
|
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
||||||
{
|
|
||||||
_logger.Info("Deleting Year record");
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
try
|
}
|
||||||
{
|
}
|
||||||
using (var conn = GetConnection())
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
conn.Open();
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
var query = "DELETE FROM Year WHERE YearId=@YearId";
|
}
|
||||||
|
}
|
||||||
using (var cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
private List<Year> ParseData(MySqlDataReader reader)
|
||||||
cmd.Parameters.AddWithValue("@YearId", year.YearId);
|
{
|
||||||
|
var yearValues = new List<Year>();
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
}
|
while (reader.Read())
|
||||||
}
|
{
|
||||||
}
|
var id = Convert.ToInt32(reader["YearId"].ToString());
|
||||||
catch (Exception ex)
|
var year = Convert.ToInt32(reader["YearValue"].ToString());
|
||||||
{
|
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
yearValues.Add(new Year
|
||||||
}
|
{
|
||||||
}
|
YearId = id,
|
||||||
|
YearValue = year,
|
||||||
private List<Year> ParseData(MySqlDataReader reader)
|
SongCount = songCount
|
||||||
{
|
});
|
||||||
var yearValues = new List<Year>();
|
}
|
||||||
|
|
||||||
while (reader.Read())
|
|
||||||
{
|
return yearValues;
|
||||||
var id = Convert.ToInt32(reader["YearId"].ToString());
|
}
|
||||||
var year = Convert.ToInt32(reader["YearValue"].ToString());
|
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
private Year ParseSingleData(MySqlDataReader reader)
|
||||||
|
{
|
||||||
yearValues.Add(new Year
|
var yearValue = new Year();
|
||||||
{
|
|
||||||
YearId = id,
|
while (reader.Read())
|
||||||
YearValue = year,
|
{
|
||||||
SongCount = songCount
|
var id = Convert.ToInt32(reader["YearId"].ToString());
|
||||||
});
|
var year = Convert.ToInt32(reader["YearValue"].ToString());
|
||||||
}
|
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
||||||
|
|
||||||
|
yearValue.YearId = id;
|
||||||
return yearValues;
|
yearValue.YearValue = year;
|
||||||
}
|
yearValue.SongCount = songCount;
|
||||||
|
}
|
||||||
private Year ParseSingleData(MySqlDataReader reader)
|
|
||||||
{
|
return yearValue;
|
||||||
var yearValue = new Year();
|
}
|
||||||
|
#endregion
|
||||||
while (reader.Read())
|
}
|
||||||
{
|
|
||||||
var id = Convert.ToInt32(reader["YearId"].ToString());
|
|
||||||
var year = Convert.ToInt32(reader["YearValue"].ToString());
|
|
||||||
var songCount = Convert.ToInt32(reader["SongCount"].ToString());
|
|
||||||
|
|
||||||
yearValue.YearId = id;
|
|
||||||
yearValue.YearValue = year;
|
|
||||||
yearValue.SongCount = songCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
return yearValue;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Update="nlog.config" CopyToOutputDirectory="PreserveNewest" />
|
<Content Update="nlog.config" CopyToOutputDirectory="PreserveNewest" />
|
||||||
|
<Content Include="Images/Stock/*.*" CopyToOutputDirectory="PreserveNewest" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
+14
-14
@@ -6,19 +6,19 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class Album
|
public class Album
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int AlbumId { get; set; }
|
public int AlbumId { get; set; }
|
||||||
[JsonProperty("title")]
|
[JsonProperty("title")]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
[JsonProperty("album_artist")]
|
[JsonProperty("album_artist")]
|
||||||
public string AlbumArtist { get; set; }
|
public string AlbumArtist { get; set; }
|
||||||
[JsonProperty("song_count")]
|
[JsonProperty("song_count")]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public int SongCount { get; set; }
|
public int SongCount { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public List<Song> Songs { get; set; }
|
public List<Song> Songs { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -6,17 +6,17 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class Artist
|
public class Artist
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int ArtistId { get; set; }
|
public int ArtistId { get; set; }
|
||||||
[JsonProperty("name")]
|
[JsonProperty("name")]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
[JsonProperty("song_count")]
|
[JsonProperty("song_count")]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public int SongCount { get; set; }
|
public int SongCount { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public List<Song> Songs { get; set; }
|
public List<Song> Songs { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class BaseResult
|
public class BaseResult
|
||||||
{
|
{
|
||||||
[JsonProperty("message")]
|
[JsonProperty("message")]
|
||||||
public string Message { get; set; }
|
public string Message { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Icarus.Models
|
||||||
|
{
|
||||||
|
public class CoverArt
|
||||||
|
{
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public int CoverArtId { get; set; }
|
||||||
|
[JsonProperty("title")]
|
||||||
|
public string SongTitle { get; set; }
|
||||||
|
[JsonIgnore]
|
||||||
|
public string ImagePath { get; set; }
|
||||||
|
|
||||||
|
[JsonIgnore]
|
||||||
|
public List<Song> Songs { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-12
@@ -6,17 +6,17 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class Genre
|
public class Genre
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int GenreId { get; set; }
|
public int GenreId { get; set; }
|
||||||
[JsonProperty("genre")]
|
[JsonProperty("genre")]
|
||||||
public string GenreName { get; set; }
|
public string GenreName { get; set; }
|
||||||
[JsonProperty("song_count")]
|
[JsonProperty("song_count")]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public int SongCount { get; set; }
|
public int SongCount { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public List<Song> Songs { get; set; }
|
public List<Song> Songs { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -4,17 +4,17 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class LoginResult : BaseResult
|
public class LoginResult : BaseResult
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int UserId { get; set; }
|
public int UserId { get; set; }
|
||||||
[JsonProperty("username")]
|
[JsonProperty("username")]
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
[JsonProperty("token")]
|
[JsonProperty("token")]
|
||||||
public string Token { get; set; }
|
public string Token { get; set; }
|
||||||
[JsonProperty("token_type")]
|
[JsonProperty("token_type")]
|
||||||
public string TokenType { get; set; }
|
public string TokenType { get; set; }
|
||||||
[JsonProperty("expiration")]
|
[JsonProperty("expiration")]
|
||||||
public int Expiration { get; set; }
|
public int Expiration { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class RegisterResult : BaseResult
|
public class RegisterResult : BaseResult
|
||||||
{
|
{
|
||||||
[JsonProperty("username")]
|
[JsonProperty("username")]
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
[JsonProperty("successfully_registered")]
|
[JsonProperty("successfully_registered")]
|
||||||
public bool SuccessfullyRegistered { get; set; }
|
public bool SuccessfullyRegistered { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-37
@@ -5,45 +5,50 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class Song
|
public class Song
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
[JsonProperty("title")]
|
[JsonProperty("title")]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
[JsonProperty("album")]
|
[JsonProperty("album")]
|
||||||
public string AlbumTitle { get; set; }
|
public string AlbumTitle { get; set; }
|
||||||
[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")]
|
||||||
public int Duration { get; set; }
|
public int Duration { get; set; }
|
||||||
[JsonProperty("filename")]
|
[JsonProperty("filename")]
|
||||||
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]
|
[JsonIgnore]
|
||||||
public Album Album { get; set; }
|
public Album Album { get; set; }
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int? AlbumId { get; set; }
|
public int? AlbumId { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public Artist SongArtist { get; set; }
|
public Artist SongArtist { get; set; }
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int? ArtistId { get; set; }
|
public int? ArtistId { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public Genre SongGenre { get; set; }
|
public Genre SongGenre { get; set; }
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int? GenreId { get; set; }
|
public int? GenreId { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public Year SongYear { get; set; }
|
public Year SongYear { get; set; }
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int? YearId { get; set; }
|
public int? YearId { get; set; }
|
||||||
}
|
|
||||||
|
[JsonIgnore]
|
||||||
|
public CoverArt SongCoverArt { get; set; }
|
||||||
|
[JsonIgnore]
|
||||||
|
public int? CoverArtId { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -3,10 +3,10 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class SongData
|
public class SongData
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public byte[] Data { get; set; }
|
public byte[] Data { get; set; }
|
||||||
public int SongId { get; set; }
|
public int SongId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class SongResult
|
public class SongResult
|
||||||
{
|
{
|
||||||
[JsonProperty("message")]
|
[JsonProperty("message")]
|
||||||
public string Message { get; set; }
|
public string Message { get; set; }
|
||||||
[JsonProperty("song_title")]
|
[JsonProperty("song_title")]
|
||||||
public string SongTitle { get; set; }
|
public string SongTitle { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-25
@@ -4,29 +4,29 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class User
|
public class User
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
[JsonProperty("username")]
|
[JsonProperty("username")]
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
[JsonProperty("nickname")]
|
[JsonProperty("nickname")]
|
||||||
public string Nickname { get; set; }
|
public string Nickname { get; set; }
|
||||||
[JsonProperty("password")]
|
[JsonProperty("password")]
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
[JsonProperty("email")]
|
[JsonProperty("email")]
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
[JsonProperty("phone_number")]
|
[JsonProperty("phone_number")]
|
||||||
public string PhoneNumber { get; set; }
|
public string PhoneNumber { get; set; }
|
||||||
[JsonProperty("first_name")]
|
[JsonProperty("first_name")]
|
||||||
public string Firstname { get; set; }
|
public string Firstname { get; set; }
|
||||||
[JsonProperty("last_name")]
|
[JsonProperty("last_name")]
|
||||||
public string Lastname { get; set; }
|
public string Lastname { get; set; }
|
||||||
[JsonProperty("email_verified")]
|
[JsonProperty("email_verified")]
|
||||||
public bool EmailVerified { get; set; }
|
public bool EmailVerified { get; set; }
|
||||||
[JsonProperty("date_created")]
|
[JsonProperty("date_created")]
|
||||||
public DateTime DateCreated { get; set; }
|
public DateTime DateCreated { get; set; }
|
||||||
[JsonProperty("last_login")]
|
[JsonProperty("last_login")]
|
||||||
public DateTime? LastLogin { get; set; }
|
public DateTime? LastLogin { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -6,17 +6,17 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class Year
|
public class Year
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int YearId { get; set; }
|
public int YearId { get; set; }
|
||||||
[JsonProperty("year")]
|
[JsonProperty("year")]
|
||||||
public int YearValue { get; set; }
|
public int YearValue { get; set; }
|
||||||
[JsonProperty("song_count")]
|
[JsonProperty("song_count")]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public int SongCount { get; set; }
|
public int SongCount { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public List<Song> Songs { get; set; }
|
public List<Song> Songs { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-30
@@ -12,37 +12,36 @@ using NLog.Web;
|
|||||||
|
|
||||||
namespace Icarus
|
namespace Icarus
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.Debug("init main");
|
logger.Debug("init main");
|
||||||
CreateWebHostBuilder(args).Build().Run();
|
CreateWebHostBuilder(args).Build().Run();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.Error(ex, "An error occurred");
|
logger.Error(ex, "An error occurred");
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
NLog.LogManager.Shutdown();
|
NLog.LogManager.Shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>()
|
||||||
.UseStartup<Startup>()
|
.UseUrls("http://localhost:5002")
|
||||||
.UseUrls("http://localhost:5002")
|
.ConfigureLogging(logging =>
|
||||||
.ConfigureLogging(logging =>
|
{
|
||||||
{
|
logging.ClearProviders();
|
||||||
logging.ClearProviders();
|
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
|
||||||
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
|
})
|
||||||
})
|
.UseNLog();
|
||||||
.UseNLog() ;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the code of conduc
|
|||||||
|
|
||||||
## Versioning
|
## Versioning
|
||||||
|
|
||||||
Currently under development. No version has been released
|
* [v0.1](https://github.com/amazing-username/Icarus/releases/tag/v0.1)
|
||||||
|
|
||||||
## Authors
|
## Authors
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ echo "Adding Genre migration"
|
|||||||
dotnet ef migrations add Genre --context GenreContext
|
dotnet ef migrations add Genre --context GenreContext
|
||||||
echo "Adding Year migration"
|
echo "Adding Year migration"
|
||||||
dotnet ef migrations add Year --context YearContext
|
dotnet ef migrations add Year --context YearContext
|
||||||
|
echo "Adding Cover art migration"
|
||||||
|
dotnet ef migrations add CoverArt --context CoverArtContext
|
||||||
|
|
||||||
echo "Updating migrations.."
|
echo "Updating migrations.."
|
||||||
echo "Updating User migration"
|
echo "Updating User migration"
|
||||||
@@ -20,4 +22,5 @@ echo "Updating Album migration"
|
|||||||
echo "Updating Artist migration"
|
echo "Updating Artist migration"
|
||||||
echo "Updating Genre migration"
|
echo "Updating Genre migration"
|
||||||
echo "Updating Year migration"
|
echo "Updating Year migration"
|
||||||
|
echo "Updating Cover art migration"
|
||||||
dotnet ef database update --context SongContext
|
dotnet ef database update --context SongContext
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
delete from Song where Id>0;
|
delete from Song;
|
||||||
delete from Album where AlbumId>0;
|
delete from Album;
|
||||||
delete from Artist where ArtistId>0;
|
delete from Artist;
|
||||||
|
delete from Genre;
|
||||||
|
delete from Year;
|
||||||
|
delete from CoverArt;
|
||||||
|
|||||||
+98
-109
@@ -29,140 +29,129 @@ using Icarus.Database.Repositories;
|
|||||||
|
|
||||||
namespace Icarus
|
namespace Icarus
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public Startup(IConfiguration configuration)
|
public Startup(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
Configuration = configuration;
|
Configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
||||||
services.AddSingleton<IConfiguration>(Configuration);
|
services.AddSingleton<IConfiguration>(Configuration);
|
||||||
|
|
||||||
string domain = $"https://{Configuration["Auth0:Domain"]}/";
|
string domain = $"https://{Configuration["Auth0:Domain"]}/";
|
||||||
|
|
||||||
services.AddAuthentication(options =>
|
services.AddAuthentication(options =>
|
||||||
{
|
{
|
||||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
}).AddJwtBearer(options =>
|
}).AddJwtBearer(options =>
|
||||||
{
|
{
|
||||||
options.Authority = domain;
|
options.Authority = domain;
|
||||||
options.Audience = Configuration["Auth0:ApiIdentifier"];
|
options.Audience = Configuration["Auth0:ApiIdentifier"];
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddAuthorization(options =>
|
services.AddAuthorization(options =>
|
||||||
{
|
{
|
||||||
options.AddPolicy("download:songs", policy =>
|
options.AddPolicy("download:songs", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("download:songs", domain)));
|
||||||
.Add(new HasScopeRequirement("download:songs", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("upload:songs", policy =>
|
options.AddPolicy("download:cover_art", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("download:cover_art", domain)));
|
||||||
.Add(new HasScopeRequirement("upload:songs", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("delete:songs", policy =>
|
options.AddPolicy("upload:songs", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("upload:songs", domain)));
|
||||||
.Add(new HasScopeRequirement("delete:songs", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("read:song_details", policy =>
|
options.AddPolicy("delete:songs", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("delete:songs", domain)));
|
||||||
.Add(new HasScopeRequirement("read:song_details", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("update:songs", policy =>
|
options.AddPolicy("read:song_details", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("read:song_details", domain)));
|
||||||
.Add(new HasScopeRequirement("update:songs", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("read:artists", policy =>
|
options.AddPolicy("update:songs", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("update:songs", domain)));
|
||||||
.Add(new HasScopeRequirement("read:artists", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("read:albums", policy =>
|
options.AddPolicy("read:artists", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("read:artists", domain)));
|
||||||
.Add(new HasScopeRequirement("read:albums", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("read:genre", policy =>
|
options.AddPolicy("read:albums", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("read:albums", domain)));
|
||||||
.Add(new HasScopeRequirement("read:genre", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("read:year", policy =>
|
options.AddPolicy("read:genre", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("read:genre", domain)));
|
||||||
.Add(new HasScopeRequirement("read:year", domain)));
|
|
||||||
|
|
||||||
options.AddPolicy("stream:songs", policy =>
|
options.AddPolicy("read:year", policy =>
|
||||||
policy
|
policy.Requirements
|
||||||
.Requirements
|
.Add(new HasScopeRequirement("read:year", domain)));
|
||||||
.Add(new HasScopeRequirement("stream:songs", domain)));
|
|
||||||
});
|
options.AddPolicy("stream:songs", policy =>
|
||||||
|
policy.Requirements
|
||||||
|
.Add(new HasScopeRequirement("stream:songs", domain)));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
services.AddSingleton<IAuthorizationHandler, HasScopeHandler>();
|
services.AddSingleton<IAuthorizationHandler, HasScopeHandler>();
|
||||||
|
|
||||||
var connString = Configuration.GetConnectionString("DefaultConnection");
|
var connString = Configuration.GetConnectionString("DefaultConnection");
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(typeof(SongRepository),
|
services.Add(new ServiceDescriptor(typeof(SongRepository),
|
||||||
new SongRepository(Configuration
|
new SongRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
.GetConnectionString("DefaultConnection"))));
|
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(typeof(AlbumRepository),
|
services.Add(new ServiceDescriptor(typeof(AlbumRepository),
|
||||||
new AlbumRepository(Configuration
|
new AlbumRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
.GetConnectionString("DefaultConnection"))));
|
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(typeof(ArtistRepository),
|
services.Add(new ServiceDescriptor(typeof(ArtistRepository),
|
||||||
new ArtistRepository(Configuration
|
new ArtistRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
.GetConnectionString("DefaultConnection"))));
|
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(typeof(GenreRepository),
|
services.Add(new ServiceDescriptor(typeof(GenreRepository),
|
||||||
new GenreRepository(Configuration
|
new GenreRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
.GetConnectionString("DefaultConnection"))));
|
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(typeof(YearRepository),
|
services.Add(new ServiceDescriptor(typeof(YearRepository),
|
||||||
new YearRepository(Configuration
|
new YearRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
.GetConnectionString("DefaultConnection"))));
|
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(typeof(UserRepository),
|
services.Add(new ServiceDescriptor(typeof(CoverArtRepository),
|
||||||
new UserRepository(Configuration
|
new CoverArtRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
.GetConnectionString("DefaultConnection"))));
|
|
||||||
|
|
||||||
services.AddDbContext<SongContext>(options => options.UseMySQL(connString));
|
services.Add(new ServiceDescriptor(typeof(UserRepository),
|
||||||
services.AddDbContext<AlbumContext>(options => options.UseMySQL(connString));
|
new UserRepository(Configuration.GetConnectionString("DefaultConnection"))));
|
||||||
services.AddDbContext<ArtistContext>(options => options.UseMySQL(connString));
|
|
||||||
services.AddDbContext<UserContext>(options => options.UseMySQL(connString));
|
|
||||||
services.AddDbContext<GenreContext>(options => options.UseMySQL(connString));
|
|
||||||
services.AddDbContext<YearContext>(options => options.UseMySQL(connString));
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
services.AddDbContext<SongContext>(options => options.UseMySQL(connString));
|
||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
services.AddDbContext<AlbumContext>(options => options.UseMySQL(connString));
|
||||||
{
|
services.AddDbContext<ArtistContext>(options => options.UseMySQL(connString));
|
||||||
if (env.IsDevelopment())
|
services.AddDbContext<UserContext>(options => options.UseMySQL(connString));
|
||||||
{
|
services.AddDbContext<GenreContext>(options => options.UseMySQL(connString));
|
||||||
app.UseDeveloperExceptionPage();
|
services.AddDbContext<YearContext>(options => options.UseMySQL(connString));
|
||||||
}
|
services.AddDbContext<CoverArtContext>(options => options.UseMySQL(connString));
|
||||||
else
|
}
|
||||||
{
|
|
||||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
||||||
app.UseHsts();
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseAuthentication();
|
// Called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
else
|
||||||
|
// The default HSTS value is 30 days.
|
||||||
|
// You may want to change this for production scenarios
|
||||||
|
app.UseHsts();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseAuthentication();
|
||||||
app.UseMvc();
|
|
||||||
}
|
app.UseHttpsRedirection();
|
||||||
}
|
app.UseMvc();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Icarus.Types
|
||||||
|
{
|
||||||
|
public enum CoverArtField
|
||||||
|
{
|
||||||
|
SongTitle = 0,
|
||||||
|
ImagePath
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Icarus.Types
|
||||||
|
{
|
||||||
|
public enum DirectoryType
|
||||||
|
{
|
||||||
|
Music = 0,
|
||||||
|
CoverArt
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -9,13 +9,14 @@
|
|||||||
"Auth0": {
|
"Auth0": {
|
||||||
"Domain": "[domain].auth0.com",
|
"Domain": "[domain].auth0.com",
|
||||||
"ApiIdentifier": "https://[identifier]/api",
|
"ApiIdentifier": "https://[identifier]/api",
|
||||||
"ClientId":"",
|
"ClientId": "",
|
||||||
"ClientSecret":""
|
"ClientSecret": ""
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=;Database=;Uid=;Pwd=;"
|
"DefaultConnection": "Server=;Database=;Uid=;Pwd=;"
|
||||||
},
|
},
|
||||||
"RootMusicPath": "/music/path/",
|
"RootMusicPath": "/music/path/",
|
||||||
"TemporaryMusicPath": "/music/temp/path/",
|
"TemporaryMusicPath": "/music/temp/path/",
|
||||||
"ArchivePath": "/archive/path/"
|
"ArchivePath": "/archive/path/",
|
||||||
|
"CoverArtPath": "/cover/art/path/"
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -9,13 +9,14 @@
|
|||||||
"Auth0": {
|
"Auth0": {
|
||||||
"Domain": "[domain].auth0.com",
|
"Domain": "[domain].auth0.com",
|
||||||
"ApiIdentifier": "https://[identifier]/api",
|
"ApiIdentifier": "https://[identifier]/api",
|
||||||
"ClientId":"",
|
"ClientId": "",
|
||||||
"ClientSecret":""
|
"ClientSecret": ""
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=;Database=;Uid=;Pwd=;"
|
"DefaultConnection": "Server=;Database=;Uid=;Pwd=;"
|
||||||
},
|
},
|
||||||
"RootMusicPath": "/music/path/",
|
"RootMusicPath": "/music/path/",
|
||||||
"TemporaryMusicPath": "/music/temp/path/",
|
"TemporaryMusicPath": "/music/temp/path/",
|
||||||
"ArchivePath": "/archive/path/"
|
"ArchivePath": "/archive/path/",
|
||||||
|
"CoverArtPath": "/cover/art/path/"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user