Sunday, 4 October 2020

CRUD operations using Azure Function

 


using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace FunctionApp1
{
    public static class Function1
    {
        public static readonly List<EMP> lst = new List<EMP>();
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function,"post", Route = "Save")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Receive new employee request.");
            IActionResult returnValue = null;
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            EMP emp = JsonConvert.DeserializeObject<EMP>(requestBody);
            try
            {
               if(emp!=null)
                {
                    lst.Add(new EMP { EID = emp.EID, NAME = emp.NAME });
                    returnValue = new OkObjectResult("Data Saved.");
                }
               else
                {
                    returnValue = new StatusCodeResult(StatusCodes.Status400BadRequest);
                }

            }
            catch (Exception ex)
            {
                log.LogError($"Exception thrown: {ex.Message}");
                returnValue = new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }
            return returnValue;
        }
        [FunctionName("Function2")]
        public static async Task<IActionResult> Run2(
           [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Gets")] HttpRequest req,
           ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            ////dynamic data = JsonConvert.DeserializeObject(requestBody);
            ////name = name ?? data?.name;

            ////string responseMessage = string.IsNullOrEmpty(name)
            ////    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            ////    : $"Hello, {name}. This HTTP triggered function executed successfully.";
            //EMP emp = JsonConvert.DeserializeObject<EMP>(requestBody);
            //lst.Add(new EMP { EID = emp.EID, NAME = emp.NAME });
            return new OkObjectResult(lst);
        }
        [FunctionName("Function3")]
        public static async Task<IActionResult> Run3(
           [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Get/{id:int}")] HttpRequest req,
           ILogger log,int id)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            ////dynamic data = JsonConvert.DeserializeObject(requestBody);
            ////name = name ?? data?.name;

            ////string responseMessage = string.IsNullOrEmpty(name)
            ////    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            ////    : $"Hello, {name}. This HTTP triggered function executed successfully.";
            //EMP emp = JsonConvert.DeserializeObject<EMP>(requestBody);
            //lst.Add(new EMP { EID = emp.EID, NAME = emp.NAME });
            EMP emp = lst.FirstOrDefault(m => m.EID == id);
            return new OkObjectResult(emp);
        }
        [FunctionName("Function4")]
        public static async Task<IActionResult> Run4(
           [HttpTrigger(AuthorizationLevel.Function, "put", Route = "Update/{id:int}")] HttpRequest req,
           ILogger log,int id)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            //dynamic data = JsonConvert.DeserializeObject(requestBody);
            //name = name ?? data?.name;

            //string responseMessage = string.IsNullOrEmpty(name)
            //    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            //    : $"Hello, {name}. This HTTP triggered function executed successfully.";
            EMP emp = JsonConvert.DeserializeObject<EMP>(requestBody);
            lst.Remove(lst.FirstOrDefault(m=>m.EID==id));
            lst.Add(new EMP { EID = emp.EID, NAME = emp.NAME });
            return new OkObjectResult("Data updated.");
        }
        [FunctionName("Function5")]
        public static async Task<IActionResult> Run5(
           [HttpTrigger(AuthorizationLevel.Function, "delete", Route = "delete/{id:int}")] HttpRequest req,
           ILogger log, int id)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            ////dynamic data = JsonConvert.DeserializeObject(requestBody);
            ////name = name ?? data?.name;

            ////string responseMessage = string.IsNullOrEmpty(name)
            ////    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            ////    : $"Hello, {name}. This HTTP triggered function executed successfully.";
            //EMP emp = JsonConvert.DeserializeObject<EMP>(requestBody);
            //lst.Add(new EMP { EID = emp.EID, NAME = emp.NAME });
             lst.Remove(lst.FirstOrDefault(m=>m.EID==id));
            return new OkObjectResult("Data Deleted.");
        }
    }
}




how to add css for mobile

.text-panel{
 margin-top:-150px ;
 @include respond-below(medium) {
  margin-top:-10px;
}
}