2022-10-17 14:29:09 +10:00
|
|
|
|
using Npgsql;
|
2022-10-20 22:24:04 +10:00
|
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
2022-10-17 14:29:09 +10:00
|
|
|
|
|
|
|
|
|
namespace NeDvachAPI
|
|
|
|
|
{
|
|
|
|
|
public class DBchat
|
|
|
|
|
{
|
|
|
|
|
// Obtain connection string information from the portal
|
|
|
|
|
private static string Host = "postgres.vdk2ch.ru";
|
|
|
|
|
private static string User = "postgres";
|
|
|
|
|
private static string DBname = "postgres";
|
|
|
|
|
private static string Password = "postgres";
|
|
|
|
|
private static string Port = "5432";
|
|
|
|
|
|
2022-10-20 11:42:03 +10:00
|
|
|
|
public static List<Post> DbList()
|
2022-10-17 14:29:09 +10:00
|
|
|
|
{
|
|
|
|
|
// Build connection string using parameters from portal
|
2022-10-20 11:35:59 +10:00
|
|
|
|
//Post[] posts = new Post[10];
|
|
|
|
|
List<Post> posts = new List<Post>();
|
|
|
|
|
//int postCount = 0;
|
2022-10-17 14:29:09 +10:00
|
|
|
|
string received = "";
|
|
|
|
|
string connString =
|
|
|
|
|
String.Format(
|
|
|
|
|
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
|
|
|
|
Host,
|
|
|
|
|
User,
|
|
|
|
|
DBname,
|
|
|
|
|
Port,
|
|
|
|
|
Password);
|
|
|
|
|
|
|
|
|
|
using (var conn = new NpgsqlConnection(connString))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn.Open();
|
|
|
|
|
|
|
|
|
|
|
2022-10-20 11:38:52 +10:00
|
|
|
|
// using ( var command = new NpgsqlCommand(@"
|
|
|
|
|
// SELECT * FROM
|
|
|
|
|
// (SELECT post_id, substring(post,1,200)
|
|
|
|
|
// FROM dvach
|
|
|
|
|
// ORDER BY post_id DESC
|
|
|
|
|
// limit 10
|
|
|
|
|
// )
|
2022-10-20 11:39:58 +10:00
|
|
|
|
// subquery ORDER BY post_id ASC", conn) )
|
2022-10-20 11:35:59 +10:00
|
|
|
|
using ( var command = new NpgsqlCommand(@"
|
2022-10-18 20:35:11 +10:00
|
|
|
|
SELECT * FROM
|
2022-10-20 11:38:52 +10:00
|
|
|
|
(
|
2022-10-29 13:36:25 +10:00
|
|
|
|
SELECT post_id, post, post_timestamp
|
2022-10-20 11:38:52 +10:00
|
|
|
|
FROM dvach
|
|
|
|
|
ORDER BY post_id DESC
|
|
|
|
|
) subquery
|
|
|
|
|
ORDER BY post_id ASC", conn) )
|
2022-10-17 14:29:09 +10:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var reader = command.ExecuteReader();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
//received +=(
|
|
|
|
|
//string.Format(
|
|
|
|
|
//"#{0}: {1}" + "\n",
|
|
|
|
|
// reader.GetInt32(0).ToString(),
|
|
|
|
|
//reader.GetString(1)
|
|
|
|
|
//)
|
|
|
|
|
//);
|
|
|
|
|
Post receivedPost = new()
|
|
|
|
|
{
|
2022-10-29 13:38:24 +10:00
|
|
|
|
//Date = DateTime.Now,
|
2022-10-29 13:40:06 +10:00
|
|
|
|
Id = reader.GetInt32(0),
|
|
|
|
|
Text = reader.GetString(1),
|
|
|
|
|
Timestamp = reader.GetString(2)
|
2022-10-17 14:29:09 +10:00
|
|
|
|
};
|
2022-10-20 11:35:59 +10:00
|
|
|
|
posts.Add(receivedPost);
|
|
|
|
|
//posts[postCount] = receivedPost;
|
|
|
|
|
//postCount++;
|
2022-10-17 14:29:09 +10:00
|
|
|
|
}
|
|
|
|
|
reader.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return posts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DbUpdate(string id, string text)
|
|
|
|
|
{
|
|
|
|
|
string connString =
|
|
|
|
|
String.Format(
|
|
|
|
|
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
|
|
|
|
Host,
|
|
|
|
|
User,
|
|
|
|
|
DBname,
|
|
|
|
|
Port,
|
|
|
|
|
Password);
|
|
|
|
|
|
|
|
|
|
using (var conn = new NpgsqlConnection(connString))
|
|
|
|
|
{
|
|
|
|
|
conn.Open();
|
|
|
|
|
|
|
|
|
|
using (var command = new NpgsqlCommand("UPDATE dvach " +
|
|
|
|
|
"SET post = @postText WHERE post_id = @ID", conn))
|
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddWithValue("ID", int.Parse(id));
|
|
|
|
|
command.Parameters.AddWithValue("postText", text);
|
|
|
|
|
|
|
|
|
|
int nRows = command.ExecuteNonQuery();
|
|
|
|
|
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Данные обновлены!");
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
}
|
2022-10-29 04:20:10 +10:00
|
|
|
|
public static void SendPost(Post postToSend) //добавлялка
|
2022-10-17 14:29:09 +10:00
|
|
|
|
{
|
|
|
|
|
string connString =
|
|
|
|
|
String.Format(
|
|
|
|
|
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
|
|
|
|
Host,
|
|
|
|
|
User,
|
|
|
|
|
DBname,
|
|
|
|
|
Port,
|
|
|
|
|
Password);
|
|
|
|
|
|
|
|
|
|
using (var conn = new NpgsqlConnection(connString))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Console.Out.WriteLine("Opening connection");
|
|
|
|
|
conn.Open();
|
|
|
|
|
|
2022-10-18 20:35:11 +10:00
|
|
|
|
using (var command = new NpgsqlCommand(@"
|
2022-10-29 13:51:57 +10:00
|
|
|
|
INSERT INTO dvach (post_id, post, post_timestamp)
|
|
|
|
|
VALUES ( DEFAULT, @postText, (
|
2022-10-29 13:56:11 +10:00
|
|
|
|
SELECT date_trunc( 'second', now()::timestamp(0) )
|
2022-10-29 13:51:57 +10:00
|
|
|
|
) )", conn))
|
2022-10-17 14:29:09 +10:00
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddWithValue("postText", postToSend.Text);
|
2022-10-29 13:48:50 +10:00
|
|
|
|
//command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
|
2022-10-17 14:29:09 +10:00
|
|
|
|
|
|
|
|
|
int nRows = command.ExecuteNonQuery();
|
2022-10-29 04:20:10 +10:00
|
|
|
|
Console.Out.WriteLine("Добавлен пост");
|
2022-10-17 14:29:09 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-10-20 22:24:04 +10:00
|
|
|
|
public static void DeletePost(string idToDel) //удалялка
|
|
|
|
|
{
|
|
|
|
|
string connString =
|
|
|
|
|
String.Format(
|
|
|
|
|
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
|
|
|
|
Host,
|
|
|
|
|
User,
|
|
|
|
|
DBname,
|
|
|
|
|
Port,
|
|
|
|
|
Password);
|
|
|
|
|
|
|
|
|
|
using (var conn = new NpgsqlConnection(connString))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//Console.Out.WriteLine("Opening connection");
|
|
|
|
|
conn.Open();
|
|
|
|
|
|
|
|
|
|
using (var command = new NpgsqlCommand(@"
|
|
|
|
|
DELETE FROM dvach
|
|
|
|
|
WHERE post_id = @ID", conn))
|
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddWithValue("ID", int.Parse(idToDel)); ;
|
2022-10-17 14:29:09 +10:00
|
|
|
|
|
|
|
|
|
int nRows = command.ExecuteNonQuery();
|
|
|
|
|
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 22:24:04 +10:00
|
|
|
|
Console.WriteLine("Данные удалены!");
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
|
2022-10-17 14:29:09 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|