resource module API
DELETE /resource
DELETE /resource/like
DELETE /resource/collection
Change-Id: Icd5a47ae44c33727597d30a93bc1ab849edebc41
diff --git a/src/main/java/com/g9/g9backend/controller/ResourceController.java b/src/main/java/com/g9/g9backend/controller/ResourceController.java
index b6726cf..c9a573c 100644
--- a/src/main/java/com/g9/g9backend/controller/ResourceController.java
+++ b/src/main/java/com/g9/g9backend/controller/ResourceController.java
@@ -8,6 +8,7 @@
import com.g9.g9backend.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -223,4 +224,64 @@
return ResponseEntity.ok("");
}
+
+ /**
+ * 删除资源
+ *
+ * @param resourceId 资源id
+ * @param userId 用户id
+ * @param password 密码
+ * @return 删除资源结果
+ */
+ @DeleteMapping
+ @ResponseStatus(HttpStatus.NO_CONTENT)
+ public ResponseEntity<String> userDelete(@RequestParam int resourceId, @RequestParam int userId, @RequestParam String password) {
+ logger.warn("Delete resource with id: {}", resourceId);
+ // 根据用户id查询该用户
+ QueryWrapper<User> userQuery = new QueryWrapper<>();
+ userQuery.eq("user_id", userId);
+ User userCheck = userService.getOne(userQuery);
+
+ // 只允许在用户上传资源页进行删除操作,所以不需要验证该资源是由该用户上传的
+ if (userCheck.getPassword().equals(password)) {
+ // 删除资源
+ resourceService.removeById(resourceId);
+ return ResponseEntity.noContent().build();
+ } else {
+ logger.warn("Delete failed. Incorrect password for account: {}", password);
+ return ResponseEntity.status(408).body("");
+ }
+ }
+
+ /**
+ * 取消点赞
+ *
+ * @param userId 用户id
+ * @param resourceId 资源id
+ * @return 取消点赞结果
+ */
+ @DeleteMapping("like")
+ @ResponseStatus(HttpStatus.NO_CONTENT)
+ public ResponseEntity<String> likeDelete(@RequestParam int userId, @RequestParam int resourceId) {
+ QueryWrapper<UserLike> userLikeQuery = new QueryWrapper<>();
+ userLikeQuery.eq("user_id", userId).eq("resource_id", resourceId);
+ userLikeService.remove(userLikeQuery);
+ return ResponseEntity.noContent().build();
+ }
+
+ /**
+ * 取消收藏
+ *
+ * @param userId 用户id
+ * @param resourceId 资源id
+ * @return 取消收藏结果
+ */
+ @DeleteMapping("collection")
+ @ResponseStatus(HttpStatus.NO_CONTENT)
+ public ResponseEntity<String> collectionDelete(@RequestParam int userId, @RequestParam int resourceId) {
+ QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>();
+ userCollectionQuery.eq("user_id", userId).eq("resource_id", resourceId);
+ userCollectionService.remove(userCollectionQuery);
+ return ResponseEntity.noContent().build();
+ }
}
\ No newline at end of file
diff --git a/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java b/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java
index 9d0600b..9d7c908 100644
--- a/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java
+++ b/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java
@@ -201,4 +201,56 @@
.content(objectMapper.writeValueAsString(new UserResourceDTO(1, 1))))
.andExpect(status().isOk());
}
+
+ // 删除资源
+ @Test
+ public void testDeleteResource_success() throws Exception {
+
+ // 密码正确
+ when(userService.getOne(any())).thenReturn(new User(1, "hcy", "123", null, 0, 0, null, 0, 0, 0));
+ when(resourceService.removeById(1)).thenReturn(true);
+
+ mockMvc.perform(delete("/resource")
+ .param("resourceId", "1")
+ .param("userId", "1")
+ .param("password", "123"))
+ .andExpect(status().isNoContent());
+ }
+
+ @Test
+ public void testDeleteResource_wrongPassword() throws Exception {
+
+ // 密码错误
+ when(userService.getOne(any())).thenReturn(new User(1, "hcy", "123", null, 0, 0, null, 0, 0, 0));
+
+ mockMvc.perform(delete("/resource")
+ .param("resourceId", "1")
+ .param("userId", "1")
+ .param("password", "wrong"))
+ .andExpect(status().is(408));
+ }
+
+ // 取消点赞
+ @Test
+ public void testLikeDelete() throws Exception {
+
+ when(userLikeService.remove(any())).thenReturn(true);
+
+ mockMvc.perform(delete("/resource/like")
+ .param("userId", "1")
+ .param("resourceId", "1"))
+ .andExpect(status().isNoContent());
+ }
+
+ // 取消收藏
+ @Test
+ public void testCollectionDelete() throws Exception {
+
+ when(userCollectionService.remove(any())).thenReturn(true);
+
+ mockMvc.perform(delete("/resource/collection")
+ .param("userId", "1")
+ .param("resourceId", "1"))
+ .andExpect(status().isNoContent());
+ }
}
\ No newline at end of file