blob: a988c8fee2096cb6f0fce2abec1779d4ea8966c2 [file] [log] [blame]
package com.example.myproject.controller;
import com.example.myproject.entity.UserDynamic;
import com.example.myproject.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/echo/dynamic")
public class DynamicController {
@Autowired
private DynamicService dynamicService;
// 创建好友动态接口
@PostMapping("/{user_id}/createDynamic")
public Map<String, Object> createDynamic(@PathVariable("user_id") Long userId,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "content") String content,
@RequestParam(value = "image_url", required = false) MultipartFile[] imageFiles) {
return dynamicService.createDynamic(userId, title, content, imageFiles);
}
//删除好友动态
@DeleteMapping("/me/deleteDynamic/{dynamic_id}")
public ResponseEntity<String> deleteDynamic(@PathVariable("dynamic_id") Long dynamicId) {
dynamicService.deleteDynamic(dynamicId);
return ResponseEntity.ok("动态删除成功");
}
@PostMapping("/{user_id}/feeds/{dynamic_id}/comments")
public ResponseEntity<Map<String, Object>> addComment(
@PathVariable("user_id") Long userId,
@PathVariable("dynamic_id") Long dynamicId,
@RequestBody Map<String, Object> content) {
String commentContent = (String) content.get("content");
Long parentCommentId = content.containsKey("parent_comment_id") && content.get("parent_comment_id") != null ?
Long.parseLong(content.get("parent_comment_id").toString()) : null;
Map<String, Object> response = dynamicService.addComment(userId, dynamicId, commentContent, parentCommentId);
return ResponseEntity.ok(response);
}
//获取某个好友的所有动态
@GetMapping("/{user_id}/getAdynamic")
public ResponseEntity<Map<String, Object>> getAllUserDynamics(@PathVariable("user_id") Long userId) {
Map<String, Object> response = dynamicService.getAllUserDynamics(userId);
if (response == null || response.isEmpty()) {
return ResponseEntity.noContent().build();
}
// 返回响应
return ResponseEntity.ok(response);
}
@GetMapping("/{user_id}/getAllDynamics")
public ResponseEntity<Map<String, Object>> getAllUserAndFriendsDynamics(@PathVariable("user_id") Long userId) {
// 获取当前用户所有好友的ID
List<Long> friendIds = dynamicService.getAllFriendIds(userId);
friendIds.add(userId);
Map<String, Object> response = dynamicService.getAllUserAndFriendsDynamics(friendIds);
if (response == null || response.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(response);
}
//点赞好友动态
@PostMapping("/like")
public ResponseEntity<String> likeDynamic(@RequestBody Map<String, Long> request) {
Long userId = request.get("userId");
Long dynamicId = request.get("dynamicId");
// 执行点赞
boolean success = dynamicService.likeDynamic(userId, dynamicId);
if (success) {
return ResponseEntity.ok("Like successful");
} else {
return ResponseEntity.badRequest().body("Like failed");
}
}
@DeleteMapping("/unlike")
public ResponseEntity<String> unlikeDynamic(@RequestBody Map<String, Long> request) {
Long userId = request.get("userId");
Long dynamicId = request.get("dynamicId");
// 执行取消点赞
boolean success = dynamicService.unlikeDynamic(userId, dynamicId);
if (success) {
return ResponseEntity.ok("Unlike successful");
} else {
return ResponseEntity.badRequest().body("Unlike failed");
}
}
}