Guest User

Azure Function Test

a guest
Oct 12th, 2020
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. #r "Newtonsoft.Json"
  2.  
  3. using System.Net;
  4. using System.Linq;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Primitives;
  7. using Newtonsoft.Json;
  8.  
  9. public class AuctionItem
  10. {
  11.     [JsonProperty("itemID")]
  12.     public string itemID { get; set; }
  13.    
  14.     [JsonProperty("price")]
  15.     public string Price { get; set; }
  16.    
  17.     [JsonProperty("amount")]
  18.     public string Amount { get; set; }
  19. }
  20.  
  21. public static IActionResult Run(HttpRequest req, out object outputDocument,
  22. IEnumerable<AuctionItem> inputDocument, ILogger log)
  23. {
  24.     log.LogInformation("C# HTTP trigger function processed a request.");
  25.  
  26.     string name = req.Query["name"];
  27.     string price = req.Query["price"];
  28.     string amount = req.Query["amount"];
  29.  
  30.     string command = req.Query["command"];
  31.  
  32.     outputDocument = null;
  33.  
  34.     if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(price) && !string.IsNullOrEmpty(amount)) {
  35.         string responseMessage = "{\"Message\":\"Success\",\n\"Data\": [" + "\n";
  36.                
  37.         if (!string.IsNullOrEmpty(command)) {
  38.             if (command == "1") {
  39.                 foreach (var item in inputDocument) {
  40.                     responseMessage += "{\n\t\"itemID\":\"" + item.itemID + "\"," + "\n";
  41.                     responseMessage += "\t\"price\":\"" + item.Price + "\"," + "\n";
  42.                     responseMessage += "\t\"amount\":\"" + item.Amount + "\"}," + "\n";
  43.                 }
  44.             } else if (command == "2") {
  45.                 var item = inputDocument.Where(x => x.itemID == name).FirstOrDefault();
  46.                 if (item != null) {
  47.                     inputDocument = inputDocument.Where(x => x != item);
  48.                 }
  49.             } else if (command == "3") {
  50.                 responseMessage += "{\n\t\"itemID\":\"" + name + "\",\n";
  51.                 responseMessage += "\t\"price\":\"" + price + "\",\n";
  52.                 responseMessage += "\t\"amount\":\"" + amount + "\"}\n";
  53.                
  54.  
  55.                 log.LogInformation(responseMessage);
  56.                 outputDocument = new {
  57.                     itemID = name,
  58.                     price = price,
  59.                     amount = amount
  60.                 };
  61.             }
  62.         }
  63.        
  64.         responseMessage += "]}";
  65.         return new OkObjectResult(responseMessage);
  66.     } else {
  67.         outputDocument = null;
  68.         return new BadRequestResult();
  69.     }
  70.            
  71. }
  72.  
Add Comment
Please, Sign In to add comment