Added threads listing controller
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
RakVhalate 2022-11-24 12:56:07 +10:00
parent f8038d9587
commit f791625102
3 changed files with 99 additions and 2 deletions

View File

@ -14,7 +14,7 @@ namespace NeDvachAPI.Controllers
public string Get([FromUri] string board, int thread) public string Get([FromUri] string board, int thread)
{ {
string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
List<Post> posts = DBchat.DbList(board, thread); List<Post> posts = DBchat.PostsList(board, thread);
string postsJson = JsonSerializer.Serialize(posts); string postsJson = JsonSerializer.Serialize(posts);
Console.WriteLine("С адреса " + ipAddress + " запрошен список постов из борды " + board + " и треда# " + thread); Console.WriteLine("С адреса " + ipAddress + " запрошен список постов из борды " + board + " и треда# " + thread);
return postsJson ; return postsJson ;

View File

@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using System.Web.Http;
using HttpGetAttribute = Microsoft.AspNetCore.Mvc.HttpGetAttribute;
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
namespace NeDvachAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class ThreadsController : ControllerBase
{
[HttpGet(Name = "GetThreads")]
public string Get([FromUri] string board)
{
string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
List<Post> posts = DBchat.ThreadsList(board);
string postsJson = JsonSerializer.Serialize(posts);
Console.WriteLine("С адреса " + ipAddress + " запрошен список тредов из борды " + board);
return postsJson;
}
}
}

View File

@ -8,7 +8,7 @@ namespace NeDvachAPI
// Obtain connection string information from the portal // Obtain connection string information from the portal
public static List<Post> DbList(string boardName, int thread) public static List<Post> PostsList(string boardName, int thread)
{ {
// Build connection string using parameters from portal // Build connection string using parameters from portal
//Post[] posts = new Post[10]; //Post[] posts = new Post[10];
@ -79,6 +79,80 @@ namespace NeDvachAPI
return posts; 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) public static void DbUpdate(string id, string text)
{ {
string connString = string connString =