SafeDispatch/SafeMobileLIB_DLL/DBmanagers/DBcallPatchManager.cs
2024-02-22 18:43:59 +02:00

194 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Npgsql;
using System.Collections;
using System.Data;
namespace SafeMobileLib
{
public class Patch
{
private string type;
public string Type
{
get { return type; }
set { type = value; }
}
private string incoming;
public string Incoming
{
get { return incoming; }
set { incoming = value; }
}
private string outgoing;
public string Outgoing
{
get { return outgoing; }
set { outgoing = value; }
}
private string incomingId;
public string IncomingId
{
get { return incomingId; }
set { incomingId = value; }
}
private string outgoingId;
public string OutgoingId
{
get { return outgoingId; }
set { outgoingId = value; }
}
public Patch()
{ }
}
public class DBcallPatchManager : DBmanager
{
public DBcallPatchManager(string p_server, string p_dbname, string p_user, string p_password, string p_port)
: base(p_server, p_dbname, p_user, p_password, p_port)
{
}
public vehResponse addPatch(Patch patch, int user_id)
{
vehResponse resp = vehResponse.done;
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand($"INSERT INTO call_patch(type, incoming,outgoing, userid) " +
$" VALUES({patch.Type}, '{patch.IncomingId}', '{patch.OutgoingId}', {user_id})", connection))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source);
resp = vehResponse.SQLerror;
}
return resp;
}
public vehResponse deletePatch(Patch patch)
{
vehResponse resp = vehResponse.done;
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = $"DELETE FROM call_patch WHERE type = {patch.Type} AND incoming='{patch.IncomingId}' AND outgoing='{patch.OutgoingId}' ";
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source);
resp = vehResponse.SQLerror;
}
return resp;
}
public vehResponse deletePatchFromAdmin(int type, string deleted)
{
vehResponse resp = vehResponse.done;
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = $"DELETE FROM call_patch WHERE type = {type} AND (incoming='{deleted}' OR outgoing='{deleted}')";
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source);
resp = vehResponse.SQLerror;
}
return resp;
}
public List<Patch> getAllPatches()
{
List<Patch> toRet = new List<Patch>();
try
{
string command = "SELECT 'Dispatcher' as type, u.login as incoming, v.login as outgoing, incoming as incoming_id, outgoing as outgoing_id " +
"FROM call_patch " +
"JOIN users u ON u.userid::text = incoming " +
"JOIN users v ON v.userid::text = outgoing " +
"WHERE type = 0 " +
"UNION " +
"SELECT 'Gateway', r.name as incoming, s.name as outgoing, incoming as incoming_id, outgoing as outgoing_id " +
"FROM call_patch " +
"JOIN radio_gw r ON r.\"GW_ID\"::text || '.' || r.\"ID\"::text = incoming " +
"JOIN radio_gw s ON s.\"GW_ID\"::text || '.' || s.\"ID\"::text = outgoing " +
"WHERE type = 1";
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
using (NpgsqlDataReader Reader = cmd.ExecuteReader())
{
while (Reader.Read())
{
Patch p = new Patch
{
Type = Reader.GetString(0),
Incoming = Reader.GetString(1),
Outgoing = Reader.GetString(2),
IncomingId = Reader.GetString(3),
OutgoingId = Reader.GetString(4)
};
toRet.Add(p);
}
}
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source);
}
return toRet;
}
}
}