From 41398461bef69829ed74efebb08256f41c9031f2 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sat, 18 May 2019 02:09:59 -0400 Subject: [PATCH] Finished API functionality for Genre and Year endpoints. #41 and #42. Working on refining the updating song api route that's part of the clean up issue. #37 --- Controllers/GenreController.cs | 31 ++++++++++++++++++++++++++--- Controllers/Managers/SongManager.cs | 28 +++++++++++++++++++++----- Controllers/SongController.cs | 18 ++++++++++++----- Controllers/SongDataController.cs | 3 +-- Controllers/YearController.cs | 31 ++++++++++++++++++++++++++--- 5 files changed, 93 insertions(+), 18 deletions(-) diff --git a/Controllers/GenreController.cs b/Controllers/GenreController.cs index bec2980..731313d 100644 --- a/Controllers/GenreController.cs +++ b/Controllers/GenreController.cs @@ -11,7 +11,6 @@ using Icarus.Models.Context; namespace Icarus.Controllers { - // TODO: Implement Genre API functionality #41 [Route("api/genre")] [ApiController] public class GenreController : ControllerBase @@ -39,7 +38,20 @@ namespace Icarus.Controllers { var genres = new List(); - return Ok(genres); + var genreStore = HttpContext + .RequestServices + .GetService(typeof(GenreStoreContext)) as GenreStoreContext; + + genres = genreStore.GetGenres(); + + if (genres.Count > 0) + { + return Ok(genres); + } + else + { + return NotFound(new List()); + } } [HttpGet("{id}")] @@ -50,7 +62,20 @@ namespace Icarus.Controllers GenreId = id }; - return Ok(genre); + var genreStore = HttpContext + .RequestServices + .GetService(typeof(GenreStoreContext)) as GenreStoreContext; + + if (genreStore.DoesGenreExist(genre)) + { + genre = genreStore.GetGenre(genre); + + return Ok(genre); + } + else + { + return NotFound(new Genre()); + } } #endregion } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 99fe9b2..403881a 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -95,6 +95,29 @@ namespace Icarus.Controllers.Managers #region Methods + // TODO: This method should update the Song, Album, and Artist records in the database + public SongResult UpdateSong(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore, + ArtistStoreContext artistStore, GenreStoreContext genreStore, + YearStoreContext yearStore) + { + var oldSongRecord = songStore.GetSong(song.Id); + song.SongPath = oldSongRecord.SongPath; + + MetadataRetriever updateMetadata = new MetadataRetriever(); + updateMetadata.UpdateMetadata(song, oldSongRecord); + + var updatedSong = updateMetadata.UpdatedSongRecord; + + // TODO: Add the following methods + // UpdateAlbumInDatabase(oldSongRecord, newSongRecord, albumStore) + // UpdateArtistInDatabase(oldSongRecord, newSongRecord, artistStore + // UpdateeGenreInDatabase(oldSongRecord, newSongRecord, genreStore) + // UpdateYearInDatabase(oldSongRecord, newSongRecord, yearStore) + // UpdateSongInDatabase(oldSongRecord, newSongRecord, songStore) + + return new SongResult(); + } + public bool DeleteSongFromFileSystem(Song songMetaData) { bool successful = false; @@ -191,11 +214,6 @@ namespace Icarus.Controllers.Managers Console.WriteLine($"An Error Occurred: {exMsg}"); } } - // TODO: This method should update the Song, Album, and Artist records in the database - public void UpdateSong(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore, - ArtistStoreContext artistStore) - { - } public async Task SaveSong(SongData songData) { diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 12fb178..d7e32eb 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -42,8 +42,7 @@ namespace Icarus.Controllers [HttpGet] - // TODO: Remember to uncomment the line below - //[Authorize("read:song_details")] + [Authorize("read:song_details")] public IActionResult Get() { List songs = new List(); @@ -67,8 +66,7 @@ namespace Icarus.Controllers } [HttpGet("{id}")] - // TODO: Remember to uncomment the line below - //[Authorize("read:song_details")] + [Authorize("read:song_details")] public IActionResult Get(int id) { MusicStoreContext context = HttpContext @@ -103,7 +101,14 @@ namespace Icarus.Controllers AlbumStoreContext albumStore = HttpContext .RequestServices .GetService(typeof(AlbumStoreContext)) as AlbumStoreContext; - // TODO: Add the GenreStoreContext and YearStoreContext #41 and #42 + + GenreStoreContext genreStore = HttpContext + .RequestServices + .GetService(typeof(GenreStoreContext)) as GenreStoreContext; + + YearStoreContext yearStore = HttpContext + .RequestServices + .GetService(typeof(YearStoreContext)) as YearStoreContext; song.Id = id; Console.WriteLine("Retrieving filepath of song"); @@ -118,6 +123,9 @@ namespace Icarus.Controllers } // TODO: Provide functionality for the UpdateSong(...) method // before removing the below return statement + var songRes = _songMgr.UpdateSong(song, context, albumStore, artistStore, genreStore, + yearStore); + return Ok("song exists"); var oldSongRecord = context.GetSong(id); diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index aa10a19..5e2062d 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -60,8 +60,7 @@ namespace Icarus.Controllers } [HttpPost] - // TODO: Remember to uncomment the line below - //[Authorize("upload:songs")] + [Authorize("upload:songs")] public async Task Post([FromForm(Name = "file")] List songData) { try diff --git a/Controllers/YearController.cs b/Controllers/YearController.cs index e2bce75..15bfc84 100644 --- a/Controllers/YearController.cs +++ b/Controllers/YearController.cs @@ -11,7 +11,6 @@ using Icarus.Models.Context; namespace Icarus.Controller { - // TODO: Implemnt Year API functionality #42 [Route("api/year")] [ApiController] public class YearController : ControllerBase @@ -39,7 +38,20 @@ namespace Icarus.Controller { var yearValues = new List(); - return Ok(); + var yearStore = HttpContext + .RequestServices + .GetService(typeof(YearStoreContext)) as YearStoreContext; + + yearValues = yearStore.GetSongYears(); + + if (yearValues.Count > 0) + { + return Ok(yearValues); + } + else + { + return NotFound(new List()); + } } [HttpGet("{id}")] @@ -50,7 +62,20 @@ namespace Icarus.Controller YearId = id }; - return Ok(year); + var yearStore = HttpContext + .RequestServices + .GetService(typeof(YearStoreContext)) as YearStoreContext; + + if (yearStore.DoesYearExist(year)) + { + year = yearStore.GetSongYear(year); + + return Ok(year); + } + else + { + return NotFound(new Year()); + } } #endregion }