您当前的位置: 首页 > 

java持续实践

暂无认证

  • 0浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

黑马十次方项目day07-13添加好友更新关注数和粉丝数

java持续实践 发布时间:2019-02-17 19:43:01 ,浏览量:0

文章目录
      • 一.Feign接口的编写
      • 二.Controller的编写
      • 三.测试

一.Feign接口的编写

在tensquare_friend中,编写Feign接口,进行服务的调用. 代码如下, 注意在@RequestMapping中加上/user/的路径 并且在@PathVariable中,填写上对应的路径值

package com.tensquare.friend.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 类名称:UserClient
 * 类描述:TODO
 *
 * @author: taohongchao
 * 创建时间:2019/2/17 19:21
 * Version 1.0
 */
@FeignClient("tensquare-user")
public interface UserClient {

    /**
     * 方法名: updateFanscountAndFollowcount
     * 方法描述: 更新粉丝数和关注数
     * 修改日期: 2019/2/17 18:59
     * @param userid
     * @param friendid
     * @return void
     * @author taohongchao
     * @throws
     */
    @RequestMapping(value = "/user/{userid}/{friendid}/{type}",method = RequestMethod.PUT)
    public void updateFanscountAndFollowcount(@PathVariable("type") int type,
                                              @PathVariable("userid") String userid,
                                              @PathVariable("friendid") String friendid);

}

二.Controller的编写

在tensquare_friend的FriendController中,进行修改 首先注入UserClient 接着当类型为添加好友, 并且添加成功时,进行Feign的调用 完整的代码如下

package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 类名称:FriendController
 * 类描述:  交友业务的Controller层
 *
 * @author: taohongchao
 * 创建时间:2019/2/17 15:05
 * Version 1.0
 */

@RestController
@CrossOrigin
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FriendService friendService;

    @Autowired
    private UserClient  userClient;


    @RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid, @PathVariable String type) {
        //验证是否登录, 并且拿到登录用户的id
        Claims claims = (Claims) request.getAttribute("claims_user");
        if (claims == null) {
            //说明当前用户没有user角色
            return new Result(false, StatusCode.LOGINERROR, "权限不足");
        }
        //得到当前登录的用户id
        String userId = claims.getId();

        //判断是添加好友,还是添加非好友
        if (type != null && !"".equals(type)) {

            if ("1".equals(type)) {
                //添加好友
                int flag = friendService.addFriend(userId, friendid);

                if (flag == 0) {
                    return new Result(false, StatusCode.ERROR, "不能重复添加好友");
                }
                if (flag == 1) {
                    userClient.updateFanscountAndFollowcount(1,userId,friendid);
                    return new Result(true, StatusCode.OK, "添加成功");
                }

            } else if ("2".equals(type)) {
                //添加非好友
                int flag = friendService.addNoFriend(userId, friendid);

                if (flag == 0) {
                    return new Result(false, StatusCode.ERROR, "不能重复添加非好友");
                }
                if (flag == 1) {
                    return new Result(true, StatusCode.OK, "添加成功");
                }

            }
            return new Result(false, StatusCode.ERROR, "参数异常");
        } else {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }

    }


}

三.测试

使用id为111的用户进行登录.获取token. 并且在数据库中把fanscount和followcount字段进行初始化为0. 带上登录的token,发送如下的put请求 http://localhost:9010/friend/like/222/1 代表111用户关注了用户222. 当请求完成后 可以在数据库中,看到如下图所示, 用户id为111的followcount数量加一了. 用户id为222的fanscount数量加一了.

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0368s