From b7723b2cba6be973f3143b7e794b43b0eda06daf Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 18 Mar 2019 21:58:26 -0400 Subject: [PATCH] Implemented functionality to upload song --- Controllers/Managers/SongManager.cs | 90 +++++++++++++++++++++- Controllers/SongController.cs | 9 ++- Controllers/SongDataController.cs | 112 ++++++++++++++++++++++++++++ Models/Song.cs | 2 - Models/SongData.cs | 12 +++ Startup.cs | 1 + icarus.sln | 18 +++++ 7 files changed, 234 insertions(+), 10 deletions(-) create mode 100644 Controllers/SongDataController.cs create mode 100644 Models/SongData.cs create mode 100644 icarus.sln diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index a23cfbf..e185cfc 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Configuration; +using System.Data; +using System.Threading.Tasks; using Microsoft.Extensions.Configuration; @@ -38,23 +40,104 @@ namespace Icarus.Controllers.Managers } public SongManager(IConfiguration config) { - Initialize(); _config = config; + Initialize(); } #endregion #region Methods - public void SaveSong(Song song) + public void SaveSongDetails(Song song) { _song = song; + try + { + Console.WriteLine($"Connection string is: {_connectionString}"); + using (MySqlConnection conn = new MySqlConnection(_connectionString)) + { + conn.Open(); + string query = "INSERT INTO Song(Title, Album, Artist, Year, Genre, Duration) " + + "VALUES(@Title, @Album, @Artist, @Year, @Genre, @Duration)"; + using (MySqlCommand cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Title", song.Title); + cmd.Parameters.AddWithValue("@Album", song.Album); + cmd.Parameters.AddWithValue("@Artist", song.Artist); + cmd.Parameters.AddWithValue("@Year", song.Year); + cmd.Parameters.AddWithValue("@Genre", song.Genre); + cmd.Parameters.AddWithValue("@Duration", song.Duration); + cmd.ExecuteNonQuery(); + } + } + } + catch (Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine($"An Error Occurred: {exMsg}"); + } + } + public async Task SaveSong(SongData songData) + { + try + { + using (MySqlConnection conn = new MySqlConnection(_connectionString)) + { + conn.Open(); + string query = "INSERT INTO SongData(Data) VALUES(@Data)"; + using (MySqlCommand cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Data", songData.Data); + + cmd.ExecuteNonQuery(); + } + } + } + catch(Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine($"An error occurred: {exMsg}"); + } } - public Song RetrieveSong(int id) + public Song RetrieveSongDetails(int id) { return new Song(); } + public async Task RetrieveSong(int id) + { + SongData song = new SongData(); + DataTable results = new DataTable(); + try + { + using (MySqlConnection conn = new MySqlConnection(_connectionString)) + { + conn.Open(); + string query = "SELECT Id, Data From SongData WHERE Id=@Id"; + using (MySqlCommand cmd = new MySqlCommand(query, conn)) + { + cmd.Parameters.AddWithValue("@Id", id); + + cmd.ExecuteNonQuery(); + + using (MySqlDataAdapter dataDump = new MySqlDataAdapter(cmd)) + { + dataDump.Fill(results); + } + } + } + DataRow row = results.Rows[0]; + song.Data = (byte[])row[1]; + + } + catch (Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine($"An error occurred: {exMsg}"); + } + + return song; + } void Initialize() @@ -62,7 +145,6 @@ namespace Icarus.Controllers.Managers try { _connectionString = _config.GetConnectionString("IcarusDev"); - Console.WriteLine(_connectionString); } catch (Exception ex) { diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 46b7440..5be9125 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Linq; using System.Threading.Tasks; @@ -16,6 +17,7 @@ namespace Icarus.Controllers public class SongController : ControllerBase { #region Fields + private IConfiguration _config; private SongManager _songMgr; #endregion @@ -27,6 +29,7 @@ namespace Icarus.Controllers #region Constructor public SongController(IConfiguration config) { + _config = config; _songMgr = new SongManager(config); } #endregion @@ -46,7 +49,7 @@ namespace Icarus.Controllers [HttpGet("{id}")] public ActionResult Get(int id) { - Song song = _songMgr.RetrieveSong(id); + Song song = _songMgr.RetrieveSongDetails(id); return song; } @@ -55,9 +58,7 @@ namespace Icarus.Controllers [HttpPost] public void Post([FromBody] Song song) { - Console.WriteLine("The song's title is: "); - Console.WriteLine(song.Title); - _songMgr.SaveSong(song); + _songMgr.SaveSongDetails(song); } // PUT api/song/5 diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs new file mode 100644 index 0000000..41d1e0c --- /dev/null +++ b/Controllers/SongDataController.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; + +using Icarus.Controllers.Managers; +using Icarus.Models; + +namespace Icarus.Controllers +{ + [Route("api/Song/data")] + [ApiController] + public class SongDataController : ControllerBase + { + #region Fields + private IConfiguration _config; + private SongManager _songMgr; + private string _songDir = @""; + #endregion + + + #region Properties + #endregion + + + #region Constructor + public SongDataController(IConfiguration config) + { + _config = config; + _songMgr = new SongManager(config); + } + #endregion + + + [HttpGet] + public ActionResult> Get() + { + List songs = new List(); + + + return songs; + } + + [HttpGet("{id}")] + public async Task Get(int id) + { + SongData song = new SongData(); + + song = await _songMgr.RetrieveSong(id); + + return File(song.Data, "application/x-msdownload", "dfdf.mp3"); + } + + [HttpPost] + public async Task Post([FromForm(Name = "file")] List songData) + { + try + { + Console.WriteLine("Uploading song..."); + + var uploads = Path.Combine(_songDir, "uploads"); + Console.WriteLine($"Song Path {uploads}"); + foreach (var sng in songData) + { + byte[] data; + if (sng.Length > 0) { + + using (var ms = new MemoryStream()) + { + sng.CopyTo(ms); + data = ms.ToArray(); + } + SongData songD = new SongData + { + Data = data + }; + + _songMgr.SaveSong(songD); + + + /** + var filePath = Path.Combine(uploads, sng.FileName); + using (var fileStream = new FileStream(filePath, FileMode.Create)) { + await sng.CopyToAsync(fileStream); + } + */ + } + } + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } + + [HttpPut("{id}")] + public void Put(int id, [FromBody] SongData song) + { + } + + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Models/Song.cs b/Models/Song.cs index 891a592..1bc3e14 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -20,7 +20,5 @@ namespace Icarus.Models public string Genre { get; set; } [JsonProperty("duration")] public int Duration { get; set; } - [JsonProperty("song_data")] - public byte[] SongFile { get; set; } } } diff --git a/Models/SongData.cs b/Models/SongData.cs new file mode 100644 index 0000000..f692092 --- /dev/null +++ b/Models/SongData.cs @@ -0,0 +1,12 @@ +using System; +using System.Text; + +namespace Icarus.Models +{ + public class SongData + { + public int Id { get; set; } + public byte[] Data { get; set; } + public int SongId { get; set; } + } +} diff --git a/Startup.cs b/Startup.cs index 28e8466..2c4ba14 100644 --- a/Startup.cs +++ b/Startup.cs @@ -26,6 +26,7 @@ namespace Icarus public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddSingleton(Configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/icarus.sln b/icarus.sln new file mode 100644 index 0000000..2f92ebd --- /dev/null +++ b/icarus.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal