Browse Source

关注用户&&取消关注

Antiloveg 8 years ago
parent
commit
b2a55ccdb0

+ 8 - 0
schema/2017-06-13.sql

@@ -0,0 +1,8 @@
+CREATE TABLE `user_interest` (
+  `id` VARCHAR(36) NOT NULL,
+  `from_uid` VARCHAR(36) NOT NULL COMMENT '关注发起用户ID',
+  `to_uid` VARCHAR(36) NOT NULL COMMENT '被关注用户ID',
+  `create_time` DATETIME NOT NULL COMMENT '关注时间',
+  PRIMARY KEY (`id`))
+ENGINE = InnoDB
+COMMENT = '用户关注';

+ 21 - 0
src/main/java/com/goafanti/common/dao/UserInterestMapper.java

@@ -0,0 +1,21 @@
+package com.goafanti.common.dao;
+
+import org.apache.ibatis.annotations.Param;
+
+import com.goafanti.common.model.UserInterest;
+
+public interface UserInterestMapper {
+    int deleteByPrimaryKey(String id);
+
+    int insert(UserInterest record);
+
+    int insertSelective(UserInterest record);
+
+    UserInterest selectByPrimaryKey(String id);
+
+    int updateByPrimaryKeySelective(UserInterest record);
+
+    int updateByPrimaryKey(UserInterest record);
+
+	UserInterest findByFromUidAndToUid(@Param("formUid")String formUid, @Param("toUid")String toUid);
+}

+ 90 - 0
src/main/java/com/goafanti/common/mapper/UserInterestMapper.xml

@@ -0,0 +1,90 @@
+<?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.goafanti.common.dao.UserInterestMapper" >
+  <resultMap id="BaseResultMap" type="com.goafanti.common.model.UserInterest" >
+    <id column="id" property="id" jdbcType="VARCHAR" />
+    <result column="from_uid" property="fromUid" jdbcType="VARCHAR" />
+    <result column="to_uid" property="toUid" jdbcType="VARCHAR" />
+    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
+  </resultMap>
+  <sql id="Base_Column_List" >
+    id, from_uid, to_uid, create_time
+  </sql>
+  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
+    select 
+    <include refid="Base_Column_List" />
+    from user_interest
+    where id = #{id,jdbcType=VARCHAR}
+  </select>
+  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
+    delete from user_interest
+    where id = #{id,jdbcType=VARCHAR}
+  </delete>
+  <insert id="insert" parameterType="com.goafanti.common.model.UserInterest" >
+    insert into user_interest (id, from_uid, to_uid, 
+      create_time)
+    values (#{id,jdbcType=VARCHAR}, #{fromUid,jdbcType=VARCHAR}, #{toUid,jdbcType=VARCHAR}, 
+      #{createTime,jdbcType=TIMESTAMP})
+  </insert>
+  <insert id="insertSelective" parameterType="com.goafanti.common.model.UserInterest" >
+    insert into user_interest
+    <trim prefix="(" suffix=")" suffixOverrides="," >
+      <if test="id != null" >
+        id,
+      </if>
+      <if test="fromUid != null" >
+        from_uid,
+      </if>
+      <if test="toUid != null" >
+        to_uid,
+      </if>
+      <if test="createTime != null" >
+        create_time,
+      </if>
+    </trim>
+    <trim prefix="values (" suffix=")" suffixOverrides="," >
+      <if test="id != null" >
+        #{id,jdbcType=VARCHAR},
+      </if>
+      <if test="fromUid != null" >
+        #{fromUid,jdbcType=VARCHAR},
+      </if>
+      <if test="toUid != null" >
+        #{toUid,jdbcType=VARCHAR},
+      </if>
+      <if test="createTime != null" >
+        #{createTime,jdbcType=TIMESTAMP},
+      </if>
+    </trim>
+  </insert>
+  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.UserInterest" >
+    update user_interest
+    <set >
+      <if test="fromUid != null" >
+        from_uid = #{fromUid,jdbcType=VARCHAR},
+      </if>
+      <if test="toUid != null" >
+        to_uid = #{toUid,jdbcType=VARCHAR},
+      </if>
+      <if test="createTime != null" >
+        create_time = #{createTime,jdbcType=TIMESTAMP},
+      </if>
+    </set>
+    where id = #{id,jdbcType=VARCHAR}
+  </update>
+  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.UserInterest" >
+    update user_interest
+    set from_uid = #{fromUid,jdbcType=VARCHAR},
+      to_uid = #{toUid,jdbcType=VARCHAR},
+      create_time = #{createTime,jdbcType=TIMESTAMP}
+    where id = #{id,jdbcType=VARCHAR}
+  </update>
+  
+  <select id="findByFromUidAndToUid" resultMap="BaseResultMap">
+   	select 
+    <include refid="Base_Column_List" />
+    from user_interest
+    where from_uid = #{fromUid,jdbcType=VARCHAR}
+    and to_uid =  #{toUid,jdbcType=VARCHAR}
+  </select>
+</mapper>

+ 54 - 0
src/main/java/com/goafanti/common/model/UserInterest.java

@@ -0,0 +1,54 @@
+package com.goafanti.common.model;
+
+import java.util.Date;
+
+public class UserInterest {
+    private String id;
+
+    /**
+    * 关注发起用户ID
+    */
+    private String fromUid;
+
+    /**
+    * 被关注用户ID
+    */
+    private String toUid;
+
+    /**
+    * 关注时间
+    */
+    private Date createTime;
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getFromUid() {
+        return fromUid;
+    }
+
+    public void setFromUid(String fromUid) {
+        this.fromUid = fromUid;
+    }
+
+    public String getToUid() {
+        return toUid;
+    }
+
+    public void setToUid(String toUid) {
+        this.toUid = toUid;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+}

+ 70 - 0
src/main/java/com/goafanti/user/controller/UserInterestApiController.java

@@ -0,0 +1,70 @@
+package com.goafanti.user.controller;
+
+import javax.annotation.Resource;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.goafanti.common.bo.Result;
+import com.goafanti.common.constant.ErrorConstants;
+import com.goafanti.common.controller.CertifyApiController;
+import com.goafanti.common.model.UserInterest;
+import com.goafanti.core.shiro.token.TokenManager;
+import com.goafanti.user.service.UserInterestService;
+
+@RestController
+@RequestMapping(value = "/api/user/interest")
+public class UserInterestApiController extends CertifyApiController{
+	@Resource
+	private UserInterestService userInterestService;
+	
+	/**
+	 * 关注用户列表
+	 */
+	@RequestMapping(value = "/interestUserList", method = RequestMethod.GET)
+	public Result interestUserList(){
+		Result res = new Result();
+		return res;
+	}
+	
+	/**
+	 * 关注用户
+	 */
+	@RequestMapping(value = "/interestUser", method = RequestMethod.POST)
+	public Result interestUser(String toUid){
+		Result res = new Result();
+		if (StringUtils.isBlank(toUid)){
+			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "被关注用户ID"));
+			return res;
+		}
+		UserInterest ui = userInterestService.findByFromUidAndToUid(TokenManager.getUserId(), toUid);
+		if (null != ui){
+			res.getError().add(buildError("", "当前用户已关注!"));
+			return res;
+		}
+		
+		res.setData(userInterestService.insert(toUid));
+		return res;
+	}
+	
+	/**
+	 * 取消关注
+	 */
+	@RequestMapping(value = "/cancelInterest", method = RequestMethod.POST)
+	public Result cancelInterest(String interestId){
+		Result res = new Result();
+		if (StringUtils.isBlank(interestId)){
+			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "关注ID"));
+			return res;
+		}
+		UserInterest ui = userInterestService.findByPrimaryKey(interestId);
+		if (null == ui || !TokenManager.getUserId().equals(ui.getFromUid())){
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "关注ID"));
+			return res;
+		}
+		res.setData(userInterestService.deleteByPrimaryKey(interestId));
+		return res;
+	}
+}

