Added some folders.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-11-25 22:40:17 +10:00
parent 50a3c94af8
commit a4b72664fd
13 changed files with 53 additions and 50 deletions

266
DBControllers/DBchat.cs Normal file
View File

@@ -0,0 +1,266 @@
using NeDvachAPI.Models;
using Npgsql;
using static System.Net.Mime.MediaTypeNames;
namespace NeDvachAPI.DBControllers
{
public class DBchat
{
// Obtain connection string information from the portal
public static List<Post> PostsList(string boardName, int thread)
{
// 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_text, content, post_timestamp
FROM posts
WHERE thread_id = {thread}
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),
ImgURL = reader.IsDBNull(2) ? null : reader.GetFieldValue<string[]>(2),
Timestamp = reader.GetString(3)
};
posts.Add(receivedPost);
//posts[postCount] = receivedPost;
//postCount++;
}
reader.Close();
}
}
return posts;
}
public static List<Post> ThreadsList(string boardName)
{
// 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_text, content, post_timestamp, is_op, thread_id
FROM posts
WHERE
is_op = {true}
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),
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);
//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) //sending post to database
{
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();
int postNum = 14;
using (var command = new NpgsqlCommand(@"
INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, is_op, post_timestamp)
VALUES (
DEFAULT,
@postText,
@postNum,
@postImgUrl,
@threadId,
@ip,
false,
(
SELECT date_trunc(
'second',
(now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP
)
) )", conn))
{
command.Parameters.AddWithValue("postText", postToSend.Text);
command.Parameters.AddWithValue("postNum", postNum);
command.Parameters.AddWithValue("postImgUrl", postToSend.ImgURL);
command.Parameters.AddWithValue("threadId", postToSend.Thread_Id);
command.Parameters.AddWithValue("ip", postToSend.Ip);
//command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
int nRows = command.ExecuteNonQuery();
Console.Out.WriteLine("Добавлен пост с текстом " + postToSend.Text + " номером " + 22);
}
}
}
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();
}
}
}

View File

@@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Mvc;
using Minio.DataModel;
using Minio;
using System.Security.Cryptography;
namespace NeDvachAPI.DBControllers
{
public class MinIOchat
{
public static async Task<JsonResult> PictureUpload(string fileroute, string filename)
{
MinioClient DvachIo = new MinioClient()
.WithEndpoint(AuthInfo.MinIo.endpoint)
.WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password)
//.WithSSL()
.Build();
Aes aesEncryption = Aes.Create();
aesEncryption.KeySize = 256;
aesEncryption.GenerateKey();
var ssec = new SSEC(aesEncryption.Key);
PutObjectArgs putObjectArgs = new PutObjectArgs()
.WithBucket(AuthInfo.MinIo.bucketName)
.WithObject(filename)
.WithFileName(fileroute)
.WithContentType("image/png");
//.WithServerSideEncryption(ssec);
await DvachIo.PutObjectAsync(putObjectArgs);
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(fileroute);
return new JsonResult("http://static.vdk2ch.ru:15555/thread-pics/" + filename);
}
}
}