From 4ba342b6257642103170597ee15dd80f4076b296 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sun, 21 Apr 2019 17:54:18 -0400 Subject: [PATCH] Added User Context #24 --- Controllers/SongCompressedDataControllers.cs | 1 + Controllers/SongController.cs | 1 + Controllers/SongDataController.cs | 1 + Icarus.csproj | 2 + Models/Context/MusicStoreContext.cs | 166 +++++++++++++++++++ Models/Context/UserContext.cs | 27 +++ Models/MusicStoreContext.cs | 164 ------------------ Models/Song.cs | 2 + Models/User.cs | 2 - 9 files changed, 200 insertions(+), 166 deletions(-) create mode 100644 Models/Context/MusicStoreContext.cs create mode 100644 Models/Context/UserContext.cs delete mode 100644 Models/MusicStoreContext.cs diff --git a/Controllers/SongCompressedDataControllers.cs b/Controllers/SongCompressedDataControllers.cs index 45748c0..862b46f 100644 --- a/Controllers/SongCompressedDataControllers.cs +++ b/Controllers/SongCompressedDataControllers.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Configuration; using Icarus.Controllers.Managers; using Icarus.Controllers.Utilities; using Icarus.Models; +using Icarus.Models.Context; namespace Icarus.Controllers { diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 7430fc4..a51ec33 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Configuration; using Icarus.Controllers.Managers; using Icarus.Models; +using Icarus.Models.Context; namespace Icarus.Controllers { diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index 5f08cfe..86f5455 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -11,6 +11,7 @@ using Microsoft.Extensions.Configuration; using Icarus.Controllers.Managers; using Icarus.Models; +using Icarus.Models.Context; namespace Icarus.Controllers { diff --git a/Icarus.csproj b/Icarus.csproj index c4e5c8d..daf221f 100644 --- a/Icarus.csproj +++ b/Icarus.csproj @@ -10,6 +10,7 @@ + runtime; build; native; contentfiles; analyzers @@ -21,6 +22,7 @@ + diff --git a/Models/Context/MusicStoreContext.cs b/Models/Context/MusicStoreContext.cs new file mode 100644 index 0000000..706995e --- /dev/null +++ b/Models/Context/MusicStoreContext.cs @@ -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 GetAllSongs() + { + List songs = new List(); + 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); + } + } +} diff --git a/Models/Context/UserContext.cs b/Models/Context/UserContext.cs new file mode 100644 index 0000000..1f9513b --- /dev/null +++ b/Models/Context/UserContext.cs @@ -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 Users { get; set; } + + + public UserContext(DbContextOptions options) + : base(options) + { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + } + } +} diff --git a/Models/MusicStoreContext.cs b/Models/MusicStoreContext.cs deleted file mode 100644 index 5487ece..0000000 --- a/Models/MusicStoreContext.cs +++ /dev/null @@ -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 GetAllSongs() - { - List songs = new List(); - 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); - } - } -} diff --git a/Models/Song.cs b/Models/Song.cs index 1906e76..708bbf0 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -2,6 +2,8 @@ using System; using Newtonsoft.Json; +using Icarus.Models.Context; + namespace Icarus.Models { public class Song diff --git a/Models/User.cs b/Models/User.cs index 8814af1..3710b9f 100644 --- a/Models/User.cs +++ b/Models/User.cs @@ -10,8 +10,6 @@ namespace Icarus.Models public int Id { get; set; } [JsonProperty("username")] public string Username { get; set; } - [JsonProperty("password")] - public string Password { get; set; } [JsonProperty("email")] public string Email { get; set; } [JsonProperty("phone_number")]