forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHomeController.cs
99 lines (89 loc) · 3.11 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
// For clarity, this sample has all code in the one file. In a real project, you'd put every
// class in a separate file.
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using React.Sample.CoreMvc.Models;
using React.Sample.CoreMvc.ViewModels;
namespace React.Sample.CoreMvc.Models
{
public class AuthorModel
{
public string Name { get; set; }
public string GithubUsername { get; set; }
}
public class CommentModel
{
public AuthorModel Author { get; set; }
public int Id { get; set; }
public string Text { get; set; }
}
}
namespace React.Sample.CoreMvc.ViewModels
{
public class IndexViewModel
{
public IEnumerable<CommentModel> Comments { get; set; }
public int CommentsPerPage { get; set; }
public bool ThrowRenderError { get; set; }
}
}
namespace React.Sample.CoreMvc.Controllers
{
public class HomeController : Controller
{
private const int COMMENTS_PER_PAGE = 3;
private readonly IDictionary<string, AuthorModel> _authors;
private readonly IList<CommentModel> _comments;
public HomeController()
{
// In reality, you would use a repository or something for fetching data
// For clarity, we'll just use a hard-coded list.
_authors = new Dictionary<string, AuthorModel>
{
{"daniel", new AuthorModel { Name = "Daniel Lo Nigro", GithubUsername = "Daniel15" }},
{"vjeux", new AuthorModel { Name = "Christopher Chedeau", GithubUsername = "vjeux" }},
{"cpojer", new AuthorModel { Name = "Christoph Pojer", GithubUsername = "cpojer" }},
{"jordwalke", new AuthorModel { Name = "Jordan Walke", GithubUsername = "jordwalke" }},
{"zpao", new AuthorModel { Name = "Paul O'Shannessy", GithubUsername = "zpao" }},
};
_comments = new List<CommentModel>
{
new CommentModel { Id = 1, Author = _authors["daniel"], Text = "First!!!!111!" },
new CommentModel { Id = 2, Author = _authors["zpao"], Text = "React is awesome!" },
new CommentModel { Id = 3, Author = _authors["cpojer"], Text = "Awesome!" },
new CommentModel { Id = 4, Author = _authors["vjeux"], Text = "Hello World" },
new CommentModel { Id = 5, Author = _authors["daniel"], Text = "Foo" },
new CommentModel { Id = 6, Author = _authors["daniel"], Text = "Bar" },
new CommentModel { Id = 7, Author = _authors["daniel"], Text = "FooBarBaz" },
};
}
public IActionResult Index()
{
return View(new IndexViewModel
{
Comments = _comments.Take(COMMENTS_PER_PAGE),
CommentsPerPage = COMMENTS_PER_PAGE,
ThrowRenderError = Request.Query.ContainsKey("throwRenderError"),
});
}
public IActionResult Comments(int page)
{
var comments = _comments.Skip((page - 1) * COMMENTS_PER_PAGE).Take(COMMENTS_PER_PAGE);
var hasMore = page * COMMENTS_PER_PAGE < _comments.Count;
return Json(new
{
comments = comments,
hasMore = hasMore
});
}
}
}