完成管理后台新闻保存草稿功能
This commit is contained in:
parent
5a61e92888
commit
d62ca60bcf
|
|
@ -1,6 +1,9 @@
|
|||
package com.jinrui.reference.admin.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftDTO;
|
||||
import com.jinrui.reference.admin.model.entity.AdminUser;
|
||||
import com.jinrui.reference.core.model.vo.ResultObject;
|
||||
import com.jinrui.reference.core.model.vo.news.NewsVO;
|
||||
import com.jinrui.reference.admin.service.AdminJwtService;
|
||||
import com.jinrui.reference.core.model.vo.PageObject;
|
||||
|
|
@ -9,6 +12,8 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
|
@ -21,9 +26,41 @@ public class NewsController {
|
|||
private static final Logger log = LoggerFactory.getLogger(NewsController.class);
|
||||
|
||||
private final NewsService newsService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public NewsController(NewsService newsService) {
|
||||
public NewsController(NewsService newsService,
|
||||
ObjectMapper objectMapper) {
|
||||
this.newsService = newsService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public ResultObject<Void> saveDraft(@RequestHeader("auth-token") String token,
|
||||
@RequestBody SaveDraftDTO saveDraftDTO) {
|
||||
if (!StringUtils.hasText(token)) {
|
||||
return ResultObject.failed("登陆Token为空!");
|
||||
}
|
||||
|
||||
try {
|
||||
AdminUser adminUser = AdminJwtService.parseToken(token);
|
||||
if (adminUser == null) {
|
||||
log.warn("解析token {}拿不到AdminUser对象!", token);
|
||||
return ResultObject.failed("登陆Token有误,请联系系统管理员!");
|
||||
}
|
||||
|
||||
if (!adminUser.isActive()) {
|
||||
log.warn("当前用户已被封禁! id = {}", adminUser.getId());
|
||||
return ResultObject.failed("当前用户已被封禁!请联系系统管理员!");
|
||||
}
|
||||
|
||||
log.info("path: /news/save, method: POST, request user id: {}, param: {}",
|
||||
adminUser.getId(), objectMapper.writeValueAsString(saveDraftDTO));
|
||||
} catch (Exception e) {
|
||||
log.error("解析登陆Token出错!", e);
|
||||
return ResultObject.failed(500, "服务端错误,请联系系统管理员!");
|
||||
}
|
||||
|
||||
return newsService.saveDraft(saveDraftDTO);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package com.jinrui.reference.core.mapper;
|
|||
import com.jinrui.reference.core.model.entity.Column;
|
||||
import com.jinrui.reference.core.model.entity.DraftColumnRel;
|
||||
import com.jinrui.reference.core.model.entity.NewsColumnRel;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Result;
|
||||
import org.apache.ibatis.annotations.Results;
|
||||
|
|
@ -15,6 +17,21 @@ import java.util.Set;
|
|||
|
||||
public interface ColumnMapper {
|
||||
|
||||
@Delete("delete from draft_column_rel where draft_id = #{draftId}")
|
||||
void deleteDraft(@Param("draftId") Long draftId);
|
||||
|
||||
@Insert("<script>" +
|
||||
"insert into draft_column_rel(draft_id, column_id" +
|
||||
"<if test=\"type != null\">" +
|
||||
", type" +
|
||||
"</if>" +
|
||||
") values (#{draftId}, #{columnId}" +
|
||||
"<if test=\"type != null\">" +
|
||||
", #{type}" +
|
||||
"</if>" +
|
||||
")")
|
||||
void saveDraftColumnRel(@Param("draftId") Long draftId, @Param("columnId") Long columnId, @Param("type") Integer type);
|
||||
|
||||
@Results({
|
||||
@Result(column = "id", property = "id", id = true),
|
||||
@Result(column = "parent_id", property = "parentId"),
|
||||
|
|
|
|||
|
|
@ -1,13 +1,46 @@
|
|||
package com.jinrui.reference.core.mapper;
|
||||
|
||||
import com.jinrui.reference.core.model.entity.News;
|
||||
import com.jinrui.reference.core.model.entity.NewsDraft;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface NewsMapper {
|
||||
|
||||
@Delete("delete from news_draft where id = #{draftId}")
|
||||
void deleteDraft(@Param("draftId") Long draftId);
|
||||
|
||||
@Update("update news " +
|
||||
"set draft_id = #{draftId}," +
|
||||
"editor_id = #{editorId}," +
|
||||
"title = #{title}," +
|
||||
"summary = #{summary}," +
|
||||
"picture = #{picture}," +
|
||||
"content = #{content}," +
|
||||
"status = #{status}," +
|
||||
"update_time = now() " +
|
||||
"where id = #{id}")
|
||||
void updateNews(News news);
|
||||
|
||||
@Select("select id, draft_id as draftId, status from news where id = #{id}")
|
||||
News getById(@Param("id") Long id);
|
||||
|
||||
@Select("select last_insert_id()")
|
||||
Long getLastInsertId();
|
||||
|
||||
@Insert("insert into news_draft(title, summary, picture, type, content, create_time, update_time)" +
|
||||
"values (#{title}, #{summary}, #{picture}, #{type}, #{content}, now(), now())")
|
||||
void saveDraft(NewsDraft newsDraft);
|
||||
|
||||
@Insert("insert into news(draft_id, title, summary, picture, type, content, create_time, update_time)" +
|
||||
"values (#{draftId}, #{title}, #{summary}, #{picture}, #{type}, #{content}, now(), now())")
|
||||
void saveNews(News news);
|
||||
|
||||
@Select("<script>" +
|
||||
"select " +
|
||||
"news.id as id," +
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package com.jinrui.reference.core.mapper;
|
||||
|
||||
import com.jinrui.reference.core.model.entity.Tag;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Result;
|
||||
import org.apache.ibatis.annotations.Results;
|
||||
|
|
@ -12,6 +14,12 @@ import java.util.List;
|
|||
|
||||
public interface TagMapper {
|
||||
|
||||
@Delete("delete from draft_tag_rel where draft_id = #{draftId}")
|
||||
void deleteDraft(@Param("draftId") Long draftId);
|
||||
|
||||
@Insert("insert into draft_tag_rel(draft_id, tag_id) values (#{draftId}, #{tagId})")
|
||||
void saveDraftTagRel(@Param("draftId") Long draftId, @Param("tagId") Long tagId);
|
||||
|
||||
@Results({
|
||||
@Result(column = "id", property = "id", id = true),
|
||||
@Result(column = "parent_id", property = "parentId"),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package com.jinrui.reference.core.model.dto.news;
|
||||
|
||||
public class SaveDraftColumn {
|
||||
|
||||
private SaveDraftColumnVip vip;
|
||||
|
||||
private SaveDraftColumnItem earlyKnow;
|
||||
|
||||
private Boolean showEverything;
|
||||
|
||||
public SaveDraftColumnVip getVip() {
|
||||
return vip;
|
||||
}
|
||||
|
||||
public void setVip(SaveDraftColumnVip vip) {
|
||||
this.vip = vip;
|
||||
}
|
||||
|
||||
public SaveDraftColumnItem getEarlyKnow() {
|
||||
return earlyKnow;
|
||||
}
|
||||
|
||||
public void setEarlyKnow(SaveDraftColumnItem earlyKnow) {
|
||||
this.earlyKnow = earlyKnow;
|
||||
}
|
||||
|
||||
public Boolean getShowEverything() {
|
||||
return showEverything;
|
||||
}
|
||||
|
||||
public void setShowEverything(Boolean showEverything) {
|
||||
this.showEverything = showEverything;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.jinrui.reference.core.model.dto.news;
|
||||
|
||||
public class SaveDraftColumnItem {
|
||||
|
||||
private Boolean show;
|
||||
|
||||
private Integer type;
|
||||
|
||||
public Boolean getShow() {
|
||||
return show;
|
||||
}
|
||||
|
||||
public void setShow(Boolean show) {
|
||||
this.show = show;
|
||||
}
|
||||
|
||||
public Integer getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Integer type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.jinrui.reference.core.model.dto.news;
|
||||
|
||||
public class SaveDraftColumnVip {
|
||||
|
||||
private Long id;
|
||||
|
||||
private Integer type;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Integer type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package com.jinrui.reference.core.model.dto.news;
|
||||
|
||||
public class SaveDraftDTO {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String summary;
|
||||
|
||||
private String picture;
|
||||
|
||||
private SaveDraftTag tag;
|
||||
|
||||
private SaveDraftColumn column;
|
||||
|
||||
private String content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getSummary() {
|
||||
return summary;
|
||||
}
|
||||
|
||||
public void setSummary(String summary) {
|
||||
this.summary = summary;
|
||||
}
|
||||
|
||||
public String getPicture() {
|
||||
return picture;
|
||||
}
|
||||
|
||||
public void setPicture(String picture) {
|
||||
this.picture = picture;
|
||||
}
|
||||
|
||||
public SaveDraftTag getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setTag(SaveDraftTag tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public SaveDraftColumn getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(SaveDraftColumn column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.jinrui.reference.core.model.dto.news;
|
||||
|
||||
public class SaveDraftTag {
|
||||
|
||||
private Long source;
|
||||
|
||||
private Long field;
|
||||
|
||||
public Long getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(Long source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public Long getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(Long field) {
|
||||
this.field = field;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
package com.jinrui.reference.core.model.entity;
|
||||
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftDTO;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
|
|
@ -68,6 +70,20 @@ public class News {
|
|||
*/
|
||||
private Date updateTime;
|
||||
|
||||
public News() {}
|
||||
|
||||
public News(SaveDraftDTO saveDraftDTO) {
|
||||
this.id = saveDraftDTO.getId();
|
||||
this.title = saveDraftDTO.getTitle();
|
||||
this.summary = saveDraftDTO.getSummary();
|
||||
this.picture = saveDraftDTO.getPicture();
|
||||
this.type = 1;
|
||||
this.content = saveDraftDTO.getContent();
|
||||
this.status = 0;
|
||||
this.createTime = new Date();
|
||||
this.updateTime = new Date();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.jinrui.reference.core.model.entity;
|
||||
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftDTO;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
|
|
@ -48,6 +50,19 @@ public class NewsDraft {
|
|||
*/
|
||||
private Date updateTime;
|
||||
|
||||
public NewsDraft() {}
|
||||
|
||||
public NewsDraft(SaveDraftDTO saveDraftDTO) {
|
||||
this.id = saveDraftDTO.getId();
|
||||
this.title = saveDraftDTO.getTitle();
|
||||
this.summary = saveDraftDTO.getSummary();
|
||||
this.picture = saveDraftDTO.getPicture();
|
||||
this.type = 1;
|
||||
this.content = saveDraftDTO.getContent();
|
||||
this.createTime = new Date();
|
||||
this.updateTime = new Date();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,19 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jinrui.reference.core.mapper.ColumnMapper;
|
||||
import com.jinrui.reference.core.mapper.NewsMapper;
|
||||
import com.jinrui.reference.core.mapper.TagMapper;
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftColumn;
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftColumnItem;
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftColumnVip;
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftDTO;
|
||||
import com.jinrui.reference.core.model.dto.news.SaveDraftTag;
|
||||
import com.jinrui.reference.core.model.entity.Column;
|
||||
import com.jinrui.reference.core.model.entity.DraftColumnRel;
|
||||
import com.jinrui.reference.core.model.entity.News;
|
||||
import com.jinrui.reference.core.model.entity.NewsColumnRel;
|
||||
import com.jinrui.reference.core.model.entity.NewsDraft;
|
||||
import com.jinrui.reference.core.model.vo.PageObject;
|
||||
import com.jinrui.reference.core.model.vo.ResultObject;
|
||||
import com.jinrui.reference.core.model.vo.column.ColumnVO;
|
||||
import com.jinrui.reference.core.model.vo.news.NewsVO;
|
||||
import org.slf4j.Logger;
|
||||
|
|
@ -31,13 +39,130 @@ public class NewsService {
|
|||
private final NewsMapper newsMapper;
|
||||
private final ColumnMapper columnMapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final TagMapper tagMapper;
|
||||
|
||||
public NewsService(NewsMapper newsMapper,
|
||||
ColumnMapper columnMapper,
|
||||
ObjectMapper objectMapper) {
|
||||
ObjectMapper objectMapper,
|
||||
TagMapper tagMapper) {
|
||||
this.newsMapper = newsMapper;
|
||||
this.columnMapper = columnMapper;
|
||||
this.objectMapper = objectMapper;
|
||||
this.tagMapper = tagMapper;
|
||||
}
|
||||
|
||||
public ResultObject<Void> saveDraft(SaveDraftDTO saveDraftDTO) {
|
||||
Long id = saveDraftDTO.getId();
|
||||
if (id == null) {
|
||||
return saveNewDraft(saveDraftDTO, null);
|
||||
}
|
||||
return updateDraft(saveDraftDTO);
|
||||
}
|
||||
|
||||
private ResultObject<Void> updateDraft(SaveDraftDTO saveDraftDTO) {
|
||||
Long id = saveDraftDTO.getId();
|
||||
News news = newsMapper.getById(id);
|
||||
Long draftId = news.getDraftId();
|
||||
if (draftId != null) {
|
||||
deleteDraft(draftId);
|
||||
}
|
||||
return saveNewDraft(saveDraftDTO, news);
|
||||
}
|
||||
|
||||
private void deleteDraft(Long draftId) {
|
||||
newsMapper.deleteDraft(draftId);
|
||||
columnMapper.deleteDraft(draftId);
|
||||
tagMapper.deleteDraft(draftId);
|
||||
}
|
||||
|
||||
private ResultObject<Void> saveNewDraft(SaveDraftDTO saveDraftDTO, News news) {
|
||||
NewsDraft newsDraft = new NewsDraft(saveDraftDTO);
|
||||
try {
|
||||
newsMapper.saveDraft(newsDraft);
|
||||
} catch (Exception e) {
|
||||
log.error("保存草稿错误!", e);
|
||||
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||
}
|
||||
|
||||
Long draftId = newsMapper.getLastInsertId();
|
||||
if (news == null) {
|
||||
news = new News(saveDraftDTO);
|
||||
news.setDraftId(draftId);
|
||||
try {
|
||||
newsMapper.saveNews(news);
|
||||
} catch (Exception e) {
|
||||
log.error("保存新闻错误!", e);
|
||||
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||
}
|
||||
} else {
|
||||
Integer status = news.getStatus();
|
||||
if (status < 2) {
|
||||
news = new News(saveDraftDTO);
|
||||
news.setStatus(status);
|
||||
news.setDraftId(draftId);
|
||||
try {
|
||||
newsMapper.updateNews(news);
|
||||
} catch (Exception e) {
|
||||
log.error("更新新闻报错!", e);
|
||||
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||
}
|
||||
} else {
|
||||
news.setDraftId(draftId);
|
||||
try {
|
||||
newsMapper.updateNews(news);
|
||||
} catch (Exception e) {
|
||||
log.error("更新新闻报错!", e);
|
||||
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
SaveDraftTag saveDraftTag = saveDraftDTO.getTag();
|
||||
if (saveDraftTag != null) {
|
||||
Long source = saveDraftTag.getSource();
|
||||
if (source != null) {
|
||||
tagMapper.saveDraftTagRel(draftId, source);
|
||||
}
|
||||
Long field = saveDraftTag.getField();
|
||||
if (field != null) {
|
||||
tagMapper.saveDraftTagRel(draftId, field);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("保存草稿标签出错!", e);
|
||||
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||
}
|
||||
|
||||
try {
|
||||
SaveDraftColumn column = saveDraftDTO.getColumn();
|
||||
if (column != null) {
|
||||
SaveDraftColumnVip vip = column.getVip();
|
||||
if (vip != null) {
|
||||
Long id = vip.getId();
|
||||
if (id != null) {
|
||||
Integer type = vip.getType();
|
||||
columnMapper.saveDraftColumnRel(draftId, id, type);
|
||||
}
|
||||
}
|
||||
SaveDraftColumnItem earlyKnow = column.getEarlyKnow();
|
||||
if (earlyKnow != null) {
|
||||
Boolean show = earlyKnow.getShow();
|
||||
if (show != null && show) {
|
||||
Integer type = earlyKnow.getType();
|
||||
columnMapper.saveDraftColumnRel(draftId, 13L, type);
|
||||
}
|
||||
}
|
||||
Boolean showEverything = column.getShowEverything();
|
||||
if (showEverything != null && showEverything) {
|
||||
columnMapper.saveDraftColumnRel(draftId, 14L, null);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("保存草稿栏目出错!", e);
|
||||
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||
}
|
||||
return ResultObject.success();
|
||||
}
|
||||
|
||||
public PageObject<NewsVO> queryNews(String keyword, Long column, Integer status, int page, int size,
|
||||
|
|
|
|||
Loading…
Reference in New Issue