forked from linezero/NETCoreBBS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeController.cs
More file actions
83 lines (79 loc) · 2.78 KB
/
Copy pathHomeController.cs
File metadata and controls
83 lines (79 loc) · 2.78 KB
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
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NetCoreBBS.Infrastructure;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using NetCoreBBS.ViewModels;
using Microsoft.AspNetCore.Mvc.Rendering;
using NetCoreBBS.Entities;
using NetCoreBBS.Interfaces;
namespace NetCoreBBS.Controllers
{
public class HomeController : Controller
{
private ITopicRepository _topic;
private IRepository<TopicNode> _node;
public UserManager<User> UserManager { get; }
public HomeController(ITopicRepository topic, IRepository<TopicNode> node, UserManager<User> userManager)
{
_topic = topic;
_node = node;
UserManager = userManager;
}
public IActionResult Index([FromServices]IUserServices userServices)
{
var pagesize = 20;
var pageindex = 1;
Page<Topic> result = null ;
if (!string.IsNullOrEmpty(Request.Query["page"]))
pageindex = Convert.ToInt32(Request.Query["page"]);
if (!string.IsNullOrEmpty(Request.Query["s"]))
result = _topic.PageList(r => r.Title.Contains(Request.Query["s"]), pagesize, pageindex);
else
result = _topic.PageList(pagesize, pageindex);
ViewBag.Topics = result.List.Select(r=>new TopicViewModel
{
Id = r.Id,
NodeId = r.Node.Id,
NodeName = r.Node.Name,
UserName = r.User.UserName,
Avatar=r.User.Avatar,
Title = r.Title,
Top = r.Top,
Type=r.Type,
ReplyCount = r.ReplyCount,
LastReplyTime = r.LastReplyTime,
CreateOn = r.CreateOn
}).ToList();
ViewBag.PageIndex = pageindex;
ViewBag.PageCount = result.GetPageCount();
ViewBag.User = userServices.User.Result;
var nodes = _node.List().ToList();
ViewBag.Nodes = nodes;
ViewBag.NodeListItem = nodes.Where(r => r.ParentId != 0).Select(r => new SelectListItem { Value = r.Id.ToString(), Text = r.Name });
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(Topic topic)
{
if (ModelState.IsValid)
{
topic.CreateOn = DateTime.Now;
topic.Type = TopicType.Normal;
_topic.Add(topic);
}
return RedirectToAction("Index");
}
public IActionResult About()
{
ViewData["Message"] = ".NET Core 版轻论坛";
return View();
}
public IActionResult Error()
{
return View();
}
}
}