diff --git a/APIThreadBuffer.cs b/APIThreadBuffer.cs new file mode 100644 index 0000000..1c8eaa5 --- /dev/null +++ b/APIThreadBuffer.cs @@ -0,0 +1,37 @@ +namespace NeDvachAPI +{ + public class APIThreadBuffer + { + private static List ThreadsPrewievs; + private static List[] Threads = new List[0]; + public static void WriteThreadPreviewsBuffer(List OPPostsToWrite) //add list of OP-posts and last post to buffer + { + ThreadsPrewievs = OPPostsToWrite; + } + + public static List GetOPPosts() + { + return ThreadsPrewievs; + } + + public static void WriteThreadsBuffer(List ThreadToAdd) //add thread's posts from DB to buffer + { + Threads = Threads.Append(ThreadToAdd).ToArray(); + } + public static List GetThread(int threadId) + { + if (threadId < Threads.Length + 1 & threadId != 0) + { + ; + return Threads[threadId - 1]; + + } + else return null; + } + + public static void RefreshThread(int threadId) + { + Threads[threadId - 1] = DBchat.PostsList("b", threadId); + } + } +} diff --git a/BufferFill.cs b/BufferFill.cs new file mode 100644 index 0000000..1ce4432 --- /dev/null +++ b/BufferFill.cs @@ -0,0 +1,20 @@ +namespace NeDvachAPI +{ + public class BufferFill + { + + public static bool FillBuffer(string board) //method to get buffer of information, which is being ran while API is started + { + List posts = DBchat.ThreadsList(board); + APIThreadBuffer.WriteThreadPreviewsBuffer(posts); + Console.WriteLine("Заполняю буфер оп-постами"); + + for (int threadId = 1; threadId <= 4; threadId++) + { + APIThreadBuffer.WriteThreadsBuffer(DBchat.PostsList(board , threadId)); + Console.WriteLine("Добавляю буферизую тред " + threadId); + } + return true; + } + } +} diff --git a/Controllers/GetPosts.cs b/Controllers/GetPosts.cs index f950878..02c98f6 100644 --- a/Controllers/GetPosts.cs +++ b/Controllers/GetPosts.cs @@ -13,10 +13,7 @@ namespace NeDvachAPI.Controllers [HttpGet(Name = "GetPosts")] public string Get([FromUri] string board, int thread) { - string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); - List posts = DBchat.PostsList(board, thread); - string postsJson = JsonSerializer.Serialize(posts); - Console.WriteLine("С адреса " + ipAddress + " запрошен список постов из борды " + board + " и треда# " + thread); + string postsJson = JsonSerializer.Serialize(APIThreadBuffer.GetThread(thread)); return postsJson ; } } diff --git a/Controllers/PostPost.cs b/Controllers/PostPost.cs index 7b174f6..cf631f3 100644 --- a/Controllers/PostPost.cs +++ b/Controllers/PostPost.cs @@ -12,8 +12,16 @@ namespace NeDvachAPI.Controllers { string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); ReceivedPost.Ip = ipAddress; - DBchat.SendPost(ReceivedPost); - return new JsonResult("Posted Successfully!"); + try + { + DBchat.SendPost(ReceivedPost); + APIThreadBuffer.RefreshThread(ReceivedPost.Thread_Id); + return new JsonResult("Сообщение успешно добавлено."); + } + catch + { + return new JsonResult("Произошла ошибка постинга"); + } } } } diff --git a/Controllers/ShowThreads.cs b/Controllers/ShowThreads.cs index c7b427e..70da5a6 100644 --- a/Controllers/ShowThreads.cs +++ b/Controllers/ShowThreads.cs @@ -13,11 +13,8 @@ namespace NeDvachAPI.Controllers [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; + string postsJson = JsonSerializer.Serialize(APIThreadBuffer.GetOPPosts()); + return postsJson; } } } diff --git a/DBchat.cs b/DBchat.cs index c004200..425139b 100644 --- a/DBchat.cs +++ b/DBchat.cs @@ -182,7 +182,7 @@ namespace NeDvachAPI Console.WriteLine("Данные обновлены!"); Console.ReadLine(); } - public static void SendPost(Post postToSend) //добавлялка + public static void SendPost(Post postToSend) //sending post to database { string connString = String.Format( @@ -201,7 +201,7 @@ namespace NeDvachAPI int postNum = 14; using (var command = new NpgsqlCommand(@" - INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, post_timestamp) + INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, is_op, post_timestamp) VALUES ( DEFAULT, @postText, @@ -209,6 +209,7 @@ namespace NeDvachAPI @postImgUrl, @threadId, @ip, + false, ( SELECT date_trunc( 'second', diff --git a/MinIOchat.cs b/MinIOchat.cs index 0dd868c..5d22a39 100644 --- a/MinIOchat.cs +++ b/MinIOchat.cs @@ -14,7 +14,6 @@ namespace NeDvachAPI .WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password) //.WithSSL() .Build(); - //Console.WriteLine(filename); Aes aesEncryption = Aes.Create(); aesEncryption.KeySize = 256; @@ -26,9 +25,7 @@ namespace NeDvachAPI .WithFileName(fileroute) .WithContentType("image/png"); //.WithServerSideEncryption(ssec); - //Console.WriteLine("Кидаю в minio..."); await DvachIo.PutObjectAsync(putObjectArgs); - //await Task.Delay(5000); System.GC.Collect(); System.GC.WaitForPendingFinalizers(); File.Delete(fileroute); diff --git a/Program.cs b/Program.cs index 385a68e..3a0c117 100644 --- a/Program.cs +++ b/Program.cs @@ -1,12 +1,11 @@ -var builder = WebApplication.CreateBuilder(args); +using NeDvachAPI; -// Add services to the container. +var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddCors(setup => { setup.AddDefaultPolicy(policyBuilder => @@ -18,24 +17,21 @@ builder.Services.AddCors(setup => }); -//builder.Services.AddSwaggerGen(); - var app = builder.Build(); -// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - //app.UseSwagger(); - //app.UseSwaggerUI(); + } -//app.UseHttpsRedirection(); - - app.UseCors(); app.UseAuthorization(); app.MapControllers(); -app.Run(); +if (BufferFill.FillBuffer("b")) +{ + app.Run(); +} +