🎨 代码优化

This commit is contained in:
ronger 2022-08-17 09:46:19 +08:00
parent d31822133c
commit 42eaab1a87
17 changed files with 199 additions and 129 deletions

View File

@ -1,7 +1,7 @@
package com.rymcu.forest.config; package com.rymcu.forest.config;
import com.alibaba.fastjson.support.spring.FastJsonJsonView; import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.rymcu.forest.core.exception.DataDuplicationException; import com.rymcu.forest.core.exception.BusinessException;
import com.rymcu.forest.core.exception.ServiceException; import com.rymcu.forest.core.exception.ServiceException;
import com.rymcu.forest.core.exception.TransactionException; import com.rymcu.forest.core.exception.TransactionException;
import com.rymcu.forest.core.result.GlobalResult; import com.rymcu.forest.core.result.GlobalResult;
@ -63,7 +63,7 @@ public class BaseExceptionHandler {
} else if (ex instanceof ServletException) { } else if (ex instanceof ServletException) {
result.setCode(ResultCode.FAIL.getCode()); result.setCode(ResultCode.FAIL.getCode());
result.setMessage(ex.getMessage()); result.setMessage(ex.getMessage());
} else if (ex instanceof DataDuplicationException) { } else if (ex instanceof BusinessException) {
result.setCode(ResultCode.FAIL.getCode()); result.setCode(ResultCode.FAIL.getCode());
result.setMessage(ex.getMessage()); result.setMessage(ex.getMessage());
} else if (ex instanceof TransactionException) { } else if (ex instanceof TransactionException) {
@ -112,7 +112,7 @@ public class BaseExceptionHandler {
} else if (ex instanceof ServletException) { } else if (ex instanceof ServletException) {
attributes.put("code", ResultCode.FAIL.getCode()); attributes.put("code", ResultCode.FAIL.getCode());
attributes.put("message", ex.getMessage()); attributes.put("message", ex.getMessage());
} else if (ex instanceof DataDuplicationException) { } else if (ex instanceof BusinessException) {
attributes.put("code", ResultCode.FAIL.getCode()); attributes.put("code", ResultCode.FAIL.getCode());
attributes.put("message", ex.getMessage()); attributes.put("message", ex.getMessage());
} else if (ex instanceof TransactionException) { } else if (ex instanceof TransactionException) {

View File

@ -0,0 +1,28 @@
package com.rymcu.forest.core.exception;
/**
* @author KKould
*/
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 3206744387536223284L;
public BusinessException() {
}
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -1,6 +1,9 @@
package com.rymcu.forest.core.exception; package com.rymcu.forest.core.exception;
public class ContentNotExistException extends RuntimeException{ /**
* @author KKould
*/
public class ContentNotExistException extends BusinessException {
private static final long serialVersionUID = 3206734387536223284L; private static final long serialVersionUID = 3206734387536223284L;
public ContentNotExistException() { public ContentNotExistException() {

View File

@ -1,28 +0,0 @@
package com.rymcu.forest.core.exception;
/**
* @author KKould
*/
public class DataDuplicationException extends RuntimeException {
private static final long serialVersionUID = 3206744387536223284L;
public DataDuplicationException() {
}
public DataDuplicationException(String message) {
super(message);
}
public DataDuplicationException(String message, Throwable cause) {
super(message, cause);
}
public DataDuplicationException(Throwable cause) {
super(cause);
}
public DataDuplicationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -1,6 +1,9 @@
package com.rymcu.forest.core.exception; package com.rymcu.forest.core.exception;
public class UltraViresException extends RuntimeException { /**
* @author KKould
*/
public class UltraViresException extends BusinessException {
private static final long serialVersionUID = 3206744387536228284L; private static final long serialVersionUID = 3206744387536228284L;

View File

@ -0,0 +1,57 @@
package com.rymcu.forest.handler;
import com.rymcu.forest.core.constant.NotificationConstant;
import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.handler.event.CommentEvent;
import com.rymcu.forest.mapper.CommentMapper;
import com.rymcu.forest.util.Html2TextUtil;
import com.rymcu.forest.util.NotificationUtils;
import com.rymcu.forest.wx.mp.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created on 2022/8/17 7:38.
*
* @author ronger
* @email ronger-x@outlook.com
* @packageName com.rymcu.forest.handler
*/
@Slf4j
@Component
public class CommentHandler {
private static final int MAX_PREVIEW = 200;
@Resource
private CommentMapper commentMapper;
@Async
@EventListener
public void processCommentCreatedEvent(CommentEvent commentEvent) throws InterruptedException {
log.info(String.format("开始执行评论发布事件:[%s]", JsonUtils.toJson(commentEvent)));
String commentContent = commentEvent.getContent();
Integer length = commentContent.length();
if (length > MAX_PREVIEW) {
length = 200;
}
String commentPreviewContent = commentContent.substring(0, length);
commentContent = Html2TextUtil.getContent(commentPreviewContent);
// 判断是否是回复消息
if (commentEvent.getCommentOriginalCommentId() != null && commentEvent.getCommentOriginalCommentId() != 0) {
Comment originalComment = commentMapper.selectByPrimaryKey(commentEvent.getCommentOriginalCommentId());
// 回复消息时,评论者不是上级评论作者则进行消息通知
if (!commentEvent.getCommentAuthorId().equals(originalComment.getCommentAuthorId())) {
NotificationUtils.saveNotification(originalComment.getCommentAuthorId(), commentEvent.getIdComment(), NotificationConstant.Comment, commentContent);
}
} else {
// 评论者不是作者本人则进行消息通知
if (!commentEvent.getCommentAuthorId().equals(commentEvent.getArticleAuthorId())) {
NotificationUtils.saveNotification(commentEvent.getArticleAuthorId(), commentEvent.getIdComment(), NotificationConstant.Comment, commentContent);
}
}
}
}

View File

@ -0,0 +1,26 @@
package com.rymcu.forest.handler.event;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Created on 2022/8/17 7:43.
*
* @author ronger
* @email ronger-x@outlook.com
* @packageName com.rymcu.forest.handler.event
*/
@Data
@AllArgsConstructor
public class CommentEvent {
private Long idComment;
private Long articleAuthorId;
private Long commentAuthorId;
private String content;
private Long commentOriginalCommentId;
}

View File

@ -3,11 +3,9 @@ package com.rymcu.forest.service;
import com.rymcu.forest.core.service.Service; import com.rymcu.forest.core.service.Service;
import com.rymcu.forest.dto.CommentDTO; import com.rymcu.forest.dto.CommentDTO;
import com.rymcu.forest.entity.Comment; import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.web.api.exception.BaseApiException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author ronger * @author ronger
@ -27,7 +25,7 @@ public interface CommentService extends Service<Comment> {
* @param request * @param request
* @return * @return
*/ */
Map postComment(Comment comment, HttpServletRequest request) throws BaseApiException; Comment postComment(Comment comment, HttpServletRequest request);
/** /**
* 获取评论列表数据 * 获取评论列表数据

View File

@ -71,12 +71,18 @@ public interface PortfolioService extends Service<Portfolio> {
*/ */
Map unbindArticle(Long idPortfolio, Long idArticle); Map unbindArticle(Long idPortfolio, Long idArticle);
/** /**
* 删除作品集 * 删除作品集
*
* @param idPortfolio * @param idPortfolio
* @param idUser
* @param roleWeights
* @return * @return
* @throws BaseApiException
* @throws IllegalAccessException
*/ */
Map deletePortfolio(Long idPortfolio) throws BaseApiException; boolean deletePortfolio(Long idPortfolio, Long idUser, Integer roleWeights) throws BaseApiException, IllegalAccessException;
/** /**
* 获取作品集列表数据 * 获取作品集列表数据

View File

@ -40,7 +40,7 @@ public interface UserService extends Service<User> {
* @param password 密码 * @param password 密码
* @return Map * @return Map
* */ * */
Map login(String account, String password); TokenUser login(String account, String password);
/** /**
* 通过 account 获取用户信息接口 * 通过 account 获取用户信息接口

View File

@ -2,7 +2,7 @@ package com.rymcu.forest.service.impl;
import com.rymcu.forest.core.constant.NotificationConstant; import com.rymcu.forest.core.constant.NotificationConstant;
import com.rymcu.forest.core.exception.ContentNotExistException; import com.rymcu.forest.core.exception.ContentNotExistException;
import com.rymcu.forest.core.exception.DataDuplicationException; import com.rymcu.forest.core.exception.BusinessException;
import com.rymcu.forest.core.exception.UltraViresException; import com.rymcu.forest.core.exception.UltraViresException;
import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.core.service.AbstractService;
import com.rymcu.forest.dto.*; import com.rymcu.forest.dto.*;
@ -28,7 +28,6 @@ import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition; import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.*; import java.util.*;
@ -199,7 +198,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
luceneService.deleteArticle(id); luceneService.deleteArticle(id);
return result; return result;
} else { } else {
throw new DataDuplicationException("已有评论的文章不允许删除!"); throw new BusinessException("已有评论的文章不允许删除!");
} }
} }

View File

@ -1,23 +1,26 @@
package com.rymcu.forest.service.impl; package com.rymcu.forest.service.impl;
import com.rymcu.forest.core.constant.NotificationConstant; import com.rymcu.forest.core.exception.ContentNotExistException;
import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.core.service.AbstractService;
import com.rymcu.forest.dto.Author; import com.rymcu.forest.dto.Author;
import com.rymcu.forest.dto.CommentDTO; import com.rymcu.forest.dto.CommentDTO;
import com.rymcu.forest.entity.Article; import com.rymcu.forest.entity.Article;
import com.rymcu.forest.entity.Comment; import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.handler.event.CommentEvent;
import com.rymcu.forest.mapper.CommentMapper; import com.rymcu.forest.mapper.CommentMapper;
import com.rymcu.forest.service.ArticleService; import com.rymcu.forest.service.ArticleService;
import com.rymcu.forest.service.CommentService; import com.rymcu.forest.service.CommentService;
import com.rymcu.forest.util.*; import com.rymcu.forest.util.Utils;
import com.rymcu.forest.web.api.exception.BaseApiException; import com.rymcu.forest.util.XssUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.*; import java.util.Date;
import java.util.List;
/** /**
* @author ronger * @author ronger
@ -29,8 +32,8 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
private CommentMapper commentMapper; private CommentMapper commentMapper;
@Resource @Resource
private ArticleService articleService; private ArticleService articleService;
@Resource
private static final int MAX_PREVIEW = 200; private ApplicationEventPublisher applicationEventPublisher;
@Override @Override
public List<CommentDTO> getArticleComments(Integer idArticle) { public List<CommentDTO> getArticleComments(Integer idArticle) {
@ -63,25 +66,19 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Map postComment(Comment comment, HttpServletRequest request) throws BaseApiException { public Comment postComment(Comment comment, HttpServletRequest request) {
comment.setCommentAuthorId(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser());
Map map = new HashMap(1);
if (comment.getCommentArticleId() == null) { if (comment.getCommentArticleId() == null) {
map.put("message", "非法访问,文章主键异常!"); throw new IllegalArgumentException("非法访问,文章主键异常!");
return map;
} }
if (comment.getCommentAuthorId() == null) { if (comment.getCommentAuthorId() == null) {
map.put("message", "非法访问,用户未登录!"); throw new IllegalArgumentException("非法访问,用户未登录!");
return map;
} }
if (StringUtils.isBlank(comment.getCommentContent())) { if (StringUtils.isBlank(comment.getCommentContent())) {
map.put("message", "回帖内容不能为空!"); throw new IllegalArgumentException("回帖内容不能为空!");
return map;
} }
Article article = articleService.findById(comment.getCommentArticleId().toString()); Article article = articleService.findById(comment.getCommentArticleId().toString());
if (article == null) { if (article == null) {
map.put("message", "文章不存在!"); throw new ContentNotExistException("文章不存在!");
return map;
} }
String ip = Utils.getIpAddress(request); String ip = Utils.getIpAddress(request);
String ua = request.getHeader("user-agent"); String ua = request.getHeader("user-agent");
@ -95,28 +92,9 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
String commentContent = comment.getCommentContent(); String commentContent = comment.getCommentContent();
if (StringUtils.isNotBlank(commentContent)) { if (StringUtils.isNotBlank(commentContent)) {
Integer length = commentContent.length(); applicationEventPublisher.publishEvent(new CommentEvent(comment.getIdComment(), article.getArticleAuthorId(), comment.getCommentAuthorId(), commentContent, comment.getCommentOriginalCommentId()));
if (length > MAX_PREVIEW) {
length = 200;
} }
String commentPreviewContent = commentContent.substring(0, length); return comment;
commentContent = Html2TextUtil.getContent(commentPreviewContent);
// 评论者不是作者本人则进行消息通知
if (!article.getArticleAuthorId().equals(comment.getCommentAuthorId())) {
NotificationUtils.saveNotification(article.getArticleAuthorId(), comment.getIdComment(), NotificationConstant.Comment, commentContent);
}
// 判断是否是回复消息
if (comment.getCommentOriginalCommentId() != null && comment.getCommentOriginalCommentId() != 0) {
Comment originalComment = commentMapper.selectByPrimaryKey(comment.getCommentOriginalCommentId());
// 回复消息时,评论者不是上级评论作者则进行消息通知
if (!comment.getCommentAuthorId().equals(originalComment.getCommentAuthorId())) {
NotificationUtils.saveNotification(originalComment.getCommentAuthorId(), comment.getIdComment(), NotificationConstant.Comment, commentContent);
}
}
}
return map;
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package com.rymcu.forest.service.impl;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.rymcu.forest.core.exception.BusinessException;
import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.core.service.AbstractService;
import com.rymcu.forest.dto.*; import com.rymcu.forest.dto.*;
import com.rymcu.forest.entity.Portfolio; import com.rymcu.forest.entity.Portfolio;
@ -171,37 +172,31 @@ public class PortfolioServiceImpl extends AbstractService<Portfolio> implements
} }
@Override @Override
public Map deletePortfolio(Long idPortfolio) throws BaseApiException { public boolean deletePortfolio(Long idPortfolio, Long idUser, Integer roleWeights) throws BaseApiException, IllegalAccessException {
Map map = new HashMap(1); if (idPortfolio == null || idPortfolio == 0) {
if (idPortfolio == null || idPortfolio.equals(0)) { throw new IllegalArgumentException("作品集数据异常!");
map.put("message", "作品集数据异常");
} }
// 鉴权 // 鉴权
User user = UserUtils.getCurrentUserByToken();
Integer roleWeights = userService.findRoleWeightsByUser(user.getIdUser());
if (roleWeights > 2) { if (roleWeights > 2) {
Portfolio portfolio = portfolioMapper.selectByPrimaryKey(idPortfolio); Portfolio portfolio = portfolioMapper.selectByPrimaryKey(idPortfolio);
if (!user.getIdUser().equals(portfolio.getPortfolioAuthorId())) { if (!idUser.equals(portfolio.getPortfolioAuthorId())) {
map.put("message", "非法访问!"); throw new IllegalAccessException("非法访问!");
return map;
} }
} }
Integer articleNumber = portfolioMapper.selectCountArticleNumber(idPortfolio); Integer articleNumber = portfolioMapper.selectCountArticleNumber(idPortfolio);
if (articleNumber > 0) { if (articleNumber > 0) {
map.put("message", "该作品集已绑定文章不允许删除!"); throw new BusinessException("该作品集已绑定文章不允许删除!");
} else { } else {
Integer result = portfolioMapper.deleteByPrimaryKey(idPortfolio); Integer result = portfolioMapper.deleteByPrimaryKey(idPortfolio);
if (result.equals(0)) { if (result.equals(0)) {
map.put("message", "操作失败!"); throw new BusinessException("操作失败!");
}else { }
PortfolioIndexUtil.deleteIndex(idPortfolio); PortfolioIndexUtil.deleteIndex(idPortfolio);
return true;
} }
} }
return map;
}
@Override @Override
public List<PortfolioDTO> findPortfolios() { public List<PortfolioDTO> findPortfolios() {
return portfolioMapper.selectPortfolios(); return portfolioMapper.selectPortfolios();

View File

@ -1,5 +1,7 @@
package com.rymcu.forest.service.impl; package com.rymcu.forest.service.impl;
import com.rymcu.forest.core.exception.CaptchaException;
import com.rymcu.forest.core.exception.ContentNotExistException;
import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.core.service.AbstractService;
import com.rymcu.forest.core.service.redis.RedisService; import com.rymcu.forest.core.service.redis.RedisService;
import com.rymcu.forest.dto.*; import com.rymcu.forest.dto.*;
@ -20,6 +22,8 @@ import com.rymcu.forest.util.Utils;
import com.rymcu.forest.web.api.common.UploadController; import com.rymcu.forest.web.api.common.UploadController;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.exceptions.TooManyResultsException; import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -114,8 +118,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
} }
@Override @Override
public Map login(String account, String password) { public TokenUser login(String account, String password) {
Map map = new HashMap(2);
User user = userMapper.findByAccount(account); User user = userMapper.findByAccount(account);
if (user != null) { if (user != null) {
if (Utils.comparePwd(password, user.getPassword())) { if (Utils.comparePwd(password, user.getPassword())) {
@ -125,16 +128,15 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
BeanCopierUtil.copy(user, tokenUser); BeanCopierUtil.copy(user, tokenUser);
tokenUser.setToken(tokenManager.createToken(account)); tokenUser.setToken(tokenManager.createToken(account));
tokenUser.setWeights(userMapper.selectRoleWeightsByUser(user.getIdUser())); tokenUser.setWeights(userMapper.selectRoleWeightsByUser(user.getIdUser()));
map.put("user", tokenUser);
// 保存登录日志 // 保存登录日志
loginRecordService.saveLoginRecord(tokenUser.getIdUser()); loginRecordService.saveLoginRecord(tokenUser.getIdUser());
return tokenUser;
} else { } else {
map.put("message", "密码错误!"); throw new AuthenticationException("密码错误");
} }
} else { } else {
map.put("message", "该账号不存在!"); throw new UnknownAccountException("账号不存在");
} }
return map;
} }
@Override @Override
@ -183,7 +185,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
Map map = new HashMap(2); Map map = new HashMap(2);
UserInfoDTO user = userMapper.selectUserInfo(idUser); UserInfoDTO user = userMapper.selectUserInfo(idUser);
if (user == null) { if (user == null) {
map.put("message", "用户不存在!"); throw new ContentNotExistException("用户不存在!");
} else { } else {
UserExtend userExtend = userExtendMapper.selectByPrimaryKey(user.getIdUser()); UserExtend userExtend = userExtendMapper.selectByPrimaryKey(user.getIdUser());
if (Objects.isNull(userExtend)) { if (Objects.isNull(userExtend)) {
@ -270,7 +272,6 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
@Override @Override
public Map updateEmail(ChangeEmailDTO changeEmailDTO) { public Map updateEmail(ChangeEmailDTO changeEmailDTO) {
Map map = new HashMap(2); Map map = new HashMap(2);
map.put("message", "验证码无效!");
Long idUser = changeEmailDTO.getIdUser(); Long idUser = changeEmailDTO.getIdUser();
String email = changeEmailDTO.getEmail(); String email = changeEmailDTO.getEmail();
String code = changeEmailDTO.getCode(); String code = changeEmailDTO.getCode();
@ -282,7 +283,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
map.put("email", email); map.put("email", email);
} }
} }
return map; throw new CaptchaException();
} }
@Override @Override

View File

@ -4,6 +4,7 @@ import com.rymcu.forest.core.result.GlobalResult;
import com.rymcu.forest.core.result.GlobalResultGenerator; import com.rymcu.forest.core.result.GlobalResultGenerator;
import com.rymcu.forest.entity.Comment; import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.service.CommentService; import com.rymcu.forest.service.CommentService;
import com.rymcu.forest.util.UserUtils;
import com.rymcu.forest.web.api.exception.BaseApiException; import com.rymcu.forest.web.api.exception.BaseApiException;
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;
@ -12,8 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException; import java.util.Objects;
import java.util.Map;
/** /**
* @author ronger * @author ronger
@ -26,8 +26,9 @@ public class CommentController {
private CommentService commentService; private CommentService commentService;
@PostMapping("/post") @PostMapping("/post")
public GlobalResult postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException { public GlobalResult<Comment> postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException {
Map map = commentService.postComment(comment,request); comment.setCommentAuthorId(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser());
return GlobalResultGenerator.genSuccessResult(map); comment = commentService.postComment(comment,request);
return GlobalResultGenerator.genSuccessResult(comment);
} }
} }

View File

@ -9,7 +9,6 @@ import com.rymcu.forest.core.service.log.annotation.VisitLogger;
import com.rymcu.forest.dto.*; import com.rymcu.forest.dto.*;
import com.rymcu.forest.entity.User; import com.rymcu.forest.entity.User;
import com.rymcu.forest.service.*; import com.rymcu.forest.service.*;
import com.rymcu.forest.util.Utils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -75,9 +74,9 @@ public class CommonApiController {
} }
@PostMapping("/login") @PostMapping("/login")
public GlobalResult<Map> login(@RequestBody User user) { public GlobalResult<TokenUser> login(@RequestBody User user) {
Map map = userService.login(user.getAccount(), user.getPassword()); TokenUser tokenUser = userService.login(user.getAccount(), user.getPassword());
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(tokenUser);
} }
@GetMapping("/heartbeat") @GetMapping("/heartbeat")
@ -117,11 +116,9 @@ public class CommonApiController {
@GetMapping("/portfolio/{id}") @GetMapping("/portfolio/{id}")
@VisitLogger @VisitLogger
public GlobalResult<Map<String, Object>> portfolio(@PathVariable Long id) { public GlobalResult<PortfolioDTO> portfolio(@PathVariable Long id) {
PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(id, 1); PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(id, 1);
Map<String, Object> map = new HashMap<>(1); return GlobalResultGenerator.genSuccessResult(portfolioDTO);
map.put("portfolio", portfolioDTO);
return GlobalResultGenerator.genSuccessResult(map);
} }
@GetMapping("/portfolio/{id}/articles") @GetMapping("/portfolio/{id}/articles")
@ -150,10 +147,8 @@ public class CommonApiController {
@GetMapping("/product/{id}") @GetMapping("/product/{id}")
@VisitLogger @VisitLogger
public GlobalResult<Map<String, Object>> product(@PathVariable Integer id) { public GlobalResult<ProductDTO> product(@PathVariable Integer id) {
ProductDTO productDTO = productService.findProductDTOById(id, 1); ProductDTO productDTO = productService.findProductDTOById(id, 1);
Map<String, Object> map = new HashMap<>(1); return GlobalResultGenerator.genSuccessResult(productDTO);
map.put("product", productDTO);
return GlobalResultGenerator.genSuccessResult(map);
} }
} }

View File

@ -6,8 +6,11 @@ import com.rymcu.forest.core.service.security.annotation.AuthorshipInterceptor;
import com.rymcu.forest.dto.PortfolioArticleDTO; import com.rymcu.forest.dto.PortfolioArticleDTO;
import com.rymcu.forest.dto.PortfolioDTO; import com.rymcu.forest.dto.PortfolioDTO;
import com.rymcu.forest.entity.Portfolio; import com.rymcu.forest.entity.Portfolio;
import com.rymcu.forest.entity.User;
import com.rymcu.forest.enumerate.Module; import com.rymcu.forest.enumerate.Module;
import com.rymcu.forest.service.PortfolioService; import com.rymcu.forest.service.PortfolioService;
import com.rymcu.forest.service.UserService;
import com.rymcu.forest.util.UserUtils;
import com.rymcu.forest.web.api.exception.BaseApiException; import com.rymcu.forest.web.api.exception.BaseApiException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -24,6 +27,8 @@ public class PortfolioController {
@Resource @Resource
private PortfolioService portfolioService; private PortfolioService portfolioService;
@Resource
private UserService userService;
@GetMapping("/detail/{idPortfolio}") @GetMapping("/detail/{idPortfolio}")
public GlobalResult detail(@PathVariable Long idPortfolio,@RequestParam(defaultValue = "0") Integer type) { public GlobalResult detail(@PathVariable Long idPortfolio,@RequestParam(defaultValue = "0") Integer type) {
@ -76,8 +81,11 @@ public class PortfolioController {
@DeleteMapping("/delete") @DeleteMapping("/delete")
@AuthorshipInterceptor(moduleName = Module.PORTFOLIO) @AuthorshipInterceptor(moduleName = Module.PORTFOLIO)
public GlobalResult delete(Long idPortfolio) throws BaseApiException { public GlobalResult delete(Long idPortfolio) throws BaseApiException, IllegalAccessException {
Map map = portfolioService.deletePortfolio(idPortfolio); User user = UserUtils.getCurrentUserByToken();
Long idUser = user.getIdUser();
Integer roleWeights = userService.findRoleWeightsByUser(idUser);
boolean map = portfolioService.deletePortfolio(idPortfolio, idUser, roleWeights);
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(map);
} }