9
9
using System ;
10
10
using Microsoft . Extensions . DependencyInjection ;
11
11
using BooksServiceSample . Services ;
12
+ using BooksServiceSample . Models ;
12
13
13
14
namespace BookFunctionApp
14
15
{
@@ -18,6 +19,7 @@ static BookFunction()
18
19
{
19
20
ConfigureServices ( ) ;
20
21
FeedSampleChapters ( ) ;
22
+ GetRequiredServices ( ) ;
21
23
}
22
24
23
25
private static void FeedSampleChapters ( )
@@ -34,6 +36,15 @@ private static void ConfigureServices()
34
36
ApplicationServices = services . BuildServiceProvider ( ) ;
35
37
}
36
38
39
+ private static void GetRequiredServices ( )
40
+ {
41
+ s_bookChaptersService =
42
+ ApplicationServices . GetRequiredService < IBookChaptersService > ( ) ;
43
+ }
44
+
45
+ private static IBookChaptersService s_bookChaptersService ;
46
+
47
+
37
48
public static IServiceProvider ApplicationServices { get ; private set ; }
38
49
39
50
[ FunctionName ( "BookFunction" ) ]
@@ -48,8 +59,10 @@ public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get"
48
59
result = DoGet ( req ) ;
49
60
break ;
50
61
case "POST" :
62
+ result = DoPost ( req ) ;
51
63
break ;
52
64
case "PUT" :
65
+ result = DoPut ( req ) ;
53
66
break ;
54
67
default :
55
68
break ;
@@ -73,5 +86,23 @@ private static IActionResult DoGet(HttpRequest req)
73
86
var chapters = bookChapterService . GetAll ( ) ;
74
87
return new OkObjectResult ( chapters ) ;
75
88
}
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
+
76
107
}
77
108
}
0 commit comments