This repository has been archived on 2023-06-30. You can view files and clone it, but cannot push or open issues or pull requests.
2chBackAPI/DBchat.cs
RakVhalate f83b76b2de
Some checks failed
continuous-integration/drone/push Build is failing
adding picture posting controller
2022-11-02 17:39:39 +10:00

179 lines
6.2 KiB
C#

using Npgsql;
using static System.Net.Mime.MediaTypeNames;
namespace NeDvachAPI
{
public class DBchat
{
// Obtain connection string information from the portal
public static List<Post> DbList()
{
// Build connection string using parameters from portal
//Post[] posts = new Post[10];
List<Post> posts = new List<Post>();
//int postCount = 0;
string received = "";
string connString =
String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.Password);
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
// using ( var command = new NpgsqlCommand(@"
// SELECT * FROM
// (SELECT post_id, substring(post,1,200)
// FROM dvach
// ORDER BY post_id DESC
// limit 10
// )
// subquery
// ORDER BY post_id ASC", conn) )
using ( var command = new NpgsqlCommand(@"
SELECT * FROM
(
SELECT post_id, post, post_timestamp, post_pic
FROM dvach
ORDER BY post_id DESC
) subquery
ORDER BY post_id ASC", conn) )
{
var reader = command.ExecuteReader();
while (reader.Read())
{
//received +=(
//string.Format(
//"#{0}: {1}" + "\n",
// reader.GetInt32(0).ToString(),
//reader.GetString(1)
//)
//);
Post receivedPost = new()
{
Id = reader.GetInt32(0),
Text = reader.GetString(1),
Timestamp = reader.GetString(2),
ImgURL = (string)reader.GetString(3)
};
posts.Add(receivedPost);
//posts[postCount] = receivedPost;
//postCount++;
}
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",
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.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();
}
public static void SendPost(Post postToSend) //добавлялка
{
string connString =
String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.Password);
using (var conn = new NpgsqlConnection(connString))
{
Console.Out.WriteLine("Opening connection");
conn.Open();
using (var command = new NpgsqlCommand(@"
INSERT INTO dvach (post_id, post, post_timestamp)
VALUES ( DEFAULT, @postText, (
SELECT date_trunc(
'second',
(now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP
)
) )", conn))
{
command.Parameters.AddWithValue("postText", postToSend.Text);
//command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
int nRows = command.ExecuteNonQuery();
Console.Out.WriteLine("Добавлен пост");
}
}
}
public static void DeletePost(string idToDel) //удалялка
{
string connString =
String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.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)); ;
int nRows = command.ExecuteNonQuery();
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
}
}
Console.WriteLine("Данные удалены!");
Console.ReadLine();
}
}
}