1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/features/json/reactjs/dotnet-comments-app/Controllers/commentsController.cs
Jovan Popovic c1b729cd6d ReactJS app moved to different folder
ReactJS app is moved to reactjs/dotnet-comment-app. Removed
project.lock.json file from source control.
2016-10-29 09:14:27 -07:00

40 lines
1.2 KiB
C#

using Belgrade.SqlClient;
using Microsoft.AspNetCore.Mvc;
using System.Data.SqlClient;
using System.IO;
using System.Threading.Tasks;
namespace ReactCommentsApp.Controllers
{
[Route("api/[controller]")]
public class commentsController : Controller
{
private readonly IQueryPipe SqlPipe;
private readonly ICommand SqlCommand;
public commentsController(ICommand sqlCommand, IQueryPipe sqlPipe)
{
this.SqlCommand = sqlCommand;
this.SqlPipe = sqlPipe;
}
// GET api/comment
[HttpGet]
public async Task Get()
{
await SqlPipe.Stream("select * from Comments FOR JSON PATH", Response.Body, "[]");
}
// POST api/comment
[HttpPost]
public async Task Post(string author, string text)
{
string comment = new StreamReader(Request.Body).ReadToEnd();
var cmd = new SqlCommand( "insert into Comments values (@author, @text)");
cmd.Parameters.AddWithValue("author", author);
cmd.Parameters.AddWithValue("text", text);
await SqlCommand.ExecuteNonQuery(cmd);
}
}
}