99using System ;
1010using Microsoft . Extensions . DependencyInjection ;
1111using BooksServiceSample . Services ;
12+ using BooksServiceSample . Models ;
1213
1314namespace BookFunctionApp
1415{
@@ -18,6 +19,7 @@ static BookFunction()
1819 {
1920 ConfigureServices ( ) ;
2021 FeedSampleChapters ( ) ;
22+ GetRequiredServices ( ) ;
2123 }
2224
2325 private static void FeedSampleChapters ( )
@@ -34,6 +36,15 @@ private static void ConfigureServices()
3436 ApplicationServices = services . BuildServiceProvider ( ) ;
3537 }
3638
39+ private static void GetRequiredServices ( )
40+ {
41+ s_bookChaptersService =
42+ ApplicationServices . GetRequiredService < IBookChaptersService > ( ) ;
43+ }
44+
45+ private static IBookChaptersService s_bookChaptersService ;
46+
47+
3748 public static IServiceProvider ApplicationServices { get ; private set ; }
3849
3950 [ FunctionName ( "BookFunction" ) ]
@@ -48,8 +59,10 @@ public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get"
4859 result = DoGet ( req ) ;
4960 break ;
5061 case "POST" :
62+ result = DoPost ( req ) ;
5163 break ;
5264 case "PUT" :
65+ result = DoPut ( req ) ;
5366 break ;
5467 default :
5568 break ;
@@ -73,5 +86,23 @@ private static IActionResult DoGet(HttpRequest req)
7386 var chapters = bookChapterService . GetAll ( ) ;
7487 return new OkObjectResult ( chapters ) ;
7588 }
89+
90+ private static IActionResult DoPost ( HttpRequest req )
91+ {
92+ string json = new StreamReader ( req . Body ) . ReadToEnd ( ) ;
93+ BookChapter chapter = JsonConvert . DeserializeObject < BookChapter > ( json ) ;
94+ s_bookChaptersService . Add ( chapter ) ;
95+ return new OkResult ( ) ;
96+ }
97+
98+
99+ private static IActionResult DoPut ( HttpRequest req )
100+ {
101+ string json = new StreamReader ( req . Body ) . ReadToEnd ( ) ;
102+ BookChapter chapter = JsonConvert . DeserializeObject < BookChapter > ( json ) ;
103+ s_bookChaptersService . Update ( chapter ) ;
104+ return new OkResult ( ) ;
105+ }
106+
76107 }
77108}
0 commit comments