完成了/news/getNewsList全数据流程
This commit is contained in:
parent
f9a6a3c481
commit
3579f93c0d
@ -1,43 +1,45 @@
|
|||||||
package org.cmh.backend.NewsManagement.controller;
|
package org.cmh.backend.NewsManagement.controller;
|
||||||
|
|
||||||
|
import org.cmh.backend.NewsManagement.dto.GetNewsByPageRequest;
|
||||||
|
import org.cmh.backend.NewsManagement.model.News;
|
||||||
|
import org.cmh.backend.NewsManagement.service.NewsService;
|
||||||
|
import org.cmh.backend.Utils.JwtVerify;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import java.util.ArrayList;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import java.util.List;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/news")
|
@RequestMapping("/news")
|
||||||
public class NewsController {
|
public class NewsController {
|
||||||
// @PostMapping("/example")
|
@Autowired
|
||||||
// public ResponseEntity<ExampleResponse> example(@RequestBody ExampleRequest request) {
|
private NewsService newsService;
|
||||||
// return new ResponseEntity<>(new ExampleResponse("Hello World!"), HttpStatus.OK);
|
|
||||||
|
@GetMapping("/getNewsPage")
|
||||||
|
@JwtVerify
|
||||||
|
public ResponseEntity<Page<News>> getNewsPage(@RequestBody GetNewsByPageRequest request) {
|
||||||
|
return new ResponseEntity<>(newsService.getNewsByPage(request), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @PostMapping
|
||||||
|
// public ResponseEntity<String> createNews(@RequestBody NewsRequest request) {
|
||||||
|
// // TODO: 实现创建资讯的逻辑
|
||||||
|
// return new ResponseEntity<>("创建成功", HttpStatus.OK);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @PutMapping("/{id}")
|
||||||
|
// public ResponseEntity<String> updateNews(@PathVariable Long id, @RequestBody NewsRequest request) {
|
||||||
|
// // TODO: 实现修改资讯的逻辑
|
||||||
|
// return new ResponseEntity<>("修改成功", HttpStatus.OK);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @DeleteMapping("/{id}")
|
||||||
|
// public ResponseEntity<String> deleteNews(@PathVariable Long id) {
|
||||||
|
// // TODO: 实现删除资讯的逻辑
|
||||||
|
// return new ResponseEntity<>("删除成功", HttpStatus.OK);
|
||||||
// }
|
// }
|
||||||
@GetMapping
|
|
||||||
public ResponseEntity<List<NewsResponse>> getNewsList(
|
|
||||||
@RequestParam(required = false) String title,
|
|
||||||
@RequestParam(required = false) String summary,
|
|
||||||
@RequestParam(required = false) String author) {
|
|
||||||
// TODO: 实现获取资讯列表的逻辑
|
|
||||||
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public ResponseEntity<String> createNews(@RequestBody NewsRequest request) {
|
|
||||||
// TODO: 实现创建资讯的逻辑
|
|
||||||
return new ResponseEntity<>("创建成功", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
|
||||||
public ResponseEntity<String> updateNews(@PathVariable Long id, @RequestBody NewsRequest request) {
|
|
||||||
// TODO: 实现修改资讯的逻辑
|
|
||||||
return new ResponseEntity<>("修改成功", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public ResponseEntity<String> deleteNews(@PathVariable Long id) {
|
|
||||||
// TODO: 实现删除资讯的逻辑
|
|
||||||
return new ResponseEntity<>("删除成功", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,13 @@
|
|||||||
|
package org.cmh.backend.NewsManagement.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.cmh.backend.Utils.JwtRequest;
|
||||||
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class GetNewsByPageRequest extends JwtRequest {
|
||||||
|
private Integer pageNo;
|
||||||
|
private Integer pageSize;
|
||||||
|
}
|
||||||
@ -1,8 +1,13 @@
|
|||||||
package org.cmh.backend.NewsManagement.repository;
|
package org.cmh.backend.NewsManagement.repository;
|
||||||
|
|
||||||
import org.cmh.backend.NewsManagement.model.News;
|
import org.cmh.backend.NewsManagement.model.News;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface NewsRepository extends JpaRepository<News, Long> {
|
public interface NewsRepository extends JpaRepository<News, Long> {
|
||||||
News findByTitle(String title);
|
News findByTitle(String title);
|
||||||
|
|
||||||
@ -16,4 +21,5 @@ public interface NewsRepository extends JpaRepository<News, Long> {
|
|||||||
|
|
||||||
News findByTitleAndSummaryAndAuthorAndImagePath(String title, String summary, String author, String imagePath);
|
News findByTitleAndSummaryAndAuthorAndImagePath(String title, String summary, String author, String imagePath);
|
||||||
|
|
||||||
|
Page<News> findAllByOrderByIdDesc(Pageable pageable);
|
||||||
}
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
package org.cmh.backend.NewsManagement.service;
|
||||||
|
|
||||||
|
import org.cmh.backend.NewsManagement.dto.GetNewsByPageRequest;
|
||||||
|
import org.cmh.backend.NewsManagement.model.News;
|
||||||
|
import org.cmh.backend.NewsManagement.repository.NewsRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class NewsService {
|
||||||
|
@Autowired
|
||||||
|
private NewsRepository newsRepository;
|
||||||
|
|
||||||
|
public Page<News> getNewsByPage(GetNewsByPageRequest request) {
|
||||||
|
int pageNo = request.getPageNo();
|
||||||
|
int pageSize = request.getPageSize();
|
||||||
|
if (pageNo < 1) {
|
||||||
|
pageNo = 1;
|
||||||
|
}
|
||||||
|
if (pageSize < 1) {
|
||||||
|
pageSize = 10;
|
||||||
|
}
|
||||||
|
Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
|
||||||
|
return newsRepository.findAllByOrderByIdDesc(pageable);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user