diff --git a/AuthInfo.cs b/AuthInfo.cs index bb96164..448a31d 100644 --- a/AuthInfo.cs +++ b/AuthInfo.cs @@ -14,9 +14,11 @@ { public static string Host = "postgres.vdk2ch.ru"; public static string User = "postgres"; - public static string DBname = "postgres"; + public static string DBname = "dvaach"; public static string Password = "postgres"; public static string Port = "5432"; } + + } } diff --git a/Controllers/GetPosts.cs b/Controllers/GetPosts.cs index b00a502..ef66f63 100644 --- a/Controllers/GetPosts.cs +++ b/Controllers/GetPosts.cs @@ -11,16 +11,12 @@ namespace NeDvachAPI.Controllers public class ListController : ControllerBase { [HttpGet(Name = "GetPosts")] - public string Get([FromUri] string board = null) + public string Get([FromUri] string board, int thread) { - - //Post[] posts = DBchat.DbList(); - List posts = DBchat.DbList(board); + List posts = DBchat.DbList(board, thread); string postsJson = JsonSerializer.Serialize(posts); - Console.WriteLine("Запрошен список постов из борды " + board); + Console.WriteLine("Запрошен список постов из борды " + board + " и треда# " + thread ); return postsJson ; - - } } } diff --git a/DBchat.cs b/DBchat.cs index 039663b..37f21a1 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) + public static List DbList(string boardName, int thread) { // Build connection string using parameters from portal //Post[] posts = new Post[10]; @@ -20,7 +20,7 @@ namespace NeDvachAPI "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", AuthInfo.DB.Host, AuthInfo.DB.User, - boardName, + AuthInfo.DB.DBname, AuthInfo.DB.Port, AuthInfo.DB.Password); @@ -40,11 +40,12 @@ namespace NeDvachAPI // ) // subquery // ORDER BY post_id ASC", conn) ) - using ( var command = new NpgsqlCommand(@" + using ( var command = new NpgsqlCommand($@" SELECT * FROM ( - SELECT post_id, post, post_timestamp, post_pic - FROM dvach + 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) ) @@ -64,8 +65,8 @@ namespace NeDvachAPI { Id = reader.GetInt32(0), Text = reader.GetString(1), - Timestamp = reader.GetString(2), - ImgURL = (string)reader.GetString(3) + ImgURL = reader.IsDBNull(2) ? null : reader.GetFieldValue(2), + Timestamp = reader.GetString(3) }; posts.Add(receivedPost); @@ -123,13 +124,16 @@ namespace NeDvachAPI Console.Out.WriteLine("Opening connection"); conn.Open(); + int postNum = 14; using (var command = new NpgsqlCommand(@" - INSERT INTO dvach (post_id, post, post_pic, post_timestamp) + INSERT INTO posts (post_id, post_text, post_number, content, thread_id, post_timestamp) VALUES ( DEFAULT, - @postText, + @postText, + @postNum, @postImgUrl, + @threadId, ( SELECT date_trunc( 'second', @@ -138,11 +142,12 @@ namespace NeDvachAPI ) )", 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("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); - int nRows = command.ExecuteNonQuery(); - Console.Out.WriteLine("Добавлен пост"); + Console.Out.WriteLine("Добавлен пост с текстом " + postToSend.Text + " номером " + 22); } } diff --git a/Post.cs b/Post.cs index 8e083e7..3572cd0 100644 --- a/Post.cs +++ b/Post.cs @@ -2,12 +2,12 @@ namespace NeDvachAPI { public class Post { - public string? Timestamp { get; set; } - + public string Timestamp { get; set; } public int Id { get; set; } - public string Text { get; set; } - - public string ImgURL { get; set; } + public string[] ImgURL { get; set; } + public int Thread_Id { get; set; } + public bool Is_OP { get; set; } + public bool Is_Deleted { get; set; } } } \ No newline at end of file