Implemented Artist HTTP endpoint functionality. #21 and #26

This commit is contained in:
amazing-username
2019-05-10 22:21:30 -04:00
parent 0c4a1612b1
commit a3c74b50bc
11 changed files with 224 additions and 102 deletions
+1
View File
@@ -13,6 +13,7 @@ namespace Icarus.Controllers
{
[Route("api/album")]
[ApiController]
// TODO: Secure the HTTP endpoint routes with Auth0 grants. #39
public class AlbumController : ControllerBase
{
#region Fields
+34 -6
View File
@@ -8,11 +8,13 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Icarus.Models;
using Icarus.Models.Context;
namespace Icarus.Controllers
{
[Route("api/artist")]
[ApiController]
// TODO: Secure the HTTP endpoint routes with Auth0 grants. #39
public class ArtistController : ControllerBase
{
#region Fields
@@ -36,19 +38,45 @@ namespace Icarus.Controllers
[HttpGet]
public IActionResult Get()
{
List<Artist> artists = new List<Artist>();
// TODO: Implement functionality
ArtistStoreContext artistStoreContext = HttpContext
.RequestServices
.GetService(typeof(ArtistStoreContext)) as ArtistStoreContext;
return Ok(artists);
var artists = artistStoreContext.GetArtists();
if (artists.Count > 0)
{
return Ok(artists);
}
else
{
return NotFound();
}
}
[HttpGet("{id}")]
public IActionResult Get(int id)
{
Artist artist = new Artist();
// TODO: Implement functionality
Artist artist = new Artist
{
ArtistId = id
};
ArtistStoreContext artistStoreContext = HttpContext
.RequestServices
.GetService(typeof(ArtistStoreContext)) as ArtistStoreContext;
return Ok(artist);
if (artistStoreContext.DoesArtistExist(artist))
{
artist = artistStoreContext.GetArtist(artist);
return Ok(artist);
}
else
{
return NotFound();
}
}
#endregion
}
+10 -3
View File
@@ -38,10 +38,17 @@ namespace Icarus.Controllers
#region HTTP endpoints
public IActionResult Post([FromBody] User user)
{
// TODO: Secure this HTTP endpoint. #38
// Currently there is no check done to determine whether or not the
// user's password sent with the request matches the password stored
// in the database. In fact there is not check if the user credentials
// sent with this request even exist in the database. I knowingly left
// this bug in here for the sole purpose of making it easier to test
// but it should now be addressed.
UserStoreContext context = HttpContext
.RequestServices
.GetService(typeof(UserStoreContext))
as UserStoreContext;
.RequestServices
.GetService(typeof(UserStoreContext)) as UserStoreContext;
user = context.RetrieveUser(user);
Console.WriteLine($"Username: {user.Username}");
+43 -12
View File
@@ -490,24 +490,33 @@ namespace Icarus.Controllers.Managers
{
_results = new DataTable();
}
private void SaveSongToDatabase(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore,
ArtistStoreContext artistStore)
{
song.AlbumId = 1;
_logger.Info("Starting process to save the song to the database");
SaveAlbumToDatabase(ref song, albumStore);
SaveArtistToDatabase(ref song, artistStore);
_logger.Info($"Song;\nTitle {song.Title}\nAlbum {song.AlbumTitle}\nAlbum Id {song.AlbumId}\nArtist {song.ArtistId}");
songStore.SaveSong(song);
}
private void SaveAlbumToDatabase(ref Song song, AlbumStoreContext albumStore)
{
_logger.Info("Starting process to save the album record of the song to the database");
var album = new Album();
album.Title = song.AlbumTitle;
album.AlbumArtist = song.Artist;
if (!albumStore.DoesAlbumExist(song))
{
album.SongCount = 1;
_logger.Info("Saving album to database");
_logger.Info($"Ablum song count before retrieving from store: {album.SongCount}");
_logger.Info($"Ablum Id before retrieving from store: {album.AlbumId}");
albumStore.SaveAlbum(album);
_logger.Info("Album suuccessfully saved to database");
album = albumStore.GetAlbum(song);
_logger.Info($"Ablum song count after retrieving from store: {album.SongCount}");
_logger.Info($"Ablum Id after retrieving from store: {album.AlbumId}");
}
else
{
@@ -517,12 +526,34 @@ namespace Icarus.Controllers.Managers
albumStore.UpdateAlbum(album);
}
song.AlbumId = album.AlbumId;
_logger.Info($"Song;\nTitle {song.Title}\nAlbum {song.AlbumTitle}\nAlbum Id {song.AlbumId}");
songStore.SaveSong(song);
_logger.Info("Successfully saved song to database");
song.AlbumId = album.AlbumId;
}
private void SaveArtistToDatabase(ref Song song, ArtistStoreContext artistStore)
{
_logger.Info("Starting process to save the artist record of the song to the database");
var artist = new Artist();
artist.Name = song.Artist;
artist.SongCount = 1;
if (!artistStore.DoesArtistExist(song))
{
artist.SongCount = 1;
artistStore.SaveArtist(artist);
artist = artistStore.GetArtist(song);
}
else
{
var artistRetrieved = artistStore.GetArtist(song);
artist.ArtistId = artistRetrieved.ArtistId;
artist.SongCount = artistRetrieved.SongCount + 1;
artistStore.UpdateArtist(artist);
}
song.ArtistId = artist.ArtistId;
}
private async Task PopulateSongDetails()
+5 -3
View File
@@ -43,14 +43,15 @@ namespace Icarus.Controllers.Managers
var tokenRequest = RetrieveTokenRequest();
var tokenObject = JsonConvert.SerializeObject(tokenRequest);
request.AddParameter("application/json; charset=utf-8",
tokenObject, ParameterType.RequestBody);
tokenObject, ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
var tokenResult = JsonConvert
.DeserializeObject<Token>(response.Content);
.DeserializeObject<Token>(response.Content);
return new LoginResult
{
@@ -81,9 +82,9 @@ namespace Icarus.Controllers.Managers
_grantType = "client_credentials";
_url = $"https://{_config["Auth0:Domain"]}";
//PrintCredentials();
}
#region Testing Methods
// For testing purposes
private void PrintCredentials()
{
@@ -94,6 +95,7 @@ namespace Icarus.Controllers.Managers
Console.WriteLine($"Url: {_url}");
}
#endregion
#endregion
#region Classes
+14 -14
View File
@@ -14,10 +14,10 @@ using Icarus.Models.Context;
namespace Icarus.Controllers
{
[Route("api/register")]
[ApiController]
public class RegisterController : ControllerBase
{
[Route("api/register")]
[ApiController]
public class RegisterController : ControllerBase
{
#region Fields
private IConfiguration _config;
#endregion
@@ -30,14 +30,13 @@ namespace Icarus.Controllers
#region Constructor
public RegisterController(IConfiguration config)
{
_config = config;
_config = config;
}
#endregion
[HttpPost]
public void Post([FromBody] User user)
{
[HttpPost]
public void Post([FromBody] User user)
{
Console.WriteLine($"Username: {user.Username}");
Console.WriteLine($"Password: {user.Password}");
@@ -46,10 +45,11 @@ namespace Icarus.Controllers
user.EmailVerified = false;
Console.WriteLine($"Hashed Password: {user.Password}");
UserStoreContext context = HttpContext.RequestServices
.GetService(typeof(UserStoreContext))
as UserStoreContext;
UserStoreContext context = HttpContext
.RequestServices
.GetService(typeof(UserStoreContext)) as UserStoreContext;
context.SaveUser(user);
}
}
}
}
}
+38 -38
View File
@@ -17,50 +17,50 @@ using Icarus.Models.Context;
namespace Icarus.Controllers
{
[Route("api/song/compressed/data")]
[ApiController]
public class SongCompressedDataController : ControllerBase
{
#region Fields
private IConfiguration _config;
private SongManager _songMgr;
private string _songTempDir;
private string _archiveDir;
#endregion
#region Properties
#endregion
#region Constructor
public SongCompressedDataController(IConfiguration config)
[Route("api/song/compressed/data")]
[ApiController]
public class SongCompressedDataController : ControllerBase
{
_config = config;
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
_archiveDir = _config.GetValue<string>("ArchivePath");
}
#endregion
#region Fields
private IConfiguration _config;
private SongManager _songMgr;
private string _songTempDir;
private string _archiveDir;
#endregion
#region API Routes
[HttpGet("{id}")]
#region Properties
#endregion
#region Constructor
public SongCompressedDataController(IConfiguration config)
{
_config = config;
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
_archiveDir = _config.GetValue<string>("ArchivePath");
}
#endregion
#region API Routes
[HttpGet("{id}")]
[Authorize("download:songs")]
public async Task<IActionResult> Get(int id)
{
MusicStoreContext context = HttpContext.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
public async Task<IActionResult> Get(int id)
{
MusicStoreContext context = HttpContext
.RequestServices
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
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");
SongData song = await cmp.RetrieveCompressedSong(context.GetSong(id));
Console.WriteLine("Starting process of retrieving comrpessed song");
SongData song = await cmp.RetrieveCompressedSong(context.GetSong(id));
return File(song.Data, "application/x-msdownload", cmp.CompressedSongFilename);
}
#endregion
}
return File(song.Data, "application/x-msdownload", cmp.CompressedSongFilename);
}
#endregion
}
}
+21 -18
View File
@@ -18,6 +18,7 @@ namespace Icarus.Controllers
{
[Route("api/song")]
[ApiController]
// TODO: Secure the HTTP endpoint routes with Auth0 grants. #39
public class SongController : ControllerBase
{
#region Fields
@@ -44,7 +45,7 @@ namespace Icarus.Controllers
[HttpGet]
[Authorize("read:song_details")]
public ActionResult<IEnumerable<Song>> Get()
public IActionResult<IEnumerable<Song>> Get()
{
List<Song> songs = new List<Song>();
Console.WriteLine("Attemtping to retrieve songs");
@@ -56,19 +57,33 @@ namespace Icarus.Controllers
songs = context.GetAllSongs();
return songs;
if (songs.Count > 0)
{
return Ok(songs);
}
else
{
return NotFound();
}
}
[HttpGet("{id}")]
public ActionResult<Song> Get(int id)
public IActionResult<Song> Get(int id)
{
MusicStoreContext context = HttpContext
.RequestServices
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
Song song = context.GetSong(id);
return song;
if (song.Id != 0)
{
return Ok(song);
}
else
{
return NotFound();
}
}
@@ -99,17 +114,5 @@ namespace Icarus.Controllers
return Ok(songResult);
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
MusicStoreContext context = HttpContext
.RequestServices
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
context.DeleteSong(id);
return Ok();
}
}
}
}