关注用户功能

This commit is contained in:
x ronger 2020-09-09 22:34:25 +08:00
parent 0a8f00d89e
commit e42e9e940e
10 changed files with 161 additions and 9 deletions

View File

@ -0,0 +1,19 @@
package com.rymcu.vertical.mapper;
import com.rymcu.vertical.core.mapper.Mapper;
import com.rymcu.vertical.entity.Follow;
import org.apache.ibatis.annotations.Param;
/**
* @author ronger
*/
public interface FollowMapper extends Mapper<Follow> {
/**
* 判断是否关注
* @param followingId
* @param followerId
* @param followingType
* @return
*/
Boolean isFollow(@Param("followingId") Integer followingId, @Param("followerId") Integer followerId, @Param("followingType") String followingType);
}

View File

@ -0,0 +1,35 @@
package com.rymcu.vertical.service;
import com.rymcu.vertical.core.service.Service;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.web.api.exception.BaseApiException;
/**
* @author ronger
*/
public interface FollowService extends Service<Follow> {
/**
* 判断是否关注
* @param followingId
* @param followingType
* @return
* @throws BaseApiException
*/
Boolean isFollow(Integer followingId, String followingType) throws BaseApiException;
/**
* 关注操作
* @param follow
* @return
* @throws BaseApiException
*/
Boolean follow(Follow follow) throws BaseApiException;
/**
* 取消关注操作
* @param follow
* @return
* @throws BaseApiException
*/
Boolean cancelFollow(Follow follow) throws BaseApiException;
}

View File

