完成管理后台新闻上下架功能
This commit is contained in:
parent
bc76c659ca
commit
061bda0258
|
|
@ -1,6 +1,7 @@
|
||||||
package com.jinrui.reference.admin.controller;
|
package com.jinrui.reference.admin.controller;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.jinrui.reference.admin.model.dto.news.PublishNewsDTO;
|
||||||
import com.jinrui.reference.core.model.dto.news.SaveNewsDTO;
|
import com.jinrui.reference.core.model.dto.news.SaveNewsDTO;
|
||||||
import com.jinrui.reference.admin.model.entity.AdminUser;
|
import com.jinrui.reference.admin.model.entity.AdminUser;
|
||||||
import com.jinrui.reference.core.model.vo.ResultObject;
|
import com.jinrui.reference.core.model.vo.ResultObject;
|
||||||
|
|
@ -36,6 +37,39 @@ public class NewsController {
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/publish")
|
||||||
|
public ResultObject<Void> publish(@RequestHeader("auth-token") String token,
|
||||||
|
@RequestBody PublishNewsDTO publishNewsDTO) {
|
||||||
|
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有误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Long adminUserId = adminUser.getId();
|
||||||
|
if (!adminUser.isActive()) {
|
||||||
|
log.warn("当前用户已被封禁! id = {}", adminUserId);
|
||||||
|
return ResultObject.failed("当前用户已被封禁!请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Long id = publishNewsDTO.getId();
|
||||||
|
if (id == null) {
|
||||||
|
return ResultObject.failed("要发布/下架的新闻ID不可为空!");
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("path: /news/publish, method: POST, request user id: {}, news id: {}", adminUserId, id);
|
||||||
|
return newsService.publish(id, adminUserId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("解析登陆Token出错!", e);
|
||||||
|
return ResultObject.failed(500, "服务端错误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/detail")
|
@GetMapping("/detail")
|
||||||
public ResultObject<NewsDetailVO> detail(@RequestHeader("auth-token") String token,
|
public ResultObject<NewsDetailVO> detail(@RequestHeader("auth-token") String token,
|
||||||
@RequestParam("id") Long id) {
|
@RequestParam("id") Long id) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.jinrui.reference.admin.model.dto.news;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class PublishNewsDTO {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -30,6 +30,12 @@ public interface NewsMapper {
|
||||||
"from news where id = #{id}")
|
"from news where id = #{id}")
|
||||||
News getNewsDetail(@Param("id") Long id);
|
News getNewsDetail(@Param("id") Long id);
|
||||||
|
|
||||||
|
@Update("update news set status = 1 where id = #{id}")
|
||||||
|
void simpleUnpublish(@Param("id") long id);
|
||||||
|
|
||||||
|
@Update("update news set status = 2, publish_time = now(), editor_id = #{editorId} where id = #{id}")
|
||||||
|
void simplePublish(@Param("id") long id, @Param("editorId") long editorId);
|
||||||
|
|
||||||
@Update("update news " +
|
@Update("update news " +
|
||||||
"set draft_id = #{draftId}," +
|
"set draft_id = #{draftId}," +
|
||||||
"editor_id = #{editorId}," +
|
"editor_id = #{editorId}," +
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package com.jinrui.reference.core.model.dto.news;
|
package com.jinrui.reference.core.model.dto.news;
|
||||||
|
|
||||||
|
import com.jinrui.reference.core.model.vo.news.NewsDetailColumn;
|
||||||
|
import com.jinrui.reference.core.model.vo.news.NewsDetailColumnVip;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class SaveDraftColumn {
|
public class SaveDraftColumn {
|
||||||
|
|
||||||
|
|
@ -9,6 +12,17 @@ public class SaveDraftColumn {
|
||||||
|
|
||||||
private Boolean showEverything;
|
private Boolean showEverything;
|
||||||
|
|
||||||
|
public SaveDraftColumn() {}
|
||||||
|
|
||||||
|
public SaveDraftColumn(NewsDetailColumn newsDetailColumn) {
|
||||||
|
NewsDetailColumnVip newsDetailColumnVip = newsDetailColumn.getVip();
|
||||||
|
if (newsDetailColumnVip != null) {
|
||||||
|
this.vip = new SaveDraftColumnVip(newsDetailColumnVip);
|
||||||
|
}
|
||||||
|
this.earlyKnow = newsDetailColumn.getEarlyKnow();
|
||||||
|
this.showEverything = newsDetailColumn.isShowEverything();
|
||||||
|
}
|
||||||
|
|
||||||
public SaveDraftColumnVip getVip() {
|
public SaveDraftColumnVip getVip() {
|
||||||
return vip;
|
return vip;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,21 @@
|
||||||
package com.jinrui.reference.core.model.dto.news;
|
package com.jinrui.reference.core.model.dto.news;
|
||||||
|
|
||||||
|
import com.jinrui.reference.core.model.vo.news.NewsDetailColumnVip;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class SaveDraftColumnVip {
|
public class SaveDraftColumnVip {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
private Integer type;
|
private Integer type;
|
||||||
|
|
||||||
|
public SaveDraftColumnVip() {}
|
||||||
|
|
||||||
|
public SaveDraftColumnVip(NewsDetailColumnVip newsDetailColumnVip) {
|
||||||
|
this.id = newsDetailColumnVip.getId();
|
||||||
|
this.type = newsDetailColumnVip.getType();
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package com.jinrui.reference.core.model.dto.news;
|
package com.jinrui.reference.core.model.dto.news;
|
||||||
|
|
||||||
|
import com.jinrui.reference.core.model.vo.news.NewsDetailTag;
|
||||||
|
import com.jinrui.reference.core.model.vo.news.NewsDetailTagItem;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class SaveDraftTag {
|
public class SaveDraftTag {
|
||||||
|
|
||||||
|
|
@ -7,6 +10,19 @@ public class SaveDraftTag {
|
||||||
|
|
||||||
private Long field;
|
private Long field;
|
||||||
|
|
||||||
|
public SaveDraftTag() {}
|
||||||
|
|
||||||
|
public SaveDraftTag(NewsDetailTag newsDetailTag) {
|
||||||
|
NewsDetailTagItem sourceItem = newsDetailTag.getSource();
|
||||||
|
if (sourceItem != null) {
|
||||||
|
this.source = sourceItem.getId();
|
||||||
|
}
|
||||||
|
NewsDetailTagItem fieldItem = newsDetailTag.getField();
|
||||||
|
if (fieldItem != null) {
|
||||||
|
this.field = fieldItem.getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Long getSource() {
|
public Long getSource() {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.jinrui.reference.core.model.dto.news;
|
package com.jinrui.reference.core.model.dto.news;
|
||||||
|
|
||||||
|
import com.jinrui.reference.core.model.vo.news.NewsDetailVO;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class SaveNewsDTO {
|
public class SaveNewsDTO {
|
||||||
|
|
||||||
|
|
@ -17,6 +19,18 @@ public class SaveNewsDTO {
|
||||||
|
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
public SaveNewsDTO() {}
|
||||||
|
|
||||||
|
public SaveNewsDTO(NewsDetailVO newsDetailVO) {
|
||||||
|
this.id = newsDetailVO.getId();
|
||||||
|
this.title = newsDetailVO.getTitle();
|
||||||
|
this.summary = newsDetailVO.getSummary();
|
||||||
|
this.picture = newsDetailVO.getPicture();
|
||||||
|
this.tag = new SaveDraftTag(newsDetailVO.getTag());
|
||||||
|
this.column = new SaveDraftColumn(newsDetailVO.getColumn());
|
||||||
|
this.content = newsDetailVO.getContent();
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,34 @@ public class NewsService {
|
||||||
this.tagMapper = tagMapper;
|
this.tagMapper = tagMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResultObject<Void> publish(long id, long editorId) {
|
||||||
|
News news = newsMapper.getById(id);
|
||||||
|
if (news == null) {
|
||||||
|
log.warn("找不到ID为{}的新闻!", id);
|
||||||
|
return ResultObject.failed("找不到ID为" + id + "的新闻!");
|
||||||
|
}
|
||||||
|
Integer status = news.getStatus();
|
||||||
|
if (status == 2) {
|
||||||
|
newsMapper.simpleUnpublish(id);
|
||||||
|
return ResultObject.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
Long draftId = news.getDraftId();
|
||||||
|
if (draftId == null) {
|
||||||
|
newsMapper.simplePublish(id, editorId);
|
||||||
|
return ResultObject.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultObject<NewsDetailVO> resultObject = detail(id);
|
||||||
|
int code = resultObject.getCode();
|
||||||
|
NewsDetailVO newsDetailVO = resultObject.getData();
|
||||||
|
if (code > 200) {
|
||||||
|
return ResultObject.failed(resultObject.getCode(), resultObject.getMsg());
|
||||||
|
}
|
||||||
|
SaveNewsDTO saveNewsDTO = new SaveNewsDTO(newsDetailVO);
|
||||||
|
return createPublish(editorId, saveNewsDTO);
|
||||||
|
}
|
||||||
|
|
||||||
public ResultObject<NewsDetailVO> detail(Long id) {
|
public ResultObject<NewsDetailVO> detail(Long id) {
|
||||||
News news = newsMapper.getById(id);
|
News news = newsMapper.getById(id);
|
||||||
if (news == null) {
|
if (news == null) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue