diff --git a/.vs/NeDvachAPI/FileContentIndex/ab543bd0-617c-45ed-ab03-ad1ed2f70181.vsidx b/.vs/NeDvachAPI/FileContentIndex/ab543bd0-617c-45ed-ab03-ad1ed2f70181.vsidx new file mode 100644 index 0000000..c68e021 Binary files /dev/null and b/.vs/NeDvachAPI/FileContentIndex/ab543bd0-617c-45ed-ab03-ad1ed2f70181.vsidx differ diff --git a/.vs/NeDvachAPI/FileContentIndex/read.lock b/.vs/NeDvachAPI/FileContentIndex/read.lock new file mode 100644 index 0000000..e69de29 diff --git a/.vs/NeDvachAPI/v17/.suo b/.vs/NeDvachAPI/v17/.suo new file mode 100644 index 0000000..9a50128 Binary files /dev/null and b/.vs/NeDvachAPI/v17/.suo differ diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..24b1642 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,8 @@ +{ + "ExpandedNodes": [ + "", + "\\Controllers" + ], + "SelectedNode": "\\Program.cs", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..4e511b8 Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/Controllers/DeletePost.cs b/Controllers/DeletePost.cs new file mode 100644 index 0000000..c7a7e01 --- /dev/null +++ b/Controllers/DeletePost.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc; +using System.Data; +using System.Text.Json; + +namespace NeDvachAPI.Controllers +{ + [ApiController] + [Route("[controller]")] + public class DeletePostController : ControllerBase + { + [HttpDelete("{id}")] + public JsonResult Get(string id) + { + DBchat.DeletePost(id); + return new JsonResult("Deleted Successfully!"); + + } + } +} diff --git a/Controllers/GetPosts.cs b/Controllers/GetPosts.cs index 5bc2400..be53cfd 100644 --- a/Controllers/GetPosts.cs +++ b/Controllers/GetPosts.cs @@ -13,8 +13,10 @@ namespace NeDvachAPI.Controllers //Post[] posts = DBchat.DbList(); List posts = DBchat.DbList(); string postsJson = JsonSerializer.Serialize(posts); + Console.WriteLine("Запрошен список постов."); return postsJson ; + } } } diff --git a/Controllers/PostPost.cs b/Controllers/PostPost.cs index 279fe82..764651b 100644 --- a/Controllers/PostPost.cs +++ b/Controllers/PostPost.cs @@ -8,9 +8,11 @@ namespace NeDvachAPI.Controllers public class PostToController : ControllerBase { [HttpPost(Name = "PostPosts")] - public void ReceivePost([FromBody] Post ReceivedPost) + public JsonResult ReceivePost([FromBody] Post ReceivedPost) { + Console.WriteLine("Принят пост"); DBchat.SendPost(ReceivedPost); + return new JsonResult("Posted Successfully!"); } } } diff --git a/Controllers/TestPosting.cs b/Controllers/TestPosting.cs index da917f4..68c0748 100644 --- a/Controllers/TestPosting.cs +++ b/Controllers/TestPosting.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.Net; using System.Text.Json; namespace NeDvachAPI.Controllers @@ -8,10 +9,11 @@ namespace NeDvachAPI.Controllers public class TestPostingController : ControllerBase { [HttpPost] - public int Area(int altitude , int height) + public int Area([FromForm] int height , int altitude) { //DBchat.SendPost(ReceivedPost); - return altitude+height; + Console.WriteLine("Received! " + height + " " + altitude ); + return 200; } } } diff --git a/DBchat.cs b/DBchat.cs index 9f54aca..d8203c6 100644 --- a/DBchat.cs +++ b/DBchat.cs @@ -1,4 +1,5 @@ using Npgsql; +using static System.Net.Mime.MediaTypeNames; namespace NeDvachAPI { @@ -64,10 +65,9 @@ namespace NeDvachAPI //); Post receivedPost = new() { - // Date = DateTime.Now, - Id = reader.GetInt32(0), - Text = reader.GetString(1), - Date = reader.GetString(2) + Date = DateTime.Now, + Id = reader.GetInt32(0), + Text = reader.GetString(1) }; posts.Add(receivedPost); //posts[postCount] = receivedPost; @@ -81,7 +81,6 @@ namespace NeDvachAPI public static void DbUpdate(string id, string text) { - // Build connection string using parameters from portal string connString = String.Format( "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", @@ -93,8 +92,6 @@ namespace NeDvachAPI using (var conn = new NpgsqlConnection(connString)) { - - //Console.Out.WriteLine("Opening connection"); conn.Open(); using (var command = new NpgsqlCommand("UPDATE dvach " + @@ -111,10 +108,8 @@ namespace NeDvachAPI Console.WriteLine("Данные обновлены!"); Console.ReadLine(); } - public static void SendPost(Post postToSend) + public static void SendPost(Post postToSend) //добавлялка { - // Build connection string using parameters from portal - // string connString = String.Format( "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", @@ -131,17 +126,49 @@ namespace NeDvachAPI conn.Open(); using (var command = new NpgsqlCommand(@" - INSERT INTO dvach " + @"(post_id, post) - VALUES (DEFAULT, @postText)", conn)) + INSERT INTO dvach " + @"(post_id, post, post_timestamp) + VALUES (DEFAULT, @postText, @postTimeStamp)", conn)) { command.Parameters.AddWithValue("postText", postToSend.Text); + command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); + + int nRows = command.ExecuteNonQuery(); + Console.Out.WriteLine("Добавлен пост"); + } + } + + } + public static void DeletePost(string idToDel) //удалялка + { + string connString = + String.Format( + "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", + Host, + User, + DBname, + Port, + 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.WriteLine("Данные удалены!"); + Console.ReadLine(); + } } } diff --git a/NeDvachAPI.csproj.user b/NeDvachAPI.csproj.user index e4f6e71..fa6d545 100644 --- a/NeDvachAPI.csproj.user +++ b/NeDvachAPI.csproj.user @@ -1,7 +1,7 @@  - MvcControllerEmptyScaffolder + MvcControllerWithActionsScaffolder root/Common/MVC/Controller \ No newline at end of file diff --git a/Post.cs b/Post.cs index 1f745dd..fb47b2b 100644 --- a/Post.cs +++ b/Post.cs @@ -2,10 +2,17 @@ namespace NeDvachAPI { public class Post { - public DateTime Date { get; set; } + public string? Timestamp { get; set; } public int Id { get; set; } public string Text { get; set; } } + + public class ReceivedValues + { + public string height { get; set; } + + public string altitude { get; set; } + } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index cfdbabc..dacd95a 100644 --- a/Program.cs +++ b/Program.cs @@ -3,7 +3,7 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); @@ -11,10 +11,13 @@ builder.Services.AddCors(setup => { setup.AddDefaultPolicy(policyBuilder => { - policyBuilder.WithOrigins("http://www.vdk2ch.ru:4200"); + + policyBuilder.WithOrigins("http://www.vdk2ch.ru:4200").WithMethods("GET", "POST").WithHeaders("*"); + //policyBuilder.WithOrigins("http://localhost:4200").WithMethods("GET", "POST").WithHeaders("*"); }); }); + //builder.Services.AddSwaggerGen(); var app = builder.Build();