use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.
the class BlogBrowseServiceImpl method getBlogMainContent.
@Override
public ResultBean<BlogMainContentDTO> getBlogMainContent(int blogId) {
// 查询数据
Blog blog = blogDao.getBlogById(blogId);
if (blog == null)
return null;
String ch = dbProperties.getStringFiledSplitCharacterForNumber();
int[] cids = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), ch);
int[] lids = StringUtils.intStringDistinctToArray(blog.getLabelIds(), ch);
List<BlogCategory> categories = cids == null ? null : categoryDao.listCategoryById(cids);
List<BlogLabel> labels = lids == null ? null : labelDao.listLabelById(lids);
// 填充数据
String sc = dbProperties.getStringFiledSplitCharacterForString();
BlogMainContentDTO dto = dataFillingManager.blogMainContentToDTO(blog, categories, labels, sc);
return new ResultBean<>(dto);
}
use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.
the class BloggerBlogServiceImpl method updateBlog.
@Override
public boolean updateBlog(int bloggerId, int blogId, int[] newCategories, int[] newLabels, BlogStatusEnum newStatus, String newTitle, String newContent, String newContentMd, String newSummary, String[] newKeyWords) {
// 1 更新博文中引用的本地图片(取消引用的useCount--,新增的useCount++)
Blog oldBlog = blogDao.getBlogById(blogId);
if (newContent != null) {
if (!oldBlog.getContent().equals(newContent)) {
// 1 2 3 4
final int[] oldIids = parseContentForImageIds(oldBlog.getContent(), bloggerId);
// 1 3 4 6
final int[] newIids = parseContentForImageIds(newContent, bloggerId);
// 求交集 1 3 4
int[] array = IntStream.of(oldIids).filter(value -> {
for (int id : newIids) if (id == value)
return true;
return false;
}).toArray();
// -- 2
int[] allM = new int[oldIids.length + array.length];
System.arraycopy(oldIids, 0, allM, 0, oldIids.length);
System.arraycopy(array, 0, allM, oldIids.length, array.length);
IntStream.of(allM).distinct().forEach(pictureDao::updateUseCountMinus);
// ++ 6
int[] allP = new int[newIids.length + array.length];
System.arraycopy(newIids, 0, allP, 0, newIids.length);
System.arraycopy(array, 0, allP, newIids.length, array.length);
IntStream.of(allP).distinct().forEach(id -> {
pictureDao.updateUseCountPlus(id);
// 将用到的图片修改为public(有必要的话)
try {
imageManager.moveImageAndUpdateDbIfNecessary(bloggerId, id, BloggerPictureCategoryEnum.PUBLIC);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
// 2 更新博文
String ch = dbProperties.getStringFiledSplitCharacterForNumber();
String chs = dbProperties.getStringFiledSplitCharacterForString();
Blog blog = new Blog();
blog.setId(blogId);
if (newCategories != null)
blog.setCategoryIds(StringUtils.intArrayToString(newCategories, ch));
if (newLabels != null)
blog.setLabelIds(StringUtils.intArrayToString(newLabels, ch));
// 博文未通过审核时不能修改状态
if (newStatus != null && !oldBlog.getState().equals(BlogStatusEnum.VERIFY.getCode()))
blog.setState(newStatus.getCode());
if (newTitle != null)
blog.setTitle(newTitle);
if (newContent != null)
blog.setContent(newContent);
if (newSummary != null)
blog.setSummary(newSummary);
if (newContentMd != null)
blog.setContentMd(newContentMd);
if (newKeyWords != null)
blog.setKeyWords(StringUtils.arrayToString(newKeyWords, chs));
int effect = blogDao.update(blog);
if (effect <= 0)
throw new SQLException();
// 3 更新lucene
try {
luceneIndexManager.update(blog);
} catch (IOException e) {
e.printStackTrace();
throw new LuceneException(e);
}
return true;
}
use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.
the class BloggerBlogServiceImpl method deleteBlog.
@Override
public boolean deleteBlog(int bloggerId, int blogId) {
Blog blog = blogDao.getBlogById(blogId);
if (blog == null)
return false;
// 1 删除博文记录
int effect = blogDao.delete(blogId);
if (effect <= 0)
return false;
// 2 删除统计信息
int effectS = statisticsDao.deleteByUnique(blogId);
// MAYBUG 断点调试时effectS始终为0,但最终事务提交时记录却会正确删除,??? 因而注释下面的判断
// if (effectS <= 0) throw new UnknownException(blog");
// 3 图片引用useCount--
int[] ids = parseContentForImageIds(blog.getContent(), bloggerId);
if (!CollectionUtils.isEmpty(ids))
Arrays.stream(ids).forEach(pictureDao::updateUseCountMinus);
// 4 删除lucene索引
try {
luceneIndexManager.delete(blogId);
} catch (IOException e) {
e.printStackTrace();
throw new LuceneException(e);
}
return true;
}
use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.
the class BloggerBlogServiceImpl method constructResult.
@Override
protected ResultBean<List<BlogListItemDTO>> constructResult(Map<Integer, Blog> blogHashMap, List<BlogStatistics> statistics, Map<Integer, int[]> blogIdMapCategoryIds, Map<Integer, String> blogImgs) {
// 重组结果
List<BlogListItemDTO> result = new ArrayList<>();
for (BlogStatistics s : statistics) {
Integer blogId = s.getBlogId();
int[] ids = blogIdMapCategoryIds.get(blogId);
List<BlogCategory> categories = CollectionUtils.isEmpty(ids) ? null : categoryDao.listCategoryById(ids);
Blog blog = blogHashMap.get(blogId);
BlogListItemDTO dto = dataFillingManager.bloggerBlogListItemToDTO(blog, s, categories);
result.add(dto);
}
return new ResultBean<>(result);
}
use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.
the class BloggerBlogController method get.
/**
* 获取指定博文
*/
@RequestMapping(value = "/{blogId}", method = RequestMethod.GET)
public ResultBean<Blog> get(HttpServletRequest request, @PathVariable Integer bloggerId, @PathVariable Integer blogId) {
handleBloggerSignInCheck(request, bloggerId);
ResultBean<Blog> blog = bloggerBlogService.getBlog(bloggerId, blogId);
if (blog == null)
handlerEmptyResult(request);
// 编码为 Unicode
Blog bg = blog.getData();
bg.setContent(StringUtils.stringToUnicode(bg.getContent()));
bg.setContentMd(StringUtils.stringToUnicode(bg.getContentMd()));
return blog;
}
Aggregations