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

110 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Npgsql;
using System.Collections;
using System.Data;
namespace SafeMobileLib
{
public class DBRadioTypeManager : DBmanager
{
public DBRadioTypeManager(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 Response setRadioType(string radioType)
{
Response resp;
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = "select count(value) from \"userSettings\" where key=\'radiotype\'";
object result = null;
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
result = cmd.ExecuteScalar();
}
if (result.ToString() == "0")
{
command = $"INSERT INTO \"userSettings\"(user_id, key, value) VALUES ( 0, 'radiotype','{radioType}')";
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.ExecuteNonQuery();
}
resp = Response.done;
}
else
{
command = $"UPDATE \"userSettings\" SET value = '{radioType}' WHERE key = 'radiotype'";
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.ExecuteNonQuery();
}
resp = Response.alreadyInDB;
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source);
resp = Response.SQLerror;
}
return resp;
}
public string getRadioType()
{
string resp = "";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = "SELECT value FROM \"userSettings\" WHERE key='radiotype'";
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
resp = reader.GetValue(0).ToString();
}
}
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message, ConsoleColor.Red);
}
return resp;
}
public enum Response
{
done,
alreadyInDB,
SQLerror,
}
}
}