Implemented functionality to upload song
This commit is contained in:
@@ -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<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()
|
||||
@@ -62,7 +145,6 @@ namespace Icarus.Controllers.Managers
|
||||
try
|
||||
{
|
||||
_connectionString = _config.GetConnectionString("IcarusDev");
|
||||
Console.WriteLine(_connectionString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -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<Song> 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
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user