+ 15 - 0
src/main/java/com/goafanti/user/service/UserInterestService.java

@@ -0,0 +1,15 @@
+package com.goafanti.user.service;
+
+import com.goafanti.common.model.UserInterest;
+
+public interface UserInterestService {
+
+	UserInterest findByFromUidAndToUid(String formUid, String toUid);
+
+	int insert(String uid);
+
+	UserInterest findByPrimaryKey(String id);
+
+	int deleteByPrimaryKey(String interestId);
+
+}

+ 44 - 0
src/main/java/com/goafanti/user/service/impl/UserInterestServiceImpl.java

@@ -0,0 +1,44 @@
+package com.goafanti.user.service.impl;
+
+import java.util.Calendar;
+import java.util.UUID;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.goafanti.common.dao.UserInterestMapper;
+import com.goafanti.common.model.UserInterest;
+import com.goafanti.core.shiro.token.TokenManager;
+import com.goafanti.user.service.UserInterestService;
+@Service
+public class UserInterestServiceImpl implements UserInterestService {
+	@Autowired
+	private UserInterestMapper userInterestMapper;
+
+	@Override
+	public UserInterest findByFromUidAndToUid(String formUid, String toUid) {
+		return userInterestMapper.findByFromUidAndToUid(formUid, toUid);
+	}
+
+	@Override
+	public int insert(String toUid) {
+		UserInterest ui = new UserInterest();
+		ui.setId(UUID.randomUUID().toString());
+		ui.setFromUid(TokenManager.getUserId());
+		ui.setToUid(toUid);
+		Calendar now = Calendar.getInstance();
+		now.set(Calendar.MILLISECOND, 0);
+		ui.setCreateTime(now.getTime());
+		return userInterestMapper.insert(ui);
+	}
+
+	@Override
+	public UserInterest findByPrimaryKey(String id) {
+		return userInterestMapper.selectByPrimaryKey(id);
+	}
+
+	@Override
+	public int deleteByPrimaryKey(String interestId) {
+		return userInterestMapper.deleteByPrimaryKey(id);
+	}
+}