package com.liuenci.vblog.controller;
import java.util.List;
import javax.validation.ConstraintViolationException;
import com.liuenci.vblog.pojo.Blog;
import com.liuenci.vblog.pojo.Catalog;
import com.liuenci.vblog.pojo.User;
import com.liuenci.vblog.pojo.Vote;
import com.liuenci.vblog.service.BlogService;
import com.liuenci.vblog.service.CatalogService;
import com.liuenci.vblog.service.UserService;
import com.liuenci.vblog.util.ConstraintViolationExceptionHandler;
import com.liuenci.vblog.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
/**
* 用户主页空间控制器.
* @author liuenci
*/
@Controller
@RequestMapping("/u")
public class UserspaceController {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private UserService userService;
@Autowired
private BlogService blogService;
@Autowired
private CatalogService catalogService;
@GetMapping("{username}")
public String userSpace(@PathVariable("username") String username, Model model) {
User user = (User)userDetailsService.loadUserByUsername(username);
model.addAttribute("user", user);
return "redirect:/u/" + username + "/blogs";
}
@GetMapping("{username}/profile")
@PreAuthorize("authentication.name.equals(#username)")
public ModelAndView profile(@PathVariable("username") String username, Model model) {
User user = (User)userDetailsService.loadUserByUsername(username);
model.addAttribute("user", user);
return new ModelAndView("/userspace/profile", "userModel", model);
}
/**
* 保存个人设置
* @param user
* @return
*/
@PostMapping("{username}/profile")
@PreAuthorize("authentication.name.equals(#username)")
public String saveProfile(@PathVariable("username") String username,User user) {
User originalUser = userService.getUserById(user.getId());
originalUser.setEmail(user.getEmail());
originalUser.setName(user.getName());
// 判断密码是否做了变更
String rawPassword = originalUser.getPassword();
PasswordEncoder encoder = new BCryptPasswordEncoder();
String encodePasswd = encoder.encode(user.getPassword());
boolean isMatch = encoder.matches(rawPassword, encodePasswd);
if (!isMatch) {
originalUser.setEncodePassword(user.getPassword());
}
userService.saveUser(originalUser);
return "redirect:/u/" + username + "/profile";
}
/**
* 获取编辑头像的界面
* @param username
* @param model
* @return
*/
@GetMapping("{username}/avatar")
@PreAuthorize("authentication.name.equals(#username)")
public ModelAndView avatar(@PathVariable("username") String username, Model model) {
User user = (User)userDetailsService.loadUserByUsername(username);
model.addAttribute("user", user);
return new ModelAndView("/userspace/avatar", "userModel", model);
}
/**
* 保存头像
* @param username
* @return
*/
@PostMapping("{username}/avatar")
@PreAuthorize("authentication.name.equals(#username)")
public ResponseEntity<Response> saveAvatar(@PathVariable("username") String username, @RequestBody User user) {
String avatarUrl = user.getAvatar();
User originalUser = userService.getUserById(user.getId());
originalUser.setAvatar(avatarUrl);
userService.saveUser(originalUser);
return ResponseEntity.ok().body(new Response(true, "处理成功", avatarUrl));
}
@GetMapping("{username}/blogs")
public String listBlogsByOrder(@PathVariable("username") String username,
@RequestParam(value="order",required=false,defaultValue="new") String order,
@RequestParam(value="catalog",required=false ) Long catalogId,
@RequestParam(value="keyword",required=false,defaultValue="" ) String keyword,
@RequestParam(value="async",required=false) boolean async,
@RequestParam(value="pageIndex",required=false,defaultValue="0") int pageIndex,
@RequestParam(value="pageSize",required=false,defaultValue="10") int pageSize,
Model model) {
User user = (User)userDetailsService.loadUserByUsername(username);
Page<Blog> page = null;
// 分类查询
if (catalogId != null && catalogId > 0) {
Catalog catalog = catalogService.getCatalogById(catalogId);
Pageable pageable = new PageRequest(pageIndex, pageSize);
page = blogService.listBlogsByCatalog(catalog, pageable);
order = "";
// 最热查询
} else if (order.equals("hot")) {
Sort sort = new Sort(Direction.DESC,"readSize","commentSize","voteSize");
Pageable pageable = new PageRequest(pageIndex, pageSize, sort);
page = blogService.listBlogsByTitleVoteAndSort(user, keyword, pageable);
// 最新查询
} else if (order.equals("new")) {
Pageable pageable = new PageRequest(pageIndex, pageSize);
page = blogService.listBlogsByTitleVote(user, keyword, pageable);
}
// 当前所在页面数据列表
List<Blog> list = page.getContent();
model.addAttribute("user", user);
model.addAttribute("order", order);
model.addAttribute("catalogId", catalogId);
model.addAttribute("keyword", keyword);
model.addAttribute("page", page);
model.addAttribute("blogList", list);
return (async==true?"/userspace/u :: #mainContainerRepleace":"/userspace/u");
}
/**
* 获取博客展示界面
* @param id
* @param model
* @return
*/
@GetMapping("{username}/blogs/{id}")
public String getBlogById(@PathVariable("username") String username,@PathVariable("id") Long id, Model model) {
User principal = null;
Blog blog = blogService.getBlogById(id);
// 每次读取,简单的可以认为阅读量增加1次
blogService.readingIncrease(id);
// 判断操作用户是否是博客的所有者
boolean isBlogOwner = false;
if (SecurityContextHolder.getContext().getAuthentication() !=null && SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
&& !SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString().equals("anonymousUser")) {
principal = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal !=null && username.equals(principal.getUsername())) {
isBlogOwner = true;
}
}
// 判断操作用户的点赞情况
List<Vote> votes = blog.getVotes();
// 当前用户的点赞情况
Vote currentVote = null;
if (principal !=null) {
for (Vote vote : votes) {
vote.getUser().getUsername().equals(principal.getUsername());
currentVote = vote;
break;
}
}
model.addAttribute("isBlogOwner", isBlogOwner);
model.addAttribute("blogModel",blog);
model.addAttribute("currentVote",currentVote);
return "/userspace/blog";
}
/**
* 删除博客
* @param id
*/
@DeleteMapping("/{username}/blogs/{id}")
@PreAuthorize("authentication.name.equals(#username)")
public ResponseEntity<Response> deleteBlog(@PathVariable("u
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
个人花大量时间整理出的真实毕业设计实战成果,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~ 个人花大量时间整理出的真实毕业设计实战成果,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~ 个人花大量时间整理出的真实毕业设计实战成果,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~
资源推荐
资源详情
资源评论
收起资源包目录
《毕业设计》--微博客-毕业设计.zip (345个子文件)
gradlew.bat 2KB
_3.cfe 363B
_2.cfe 363B
_g.cfe 363B
_d.cfe 363B
_9.cfe 363B
_a.cfe 363B
_4.cfe 363B
_7.cfe 363B
_b.cfe 363B
_0.cfe 363B
_b.cfs 7KB
_3.cfs 5KB
_d.cfs 5KB
_g.cfs 5KB
_9.cfs 5KB
_2.cfs 5KB
_4.cfs 5KB
_7.cfs 5KB
_a.cfs 5KB
_0.cfs 4KB
translog-3.ckp 20B
translog.ckp 20B
translog-1.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-1.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-1.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-1.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-11.ckp 20B
translog.ckp 20B
translog-11.ckp 20B
translog-9.ckp 20B
translog-8.ckp 20B
translog-7.ckp 20B
translog-12.ckp 20B
translog-14.ckp 20B
translog-10.ckp 20B
translog.ckp 20B
translog-13.ckp 20B
translog.ckp 20B
translog-12.ckp 20B
translog.ckp 20B
translog-15.ckp 20B
translog-11.ckp 20B
translog-12.ckp 20B
translog-14.ckp 20B
translog.ckp 20B
translog-13.ckp 20B
translog-1.ckp 20B
translog-3.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-1.ckp 20B
translog-3.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-3.ckp 20B
translog.ckp 20B
translog-4.ckp 20B
translog-1.ckp 20B
translog-3.ckp 20B
translog-2.ckp 20B
translog.ckp 20B
translog-3.ckp 20B
translog.ckp 20B
translog-4.ckp 20B
bootstrap.min.css 197KB
bootstrap.css 187KB
twemoji.css 154KB
thinker-md.vendor.css 62KB
object.css 46KB
Sysmbols.css 36KB
font-awesome.css 36KB
people.css 34KB
font-awesome.min.css 30KB
bootstrap-grid.css 25KB
nature.css 20KB
place.css 19KB
bootstrap-grid.min.css 18KB
component-chosen.css 12KB
component-chosen.min.css 10KB
tether-theme-arrows.css 7KB
tether-theme-arrows-dark.css 7KB
toastr.css 7KB
bootstrap-table.css 7KB
tether-theme-arrows-dark.min.css 6KB
tether-theme-arrows.min.css 6KB
toastr.min.css 6KB
bootstrap-reboot.css 6KB
bootstrap-table.min.css 6KB
bootstrap-reboot.min.css 5KB
cropbox.css 2KB
blog.css 1KB
共 345 条
- 1
- 2
- 3
- 4
资源评论
季风泯灭的季节
- 粉丝: 600
- 资源: 2920
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- IMG_0694.GIF
- 基于图像的三维模型重建C++源代码+文档说明(高分课程设计)
- 基于聚焦法的工件立体测量方案,根据数据进行三维重建 使用HALCON处理图像,MATLAB拟合数据+源代码+数据集+效果图
- 锄战三国村 修改:货币使用不减 v1.10(2) 原创 (中文).apk
- 基于python实现的单目双目视觉三维重建+源代码+图像图片(高分课程设计)
- 基于C+++OPENCV的全景图像拼接源码(课程设计)
- 基于Python+OpenCV对多张图片进行全景图像拼接,消除鬼影,消除裂缝+源代码+文档说明+界面截图(高分课程设计)
- 基于C++实现的全景图像拼接源码(课程设计)
- 基于SIFT特征点提取和RASIC算法实现全景图像拼接python源码+文档说明+界面截图+详细注释(95分以上课程大作业)
- 基于matlab实现眼部判别的疲劳检测系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功