minor adjustments to backend
This commit is contained in:
+27
-21
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
@@ -6,15 +5,16 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
// there should be a better way to deal with data comming from sql
|
||||
public class All
|
||||
{
|
||||
public object f_name { get; set; }
|
||||
public object l_name { get; set; }
|
||||
public object mail { get; set; }
|
||||
public object name { get; set; }
|
||||
public object time { get; set; }
|
||||
public object date { get; set; }
|
||||
public object user { get; set; }
|
||||
public object? f_name { get; set; }
|
||||
public object? l_name { get; set; }
|
||||
public object? mail { get; set; }
|
||||
public object? name { get; set; }
|
||||
public object? time { get; set; }
|
||||
public object? date { get; set; }
|
||||
public object? user { get; set; }
|
||||
}
|
||||
|
||||
public class Getall
|
||||
@@ -26,14 +26,16 @@ namespace Server
|
||||
// Open the connection
|
||||
conn.Open();
|
||||
// Prepare the SQL query
|
||||
MySqlCommand myCommand = new MySqlCommand();
|
||||
myCommand.Connection = conn;
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
cmd.Connection = conn;
|
||||
// get url params
|
||||
var queryString = request.QueryString;
|
||||
string from = queryString["from"];
|
||||
string to = queryString["to"];
|
||||
string sortby = queryString["sortby"];
|
||||
string offset = queryString["offset"];
|
||||
|
||||
string? from = queryString["from"];
|
||||
string? to = queryString["to"];
|
||||
string? sortby = queryString["sortby"];
|
||||
string? offset = queryString["offset"];
|
||||
string? order = queryString["order"];
|
||||
order = order == "true" ? "ASC" : "DESC";
|
||||
// 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
|
||||
@@ -41,6 +43,7 @@ namespace Server
|
||||
INNER JOIN Project p ON p.id=t.project
|
||||
INNER JOIN User u ON u.id=t.user ";
|
||||
string offsetQ = " LIMIT 10 OFFSET " + offset + ";";
|
||||
// depending on the incoming parameters construct a Query
|
||||
if (!string.IsNullOrEmpty(to) && !string.IsNullOrEmpty(from))
|
||||
{
|
||||
string whereQ = " WHERE t.date BETWEEN @from AND @to ";
|
||||
@@ -48,15 +51,18 @@ namespace Server
|
||||
}
|
||||
if (!string.IsNullOrEmpty(sortby))
|
||||
{
|
||||
string orderQ = " ORDER BY " + sortby;
|
||||
string orderQ = " ORDER BY " + sortby + " " + order;
|
||||
sqlQ = sqlQ + orderQ;
|
||||
}
|
||||
myCommand.CommandText = sqlQ + offsetQ;
|
||||
myCommand.Parameters.AddWithValue("@from", from);
|
||||
myCommand.Parameters.AddWithValue("@to", to);
|
||||
// add the final line to the query
|
||||
cmd.CommandText = sqlQ + offsetQ;
|
||||
// those don't produce error if they don't find their variables
|
||||
cmd.Parameters.AddWithValue("@from", from);
|
||||
cmd.Parameters.AddWithValue("@to", to);
|
||||
|
||||
// Execute the query and read the results
|
||||
MySqlDataReader reader = myCommand.ExecuteReader();
|
||||
MySqlDataReader reader = cmd.ExecuteReader();
|
||||
|
||||
List<All> entries = new List<All>();
|
||||
while (reader.Read())
|
||||
{
|
||||
@@ -71,7 +77,7 @@ namespace Server
|
||||
mail = reader["mail"],
|
||||
});
|
||||
}
|
||||
// Serialize the data to JSON
|
||||
// serialize the data to JSON
|
||||
string jsonResponse = JsonConvert.SerializeObject(entries);
|
||||
// prepare response
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(jsonResponse);
|
||||
|
||||
Reference in New Issue
Block a user