79 行
3.1 KiB
Java
79 行
3.1 KiB
Java
|
|
package com.xuqm.im.controller;
|
||
|
|
|
||
|
|
import com.xuqm.common.model.ApiResponse;
|
||
|
|
import com.xuqm.im.entity.ImFriendEntity;
|
||
|
|
import com.xuqm.im.repository.ImFriendRepository;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
||
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
||
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
||
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
||
|
|
import org.springframework.web.bind.annotation.RestController;
|
||
|
|
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api/im/friends")
|
||
|
|
public class FriendController {
|
||
|
|
|
||
|
|
private final ImFriendRepository friendRepository;
|
||
|
|
|
||
|
|
public FriendController(ImFriendRepository friendRepository) {
|
||
|
|
this.friendRepository = friendRepository;
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping
|
||
|
|
public ResponseEntity<ApiResponse<List<String>>> listFriends(
|
||
|
|
@AuthenticationPrincipal String userId,
|
||
|
|
@RequestParam String appId) {
|
||
|
|
List<String> friendIds = friendRepository.findByAppIdAndUserId(appId, userId)
|
||
|
|
.stream()
|
||
|
|
.map(ImFriendEntity::getFriendId)
|
||
|
|
.toList();
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(friendIds));
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping
|
||
|
|
public ResponseEntity<ApiResponse<ImFriendEntity>> addFriend(
|
||
|
|
@AuthenticationPrincipal String userId,
|
||
|
|
@RequestParam String appId,
|
||
|
|
@RequestParam String friendId) {
|
||
|
|
// Insert userId -> friendId if not already present
|
||
|
|
ImFriendEntity forward = friendRepository
|
||
|
|
.findByAppIdAndUserIdAndFriendId(appId, userId, friendId)
|
||
|
|
.orElseGet(() -> {
|
||
|
|
ImFriendEntity e = new ImFriendEntity();
|
||
|
|
e.setAppId(appId);
|
||
|
|
e.setUserId(userId);
|
||
|
|
e.setFriendId(friendId);
|
||
|
|
return friendRepository.save(e);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Insert friendId -> userId bi-directionally if not already present
|
||
|
|
friendRepository.findByAppIdAndUserIdAndFriendId(appId, friendId, userId)
|
||
|
|
.orElseGet(() -> {
|
||
|
|
ImFriendEntity e = new ImFriendEntity();
|
||
|
|
e.setAppId(appId);
|
||
|
|
e.setUserId(friendId);
|
||
|
|
e.setFriendId(userId);
|
||
|
|
return friendRepository.save(e);
|
||
|
|
});
|
||
|
|
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(forward));
|
||
|
|
}
|
||
|
|
|
||
|
|
@DeleteMapping("/{friendId}")
|
||
|
|
public ResponseEntity<ApiResponse<Void>> removeFriend(
|
||
|
|
@AuthenticationPrincipal String userId,
|
||
|
|
@PathVariable String friendId,
|
||
|
|
@RequestParam String appId) {
|
||
|
|
// Remove both directions
|
||
|
|
friendRepository.deleteByAppIdAndUserIdAndFriendId(appId, userId, friendId);
|
||
|
|
friendRepository.deleteByAppIdAndUserIdAndFriendId(appId, friendId, userId);
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(null));
|
||
|
|
}
|
||
|
|
}
|