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/DBControllers/DBchat.cs

212 lines
7.6 KiB
C#
Raw Normal View History

2022-11-25 22:40:17 +10:00
using NeDvachAPI.Models;
using Npgsql;
2022-10-17 14:29:09 +10:00
2022-11-25 22:40:17 +10:00
namespace NeDvachAPI.DBControllers
2022-10-17 14:29:09 +10:00
{
public class DBchat
{
2022-11-24 12:56:07 +10:00
public static List<Post> PostsList(string boardName, int thread)
2022-10-17 14:29:09 +10:00
{
2022-10-20 11:35:59 +10:00
List<Post> posts = new List<Post>();
2022-10-17 14:29:09 +10:00
string connString =
2022-11-25 22:40:17 +10:00
string.Format(
2022-10-17 14:29:09 +10:00
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
2022-11-02 17:39:39 +10:00
AuthInfo.DB.Host,
AuthInfo.DB.User,
2022-11-18 14:46:31 +10:00
AuthInfo.DB.DBname,
2022-11-02 17:39:39 +10:00
AuthInfo.DB.Port,
AuthInfo.DB.Password);
2022-10-17 14:29:09 +10:00
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
2022-11-25 22:40:17 +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-11-18 14:46:31 +10:00
SELECT post_id, post_text, content, post_timestamp
FROM posts
WHERE thread_id = {thread}
2022-10-20 11:38:52 +10:00
ORDER BY post_id DESC
) subquery
2022-11-25 22:40:17 +10:00
ORDER BY post_id ASC", conn))
2022-10-17 14:29:09 +10:00
{
var reader = command.ExecuteReader();
while (reader.Read())
{
Post receivedPost = new()
{
Id = reader.GetInt32(0),
2022-10-29 04:20:10 +10:00
Text = reader.GetString(1),
2022-11-18 14:46:31 +10:00
ImgURL = reader.IsDBNull(2) ? null : reader.GetFieldValue<string[]>(2),
Timestamp = reader.GetString(3)
2022-11-02 01:55:21 +10:00
2022-10-17 14:29:09 +10:00
};
2022-10-20 11:35:59 +10:00
posts.Add(receivedPost);
2022-10-17 14:29:09 +10:00
}
reader.Close();
}
}
return posts;
}
2022-11-24 12:56:07 +10:00
public static List<Post> ThreadsList(string boardName)
{
List<Post> posts = new List<Post>();
string connString =
2022-11-25 22:40:17 +10:00
string.Format(
2022-11-24 12:56:07 +10:00
"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, post_text, content, post_timestamp, is_op, thread_id
FROM posts
WHERE
is_op = {true}
2022-12-21 17:06:10 +10:00
ORDER BY thread_id DESC
2022-11-24 12:56:07 +10:00
) subquery
2022-12-21 17:06:10 +10:00
ORDER BY thread_id ASC", conn))
2022-11-24 12:56:07 +10:00
{
var reader = command.ExecuteReader();
while (reader.Read())
{
Post receivedPost = new()
{
Id = reader.GetInt32(0),
Text = reader.GetString(1),
ImgURL = reader.IsDBNull(2) ? null : reader.GetFieldValue<string[]>(2),
Timestamp = reader.GetString(3),
Is_OP = reader.GetBoolean(4),
Thread_Id = reader.GetInt32(5)
};
posts.Add(receivedPost);
}
reader.Close();
}
}
return posts;
}
2022-10-17 14:29:09 +10:00
public static void DbUpdate(string id, string text)
{
string connString =
2022-11-25 22:40:17 +10:00
string.Format(
2022-10-17 14:29:09 +10:00
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
2022-11-02 17:39:39 +10:00
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.Password);
2022-10-17 14:29:09 +10:00
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();
2022-11-25 22:40:17 +10:00
Console.Out.WriteLine(string.Format("Number of rows updated={0}", nRows));
2022-10-17 14:29:09 +10:00
}
}
Console.WriteLine("Данные обновлены!");
Console.ReadLine();
}
2022-11-25 12:37:26 +10:00
public static void SendPost(Post postToSend) //sending post to database
2022-10-17 14:29:09 +10:00
{
string connString =
2022-11-25 22:40:17 +10:00
string.Format(
2022-10-17 14:29:09 +10:00
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
2022-11-02 17:39:39 +10:00
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.Password);
2022-10-17 14:29:09 +10:00
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
2022-11-18 14:46:31 +10:00
int postNum = 14;
2022-10-17 14:29:09 +10:00
2022-10-18 20:35:11 +10:00
using (var command = new NpgsqlCommand(@"
2022-11-25 12:37:26 +10:00
INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, is_op, post_timestamp)
2022-11-03 15:13:58 +10:00
VALUES (
DEFAULT,
2022-11-18 14:46:31 +10:00
@postText,
@postNum,
2022-11-03 15:13:58 +10:00
@postImgUrl,
2022-11-18 14:46:31 +10:00
@threadId,
2022-11-22 18:03:38 +10:00
@ip,
2022-11-25 12:37:26 +10:00
false,
2022-11-03 15:13:58 +10:00
(
2022-10-29 14:11:01 +10:00
SELECT date_trunc(
'second',
(now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP
)
2022-10-29 13:51:57 +10:00
) )", conn))
2022-10-17 14:29:09 +10:00
{
command.Parameters.AddWithValue("postText", postToSend.Text);
2022-11-18 14:46:31 +10:00
command.Parameters.AddWithValue("postNum", postNum);
2022-11-03 15:13:58 +10:00
command.Parameters.AddWithValue("postImgUrl", postToSend.ImgURL);
2022-11-18 14:46:31 +10:00
command.Parameters.AddWithValue("threadId", postToSend.Thread_Id);
2022-11-22 18:03:38 +10:00
command.Parameters.AddWithValue("ip", postToSend.Ip);
2022-10-17 14:29:09 +10:00
int nRows = command.ExecuteNonQuery();
2022-11-18 14:46:31 +10:00
Console.Out.WriteLine("Добавлен пост с текстом " + postToSend.Text + " номером " + 22);
2022-10-17 14:29:09 +10:00
}
}
}
2022-10-20 22:24:04 +10:00
public static void DeletePost(string idToDel) //удалялка
{
string connString =
2022-11-25 22:40:17 +10:00
string.Format(
2022-10-20 22:24:04 +10:00
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
2022-11-02 17:39:39 +10:00
AuthInfo.DB.Host,
AuthInfo.DB.User,
AuthInfo.DB.DBname,
AuthInfo.DB.Port,
AuthInfo.DB.Password);
2022-10-20 22:24:04 +10:00
using (var conn = new NpgsqlConnection(connString))
{
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();
2022-11-25 22:40:17 +10:00
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
}
}