Skip to content

Instantly share code, notes, and snippets.

@nonotest
Created October 5, 2017 06:59
Show Gist options
  • Save nonotest/d0c81ad441c93e5ee46b534d8195696f to your computer and use it in GitHub Desktop.
Save nonotest/d0c81ad441c93e5ee46b534d8195696f to your computer and use it in GitHub Desktop.
other calls
using System.Net;
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Http.Headers;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
var successful =true;
try
{
switch(req.Method.ToString()) {
case "GET":
var getFidget = await req.Content.ReadAsAsync<GetFidgetRequest>();
var fidgets = getOneFidget(getFidget, log);
var resp = req.CreateResponse(HttpStatusCode.OK, fidgets);
return resp;
case "PUT":
var fidgett = await req.Content.ReadAsAsync<Fidget>();
updateFidget(fidgett, log);
var resppp = req.CreateResponse(HttpStatusCode.OK, fidgett);
return resppp;
case "DELETE":
var fidgReq = await req.Content.ReadAsAsync<GetFidgetRequest>();
var fidg = deleteFidget(fidgReq, log);
var respppq = req.CreateResponse(HttpStatusCode.OK, fidg);
return respppq;
}
}
catch(Exception e)
{
successful=false;
log.Info(e.ToString());
}
return !successful
? req.CreateResponse(HttpStatusCode.BadRequest, "Unable to process your request! wrong")
: req.CreateResponse(HttpStatusCode.OK, "Logged in!");
}
public static Fidget getOneFidget(GetFidgetRequest req, TraceWriter log) {
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
var f = new Fidget();
using(var connection = new SqlConnection(cnnString))
{
try
{
connection.Open();
// insert a log to the database
var sql = "SELECT * FROM [dbo].[fidgets] WHERE id = " + req.fidgetId;
SqlCommand comm = new SqlCommand(sql, connection);
var reader = comm.ExecuteReader();
while (reader.Read()) {
f = new Fidget { id = reader.GetInt32(0), name = reader.GetString(1), type = reader.GetString(2), rating = reader.GetInt32(3) };
}
connection.Close();
}
catch(Exception e)
{
log.Info(e.ToString());
}
}
return f;
}
public static Fidget deleteFidget(GetFidgetRequest req, TraceWriter log) {
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
var fidgets = getOneFidget(req, log);
using(var connection = new SqlConnection(cnnString))
{
try
{
connection.Open();
var command = connection.CreateCommand();
// insert a log to the database
command.CommandText = "DELETE [dbo].[Fidgets] Where id = @id";
command.Parameters.AddWithValue("@id", req.fidgetId);
connection.Close();
}
catch(Exception e)
{
log.Info(e.ToString());
}
}
return fidgets;
}
public static void updateFidget(Fidget fidget, TraceWriter log) {
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
using(var connection = new SqlConnection(cnnString))
{
try
{
connection.Open();
var command = connection.CreateCommand();
// insert a log to the database
command.CommandText = "UPDATE [dbo].[Fidgets] SET name = @name, rating = @rating, type=@type Where id = @id";
command.Parameters.AddWithValue("@name", fidget.name);
command.Parameters.AddWithValue("@type", fidget.type);
command.Parameters.AddWithValue("@rating", fidget.rating);
command.Parameters.AddWithValue("@id", fidget.id);
connection.Close();
}
catch(Exception e)
{
log.Info(e.ToString());
}
}
}
public class Fidget
{
public string name{get;set;}
public string type{get;set;}
public int id{get;set;}
public int rating{get;set;}
}
public class GetFidgetRequest
{
public string fidgetId{get;set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment