diff --git a/Nevyn/Classes/Mysql.cs b/Nevyn/Classes/Mysql.cs index d36c96c..470f094 100644 --- a/Nevyn/Classes/Mysql.cs +++ b/Nevyn/Classes/Mysql.cs @@ -1,6 +1,116 @@ -namespace Nevyn.Classes +using MySql.Data.MySqlClient; + +namespace Nevyn.Classes { public class Mysql { + private MySqlConnection connection; + private string server; + private string database; + private string uid; + private string password; + + //Constructor + public Mysql() + { + Initialize(); + } + + //Initialize values + private void Initialize() + { + server = "192.168.1.108"; + database = "nevyn"; + uid = "nevyn"; + password = "3edc4RFV"; + string connectionString; + connectionString = "SERVER=" + server + ";" + "DATABASE=" + + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; + + connection = new MySqlConnection(connectionString); + } + //open connection to database + private bool OpenConnection() + { + try + { + connection.Open(); + return true; + } + catch (MySqlException ex) + { + //When handling errors, you can your application's response based + //on the error number. + //The two most common error numbers when connecting are as follows: + //0: Cannot connect to server. + //1045: Invalid user name and/or password. + switch (ex.Number) + { + case 0: + Console.Write("Cannot connect to server. Contact administrator"); + break; + + case 1045: + Console.Write("Invalid username/password, please try again"); + break; + } + return false; + } + } + + //Close connection + private bool CloseConnection() + { + try + { + connection.Close(); + return true; + } + catch (MySqlException ex) + { + Console.Write(ex.Message); + return false; + } + } + + public Models.Wallet SelectWallet() + { + string query = "SELECT * FROM tblWallet"; + + //Create a list to store the result + Models.Wallet wallet = new Models.Wallet(); + + //Open connection + if (this.OpenConnection() == true) + { + //Create Command + MySqlCommand cmd = new MySqlCommand(query, connection); + //Create a data reader and Execute the command + MySqlDataReader dataReader = cmd.ExecuteReader(); + + //Read the data and store them in the list + while (dataReader.Read()) + { + wallet.Id = (int)dataReader["idtblWallet"]; + wallet.Kort = (int)dataReader["Kort"]; + wallet.Buffert = (int)dataReader["Buffert"]; + wallet.Spara = (int)dataReader["Spara"]; + + } + + //close Data Reader + dataReader.Close(); + + //close Connection + this.CloseConnection(); + + //return list to be displayed + return wallet; + } + else + { + return wallet; + } + } } } diff --git a/Nevyn/Controllers/MoneyController.cs b/Nevyn/Controllers/MoneyController.cs index e6fb5f3..563ace4 100644 --- a/Nevyn/Controllers/MoneyController.cs +++ b/Nevyn/Controllers/MoneyController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Nevyn.Models; +using Nevyn.Classes; namespace Nevyn.Controllers { diff --git a/Nevyn/Models/Money.cs b/Nevyn/Models/Money.cs index f65a2e0..de3039f 100644 --- a/Nevyn/Models/Money.cs +++ b/Nevyn/Models/Money.cs @@ -47,6 +47,12 @@ namespace Nevyn.Models } } + public class updateFromDatabase + { + Classes.Mysql mysql = new Classes.Mysql(); + // = mysql.selectWallet(); + + } } public class WalletDTO diff --git a/Nevyn/Nevyn.csproj b/Nevyn/Nevyn.csproj index d770bf0..b76d562 100644 --- a/Nevyn/Nevyn.csproj +++ b/Nevyn/Nevyn.csproj @@ -23,6 +23,10 @@ + + + + diff --git a/Nevyn/Properties/launchSettings.json b/Nevyn/Properties/launchSettings.json index 152cb9c..96e7ce2 100644 --- a/Nevyn/Properties/launchSettings.json +++ b/Nevyn/Properties/launchSettings.json @@ -13,11 +13,10 @@ "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", + "applicationUrl": "http://localhost:8080", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:8080", - "dotnetRunMessages": true + } }, "IIS Express": { "commandName": "IISExpress", @@ -31,8 +30,7 @@ "commandName": "Docker", "launchBrowser": true, "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", - "publishAllPorts": true, - "useSSL": true + "environmentVariables": {} } } } \ No newline at end of file