From f791625102082b0318f09ac3a2740330cae9ca37 Mon Sep 17 00:00:00 2001 From: RakVhalate Date: Thu, 24 Nov 2022 12:56:07 +1000 Subject: [PATCH] Added threads listing controller --- Controllers/GetPosts.cs | 2 +- Controllers/ShowThreads.cs | 23 ++++++++++++ DBchat.cs | 76 +++++++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 Controllers/ShowThreads.cs diff --git a/Controllers/GetPosts.cs b/Controllers/GetPosts.cs index 0c5f257..f950878 100644 --- a/Controllers/GetPosts.cs +++ b/Controllers/GetPosts.cs @@ -14,7 +14,7 @@ namespace NeDvachAPI.Controllers public string Get([FromUri] string board, int thread) { string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); - List posts = DBchat.DbList(board, thread); + List posts = DBchat.PostsList(board, thread); string postsJson = JsonSerializer.Serialize(posts); Console.WriteLine("С адреса " + ipAddress + " запрошен список постов из борды " + board + " и треда# " + thread); return postsJson ; diff --git a/Controllers/ShowThreads.cs b/Controllers/ShowThreads.cs new file mode 100644 index 0000000..c7b427e --- /dev/null +++ b/Controllers/ShowThreads.cs @@ -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 posts = DBchat.ThreadsList(board); + string postsJson = JsonSerializer.Serialize(posts); + Console.WriteLine("С адреса " + ipAddress + " запрошен список тредов из борды " + board); + return postsJson; + } + } +} diff --git a/DBchat.cs b/DBchat.cs index 2371c51..c004200 100644 --- a/DBchat.cs +++ b/DBchat.cs @@ -8,7 +8,7 @@ namespace NeDvachAPI // Obtain connection string information from the portal - public static List DbList(string boardName, int thread) + public static List PostsList(string boardName, int thread) { // Build connection string using parameters from portal //Post[] posts = new Post[10]; @@ -79,6 +79,80 @@ namespace NeDvachAPI return posts; } + public static List ThreadsList(string boardName) + { + // Build connection string using parameters from portal + //Post[] posts = new Post[10]; + List posts = new List(); + //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(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 =