Created
October 5, 2017 06:57
-
-
Save nonotest/bce5dd5b61a0f213ca307e221650c600 to your computer and use it in GitHub Desktop.
headers for admin (x total count)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ | |
log.Info("VERB: " + req.Method.ToString()); | |
switch(req.Method.ToString()) { | |
case "GET": | |
var getFidget = await req.Content.ReadAsAsync<GetFidgetRequest>(); | |
var fidgets = getFidgets(getFidget, log); | |
var resp = req.CreateResponse(HttpStatusCode.OK, fidgets); | |
resp.Content.Headers.Add("X-Total-Count", "" + fidgets.Length); | |
resp.Content.Headers.Add("Access-Control-Expose-Headers", "X-Total-Count"); | |
return resp; | |
case "POST": | |
var fidget = await req.Content.ReadAsAsync<Fidget>(); | |
insertFidget(fidget, log); | |
var respp = req.CreateResponse(HttpStatusCode.OK, fidget); | |
return respp; | |
} | |
} | |
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 void insertFidget(Fidget fidget, TraceWriter log) { | |
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString; | |
using(var connection = new SqlConnection(cnnString)) | |
{ | |
try | |
{ | |
connection.Open(); | |
// insert a log to the database | |
connection.Execute("INSERT INTO [dbo].[Fidgets] (name, type, rating, id) VALUES (@name, @type,@rating,@id)", fidget); | |
connection.Close(); | |
} | |
catch(Exception e) | |
{ | |
log.Info(e.ToString()); | |
} | |
} | |
} | |
public static Fidget[] getFidgets(GetFidgetRequest req, TraceWriter log) { | |
Fidget[] allRecords = null; | |
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString; | |
using(var connection = new SqlConnection(cnnString)) | |
{ | |
try | |
{ | |
connection.Open(); | |
// insert a log to the database | |
var sql = "SELECT * FROM [dbo].[fidgets]"; | |
SqlCommand comm = new SqlCommand(sql, connection); | |
var list = new List<Fidget>(); | |
var reader = comm.ExecuteReader(); | |
while (reader.Read()) { | |
list.Add(new Fidget { id = reader.GetInt32(0), name = reader.GetString(1), type = reader.GetString(2), rating = reader.GetInt32(3) }); | |
} | |
allRecords = list.ToArray(); | |
connection.Close(); | |
} | |
catch(Exception e) | |
{ | |
log.Info(e.ToString()); | |
} | |
} | |
return allRecords; | |
} | |
public class UserRequest | |
{ | |
public string Username{get;set;} | |
public string Password{get;set;} | |
} | |
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 Sort{get;set;} | |
public string Order{get;set;} | |
public string Start{get;set;} | |
public string End{get;set;} //ee | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment