Implemented functionality to upload song
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
|
using System.Data;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
@@ -38,23 +40,104 @@ namespace Icarus.Controllers.Managers
|
|||||||
}
|
}
|
||||||
public SongManager(IConfiguration config)
|
public SongManager(IConfiguration config)
|
||||||
{
|
{
|
||||||
Initialize();
|
|
||||||
_config = config;
|
_config = config;
|
||||||
|
Initialize();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public void SaveSong(Song song)
|
public void SaveSongDetails(Song song)
|
||||||
{
|
{
|
||||||
_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();
|
return new Song();
|
||||||
}
|
}
|
||||||
|
public async Task<SongData> 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()
|
void Initialize()
|
||||||
@@ -62,7 +145,6 @@ namespace Icarus.Controllers.Managers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_connectionString = _config.GetConnectionString("IcarusDev");
|
_connectionString = _config.GetConnectionString("IcarusDev");
|
||||||
Console.WriteLine(_connectionString);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Configuration;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ namespace Icarus.Controllers
|
|||||||
public class SongController : ControllerBase
|
public class SongController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
|
private IConfiguration _config;
|
||||||
private SongManager _songMgr;
|
private SongManager _songMgr;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -27,6 +29,7 @@ namespace Icarus.Controllers
|
|||||||
#region Constructor
|
#region Constructor
|
||||||
public SongController(IConfiguration config)
|
public SongController(IConfiguration config)
|
||||||
{
|
{
|
||||||
|
_config = config;
|
||||||
_songMgr = new SongManager(config);
|
_songMgr = new SongManager(config);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -46,7 +49,7 @@ namespace Icarus.Controllers
|
|||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public ActionResult<Song> Get(int id)
|
public ActionResult<Song> Get(int id)
|
||||||
{
|
{
|
||||||
Song song = _songMgr.RetrieveSong(id);
|
Song song = _songMgr.RetrieveSongDetails(id);
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
@@ -55,9 +58,7 @@ namespace Icarus.Controllers
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public void Post([FromBody] Song song)
|
public void Post([FromBody] Song song)
|
||||||
{
|
{
|
||||||
Console.WriteLine("The song's title is: ");
|
_songMgr.SaveSongDetails(song);
|
||||||
Console.WriteLine(song.Title);
|
|
||||||
_songMgr.SaveSong(song);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT api/song/5
|
// PUT api/song/5
|
||||||
|
|||||||
@@ -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<IEnumerable<SongData>> Get()
|
||||||
|
{
|
||||||
|
List<SongData> songs = new List<SongData>();
|
||||||
|
|
||||||
|
|
||||||
|
return songs;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public async Task<IActionResult> 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<IFormFile> 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,7 +20,5 @@ namespace Icarus.Models
|
|||||||
public string Genre { get; set; }
|
public string Genre { get; set; }
|
||||||
[JsonProperty("duration")]
|
[JsonProperty("duration")]
|
||||||
public int Duration { get; set; }
|
public int Duration { get; set; }
|
||||||
[JsonProperty("song_data")]
|
|
||||||
public byte[] SongFile { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ namespace Icarus
|
|||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
||||||
|
services.AddSingleton<IConfiguration>(Configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
|||||||
+18
@@ -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
|
||||||
Reference in New Issue
Block a user