Netty+SpringBoot+FastDFS+Html5實現聊天App,項目介紹。
Netty+SpringBoot+FastDFS+Html5實現聊天App,項目github鏈接。
本章完整代碼鏈接。
本章內容
(1) 查詢好友列表的接口
(2)通過或忽略好友請求的接口
(3)添加好友功能展示
查詢好友列表的接口
/**
* @Description: 查詢我的好友列表
*/
@PostMapping("/myFriends")
public IMoocJSONResult myFriends(String userId) {
// 0. userId 判斷不能爲空
if (StringUtils.isBlank(userId)) {
return IMoocJSONResult.errorMsg("");
}
// 1. 數據庫查詢好友列表
List<MyFriendsVO> myFirends = userService.queryMyFriends(userId);
return IMoocJSONResult.ok(myFirends);
}
通過或忽略好友請求的接口
定義枚舉類型
/**
*
* @Description: 忽略或者通過 好友請求的枚舉
*/
public enum OperatorFriendRequestTypeEnum {
IGNORE(0, "忽略"),
PASS(1, "通過");
public final Integer type;
public final String msg;
OperatorFriendRequestTypeEnum(Integer type, String msg){
this.type = type;
this.msg = msg;
}
public Integer getType() {
return type;
}
public static String getMsgByType(Integer type) {
for (OperatorFriendRequestTypeEnum operType : OperatorFriendRequestTypeEnum.values()) {
if (operType.getType() == type) {
return operType.msg;
}
}
return null;
}
}
controller中提供通過或忽略好友請求的接口
/**
* @Description: 接受方 通過或者忽略朋友請求
*/
@PostMapping("/operFriendRequest")
public IMoocJSONResult operFriendRequest(String acceptUserId, String sendUserId,
Integer operType) {
// 0. acceptUserId sendUserId operType 判斷不能爲空
if (StringUtils.isBlank(acceptUserId)
|| StringUtils.isBlank(sendUserId)
|| operType == null) {
return IMoocJSONResult.errorMsg("");
}
// 1. 如果operType 沒有對應的枚舉值,則直接拋出空錯誤信息
if (StringUtils.isBlank(OperatorFriendRequestTypeEnum.getMsgByType(operType))) {
return IMoocJSONResult.errorMsg("");
}
if (operType == OperatorFriendRequestTypeEnum.IGNORE.type) {
// 2. 判斷如果忽略好友請求,則直接刪除好友請求的數據庫表記錄
userService.deleteFriendRequest(sendUserId, acceptUserId);
} else if (operType == OperatorFriendRequestTypeEnum.PASS.type) {
// 3. 判斷如果是通過好友請求,則互相增加好友記錄到數據庫對應的表
// 然後刪除好友請求的數據庫表記錄
userService.passFriendRequest(sendUserId, acceptUserId);
}
// 4. 數據庫查詢好友列表
List<MyFriendsVO> myFirends = userService.queryMyFriends(acceptUserId);
// 5. 將查詢到的好友列表返回給前端
return IMoocJSONResult.ok(myFirends);
}