added tests for backend, router dom to frontend
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public class TopTen
|
||||
{
|
||||
public object? user { get; set; }
|
||||
public object? date { get; set; }
|
||||
public object? project { get; set; }
|
||||
public object? f_name { get; set; }
|
||||
public object? l_name { get; set; }
|
||||
public object? name { get; set; }
|
||||
public object? total_time { get; set; }
|
||||
}
|
||||
public class Gettopten : Route
|
||||
{
|
||||
public static void HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
|
||||
{
|
||||
try
|
||||
{
|
||||
// prepare SQL query
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
var queryString = request.QueryString;
|
||||
string? from = queryString["from"];
|
||||
string? to = queryString["to"];
|
||||
string? filterBy = queryString["filterBy"];
|
||||
|
||||
if (!string.IsNullOrEmpty(to) && !string.IsNullOrEmpty(from))
|
||||
{
|
||||
Regex regex = new Regex(@"^\d{4}-\d{2}-\d{2}$");
|
||||
if (!regex.IsMatch(to) || !regex.IsMatch(from))
|
||||
{
|
||||
throw new Exception("Incorrect date format");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Empty date format");
|
||||
}
|
||||
if (string.IsNullOrEmpty(filterBy))
|
||||
{
|
||||
throw new Exception("Empty filterby");
|
||||
}
|
||||
if (filterBy != "user" && filterBy != "project")
|
||||
{
|
||||
throw new Exception("Incorrect filterby");
|
||||
}
|
||||
|
||||
// this shenanigan is needed to remove the "" around
|
||||
// group by
|
||||
string req = @"SELECT t.user,t.date,t.project,u.f_name,u.l_name,p.name,SUM(t.time) as total_time
|
||||
FROM Timelog t
|
||||
INNER JOIN Project p ON p.id=t.project
|
||||
INNER JOIN User u ON u.id=t.user
|
||||
WHERE t.date BETWEEN @from AND @to
|
||||
GROUP BY " + filterBy + @" ORDER BY total_time DESC
|
||||
LIMIT 10;";
|
||||
cmd.CommandText = req;
|
||||
cmd.Parameters.AddWithValue("@from", from);
|
||||
cmd.Parameters.AddWithValue("@to", to);
|
||||
|
||||
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||||
{
|
||||
cmd.Connection = conn;
|
||||
conn.Open();
|
||||
// Execute the query and read the results
|
||||
MySqlDataReader reader = cmd.ExecuteReader();
|
||||
List<TopTen> entries = new List<TopTen>();
|
||||
while (reader.Read())
|
||||
{
|
||||
entries.Add(new TopTen
|
||||
{
|
||||
user = reader["user"],
|
||||
date = reader["date"],
|
||||
project = reader["project"],
|
||||
f_name = reader["f_name"],
|
||||
l_name = reader["l_name"],
|
||||
name = reader["name"],
|
||||
total_time = reader["total_time"],
|
||||
});
|
||||
}
|
||||
// Serialize the data to JSON
|
||||
string jsonResponse = JsonConvert.SerializeObject(entries);
|
||||
// prepare response
|
||||
SendSuccess(response, jsonResponse);
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SendError(response, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user