ff
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Simple_Not 2022-10-29 13:37:47 +10:00
commit c4e266450d
13 changed files with 90 additions and 20 deletions

BIN
.vs/NeDvachAPI/v17/.suo Normal file

Binary file not shown.

View File

@ -0,0 +1,8 @@
{
"ExpandedNodes": [
"",
"\\Controllers"
],
"SelectedNode": "\\Program.cs",
"PreviewInSolutionExplorer": false
}

BIN
.vs/slnx.sqlite Normal file

Binary file not shown.

19
Controllers/DeletePost.cs Normal file
View File

@ -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!");
}
}
}

View File

@ -13,8 +13,10 @@ namespace NeDvachAPI.Controllers
//Post[] posts = DBchat.DbList(); //Post[] posts = DBchat.DbList();
List<Post> posts = DBchat.DbList(); List<Post> posts = DBchat.DbList();
string postsJson = JsonSerializer.Serialize(posts); string postsJson = JsonSerializer.Serialize(posts);
Console.WriteLine("Запрошен список постов.");
return postsJson ; return postsJson ;
} }
} }
} }

View File

@ -8,9 +8,11 @@ namespace NeDvachAPI.Controllers
public class PostToController : ControllerBase public class PostToController : ControllerBase
{ {
[HttpPost(Name = "PostPosts")] [HttpPost(Name = "PostPosts")]
public void ReceivePost([FromBody] Post ReceivedPost) public JsonResult ReceivePost([FromBody] Post ReceivedPost)
{ {
Console.WriteLine("Принят пост");
DBchat.SendPost(ReceivedPost); DBchat.SendPost(ReceivedPost);
return new JsonResult("Posted Successfully!");
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Text.Json; using System.Text.Json;
namespace NeDvachAPI.Controllers namespace NeDvachAPI.Controllers
@ -8,10 +9,11 @@ namespace NeDvachAPI.Controllers
public class TestPostingController : ControllerBase public class TestPostingController : ControllerBase
{ {
[HttpPost] [HttpPost]
public int Area(int altitude , int height) public int Area([FromForm] int height , int altitude)
{ {
//DBchat.SendPost(ReceivedPost); //DBchat.SendPost(ReceivedPost);
return altitude+height; Console.WriteLine("Received! " + height + " " + altitude );
return 200;
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Npgsql; using Npgsql;
using static System.Net.Mime.MediaTypeNames;
namespace NeDvachAPI namespace NeDvachAPI
{ {
@ -64,10 +65,9 @@ namespace NeDvachAPI
//); //);
Post receivedPost = new() Post receivedPost = new()
{ {
// Date = DateTime.Now, Date = DateTime.Now,
Id = reader.GetInt32(0), Id = reader.GetInt32(0),
Text = reader.GetString(1), Text = reader.GetString(1)
Date = reader.GetString(2)
}; };
posts.Add(receivedPost); posts.Add(receivedPost);
//posts[postCount] = receivedPost; //posts[postCount] = receivedPost;
@ -81,7 +81,6 @@ namespace NeDvachAPI
public static void DbUpdate(string id, string text) public static void DbUpdate(string id, string text)
{ {
// Build connection string using parameters from portal
string connString = string connString =
String.Format( String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", "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)) using (var conn = new NpgsqlConnection(connString))
{ {
//Console.Out.WriteLine("Opening connection");
conn.Open(); conn.Open();
using (var command = new NpgsqlCommand("UPDATE dvach " + using (var command = new NpgsqlCommand("UPDATE dvach " +
@ -111,10 +108,8 @@ namespace NeDvachAPI
Console.WriteLine("Данные обновлены!"); Console.WriteLine("Данные обновлены!");
Console.ReadLine(); Console.ReadLine();
} }
public static void SendPost(Post postToSend) public static void SendPost(Post postToSend) //добавлялка
{ {
// Build connection string using parameters from portal
//
string connString = string connString =
String.Format( String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
@ -131,17 +126,49 @@ namespace NeDvachAPI
conn.Open(); conn.Open();
using (var command = new NpgsqlCommand(@" using (var command = new NpgsqlCommand(@"
INSERT INTO dvach " + @"(post_id, post) INSERT INTO dvach " + @"(post_id, post, post_timestamp)
VALUES (DEFAULT, @postText)", conn)) VALUES (DEFAULT, @postText, @postTimeStamp)", conn))
{ {
command.Parameters.AddWithValue("postText", postToSend.Text); 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(); int nRows = command.ExecuteNonQuery();
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows)); Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
} }
} }
Console.WriteLine("С добавлением закончено"); Console.WriteLine("Данные удалены!");
Console.ReadLine();
} }
} }
} }

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID> <Controller_SelectedScaffolderID>MvcControllerWithActionsScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath> <Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -2,10 +2,17 @@ namespace NeDvachAPI
{ {
public class Post public class Post
{ {
public DateTime Date { get; set; } public string? Timestamp { get; set; }
public int Id { get; set; } public int Id { get; set; }
public string Text { get; set; } public string Text { get; set; }
} }
public class ReceivedValues
{
public string height { get; set; }
public string altitude { get; set; }
}
} }

View File

@ -3,7 +3,7 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
@ -11,10 +11,13 @@ builder.Services.AddCors(setup =>
{ {
setup.AddDefaultPolicy(policyBuilder => 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(); //builder.Services.AddSwaggerGen();
var app = builder.Build(); var app = builder.Build();