1+ /*
2+ * Copyright (c) 2014-2015, Facebook, Inc.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-style license found in the
6+ * LICENSE file in the root directory of this source tree. An additional grant
7+ * of patent rights can be found in the PATENTS file in the same directory.
8+ */
9+
10+ using System ;
11+ using System . Collections . Generic ;
12+ using System . Linq ;
13+ using System . Threading . Tasks ;
14+
15+ using Microsoft . Owin ;
16+
17+ using Newtonsoft . Json ;
18+
19+ using React . Sample . Owin . Models ;
20+
21+ namespace React . Sample . Owin . Models
22+ {
23+ public class AuthorModel
24+ {
25+ public string Name { get ; set ; }
26+ public string Facebook { get ; set ; }
27+ }
28+ public class CommentModel
29+ {
30+ public AuthorModel Author { get ; set ; }
31+ public string Text { get ; set ; }
32+ }
33+ }
34+
35+ namespace React . Sample . Owin
36+ {
37+ internal class CommentsMiddleware
38+ {
39+ private const int COMMENTS_PER_PAGE = 3 ;
40+
41+ private readonly Func < IDictionary < string , object > , Task > _next ;
42+ private readonly List < CommentModel > _comments ;
43+
44+ public CommentsMiddleware ( Func < IDictionary < string , object > , Task > next )
45+ {
46+ _next = next ;
47+
48+ // In reality, you would use a repository or something for fetching data
49+ // For clarity, we'll just use a hard-coded list.
50+ var authors = new Dictionary < string , AuthorModel >
51+ {
52+ { "daniel" , new AuthorModel { Name = "Daniel Lo Nigro" , Facebook = "daaniel" } } ,
53+ { "vjeux" , new AuthorModel { Name = "Christopher Chedeau" , Facebook = "vjeux" } } ,
54+ { "cpojer" , new AuthorModel { Name = "Christoph Pojer" , Facebook = "cpojer" } } ,
55+ { "jordwalke" , new AuthorModel { Name = "Jordan Walke" , Facebook = "jordwalke" } } ,
56+ { "zpao" , new AuthorModel { Name = "Paul O'Shannessy" , Facebook = "zpao" } } ,
57+ } ;
58+
59+ _comments = new List < CommentModel >
60+ {
61+ new CommentModel { Author = authors [ "daniel" ] , Text = "First!!!!111!" } ,
62+ new CommentModel { Author = authors [ "zpao" ] , Text = "React is awesome!" } ,
63+ new CommentModel { Author = authors [ "cpojer" ] , Text = "Awesome!" } ,
64+ new CommentModel { Author = authors [ "vjeux" ] , Text = "Hello World" } ,
65+ new CommentModel { Author = authors [ "daniel" ] , Text = "Foo" } ,
66+ new CommentModel { Author = authors [ "daniel" ] , Text = "Bar" } ,
67+ new CommentModel { Author = authors [ "daniel" ] , Text = "FooBarBaz" } ,
68+ } ;
69+ }
70+
71+ public async Task Invoke ( IDictionary < string , object > environment )
72+ {
73+ var context = new OwinContext ( environment ) ;
74+
75+ // Determine if this middleware should handle the request
76+ if ( ! context . Request . Path . Value . StartsWith ( "/comments/page-" ) || context . Request . Method != "GET" )
77+ {
78+ await _next ( environment ) ;
79+ return ;
80+ }
81+
82+ // prepare the response data
83+ int page = int . Parse ( context . Request . Path . Value . Replace ( "/comments/page-" , string . Empty ) ) ;
84+ var responseObject = new
85+ {
86+ comments = _comments . Skip ( ( page - 1 ) * COMMENTS_PER_PAGE ) . Take ( COMMENTS_PER_PAGE ) ,
87+ hasMore = page * COMMENTS_PER_PAGE < _comments . Count
88+ } ;
89+
90+ var json = await Task . Factory . StartNew ( ( ) => JsonConvert . SerializeObject ( responseObject ) ) ;
91+
92+ await context . Response . WriteAsync ( json ) ;
93+ }
94+ }
95+ }
0 commit comments