Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 93ddbec

Browse files
committed
User Info
add user center
1 parent 0e1aca0 commit 93ddbec

File tree

14 files changed

+299
-18
lines changed

14 files changed

+299
-18
lines changed

‎src/Infrastructure/Repositorys/TopicRepository.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public override Topic GetById(int id)
2222
{
2323
return _dbContext.Topics.Include(r => r.User).Include(r => r.Node).Include(r => r.LastReplyUser).FirstOrDefault(r => r.Id == id);
2424
}
25+
public override IEnumerable<Topic> List(Expression<Func<Topic, bool>> predicate)
26+
{
27+
return _dbContext.Topics.Include(r => r.Node).Where(predicate);
28+
}
2529

2630
public Page<Topic> PageList(int pagesize=20, int pageindex=1)
2731
{
@@ -30,7 +34,7 @@ public Page<Topic> PageList(int pagesize=20, int pageindex=1)
3034

3135
public Page<Topic> PageList(Expression<Func<Topic, bool>> predicate, int pagesize=20, int pageindex=1)
3236
{
33-
var topics = _dbContext.Topics.Include(r=>r.User).Include(r=>r.Node).Include(r=>r.LastReplyUser).AsQueryable();
37+
var topics = _dbContext.Topics.Include(r=>r.User).Include(r=>r.Node).Include(r=>r.LastReplyUser).AsQueryable().AsNoTracking();
3438
if (predicate != null)
3539
{
3640
topics=topics.Where(predicate);

‎src/NetCoreBBS/Controllers/HomeController.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public IActionResult Index([FromServices]IUserServices user)
3939
NodeId = r.Node.Id,
4040
NodeName = r.Node.Name,
4141
UserName = r.User.UserName,
42+
Avatar=r.User.Avatar,
4243
Title = r.Title,
4344
Top = r.Top,
4445
Type=r.Type,

‎src/NetCoreBBS/Controllers/TopicController.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public IActionResult Node(string name)
7777
NodeId = r.Node.Id,
7878
NodeName = r.Node.Name,
7979
UserName = r.User.UserName,
80+
Avatar=r.User.Avatar,
8081
Title = r.Title,
8182
Top = r.Top,
8283
Type = r.Type,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using NetCoreBBS.Interfaces;
7+
using NetCoreBBS.Entities;
8+
using Microsoft.AspNetCore.Authorization;
9+
using NetCoreBBS.ViewModels;
10+
using Microsoft.AspNetCore.Identity;
11+
using Microsoft.AspNetCore.Hosting;
12+
using System.IO;
13+
14+
namespace NetCoreBBS.Controllers
15+
{
16+
[Authorize]
17+
public class UserController : Controller
18+
{
19+
private ITopicRepository _topic;
20+
private ITopicReplyRepository _reply;
21+
private UserManager<User> UserManager;
22+
private IHostingEnvironment _env;
23+
public UserController(ITopicRepository topic, ITopicReplyRepository reply, UserManager<User> userManager, IHostingEnvironment env)
24+
{
25+
_topic = topic;
26+
_reply = reply;
27+
UserManager = userManager;
28+
_env = env;
29+
}
30+
public IActionResult Index()
31+
{
32+
var u = UserManager.GetUserAsync(User).Result;
33+
var topics = _topic.List(r => r.UserId == u.Id).ToList();
34+
var replys = _reply.List(r => r.ReplyUserId == u.Id).ToList();
35+
ViewBag.Topics = topics;
36+
ViewBag.Replys = replys;
37+
return View(u);
38+
}
39+
40+
public IActionResult Edit()
41+
{
42+
return View(UserManager.GetUserAsync(User).Result);
43+
}
44+
45+
[HttpPost]
46+
[ValidateAntiForgeryToken]
47+
public async Task<IActionResult> Edit(UserViewModel usermodel)
48+
{
49+
var user = UserManager.GetUserAsync(User).Result;
50+
if (ModelState.IsValid)
51+
{
52+
if (usermodel.Avatar != null)
53+
{
54+
var avatar = usermodel.Avatar;
55+
if (avatar.Length / 1024 > 100)
56+
{
57+
return Content("Í·ÏñÎÄ1⁄4þ ́óС3¬1ý100KB");
58+
}
59+
var ext = Path.GetExtension(avatar.FileName);
60+
var avatarpath = Path.Combine("images", "avatar", user.Id + ext);
61+
var filepath = Path.Combine(_env.WebRootPath, avatarpath);
62+
using (FileStream fs = new FileStream(filepath, FileMode.Create))
63+
{
64+
avatar.CopyTo(fs);
65+
fs.Flush();
66+
}
67+
user.Avatar = avatarpath;
68+
}
69+
user.Email = usermodel.Email;
70+
user.Url = usermodel.Url;
71+
user.GitHub = usermodel.GitHub;
72+
user.Profile = usermodel.Profile;
73+
await UserManager.UpdateAsync(user);
74+
return RedirectToAction("Index");
75+
}
76+
return View(user);
77+
}
78+
}
79+
}

‎src/NetCoreBBS/NetCoreBBS.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@
5454
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
5555
</ItemGroup>
5656

57+
<ItemGroup>
58+
<Folder Include="wwwroot\images\avatar\" />
59+
</ItemGroup>
60+
5761
</Project>

‎src/NetCoreBBS/ViewModels/TopicViewModel.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class TopicViewModel
1111
public int NodeId { get; set; }
1212
public string NodeName { get; set; }
1313
public string UserName { get; set; }
14+
public string Avatar { get; set; }
1415
public string Title { get; set; }
1516
public int Top { get; set; }
1617
public Entities.TopicType Type { get; set; }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace NetCoreBBS.ViewModels
8+
{
9+
public class UserViewModel
10+
{
11+
public string Email { get; set; }
12+
public IFormFile Avatar { get; set; }
13+
public string Profile { get; set; }
14+
public string Url { get; set; }
15+
public string GitHub { get; set; }
16+
}
17+
}

‎src/NetCoreBBS/Views/Home/Index.cshtml‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{
2323
<div class="topic">
2424
<div class="avatar media-left">
25-
<a href="#"><img class="avatar-48" src="~/images/96.png" alt="96"></a>
25+
<a href="#"><img class="avatar-48" src="@(string.IsNullOrEmpty(item.Avatar)?"/images/96.png":"/"+item.Avatar)" alt="96"></a>
2626
</div>
2727
<div class="media-body">
2828
<div class="title">
@@ -31,7 +31,7 @@
3131
</a>
3232
</div>
3333
<div class="info">
34-
<a href="/topic/node/@item.NodeId">@item.NodeName</a> <label>@item.UserName</label>发布于 @item.CreateOn.ToString("MM-dd HH:mm")
34+
<a href="/topic/node/@item.NodeId">@item.NodeName</a> &nbsp;<label>@item.UserName</label>&nbsp;发布于 @item.CreateOn.ToString("MM-dd HH:mm")
3535
@if (item.LastReplyTime != DateTime.MinValue)
3636
{
3737
<label>最后回复 @item.LastReplyTime.ToString("MM-dd HH:mm")</label>
@@ -87,9 +87,7 @@
8787
}
8888
else
8989
{
90-
<a class="btn btn-primary btn-block" href="/topic/new">发布新话题</a>
91-
<p>需要<a asp-controller="Account" asp-action="Login" class="btn btn-primary">登录</a>发布新话题,
92-
如果你还没有账号请点击这里 <a asp-controller="Account" asp-action="Register" class="btn btn-danger">注册</a>。</p>
90+
<a class="btn btn-primary btn-block" asp-controller="Account" asp-action="Login">发布新话题</a>
9391
}
9492
</div>
9593
<div id="sections" class="panel panel-default">

‎src/NetCoreBBS/Views/Shared/_LoginPartial.cshtml‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
<form asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
77
<ul class="nav navbar-nav navbar-right">
88
<li>
9-
@if (UserManager.GetUserName(User).Equals("admin"))
10-
{
11-
<a asp-area="Admin" asp-controller="Manage" asp-action="Index" title="Manage">@UserManager.GetUserName(User)</a>
12-
}
13-
else {
14-
<a href="javascript:;">@UserManager.GetUserName(User)</a>
15-
}
9+
<div class="btn-group" style="margin-top:8px;">
10+
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
11+
@UserManager.GetUserName(User)<span class="caret"></span>
12+
</button>
13+
<ul class="dropdown-menu">
14+
<li><a asp-controller="User" asp-action="Index">个人中心</a></li>
15+
<li><a asp-controller="User" asp-action="Edit">编辑个人信息</a></li>
16+
@if (UserManager.GetUserName(User).Equals("admin"))
17+
{
18+
<li role="separator" class="divider"></li>
19+
<li><a asp-area="Admin" asp-controller="Manage" asp-action="Index">管理中心</a></li>
20+
}
21+
</ul>
22+
</div>
1623
</li>
1724
<li><a href="javascript:document.getElementById('logoutForm').submit()">注销</a></li>
1825
<li><a asp-controller="Home" asp-action="About">关于</a></li>

‎src/NetCoreBBS/Views/Topic/Index.cshtml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</div>
2323
</div>
2424
<div class="media-right">
25-
<a href="#"><img class="media-object avatar-48" src="/images/96.png"></a>
25+
<a href="#"><img class="media-object avatar-48" src="@(string.IsNullOrEmpty(Model.User.Avatar)?"/images/96.png": "/"+Model.User.Avatar)"></a>
2626
</div>
2727
</div>
2828
<div class="panel-body markdown">
@@ -40,7 +40,7 @@
4040
<div class="reply" data-id="@item.Id" id="reply@(i+1)">
4141
<div class="avatar">
4242
<a href="#">
43-
<img class="media-object avatar-48" src="/images/96.png">
43+
<img class="media-object avatar-48" src="@(string.IsNullOrEmpty(item.ReplyUser.Avatar)?"/images/96.png": "/"+item.ReplyUser.Avatar)">
4444
</a>
4545
</div>
4646
<div class="infos">
@@ -53,7 +53,6 @@
5353
</span>
5454
<span class="opts pull-right">
5555
<a title="" data-count="0" data-state="" data-type="Reply" data-id="297575" class="likeable " href="#"><i class="fa fa-thumbs-up"></i> <span></span></a>
56-
<a class="edit fa fa-pencil" data-uid="20859" title="修改回帖" href="/topics/30177/replies/297575/edit"></a>
5756
<a data-floor="1" data-login="adamshen" title="回复此楼" class="btn-reply fa fa-mail-reply" href="#"></a>
5857
</span>
5958
</div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /