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
{
#region Fields
private MySqlConnection _conn;
private MySqlCommand _cmd;
private MySqlDataAdapter _dataDump;
private DataTable _results;
private List<Song> _songs;
private Song _song;
private IConfiguration _config;
@@ -48,23 +52,27 @@ namespace Icarus.Controllers.Managers
public SongManager()
{
Initialize();
InitializeConnection();
}
public SongManager(Song song)
{
Initialize();
InitializeConnection();
_song = song;
}
public SongManager(IConfiguration config)
{
_config = config;
Initialize();
InitializeConnection();
}
public SongManager(IConfiguration config, string tempDirectoryRoot)
{
_config = config;
_tempDirectoryRoot = tempDirectoryRoot;
Initialize();
InitializeConnection();
}
#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)
{
DataTable results = new DataTable();
@@ -315,6 +353,56 @@ namespace Icarus.Controllers.Managers
Console.WriteLine($"Error Occurred: {ex.Message}");
}
}
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
}
+1 -5
View File
@@ -35,17 +35,16 @@ namespace Icarus.Controllers
#endregion
// GET api/song
[HttpGet]
public ActionResult<IEnumerable<Song>> Get()
{
List<Song> songs = new List<Song>();
songs = _songMgr.RetrieveAllSongDetails().Result;
return songs;
}
// GET api/song/5
[HttpGet("{id}")]
public ActionResult<Song> Get(int id)
{
@@ -54,20 +53,17 @@ namespace Icarus.Controllers
return song;
}
// POST api/song
[HttpPost]
public void Post([FromBody] Song song)
{
_songMgr.SaveSongDetails(song);
}
// PUT api/song/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] Song song)
{
}
// DELETE api/song/5
[HttpDelete("{id}")]
public void Delete(int id)
{