60 lines
2.3 KiB
C#
60 lines
2.3 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using MySql.Data.MySqlClient;
|
|
using System.Dynamic;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Server
|
|
{
|
|
public class Getuser
|
|
{
|
|
public static void run(MySqlConnection conn, HttpListenerRequest request, HttpListenerResponse response)
|
|
{
|
|
try
|
|
{
|
|
// Open the connection
|
|
conn.Open();
|
|
// Prepare the SQL query
|
|
MySqlCommand cmd = new MySqlCommand();
|
|
cmd.Connection = conn;
|
|
cmd.CommandText = @"SELECT p.name, SUM(t.time)
|
|
FROM Timelog t
|
|
INNER JOIN Project p ON p.id=t.project
|
|
INNER JOIN User u ON u.id=t.user
|
|
WHERE User = @userid
|
|
GROUP BY name;";
|
|
var queryString = request.QueryString;
|
|
string? userid = queryString["userid"];
|
|
cmd.Parameters.AddWithValue("@userid", userid);
|
|
// Execute the query and read the results
|
|
MySqlDataReader reader = cmd.ExecuteReader();
|
|
dynamic expando = new ExpandoObject();
|
|
while (reader.Read())
|
|
{
|
|
((IDictionary<string?, object>)expando)[reader["name"].ToString()] = reader["SUM(t.time)"];
|
|
|
|
}
|
|
// Serialize the data to JSON
|
|
string jsonResponse = JsonConvert.SerializeObject(expando);
|
|
// prepare response
|
|
byte[] buffer = Encoding.UTF8.GetBytes(jsonResponse);
|
|
response.ContentType = "application/json";
|
|
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);
|
|
}
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
|