Added User Context #24
This commit is contained in:
@@ -12,6 +12,7 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using Icarus.Controllers.Managers;
|
using Icarus.Controllers.Managers;
|
||||||
using Icarus.Controllers.Utilities;
|
using Icarus.Controllers.Utilities;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
using Icarus.Models.Context;
|
||||||
|
|
||||||
namespace Icarus.Controllers
|
namespace Icarus.Controllers
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Configuration;
|
|||||||
|
|
||||||
using Icarus.Controllers.Managers;
|
using Icarus.Controllers.Managers;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
using Icarus.Models.Context;
|
||||||
|
|
||||||
namespace Icarus.Controllers
|
namespace Icarus.Controllers
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using Microsoft.Extensions.Configuration;
|
|||||||
|
|
||||||
using Icarus.Controllers.Managers;
|
using Icarus.Controllers.Managers;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
using Icarus.Models.Context;
|
||||||
|
|
||||||
namespace Icarus.Controllers
|
namespace Icarus.Controllers
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
<PackageReference Include="EntityFramework" Version="6.2.0" />
|
<PackageReference Include="EntityFramework" Version="6.2.0" />
|
||||||
<PackageReference Include="ID3" Version="0.6.0" />
|
<PackageReference Include="ID3" Version="0.6.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.3">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.3">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||||
@@ -21,6 +22,7 @@
|
|||||||
<PackageReference Include="MySql.Data.EntityFrameworkCore.Design" Version="8.0.15" />
|
<PackageReference Include="MySql.Data.EntityFrameworkCore.Design" Version="8.0.15" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
||||||
<PackageReference Include="SevenZip" Version="19.0.0" />
|
<PackageReference Include="SevenZip" Version="19.0.0" />
|
||||||
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.4.0" />
|
||||||
<PackageReference Include="taglib" Version="2.1.0" />
|
<PackageReference Include="taglib" Version="2.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,166 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
namespace Icarus.Models.Context
|
||||||
|
{
|
||||||
|
public class MusicStoreContext
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; set; }
|
||||||
|
|
||||||
|
public MusicStoreContext(string connectionString)
|
||||||
|
{
|
||||||
|
this.ConnectionString = connectionString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertSongDetails()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void SaveSong(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
string query = "INSERT INTO Songs(Title, Album, Artist," +
|
||||||
|
" Year, Genre, Duration, Filename, SongPath) " +
|
||||||
|
"VALUES(@Title, @Album, @Artist, @Year, @Genre, " +
|
||||||
|
"@Duration, @Filename, @SongPath)";
|
||||||
|
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.Parameters.AddWithValue("@Filename", song.Filename);
|
||||||
|
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception ex)
|
||||||
|
{
|
||||||
|
var exMsg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred:\n{exMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteSong(int id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
string query = "DELETE FROM Songs WHERE Id=@Id";
|
||||||
|
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Id", id);
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var exMsg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred:\n{exMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Song> GetAllSongs()
|
||||||
|
{
|
||||||
|
List<Song> songs = new List<Song>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "SELECT * FROM Songs";
|
||||||
|
MySqlCommand cmd = new MySqlCommand(query, conn);
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
songs.Add(new Song
|
||||||
|
{
|
||||||
|
Id = Convert.ToInt32(reader["Id"]),
|
||||||
|
Title = reader["Title"].ToString(),
|
||||||
|
Album = reader["Album"].ToString(),
|
||||||
|
Artist = reader["Artist"].ToString(),
|
||||||
|
Year = Convert.ToInt32(reader["Year"]),
|
||||||
|
Genre = reader["Genre"].ToString(),
|
||||||
|
Duration = Convert.ToInt32(reader["Duration"]),
|
||||||
|
Filename = reader["Filename"].ToString(),
|
||||||
|
SongPath = reader["SongPath"].ToString()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var exMsg = ex.Message;
|
||||||
|
Console.WriteLine($"An error ocurred:\n{exMsg}");
|
||||||
|
songs.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
return songs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Song GetSong(int id)
|
||||||
|
{
|
||||||
|
Song song = new Song();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MySqlConnection conn = GetConnection())
|
||||||
|
{
|
||||||
|
conn.Open();
|
||||||
|
var query = "SELECT * FROM Songs WHERE Id=@Id";
|
||||||
|
|
||||||
|
MySqlCommand cmd = new MySqlCommand(query, conn);
|
||||||
|
cmd.Parameters.AddWithValue("@Id", id);
|
||||||
|
|
||||||
|
using (var reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
song = new Song
|
||||||
|
{
|
||||||
|
Id = Convert.ToInt32(reader["Id"]),
|
||||||
|
Title = reader["Title"].ToString(),
|
||||||
|
Album = reader["Album"].ToString(),
|
||||||
|
Artist = reader["Artist"].ToString(),
|
||||||
|
Year = Convert.ToInt32(reader["Year"]),
|
||||||
|
Genre = reader["Genre"].ToString(),
|
||||||
|
Duration = Convert.ToInt32(reader["Duration"]),
|
||||||
|
Filename = reader["Filename"].ToString(),
|
||||||
|
SongPath = reader["SongPath"].ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception ex)
|
||||||
|
{
|
||||||
|
var exMsg = ex.Message;
|
||||||
|
Console.WriteLine($"An error ocurred: {exMsg}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return song;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MySqlConnection GetConnection()
|
||||||
|
{
|
||||||
|
return new MySqlConnection(ConnectionString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MySql.Data;
|
||||||
|
using MySql.Data.EntityFrameworkCore.Extensions;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
using Icarus.Models;
|
||||||
|
|
||||||
|
namespace Icarus.Models.Context
|
||||||
|
{
|
||||||
|
public class UserContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<User> Users { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public UserContext(DbContextOptions<UserContext> options)
|
||||||
|
: base(options)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<User>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
using MySql.Data.MySqlClient;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Icarus.Models
|
|
||||||
{
|
|
||||||
public class MusicStoreContext
|
|
||||||
{
|
|
||||||
public string ConnectionString { get; set; }
|
|
||||||
|
|
||||||
public MusicStoreContext(string connectionString)
|
|
||||||
{
|
|
||||||
this.ConnectionString = connectionString;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void InsertSongDetails()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void SaveSong(Song song)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
string query = "INSERT INTO Songs(Title, Album, Artist, Year, Genre, Duration, " +
|
|
||||||
"Filename, SongPath) VALUES(@Title, @Album, @Artist, @Year, @Genre, " +
|
|
||||||
"@Duration, @Filename, @SongPath)";
|
|
||||||
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.Parameters.AddWithValue("@Filename", song.Filename);
|
|
||||||
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
var exMsg = ex.Message;
|
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void DeleteSong(int id)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
string query = "DELETE FROM Songs WHERE Id=@Id";
|
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
||||||
{
|
|
||||||
cmd.Parameters.AddWithValue("@Id", id);
|
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var exMsg = ex.Message;
|
|
||||||
Console.WriteLine($"An error occurred:\n{exMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Song> GetAllSongs()
|
|
||||||
{
|
|
||||||
List<Song> songs = new List<Song>();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "SELECT * FROM Songs";
|
|
||||||
MySqlCommand cmd = new MySqlCommand(query, conn);
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
while (reader.Read())
|
|
||||||
{
|
|
||||||
songs.Add(new Song
|
|
||||||
{
|
|
||||||
Id = Convert.ToInt32(reader["Id"]),
|
|
||||||
Title = reader["Title"].ToString(),
|
|
||||||
Album = reader["Album"].ToString(),
|
|
||||||
Artist = reader["Artist"].ToString(),
|
|
||||||
Year = Convert.ToInt32(reader["Year"]),
|
|
||||||
Genre = reader["Genre"].ToString(),
|
|
||||||
Duration = Convert.ToInt32(reader["Duration"]),
|
|
||||||
Filename = reader["Filename"].ToString(),
|
|
||||||
SongPath = reader["SongPath"].ToString()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var exMsg = ex.Message;
|
|
||||||
Console.WriteLine($"An error ocurred:\n{exMsg}");
|
|
||||||
songs.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return songs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Song GetSong(int id)
|
|
||||||
{
|
|
||||||
Song song = new Song();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MySqlConnection conn = GetConnection())
|
|
||||||
{
|
|
||||||
conn.Open();
|
|
||||||
var query = "SELECT * FROM Songs WHERE Id=@Id";
|
|
||||||
|
|
||||||
MySqlCommand cmd = new MySqlCommand(query, conn);
|
|
||||||
cmd.Parameters.AddWithValue("@Id", id);
|
|
||||||
|
|
||||||
using (var reader = cmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
while (reader.Read())
|
|
||||||
{
|
|
||||||
song = new Song
|
|
||||||
{
|
|
||||||
Id = Convert.ToInt32(reader["Id"]),
|
|
||||||
Title = reader["Title"].ToString(),
|
|
||||||
Album = reader["Album"].ToString(),
|
|
||||||
Artist = reader["Artist"].ToString(),
|
|
||||||
Year = Convert.ToInt32(reader["Year"]),
|
|
||||||
Genre = reader["Genre"].ToString(),
|
|
||||||
Duration = Convert.ToInt32(reader["Duration"]),
|
|
||||||
Filename = reader["Filename"].ToString(),
|
|
||||||
SongPath = reader["SongPath"].ToString()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
var exMsg = ex.Message;
|
|
||||||
Console.WriteLine($"An error ocurred: {exMsg}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return song;
|
|
||||||
}
|
|
||||||
|
|
||||||
private MySqlConnection GetConnection()
|
|
||||||
{
|
|
||||||
return new MySqlConnection(ConnectionString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,8 @@ using System;
|
|||||||
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
using Icarus.Models.Context;
|
||||||
|
|
||||||
namespace Icarus.Models
|
namespace Icarus.Models
|
||||||
{
|
{
|
||||||
public class Song
|
public class Song
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ namespace Icarus.Models
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
[JsonProperty("username")]
|
[JsonProperty("username")]
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
[JsonProperty("password")]
|
|
||||||
public string Password { get; set; }
|
|
||||||
[JsonProperty("email")]
|
[JsonProperty("email")]
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
[JsonProperty("phone_number")]
|
[JsonProperty("phone_number")]
|
||||||
|
|||||||
Reference in New Issue
Block a user