From 92da07a772f4ae5ebe423249a69f2a4363d1b91b Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Tue, 6 Mar 2018 08:15:53 -0800 Subject: [PATCH] add missing code --- .../BookFunctionApp/BookFunction.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/API/Sync/BooksServiceSample/BookFunctionApp/BookFunction.cs b/API/Sync/BooksServiceSample/BookFunctionApp/BookFunction.cs index 02305056..0d9ef18d 100644 --- a/API/Sync/BooksServiceSample/BookFunctionApp/BookFunction.cs +++ b/API/Sync/BooksServiceSample/BookFunctionApp/BookFunction.cs @@ -9,6 +9,7 @@ using System; using Microsoft.Extensions.DependencyInjection; using BooksServiceSample.Services; +using BooksServiceSample.Models; namespace BookFunctionApp { @@ -18,6 +19,7 @@ static BookFunction() { ConfigureServices(); FeedSampleChapters(); + GetRequiredServices(); } private static void FeedSampleChapters() @@ -34,6 +36,15 @@ private static void ConfigureServices() ApplicationServices = services.BuildServiceProvider(); } + private static void GetRequiredServices() + { + s_bookChaptersService = + ApplicationServices.GetRequiredService(); + } + + private static IBookChaptersService s_bookChaptersService; + + public static IServiceProvider ApplicationServices { get; private set; } [FunctionName("BookFunction")] @@ -48,8 +59,10 @@ public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get" result = DoGet(req); break; case "POST": + result = DoPost(req); break; case "PUT": + result = DoPut(req); break; default: break; @@ -73,5 +86,23 @@ private static IActionResult DoGet(HttpRequest req) var chapters = bookChapterService.GetAll(); return new OkObjectResult(chapters); } + + private static IActionResult DoPost(HttpRequest req) + { + string json = new StreamReader(req.Body).ReadToEnd(); + BookChapter chapter = JsonConvert.DeserializeObject(json); + s_bookChaptersService.Add(chapter); + return new OkResult(); + } + + + private static IActionResult DoPut(HttpRequest req) + { + string json = new StreamReader(req.Body).ReadToEnd(); + BookChapter chapter = JsonConvert.DeserializeObject(json); + s_bookChaptersService.Update(chapter); + return new OkResult(); + } + } }