-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathTopicController.cs
More file actions
95 lines (92 loc) · 3.54 KB
/
Copy pathTopicController.cs
File metadata and controls
95 lines (92 loc) · 3.54 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
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreBBS.Infrastructure;
using NetCoreBBS.Entities;
using NetCoreBBS.Interfaces;
using NetCoreBBS.ViewModels;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace NetCoreBBS.Controllers
{
public class TopicController : Controller
{
private ITopicRepository _topic;
private IRepository<TopicNode> _node;
private ITopicReplyRepository _reply;
public TopicController(ITopicRepository topic, IRepository<TopicNode> node, ITopicReplyRepository reply)
{
_topic = topic;
_node = node;
_reply = reply;
}
// GET: /Topic/1
[Route("/Topic/{id}")]
public IActionResult Index(int id)
{
if (id <= 0) return Redirect("/");
var topic = _topic.GetById(id);
if (topic == null) return Redirect("/");
var replys = _reply.List(r => r.TopicId == id).ToList();
topic.ViewCount += 1;
_topic.Edit(topic);
ViewBag.Replys = replys;
return View(topic);
}
[HttpPost]
[ValidateAntiForgeryToken]
[Route("/Topic/{id}")]
public IActionResult Index([Bind("TopicId,ReplyUserId,ReplyEmail,ReplyContent")]TopicReply reply)
{
if (ModelState.IsValid&&!string.IsNullOrEmpty(reply.ReplyContent))
{
reply.CreateOn = DateTime.Now;
_reply.Add(reply);
var topic = _topic.GetById(reply.TopicId);
topic.LastReplyUserId = reply.ReplyUserId;
topic.LastReplyTime = reply.CreateOn;
topic.ReplyCount += 1;
_topic.Edit(topic);
}
return RedirectToAction("Index", "Topic", new { Id = reply.TopicId });
}
[Route("/Topic/Node/{name}")]
public IActionResult Node(string name)
{
if (string.IsNullOrEmpty(name)) return Redirect("/");
var node = _node.List(r => r.NodeName == name).FirstOrDefault();
if (node == null)
node= _node.GetById(Convert.ToInt32(name));
if (node == null) return Redirect("/");
var pagesize = 20;
var pageindex = 1;
Page<Topic> result;
if (!string.IsNullOrEmpty(Request.Query["page"]))
pageindex = Convert.ToInt32(Request.Query["page"]);
if (!string.IsNullOrEmpty(Request.Query["s"]))
result = _topic.PageList(r => r.NodeId == node.Id && r.Title.Contains(Request.Query["s"]),pagesize,pageindex);
else
result = _topic.PageList(r => r.NodeId == node.Id, 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.Node = node;
ViewBag.Count = result.Total;
return View();
}
}
}