package com.wiblog.core.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wiblog.core.aop.AuthorizeCheck;
import com.wiblog.core.aop.OpsRecord;
import com.wiblog.core.aop.RequestRequire;
import com.wiblog.core.common.BaseController;
import com.wiblog.core.common.Constant;
import com.wiblog.core.common.RoleEnum;
import com.wiblog.core.common.ServerResponse;
import com.wiblog.core.entity.Article;
import com.wiblog.core.entity.EsArticle;
import com.wiblog.core.entity.User;
import com.wiblog.core.elastic.EsArticleRepository;
import com.wiblog.core.service.IArticleService;
import com.wiblog.core.utils.PinYinUtil;
import com.wiblog.core.utils.WiblogUtil;
import com.wiblog.core.utils.WordFilterUtil;
import com.wiblog.core.vo.ArticleDetailVo;
import com.wiblog.core.vo.ArticlePageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
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.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 文章控制层
*
* @author pwm
* @date 2019-06-12
*/
@Slf4j
@RestController
@RequestMapping("/post")
public class ArticleController extends BaseController {
private static final Pattern PATTERN_HIGH_LIGHT = Pattern.compile(Constant.Regular.HIGH_LIGHT);
private final IArticleService articleService;
private final WordFilterUtil wordFilterUtil;
private final PinYinUtil pinYinUtil;
private final ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired
public ArticleController(IArticleService articleService, WordFilterUtil wordFilterUtil, PinYinUtil pinYinUtil, ElasticsearchRestTemplate elasticsearchTemplate) {
this.articleService = articleService;
this.wordFilterUtil = wordFilterUtil;
this.pinYinUtil = pinYinUtil;
this.elasticsearchTemplate = elasticsearchTemplate;
}
/**
* 首页文章列表
*/
@PostMapping("/articles")
public ServerResponse<IPage<ArticlePageVo>> articlePageList(
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
Long categoryId) {
return articleService.articlePageList(pageNum, pageSize, categoryId);
}
/**
* 后台管理文章列表
*/
@PostMapping("/articlesManage")
@AuthorizeCheck(grade = RoleEnum.ADMIN)
public ServerResponse<IPage<ArticlePageVo>> articlesManage(
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
return articleService.articlesManage(pageNum, pageSize);
}
/**
* 获取所有文章标题列表
* 评论管理界面-按文章标题查找
* 管理员权限
*
* @return ServerResponse
*/
@PostMapping("/allArticles")
@AuthorizeCheck(grade = RoleEnum.ADMIN)
public ServerResponse<?> articlePageList() {
LambdaQueryWrapper<Article> queryWrapper = new QueryWrapper<Article>().lambda();
queryWrapper.select(Article::getTitle).eq(Article::getState, 1);
List<Map<String, Object>> list = articleService.listMaps(queryWrapper);
return ServerResponse.success(list);
}
/**
* 文章详细信息
*/
@GetMapping("/get/{id}")
public ServerResponse<Article> getArticleById(@PathVariable Long id) {
Article article = articleService.getById(id);
return ServerResponse.success(article, "获取文章成功");
}
/**
* 通过url获取文章信息
*/
@GetMapping("/getArticle")
public ServerResponse<ArticleDetailVo> getArticle(HttpServletRequest request, String url) {
User user = getLoginUser(request);
return articleService.getArticle(url, user);
}
/**
* 发表文章 管理员权限
*
* @param article article
* @return ServerResponse
*/
@PostMapping("/push")
@AuthorizeCheck(grade = RoleEnum.ADMIN)
@OpsRecord(msg = "发表了文章<<{0}>>")
@RequestRequire(require = "title,content,tags,categoryId,imgUrl", parameter = Article.class)
public ServerResponse<String> pushArticle(HttpServletRequest request, Article article) {
// 分词
List<String> titles = wordFilterUtil.getParticiple(article.getTitle());
String title = pinYinUtil.getStringPinYin(titles);
String articleUrl = "/post/" + title;
Article sameUrlArticle = articleService.getOne(new QueryWrapper<Article>().eq("article_url", articleUrl));
if (sameUrlArticle != null) {
return ServerResponse.error("文章发表失败,已存在相同标题", 30001);
}
Date date = new Date();
// 提取纯文本
String content = StringUtils.substring(article.getContent(), 0, 500);
content = WiblogUtil.mdToHtml(content)
.replaceAll(Constant.Regular.HTML, "")
.replaceAll(Constant.Regular.WRAP, "");
content = StringUtils.substring(content, 0, 100);
User user = getLoginUser(request);
// 赋值
article.setUpdateTime(date)
.setCreateTime(date)
.setArticleUrl(articleUrl)
.setUid(user.getUid())
.setAuthor(user.getUsername())
.setArticleSummary(content);
boolean bool = articleService.save(article);
if (!bool) {
return ServerResponse.error("文章发表失败", 30001);
}
return ServerResponse.success(articleUrl, "文章发表成功", title);
}
/**
* 修改文章 管理员权限
*
* @param article article
* @return ServerResponse
*/
@PostMapping("/update")
@AuthorizeCheck(grade = RoleEnum.ADMIN)
@OpsRecord(msg = "修改了文章<<{0}>>")
@RequestRequire(require = "id,title,content,tags,categoryId", parameter = Article.class)
public ServerResponse<String> updateArticle(Article article) {
Date date = new Date();
article.setUpdateTime(date);
// 提取纯文本
String content = WiblogUtil.mdToHtml(article.getContent());
content = content.replaceAll("<[^>]+>", "");
content = content.replaceAll("\\s*|\t|\r|\n", "");
article.setArticleSummary(content.substring(0, 100));
boolean bool = articleService.updateById(article);
if (!bool) {
return
没有合适的资源?快使用搜索试试~ 我知道了~
基于spring boot的博客网站
共656个文件
js:225个
html:124个
java:116个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 103 浏览量
2022-04-28
15:03:04
上传
评论
收藏 3.7MB ZIP 举报
温馨提示
JDK: 1.8 Maven: 3.3+ MySql: 5.6+ 安装教程 Redis: 4.0+ 安装教程 elasticSearch 7.x 安装教程 IDE插件 Lombok Plugin 创建数据库 打开Navicat(此处可以选择其他的客户端)-> 连接数据库 -> 运行sql文件 -> 选择 \src\main\resources\myblog.sql 修改配置文件 配置\src\main\resources\application-dev.properties(配置服务器ip/数据库账号密码/elasticsearch ip地址) 配置\src\main\resources\wiblog.properties(腾讯云密钥图片存储/公众号/github第三方登录) 打包运行 # 打包 mvn clean package # 运行 java -jar target\core-0.0.1-SNAPSHOT.jar
资源推荐
资源详情
资源评论
收起资源包目录
基于spring boot的博客网站 (656个子文件)
AUTHORS 6KB
index.css 227KB
bootstrap.min.css 118KB
editormd.css 76KB
editormd.min.css 60KB
animate.min.css 57KB
editormd.preview.css 55KB
editormd.preview.min.css 44KB
font-awesome.min.css 32KB
ambiance.css 26KB
all.css 9KB
style.css 9KB
codemirror.css 8KB
article.css 7KB
user.css 5KB
codemirror.min.css 5KB
mdn-like.css 5KB
solarized.css 5KB
index.css 4KB
merge.css 3KB
lint.css 3KB
city-picker.css 3KB
xq-dark.css 3KB
loginAndRegister.css 3KB
lesser-dark.css 2KB
pastel-on-dark.css 2KB
xq-light.css 2KB
tomorrow-night-eighties.css 2KB
editormd.logo.css 2KB
erlang-dark.css 2KB
zenburn.css 2KB
twilight.css 2KB
midnight.css 2KB
vibrant-ink.css 2KB
mbo.css 2KB
base16-dark.css 2KB
base16-light.css 2KB
tern.css 2KB
3024-night.css 2KB
paraiso-dark.css 2KB
paraiso-light.css 2KB
tomorrow-night-bright.css 2KB
3024-day.css 2KB
blackboard.css 2KB
colorforth.css 2KB
editormd.logo.min.css 2KB
the-matrix.css 2KB
night.css 2KB
rubyblue.css 2KB
superfish.min.css 2KB
monokai.css 1KB
cobalt.css 1KB
admin-common.css 1KB
simplescrollbars.css 1KB
home.css 1KB
eclipse.css 1KB
article-edit.css 1KB
neo.css 932B
search.css 855B
elegant.css 768B
neat.css 693B
show-hint.css 662B
picture.css 653B
dialog.css 502B
tiki.css 440B
foldgutter.css 435B
tiddlywiki.css 220B
matchesonscrollbar.css 188B
fullscreen.css 116B
ambiance-mobile.css 103B
log.css 40B
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 59KB
glyphicons-halflings-regular.eot 20KB
editormd-logo.eot 1KB
loading@3x.gif 21KB
loading@2x.gif 16KB
loading.gif 8KB
loading-upload.gif 2KB
.gitignore 62B
scala.html 28KB
article.html 27KB
user.html 24KB
index.html 22KB
index.html 17KB
index.html 13KB
index.html 13KB
test.html 11KB
index.html 11KB
index.html 10KB
index.html 9KB
index.html 8KB
register.html 8KB
index.html 8KB
index.html 7KB
index.html 7KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
共 656 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
自不量力的A同学
- 粉丝: 47
- 资源: 2711
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功