Netty+SpringBoot+FastDFS+Html5實現聊天App詳解(三)

Netty+SpringBoot+FastDFS+Html5實現聊天App,項目介紹

Netty+SpringBoot+FastDFS+Html5實現聊天App,項目github鏈接

本章完整代碼鏈接


本節主要講解聊天App PigChat中關於好友申請的發送與接受。

包含以下內容:

(1)搜索好友接口

(2)發送添加好友申請的接口

(3)接受添加好友申請的接口

搜索好友接口

定義枚舉類型 SearchFriendsStatusEnum,表示添加好友的前置狀態

    SUCCESS(0, "OK"),
    USER_NOT_EXIST(1, "無此用戶..."),    
    NOT_YOURSELF(2, "不能添加你自己..."),            
    ALREADY_FRIENDS(3, "該用戶已經是你的好友...");



在service中定義搜索朋友的前置條件判斷的方法preconditionSearchFriends。

傳入的是用戶的Id以及搜索的用戶的名稱。

【1】首先根據搜索的用戶的名稱查找是否存在這個用戶。
【2】如果搜索的用戶不存在,則返回[無此用戶]。
【3】如果搜索的用戶是你自己,則返回[不能添加自己]。
【4】如果搜索的用戶已經是你的好友,則返回[該用戶已經是你的好友]。
【5】否則返回成功。

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Integer preconditionSearchFriends(String myUserId, String friendUsername) {

        //1. 查找要添加的朋友是否存在
        Users user = queryUserInfoByUsername(friendUsername);
        
        // 2. 搜索的用戶如果不存在,返回[無此用戶]
        if (user == null) {
            return SearchFriendsStatusEnum.USER_NOT_EXIST.status;
        }
        
        // 3. 搜索賬號是你自己,返回[不能添加自己]
        if (user.getId().equals(myUserId)) {
            return SearchFriendsStatusEnum.NOT_YOURSELF.status;
        }
        
        // 4. 搜索的朋友已經是你的好友,返回[該用戶已經是你的好友]
        Example mfe = new Example(MyFriends.class);
        Criteria mfc = mfe.createCriteria();
        mfc.andEqualTo("myUserId", myUserId);
        mfc.andEqualTo("myFriendUserId", user.getId());
        MyFriends myFriendsRel = myFriendsMapper.selectOneByExample(mfe);
        if (myFriendsRel != null) {
            return SearchFriendsStatusEnum.ALREADY_FRIENDS.status;
        }
        
        //返回成功
        return SearchFriendsStatusEnum.SUCCESS.status;
    }



在controller中創建搜索好友接口 searchUser。
傳入的是用戶的Id,以及要搜索的用戶的名字。

【0】首先判斷傳入的參數是否爲空。
【1】通過userService的preconditionSearchFriends方法得到前置條件。
【2】如果搜索前置條件爲成功,則向前端返回搜索用戶的信息。
【3】否則搜索失敗。

    /**
     * @Description: 搜索好友接口, 根據賬號做匹配查詢而不是模糊查詢
     */
    @PostMapping("/search")
    public IMoocJSONResult searchUser(String myUserId, String friendUsername)
            throws Exception {
        
        // 0. 判斷 myUserId friendUsername 不能爲空
        if (StringUtils.isBlank(myUserId) 
                || StringUtils.isBlank(friendUsername)) {
            return IMoocJSONResult.errorMsg("");
        }
        
        // 前置條件 - 1. 搜索的用戶如果不存在,返回[無此用戶]
        // 前置條件 - 2. 搜索賬號是你自己,返回[不能添加自己]
        // 前置條件 - 3. 搜索的朋友已經是你的好友,返回[該用戶已經是你的好友]
        //1. 得到前置條件狀態
        Integer status = userService.preconditionSearchFriends(myUserId, friendUsername);
        //2. 搜索成功,返回搜索用戶的信息
        if (status == SearchFriendsStatusEnum.SUCCESS.status) {
            Users user = userService.queryUserInfoByUsername(friendUsername);
            UsersVO userVO = new UsersVO();
            BeanUtils.copyProperties(user, userVO);
            return IMoocJSONResult.ok(userVO);
        } else {
        //3. 搜索失敗
            String errorMsg = SearchFriendsStatusEnum.getMsgByKey(status);
            return IMoocJSONResult.errorMsg(errorMsg);
        }
    }

