Implemented functionality for retrieving all song details

This commit is contained in:
amazing-username
2019-03-31 18:13:27 -04:00
parent 798db84ce9
commit 1f15fe99e5
3 changed files with 91 additions and 7 deletions
+88
View File
@@ -21,6 +21,10 @@ namespace Icarus.Controllers.Managers
public class SongManager public class SongManager
{ {
#region Fields #region Fields
private MySqlConnection _conn;
private MySqlCommand _cmd;
private MySqlDataAdapter _dataDump;
private DataTable _results;
private List<Song> _songs; private List<Song> _songs;
private Song _song; private Song _song;
private IConfiguration _config; private IConfiguration _config;
@@ -48,23 +52,27 @@ namespace Icarus.Controllers.Managers
public SongManager() public SongManager()
{ {
Initialize(); Initialize();
InitializeConnection();
} }
public SongManager(Song song) public SongManager(Song song)
{ {
Initialize(); Initialize();
InitializeConnection();
_song = song; _song = song;
} }
public SongManager(IConfiguration config) public SongManager(IConfiguration config)
{ {
_config = config; _config = config;
Initialize(); Initialize();
InitializeConnection();
} }
public SongManager(IConfiguration config, string tempDirectoryRoot) public SongManager(IConfiguration config, string tempDirectoryRoot)
{ {
_config = config; _config = config;
_tempDirectoryRoot = tempDirectoryRoot; _tempDirectoryRoot = tempDirectoryRoot;
Initialize(); Initialize();
InitializeConnection();
} }
#endregion #endregion
@@ -191,6 +199,36 @@ namespace Icarus.Controllers.Managers
} }
} }
public async Task<List<Song>> RetrieveAllSongDetails()
{
try
{
InitializeResults();
_songs = new List<Song>();
_conn.Open();
string query = "SELECT * FROM Song";
_cmd = new MySqlCommand(query, _conn);
_cmd.ExecuteNonQuery();
_dataDump = new MySqlDataAdapter(_cmd);
_dataDump.Fill(_results);
_dataDump.Dispose();
await PopulateSongDetails();
_conn.Close();
}
catch (Exception ex)
{
var exMsg = ex.Message;
Console.WriteLine($"An error ocurred: {exMsg}");
}
return _songs;
}
public async Task<Song> RetrieveSongDetails(int id) public async Task<Song> RetrieveSongDetails(int id)
{ {
DataTable results = new DataTable(); DataTable results = new DataTable();
@@ -316,6 +354,56 @@ namespace Icarus.Controllers.Managers
} }
} }
void InitializeConnection()
{
_conn = new MySqlConnection(_connectionString);
}
void InitializeResults()
{
_results = new DataTable();
}
async Task PopulateSongDetails()
{
foreach (DataRow row in _results.Rows)
{
Song song = new Song();
foreach (DataColumn col in _results.Columns)
{
string colStr = col.ToString().ToUpper();
switch (colStr)
{
case "ID":
song.Id = Int32.Parse(row[col].ToString());
break;
case "TITLE":
song.Title = row[col].ToString();
break;
case "ALBUM":
song.Album = row[col].ToString();
break;
case "ARTIST":
song.Artist = row[col].ToString();
break;
case "YEAR":
song.Year = Int32.Parse(row[col].ToString());
break;
case "GENRE":
song.Genre = row[col].ToString();
break;
case "DURATION":
song.Duration = Int32.Parse(row[col].ToString());
break;
case "FILENAME":
song.Filename = row[col].ToString();
break;
case "SONGPATH":
song.SongPath = row[col].ToString();
break;
}
}
_songs.Add(song);
}
}
#endregion #endregion
} }
} }
+1 -5
View File
@@ -35,17 +35,16 @@ namespace Icarus.Controllers
#endregion #endregion
// GET api/song
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<Song>> Get() public ActionResult<IEnumerable<Song>> Get()
{ {
List<Song> songs = new List<Song>(); List<Song> songs = new List<Song>();
songs = _songMgr.RetrieveAllSongDetails().Result;
return songs; return songs;
} }
// GET api/song/5
[HttpGet("{id}")] [HttpGet("{id}")]
public ActionResult<Song> Get(int id) public ActionResult<Song> Get(int id)
{ {
@@ -54,20 +53,17 @@ namespace Icarus.Controllers
return song; return song;
} }
// POST api/song
[HttpPost] [HttpPost]
public void Post([FromBody] Song song) public void Post([FromBody] Song song)
{ {
_songMgr.SaveSongDetails(song); _songMgr.SaveSongDetails(song);
} }
// PUT api/song/5
[HttpPut("{id}")] [HttpPut("{id}")]
public void Put(int id, [FromBody] Song song) public void Put(int id, [FromBody] Song song)
{ {
} }
// DELETE api/song/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
+2 -2
View File
@@ -18,8 +18,8 @@ namespace Icarus.Controllers
[ApiController] [ApiController]
public class SongDataController : ControllerBase public class SongDataController : ControllerBase
{ {
#region Fields #region Fields
private IConfiguration _config; private IConfiguration _config;
private SongManager _songMgr; private SongManager _songMgr;
private string _songTempDir; private string _songTempDir;
#endregion #endregion