@ -118,7 +118,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
String articleTags = article.getArticleTags(); String articleTags = article.getArticleTags();
String articleContent = article.getArticleContent(); String articleContent = article.getArticleContent();
String articleContentHtml = article.getArticleContentHtml(); String articleContentHtml = article.getArticleContentHtml();
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
String reservedTag = checkTags(articleTags); String reservedTag = checkTags(articleTags);
boolean notification = false; boolean notification = false;
if (StringUtils.isNotBlank(reservedTag)) { if (StringUtils.isNotBlank(reservedTag)) {
@ -249,7 +249,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
@Override @Override
public Map share(Integer id) throws BaseApiException { public Map share(Integer id) throws BaseApiException {
Article article = articleMapper.selectByPrimaryKey(id); Article article = articleMapper.selectByPrimaryKey(id);
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink()); StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink());
shareUrl.append("?s=").append(user.getNickname()); shareUrl.append("?s=").append(user.getNickname());
Map map = new HashMap(1); Map map = new HashMap(1);
@ -259,7 +259,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
@Override @Override
public List<ArticleDTO> findDrafts() throws BaseApiException { public List<ArticleDTO> findDrafts() throws BaseApiException {
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
List<ArticleDTO> list = articleMapper.selectDrafts(user.getIdUser()); List<ArticleDTO> list = articleMapper.selectDrafts(user.getIdUser());
list.forEach(article -> { list.forEach(article -> {
genArticle(article, 0); genArticle(article, 0);

View File

@ -0,0 +1,45 @@
package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.service.AbstractService;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.mapper.FollowMapper;
import com.rymcu.vertical.service.FollowService;
import com.rymcu.vertical.util.UserUtils;
import com.rymcu.vertical.web.api.exception.BaseApiException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author ronger
*/
@Service
public class FollowServiceImpl extends AbstractService<Follow> implements FollowService {
@Resource
private FollowMapper followMapper;
@Override
public Boolean isFollow(Integer followingId, String followingType) throws BaseApiException {
User tokenUser = UserUtils.getCurrentUserByToken();
Boolean b = followMapper.isFollow(followingId, tokenUser.getIdUser(), followingType);
return b;
}
@Override
public Boolean follow(Follow follow) throws BaseApiException {
User tokenUser = UserUtils.getCurrentUserByToken();
follow.setFollowerId(tokenUser.getIdUser());
int result = followMapper.insertSelective(follow);
return result > 0;
}
@Override
public Boolean cancelFollow(Follow follow) throws BaseApiException {
User tokenUser = UserUtils.getCurrentUserByToken();
follow.setFollowerId(tokenUser.getIdUser());
int result = followMapper.delete(follow);
return result == 0;
}
}

View File

@ -62,7 +62,7 @@ public class PortfolioServiceImpl extends AbstractService<Portfolio> implements
@Override @Override
public Portfolio postPortfolio(Portfolio portfolio) throws BaseApiException { public Portfolio postPortfolio(Portfolio portfolio) throws BaseApiException {
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
if (portfolio.getIdPortfolio() == null || portfolio.getIdPortfolio() == 0) { if (portfolio.getIdPortfolio() == null || portfolio.getIdPortfolio() == 0) {
portfolio.setPortfolioAuthorId(user.getIdUser()); portfolio.setPortfolioAuthorId(user.getIdUser());
portfolio.setCreatedTime(new Date()); portfolio.setCreatedTime(new Date());
@ -78,7 +78,7 @@ public class PortfolioServiceImpl extends AbstractService<Portfolio> implements
@Override @Override
public Map findUnbindArticles(Integer page, Integer rows, String searchText, Integer idPortfolio) throws BaseApiException { public Map findUnbindArticles(Integer page, Integer rows, String searchText, Integer idPortfolio) throws BaseApiException {
Map map = new HashMap(1); Map map = new HashMap(1);
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
Portfolio portfolio = portfolioMapper.selectByPrimaryKey(idPortfolio); Portfolio portfolio = portfolioMapper.selectByPrimaryKey(idPortfolio);
if (portfolio == null) { if (portfolio == null) {
map.put("message", "该作品集不存在或已被删除!"); map.put("message", "该作品集不存在或已被删除!");

View File

@ -39,7 +39,7 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
@Override @Override
@Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) @Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class })
public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException { public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException {
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
String articleTags = article.getArticleTags(); String articleTags = article.getArticleTags();
if(StringUtils.isNotBlank(articleTags)){ if(StringUtils.isNotBlank(articleTags)){
String[] tags = articleTags.split(","); String[] tags = articleTags.split(",");

View File

@ -25,7 +25,7 @@ public class UserUtils {
* 通过token获取当前用户的信息 * 通过token获取当前用户的信息
* @return * @return
*/ */
public static User getWxCurrentUser() throws BaseApiException { public static User getCurrentUserByToken() throws BaseApiException {
String authHeader = ContextHolderUtils.getRequest().getHeader(JwtConstants.AUTHORIZATION); String authHeader = ContextHolderUtils.getRequest().getHeader(JwtConstants.AUTHORIZATION);
if (authHeader == null) { if (authHeader == null) {
return null; return null;

View File

@ -0,0 +1,45 @@
package com.rymcu.vertical.web.api.follow;
import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.jwt.def.JwtConstants;
import com.rymcu.vertical.service.FollowService;
import com.rymcu.vertical.web.api.exception.BaseApiException;
import com.rymcu.vertical.web.api.exception.ErrorCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @author ronger
*/
@RestController
@RequestMapping("/api/v1/follow")
public class FollowController {
@Resource
private FollowService followService;
@GetMapping("/is-follow")
public GlobalResult isFollow(@RequestParam Integer followingId, @RequestParam String followingType) throws BaseApiException {
Boolean b = followService.isFollow(followingId, followingType);
return GlobalResultGenerator.genSuccessResult(b);
}
@PostMapping
public GlobalResult follow(@RequestBody Follow follow) throws BaseApiException {
Boolean b = followService.follow(follow);
return GlobalResultGenerator.genSuccessResult(b);
}
@PostMapping("cancel-follow")
public GlobalResult cancelFollow(@RequestBody Follow follow) throws BaseApiException {
Boolean b = followService.cancelFollow(follow);
return GlobalResultGenerator.genSuccessResult(b);
}
}

View File

@ -30,7 +30,7 @@ public class NotificationController {
@GetMapping("/all") @GetMapping("/all")
public GlobalResult notifications(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { public GlobalResult notifications(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException {
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
PageHelper.startPage(page, rows); PageHelper.startPage(page, rows);
List<NotificationDTO> list = notificationService.findNotifications(user.getIdUser()); List<NotificationDTO> list = notificationService.findNotifications(user.getIdUser());
PageInfo<NotificationDTO> pageInfo = new PageInfo(list); PageInfo<NotificationDTO> pageInfo = new PageInfo(list);
@ -40,7 +40,7 @@ public class NotificationController {
@GetMapping("/unread") @GetMapping("/unread")
public GlobalResult unreadNotification(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { public GlobalResult unreadNotification(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException {
User user = UserUtils.getWxCurrentUser(); User user = UserUtils.getCurrentUserByToken();
PageHelper.startPage(page, rows); PageHelper.startPage(page, rows);
List<Notification> list = notificationService.findUnreadNotifications(user.getIdUser()); List<Notification> list = notificationService.findUnreadNotifications(user.getIdUser());
PageInfo<Notification> pageInfo = new PageInfo(list); PageInfo<Notification> pageInfo = new PageInfo(list);

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.rymcu.vertical.mapper.FollowMapper">
<select id="isFollow" resultType="java.lang.Boolean">
select ifnull((select true from vertical_follow where follower_id = #{followerId}
and following_id = #{followingId} and following_type = #{followingType}), false)
</select>
</mapper>