完成了delete,尝试@JwtVerify校验字符串参数的用法
This commit is contained in:
parent
e4a11a8a53
commit
a4713648a2
@ -1,5 +1,6 @@
|
||||
package org.cmh.backend.NewsManagement.controller;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.cmh.backend.NewsManagement.dto.MessageResponse;
|
||||
import org.cmh.backend.NewsManagement.dto.NewsRequest;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
@ -48,10 +49,15 @@ public class NewsController {
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
|
||||
}
|
||||
//
|
||||
// @DeleteMapping("/{id}")
|
||||
// public ResponseEntity<String> deleteNews(@PathVariable Long id) {
|
||||
// // TODO: 实现删除资讯的逻辑
|
||||
// return new ResponseEntity<>("删除成功", HttpStatus.OK);
|
||||
// }
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> deleteNews(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
newsService.deleteNews(id);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("删除失败,文章不存在"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,21 +5,6 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public interface NewsRepository extends JpaRepository<News, Long> {
|
||||
News findByTitle(String title);
|
||||
|
||||
News findBySummary(String summary);
|
||||
|
||||
News findByAuthor(String author);
|
||||
|
||||
News findByImagePath(String imagePath);
|
||||
|
||||
News findByContent(String content);
|
||||
|
||||
News findByTitleAndSummaryAndAuthorAndImagePath(String title, String summary, String author, String imagePath);
|
||||
|
||||
Page<News> findAllByOrderByIdDesc(Pageable pageable);
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package org.cmh.backend.NewsManagement.service;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.cmh.backend.NewsManagement.dto.NewsRequest;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
import org.cmh.backend.NewsManagement.repository.NewsRepository;
|
||||
@ -46,4 +47,11 @@ public class NewsService {
|
||||
newsRepository.save(news);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteNews(Long id) {
|
||||
if (!newsRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException("News with id " + id + " not found.");
|
||||
}
|
||||
newsRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user