發送添加好友申請的接口

在service中定義添加好友請求記錄,保存到數據庫的sendFriendRequest方法。
傳入的是添加好友記錄的發送方——用戶的Id,以及記錄的接收方——想要添加的朋友的名稱。

【1】首先根據用戶名把朋友信息查詢出來。
【2】然後查詢發送好友請求記錄表。
【3】如果不是你的好友,並且好友記錄沒有添加,則新增好友請求記錄。這樣可以保證打你發送了兩次請求之後,數據庫中仍然只記錄一次請求的數據。

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void sendFriendRequest(String myUserId, String friendUsername) {
        
        // 1. 根據用戶名把朋友信息查詢出來
        Users friend = queryUserInfoByUsername(friendUsername);
        
        // 2. 查詢發送好友請求記錄表
        Example fre = new Example(FriendsRequest.class);
        Criteria frc = fre.createCriteria();
        frc.andEqualTo("sendUserId", myUserId);
        frc.andEqualTo("acceptUserId", friend.getId());
        FriendsRequest friendRequest = friendsRequestMapper.selectOneByExample(fre);
        if (friendRequest == null) {
            // 3. 如果不是你的好友,並且好友記錄沒有添加,則新增好友請求記錄
            String requestId = sid.nextShort();
            
            FriendsRequest request = new FriendsRequest();
            request.setId(requestId);
            request.setSendUserId(myUserId);
            request.setAcceptUserId(friend.getId());
            request.setRequestDateTime(new Date());
            friendsRequestMapper.insert(request);
        }
    }

在controller中創建發送添加好友請求的接口。
傳入的是添加好友記錄的發送方——用戶的Id,以及記錄的接收方——想要添加的朋友的名稱。

【0】首先判斷傳入參數不爲空。
【1】然後判斷前置條件,若爲成功則通過userService的sendFriendRequest方法發送好友請求,否則返回失敗。

    /**
     * @Description: 發送添加好友的請求
     */
    @PostMapping("/addFriendRequest")
    public IMoocJSONResult addFriendRequest(String myUserId, String friendUsername)
            throws Exception {
        
        // 0. 判斷 myUserId friendUsername 不能爲空
        if (StringUtils.isBlank(myUserId) 
                || StringUtils.isBlank(friendUsername)) {
            return IMoocJSONResult.errorMsg("");
        }
        
        // 前置條件 - 1. 搜索的用戶如果不存在,返回[無此用戶]
        // 前置條件 - 2. 搜索賬號是你自己,返回[不能添加自己]
        // 前置條件 - 3. 搜索的朋友已經是你的好友,返回[該用戶已經是你的好友]
        // 1. 判斷前置條件
        Integer status = userService.preconditionSearchFriends(myUserId, friendUsername);
        if (status == SearchFriendsStatusEnum.SUCCESS.status) {
            userService.sendFriendRequest(myUserId, friendUsername);
        } else {
            String errorMsg = SearchFriendsStatusEnum.getMsgByKey(status);
            return IMoocJSONResult.errorMsg(errorMsg);
        }
        
        return IMoocJSONResult.ok();
    }

最終實現效果

在這裏插入圖片描述

接受添加好友申請的接口

在service中定義查詢好友請求列表的queryFriendRequestList方法。

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<FriendRequestVO> queryFriendRequestList(String acceptUserId) {
        return usersMapperCustom.queryFriendRequestList(acceptUserId);
    }



在controller中定義接受添加好友請求的接口queryFriendRequests。

    /**
     * @Description: 發送添加好友的請求
     */
    @PostMapping("/queryFriendRequests")
    public IMoocJSONResult queryFriendRequests(String userId) {
        
        // 0. 判斷不能爲空
        if (StringUtils.isBlank(userId)) {
            return IMoocJSONResult.errorMsg("");
        }
        
        // 1. 查詢用戶接受到的朋友申請
        return IMoocJSONResult.ok(userService.queryFriendRequestList(userId));
    }
    

最終實現效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章