Beispiel #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("Customer Insertion begins");

            HttpResponseMessage response;

            try
            {
                var customer = await req.Content.ReadAsAsync <Customer>();

                if (null != customer)
                {
                    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebapiDBEntities"].ConnectionString;
                    using (var db = new WebapiDBEntities(connectionString))
                    {
                        db.Customers.Add(customer);
                        await db.SaveChangesAsync();
                    }
                    response = req.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    throw new Exception("Failed to serialize object!");
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                response = req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return(response);
        }
Beispiel #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("Customer View begins");

            List <CustomerDetail> customers = null;
            string jsonValue = "";
            HttpResponseMessage response;

            try
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebapiDBEntities"].ConnectionString;
                using (var db = new WebapiDBEntities(connectionString))
                {
                    customers = db.Customers.Select(s => new CustomerDetail
                    {
                        FirstName   = s.FirstName,
                        LastName    = s.LastName,
                        Address     = s.Address,
                        Phonenumber = s.Phonenumber.ToString()
                    }).ToList();
                    jsonValue = JsonConvert.SerializeObject(customers);
                }
                response = req.CreateResponse(HttpStatusCode.OK, jsonValue);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.InnerException.Message);
                response = req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return(response);
        }
Beispiel #3
0
 public CustomerData(string con)
 {
     _dbContext = new WebapiDBEntities(con);
 }