minor adjustments to backend
This commit is contained in:
+10
-36
@@ -1,37 +1,11 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System.Dynamic;
|
||||
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)
|
||||
@@ -41,27 +15,27 @@ namespace Server
|
||||
// Open the connection
|
||||
conn.Open();
|
||||
// Prepare the SQL query
|
||||
MySqlCommand myCommand = new MySqlCommand();
|
||||
myCommand.Connection = conn;
|
||||
myCommand.CommandText = @"SELECT p.name, SUM(t.time)
|
||||
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"];
|
||||
myCommand.Parameters.AddWithValue("@userid", userid);
|
||||
string? userid = queryString["userid"];
|
||||
cmd.Parameters.AddWithValue("@userid", userid);
|
||||
// Execute the query and read the results
|
||||
MySqlDataReader reader = myCommand.ExecuteReader();
|
||||
DynamicObject dO = new DynamicObject();
|
||||
MySqlDataReader reader = cmd.ExecuteReader();
|
||||
dynamic expando = new ExpandoObject();
|
||||
while (reader.Read())
|
||||
{
|
||||
dO.AddField(reader["name"], reader["SUM(t.time)"]);
|
||||
((IDictionary<string?, object>)expando)[reader["name"].ToString()] = reader["SUM(t.time)"];
|
||||
|
||||
}
|
||||
// Serialize the data to JSON
|
||||
string jsonResponse = JsonConvert.SerializeObject(dO);
|
||||
string jsonResponse = JsonConvert.SerializeObject(expando);
|
||||
// prepare response
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(jsonResponse);
|
||||
response.ContentType = "application/json";
|
||||
|
||||
Reference in New Issue
Block a user