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+ } 
0 commit comments