更新联系人相关功能:添加联系人控制器、服务层和Mapper实现
This commit is contained in:
@@ -3,6 +3,8 @@ package com.bao.dating.controller;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.service.ContactsService;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -12,6 +14,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/contacts")
|
||||
public class ContactsController {
|
||||
@Resource
|
||||
private ContactsService contactsService;
|
||||
@@ -42,4 +45,56 @@ public class ContactsController {
|
||||
result.put("data", friends);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 拉黑联系人接口
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被拉黑联系人ID
|
||||
* @return 接口响应
|
||||
*/
|
||||
@PostMapping("/blacklist/{userId}/{contactUserId}")
|
||||
public ResponseEntity<Map<String, Object>> blacklistContact(
|
||||
@PathVariable Long userId,
|
||||
@PathVariable Long contactUserId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
boolean success = contactsService.blacklistContact(userId, contactUserId);
|
||||
if (success) {
|
||||
result.put("code", 200);
|
||||
result.put("msg", "拉黑联系人成功");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.ok(result);
|
||||
} else {
|
||||
result.put("code", 500);
|
||||
result.put("msg", "拉黑联系人失败,联系人不存在或参数错误");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除联系人接口
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被删除联系人ID
|
||||
* @return 接口响应
|
||||
*/
|
||||
@PostMapping("/delete/{userId}/{contactUserId}")
|
||||
public ResponseEntity<Map<String, Object>> deleteContact(
|
||||
@PathVariable Long userId,
|
||||
@PathVariable Long contactUserId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
boolean success = contactsService.deleteContact(userId, contactUserId);
|
||||
if (success) {
|
||||
result.put("code", 200);
|
||||
result.put("msg", "删除联系人成功");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.ok(result);
|
||||
} else {
|
||||
result.put("code", 500);
|
||||
result.put("msg", "删除联系人失败,联系人不存在或参数错误");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,20 @@ public interface ContactMapper {
|
||||
*/
|
||||
List<Map<String, Object>> selectFriendsByUserId(@Param("userId") Long userId);
|
||||
|
||||
|
||||
/**
|
||||
* 拉黑联系人
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被拉黑的联系人ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
int blacklistContact(@Param("userId") Long userId, @Param("contactUserId") Long contactUserId);
|
||||
|
||||
/**
|
||||
* 删除联系人(逻辑删除)
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被删除的联系人ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
int deleteContact(@Param("userId") Long userId, @Param("contactUserId") Long contactUserId);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,20 @@ public interface ContactsService {
|
||||
List<Map<String, Object>> getFriendsByUserId(Long userId);
|
||||
|
||||
|
||||
/**
|
||||
* 拉黑联系人
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被拉黑联系人ID
|
||||
* @return 操作是否成功
|
||||
*/
|
||||
boolean blacklistContact(Long userId, Long contactUserId);
|
||||
|
||||
/**
|
||||
* 删除联系人(逻辑删除)
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被删除联系人ID
|
||||
* @return 操作是否成功
|
||||
*/
|
||||
boolean deleteContact(Long userId, Long contactUserId);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.bao.dating.service.ContactsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -19,4 +20,34 @@ public class ContactServiceImpl implements ContactsService {
|
||||
// 直接调用Mapper查询,无额外封装
|
||||
return contactMapper.selectFriendsByUserId(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉黑联系人
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean blacklistContact(Long userId, Long contactUserId) {
|
||||
// 参数校验
|
||||
if (userId == null || contactUserId == null || userId.equals(contactUserId)) {
|
||||
return false;
|
||||
}
|
||||
// 执行拉黑操作
|
||||
int affectRows = contactMapper.blacklistContact(userId, contactUserId);
|
||||
return affectRows > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除联系人
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean deleteContact(Long userId, Long contactUserId) {
|
||||
// 参数校验
|
||||
if (userId == null || contactUserId == null || userId.equals(contactUserId)) {
|
||||
return false;
|
||||
}
|
||||
// 执行删除操作
|
||||
int affectRows = contactMapper.deleteContact(userId, contactUserId);
|
||||
return affectRows > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,5 +21,29 @@
|
||||
AND c.relation_type != 3 -- 排除黑名单
|
||||
AND c.user_id != c.contact_user_id -- 排除自己
|
||||
</select>
|
||||
|
||||
|
||||
<!-- 拉黑联系人:更新relation_type为3(黑名单)、contact_status为3(已拉黑) -->
|
||||
<update id="blacklistContact">
|
||||
UPDATE contacts
|
||||
SET relation_type = 3,
|
||||
contact_status = 3,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE user_id = #{userId}
|
||||
AND contact_user_id = #{contactUserId}
|
||||
AND contact_status != 2
|
||||
</update>
|
||||
|
||||
<!-- 删除联系人:更新contact_status为2(已删除) -->
|
||||
<update id="deleteContact">
|
||||
UPDATE contacts
|
||||
SET contact_status = 2,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE user_id = #{userId}
|
||||
AND contact_user_id = #{contactUserId}
|
||||
AND contact_status != 2
|
||||
</update>
|
||||
</mapper>
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user