86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Text;
|
|
using MySql.Data.MySqlClient;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Server
|
|
{
|
|
class DynamicObject
|
|
{
|
|
// A dictionary to store dynamic properties/fields
|
|
public Dictionary<object, object> Fields { get; set; }
|
|
|
|
public DynamicObject()
|
|
{
|
|
Fields = new Dictionary<object, object>();
|
|
}
|
|
|
|
// Adding a dynamic field
|
|
public void AddField(object key, object value)
|
|
{
|
|
Fields[key] = value;
|
|
}
|
|
|
|
// Retrieving a dynamic field
|
|
public object GetField(object key)
|
|
{
|
|
if (Fields.ContainsKey(key))
|
|
{
|
|
return Fields[key];
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
public class Getuser
|
|
{
|
|
public static void run(MySqlConnection conn, HttpListenerRequest request, HttpListenerResponse response)
|
|
{
|
|
try
|
|
{
|
|
// Open the connection
|
|
conn.Open();
|
|
// Prepare the SQL query
|
|
MySqlCommand myCommand = new MySqlCommand();
|
|
myCommand.Connection = conn;
|
|
myCommand.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"];
|
|
myCommand.Parameters.AddWithValue("@userid", userid);
|
|
// Execute the query and read the results
|
|
MySqlDataReader reader = myCommand.ExecuteReader();
|
|
DynamicObject dO = new DynamicObject();
|
|
while (reader.Read())
|
|
{
|
|
dO.AddField(reader["name"], reader["SUM(t.time)"]);
|
|
|
|
}
|
|
// Serialize the data to JSON
|
|
string jsonResponse = JsonConvert.SerializeObject(dO);
|
|
// 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();
|
|
}
|
|
}
|
|
}
|
|
|