diff --git a/backendCs/CreateProcedure.cs b/backendCs/CreateProcedure.cs index 1374120..14abe30 100644 --- a/backendCs/CreateProcedure.cs +++ b/backendCs/CreateProcedure.cs @@ -9,9 +9,7 @@ namespace Server { try { - // Open the connection conn.Open(); - // Prepare the SQL query MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = @" @@ -120,7 +118,6 @@ BEGIN } catch (Exception ex) { - // Handle any connection errors string errorMessage = $"Error: {ex.Message}"; byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); response.ContentType = "text/plain"; diff --git a/backendCs/Getall.cs b/backendCs/Getall.cs index 160f781..a831998 100644 --- a/backendCs/Getall.cs +++ b/backendCs/Getall.cs @@ -23,9 +23,9 @@ namespace Server { try { - // Open the connection + // open connection conn.Open(); - // Prepare the SQL query + // prepare SQL query MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; // get url params @@ -36,8 +36,7 @@ namespace Server string? offset = queryString["offset"]; string? order = queryString["order"]; order = order == "true" ? "ASC" : "DESC"; - // this shenanigan is needed to remove the "" around - // group by + // this shenanigan is needed to remove the "" around group by string sqlQ = @"SELECT u.f_name,u.l_name,u.mail,p.name,t.time,t.date,t.user FROM Timelog t INNER JOIN Project p ON p.id=t.project @@ -60,7 +59,7 @@ namespace Server cmd.Parameters.AddWithValue("@from", from); cmd.Parameters.AddWithValue("@to", to); - // Execute the query and read the results + // execute query and read results MySqlDataReader reader = cmd.ExecuteReader(); List entries = new List(); @@ -77,7 +76,7 @@ namespace Server mail = reader["mail"], }); } - // serialize the data to JSON + // serialize JSON string jsonResponse = JsonConvert.SerializeObject(entries); // prepare response byte[] buffer = Encoding.UTF8.GetBytes(jsonResponse); @@ -88,7 +87,6 @@ namespace Server } catch (Exception ex) { - // Handle any connection errors string errorMessage = $"Error: {ex.Message}"; byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); response.ContentType = "text/plain"; diff --git a/backendCs/Gettopten.cs b/backendCs/Gettopten.cs index b1d818f..7f0e815 100644 --- a/backendCs/Gettopten.cs +++ b/backendCs/Gettopten.cs @@ -21,9 +21,9 @@ namespace Server { try { - // Open the connection + // open db connection conn.Open(); - // Prepare the SQL query + // prepare SQL query MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; var queryString = request.QueryString; @@ -70,7 +70,6 @@ namespace Server } catch (Exception ex) { - // Handle any connection errors string errorMessage = $"Error: {ex.Message}"; byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); response.ContentType = "text/plain"; diff --git a/backendCs/Getuser.cs b/backendCs/Getuser.cs index 96e5722..09d1038 100644 --- a/backendCs/Getuser.cs +++ b/backendCs/Getuser.cs @@ -12,9 +12,9 @@ namespace Server { try { - // Open the connection + // open connection conn.Open(); - // Prepare the SQL query + // prepare SQL query MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = @"SELECT p.name, SUM(t.time) @@ -26,7 +26,7 @@ namespace Server var queryString = request.QueryString; string? userid = queryString["userid"]; cmd.Parameters.AddWithValue("@userid", userid); - // Execute the query and read the results + // execute query and read results MySqlDataReader reader = cmd.ExecuteReader(); dynamic expando = new ExpandoObject(); while (reader.Read()) @@ -34,7 +34,7 @@ namespace Server ((IDictionary)expando)[reader["name"].ToString()] = reader["SUM(t.time)"]; } - // Serialize the data to JSON + // serialize JSON string jsonResponse = JsonConvert.SerializeObject(expando); // prepare response byte[] buffer = Encoding.UTF8.GetBytes(jsonResponse); @@ -45,13 +45,13 @@ namespace Server } catch (Exception ex) { - // Handle any connection errors string errorMessage = $"Error: {ex.Message}"; byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); response.ContentType = "text/plain"; response.ContentLength64 = buffer.Length; response.OutputStream.Write(buffer, 0, buffer.Length); } + // close db connection conn.Close(); } } diff --git a/backendCs/Program.cs b/backendCs/Program.cs index 48b5195..d4f0787 100644 --- a/backendCs/Program.cs +++ b/backendCs/Program.cs @@ -19,12 +19,9 @@ namespace Server listener.Prefixes.Add("http://localhost:5000/api/reset/"); listener.Prefixes.Add("http://localhost:5000/api/createp/"); - // Start listening for incoming requests + // listen listener.Start(); Console.WriteLine("Server is listening on http://localhost:5000/"); - // god knows what that is - /* Thread listenerThread = new Thread(() => */ - /* { */ while (true) { // wait for request @@ -37,8 +34,7 @@ namespace Server MySqlConnection conn = new MySqlConnection(connectionString); // url after localhost:5000/ - // i think the validation is unnecessry but the compiler has - // more experience + // i think the validation is unnecessry but the compiler doesn't string uri; if (request != null && request.Url != null) { @@ -74,12 +70,9 @@ namespace Server response.OutputStream.Write(buffer, 0, buffer.Length); break; } - // Close the response + // send the response response.OutputStream.Close(); } - /* }); */ - /* // Start the listener thread */ - /* listenerThread.Start(); */ } } } diff --git a/backendCs/Program_Mysql.2cs b/backendCs/Program_Mysql.2cs deleted file mode 100644 index 1d99be5..0000000 --- a/backendCs/Program_Mysql.2cs +++ /dev/null @@ -1,108 +0,0 @@ -using System; -using System.Net; -using System.Text; -using System.Threading; -using MySql.Data.MySqlClient; -using Newtonsoft.Json; -using System.Collections.Generic; - -class Program -{ - static void Main() - { - // Create an HttpListener to handle incoming HTTP requests - HttpListener listener = new HttpListener(); - - // Add a prefix to listen to, e.g., http://localhost:8080/ - listener.Prefixes.Add("http://localhost:5000/"); - - // Start listening for incoming requests - listener.Start(); - Console.WriteLine("Server is listening on http://localhost:5000/"); - - // Handle requests on a separate thread to keep server responsive - Thread listenerThread = new Thread(() => - { - while (true) - { - // Wait for a request - HttpListenerContext context = listener.GetContext(); - // Get the request and response objects - HttpListenerRequest request = context.Request; - HttpListenerResponse response = context.Response; - // Check if the requested URL is /api - if (request.Url.AbsolutePath == "/api") - { - string connectionString = "Server=localhost;Port=5050;Database=your_database;User ID=your_user;Password=your_password;"; - MySqlConnection conn = new MySqlConnection(connectionString); - - try - { - // Open the connection - conn.Open(); - - // Prepare the SQL query - string query = "SELECT * FROM User;"; - MySqlCommand cmd = new MySqlCommand(query, conn); - - // Execute the query and read the results - MySqlDataReader reader = cmd.ExecuteReader(); - StringBuilder result = new StringBuilder(); - - while (reader.Read()) - { - // Assuming User table has columns "id", "name", and "email" - result.AppendLine($"ID: {reader["id"]}, Name: {reader["name"]}, Email: {reader["email"]}"); - // Prepare the response message - string responseMessage = result.ToString(); - - - if (string.IsNullOrEmpty(responseMessage)) - { - responseMessage = "No data found."; - } - - byte[] buffer = Encoding.UTF8.GetBytes(responseMessage); - - // Set the response content type and write the response - response.ContentType = "text/plain"; - response.ContentLength64 = buffer.Length; - response.OutputStream.Write(buffer, 0, buffer.Length); - } - catch (Exception ex) - { - // Handle any connection errors - string errorMessage = $"Error: {ex.Message}"; - byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); - response.ContentType = "text/plain"; - response.ContentLength64 = buffer.Length; - response.OutputStream.Write(buffer, 0, buffer.Length); - } - finally - { - // Close the connection - conn.Close(); - } - - } - else - { - // Return 404 for other paths - response.StatusCode = 404; - string errorMessage = "Not Found"; - byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); - response.ContentType = "text/plain"; - response.ContentLength64 = buffer.Length; - response.OutputStream.Write(buffer, 0, buffer.Length); - } - - // Close the response - response.OutputStream.Close(); - } - }); - - // Start the listener thread - listenerThread.Start(); - } -} - diff --git a/backendCs/Program_http.2cs b/backendCs/Program_http.2cs deleted file mode 100644 index 5bc9877..0000000 --- a/backendCs/Program_http.2cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Net; -using System.Text; -using System.Threading; -using MySql.Data.MySqlClient; -using Newtonsoft.Json; -using System.Collections.Generic; - -class Program -{ - static void Main() - { - // Create an HttpListener to handle incoming HTTP requests - HttpListener listener = new HttpListener(); - - // Add a prefix to listen to, e.g., http://localhost:8080/ - listener.Prefixes.Add("http://localhost:5000/"); - - // Start listening for incoming requests - listener.Start(); - Console.WriteLine("Server is listening on http://localhost:5000/"); - - // Handle requests on a separate thread to keep server responsive - Thread listenerThread = new Thread(() => - { - while (true) - { - // Wait for a request - HttpListenerContext context = listener.GetContext(); - - // Get the request and response objects - HttpListenerRequest request = context.Request; - HttpListenerResponse response = context.Response; - - // Check if the requested URL is /api - if (request.Url.AbsolutePath == "/api") - { - // Prepare a response message - string responseMessage = "Hello from the API endpoint!"; - byte[] buffer = Encoding.UTF8.GetBytes(responseMessage); - - // Set the response content type and write the response - response.ContentType = "text/plain"; - response.ContentLength64 = buffer.Length; - response.OutputStream.Write(buffer, 0, buffer.Length); - } - else - { - // Return 404 for other paths - response.StatusCode = 404; - string errorMessage = "Not Found"; - byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); - response.ContentType = "text/plain"; - response.ContentLength64 = buffer.Length; - response.OutputStream.Write(buffer, 0, buffer.Length); - } - - // Close the response - response.OutputStream.Close(); - } - }); - - // Start the listener thread - listenerThread.Start(); - } -} - diff --git a/backendCs/Reset.cs b/backendCs/Reset.cs index 27c7dd4..4c117cc 100644 --- a/backendCs/Reset.cs +++ b/backendCs/Reset.cs @@ -10,9 +10,9 @@ namespace Server { try { - // Open the connection + // open connection conn.Open(); - // Prepare the SQL query + // prepare SQL query MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = "CALL InitDB"; diff --git a/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfo.cs b/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfo.cs index de6aa4e..b09494c 100644 --- a/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfo.cs +++ b/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("TimelogBackend")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4496672d19c31da56be4394168028d97a19d3a6b")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+06ea52ead66c1038b248ca644ce0e0a29ee0065f")] [assembly: System.Reflection.AssemblyProductAttribute("TimelogBackend")] [assembly: System.Reflection.AssemblyTitleAttribute("TimelogBackend")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfoInputs.cache b/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfoInputs.cache index dd5ef12..9dde334 100644 --- a/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfoInputs.cache +++ b/backendCs/obj/Debug/net8.0/TimelogBackend.AssemblyInfoInputs.cache @@ -1 +1 @@ -9366787f5f967306279f95e5eb689ad57d8d916a1aff008ae6b71d7ccaf910fe +b1cc7558bcbf1cf4d6ac3236c7ebc2c474885e8b8b600c803a037c8c84382d4c diff --git a/frontend/src/components/LeftSide.tsx b/frontend/src/components/LeftSide.tsx index f82b274..1735410 100644 --- a/frontend/src/components/LeftSide.tsx +++ b/frontend/src/components/LeftSide.tsx @@ -44,9 +44,7 @@ const LeftSide = ({ useEffect(() => { async function fetchData() { - const resp = await api.get("/getall", { - params, - }); + const resp = await api.get("/getall", { params }); if (resp.data.length) setUsers(resp.data); } async function resetData() { @@ -62,9 +60,7 @@ const LeftSide = ({ const viewProjectHours = (userid: number) => { async function fetchHours() { - const resp = await api.get("/getuser", { - params: { userid }, - }); + const resp = await api.get("/getuser", { params: { userid } }); const entriesArray = Object.entries(resp.data); alert(entriesArray); } diff --git a/frontend/src/utils/api.ts b/frontend/src/utils/api.ts index e6441a0..ed4b7b2 100644 --- a/frontend/src/utils/api.ts +++ b/frontend/src/utils/api.ts @@ -2,6 +2,7 @@ import axios from "axios"; const api = axios.create({ baseURL: "http://localhost:5000/api", + timeout: 1000, headers: { "Content-Type": "application/json", },