完成管理后台新闻删除功能
This commit is contained in:
parent
1e3f6c5d43
commit
3166c69f19
|
|
@ -11,6 +11,7 @@ import com.jinrui.reference.core.service.NewsService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
@ -34,6 +35,34 @@ public class NewsController {
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public ResultObject<Void> deleteNews(@RequestHeader("auth-token") String token,
|
||||||
|
@RequestParam("id") Long id) {
|
||||||
|
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("当前用户已被封禁!请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("path: /news, method: DELETE, request user id: {}, news id: {}", adminUserId, id);
|
||||||
|
return newsService.deleteNews(id);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("解析登陆Token出错!", e);
|
||||||
|
return ResultObject.failed(500, "服务端错误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/create/publish")
|
@PostMapping("/create/publish")
|
||||||
public ResultObject<Void> createPublish(@RequestHeader("auth-token") String token,
|
public ResultObject<Void> createPublish(@RequestHeader("auth-token") String token,
|
||||||
@RequestBody SaveNewsDTO saveNewsDTO) {
|
@RequestBody SaveNewsDTO saveNewsDTO) {
|
||||||
|
|
|
||||||
|
|
@ -130,14 +130,35 @@ public class NewsService {
|
||||||
return saveNewDraft(saveNewsDTO, news);
|
return saveNewDraft(saveNewsDTO, news);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteNews(News news) {
|
public ResultObject<Void> deleteNews(Long newsId) {
|
||||||
Long newsId = news.getId();
|
News news = newsMapper.getById(newsId);
|
||||||
|
Integer status = news.getStatus();
|
||||||
|
if (status == 2) {
|
||||||
|
return ResultObject.failed("请先手动下架新闻然后进行删除!");
|
||||||
|
}
|
||||||
|
|
||||||
Long draftId = news.getDraftId();
|
Long draftId = news.getDraftId();
|
||||||
if (draftId != null) {
|
if (draftId != null) {
|
||||||
|
try {
|
||||||
deleteDraft(draftId);
|
deleteDraft(draftId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("删除新闻草稿异常!", e);
|
||||||
|
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
deleteNewsColumnAndTag(newsId);
|
deleteNewsColumnAndTag(newsId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("删除新闻栏目标签异常!", e);
|
||||||
|
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
try {
|
||||||
newsMapper.deleteNews(newsId);
|
newsMapper.deleteNews(newsId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("删除新闻异常!", e);
|
||||||
|
return ResultObject.failed(500, "服务器错误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
return ResultObject.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteNewsColumnAndTag(Long newsId) {
|
private void deleteNewsColumnAndTag(Long newsId) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue