diff --git a/pom.xml b/pom.xml index 2968e72..75fc48a 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,11 @@ org.springframework.boot spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-validation + @@ -65,6 +70,7 @@ import + diff --git a/src/main/java/com/qf/myafterprojecy/GlobalExceptionHandler.java b/src/main/java/com/qf/myafterprojecy/GlobalExceptionHandler.java new file mode 100644 index 0000000..5e7f4e6 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/GlobalExceptionHandler.java @@ -0,0 +1,13 @@ +package com.qf.myafterprojecy; + +import com.qf.myafterprojecy.pojo.ResponseMessage; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(value=Exception.class) + public ResponseMessage handleException(Exception e){ + return new ResponseMessage<>(500,"服务器异常",e.getMessage()); + } +} diff --git a/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java b/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java index 2cf5fe3..e913b7d 100644 --- a/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java +++ b/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java @@ -5,6 +5,7 @@ import com.qf.myafterprojecy.pojo.ResponseMessage; import com.qf.myafterprojecy.pojo.dto.ArticleDto; import com.qf.myafterprojecy.service.IArticleService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @RestController @@ -17,14 +18,24 @@ public class ArticleController { @Autowired IArticleService ArticleService; - @PostMapping - public ResponseMessage> getArticle(){ - return ArticleService.getArticleAll(); - }; @GetMapping - public ResponseMessage
geybyID(@RequestBody ArticleDto articleDto){ - System.out.println("id:"+ articleDto.getArticleid()); - return ArticleService.getArticleByid(Integer.valueOf(articleDto.getArticleid())); + public ResponseMessage> getArticleAllByID(@Validated @RequestBody ArticleDto articleDto){ + return ArticleService.getArticleAllByID(articleDto); + } + + @PutMapping + public ResponseMessage
UpdateArticle(@RequestBody ArticleDto articleDto){ + return ArticleService.SaveArticle(articleDto); + }; + + @PostMapping + public ResponseMessage
AddArticle(@RequestBody ArticleDto articleDto){ + return ArticleService.SaveArticle(articleDto); + } + + @DeleteMapping + public ResponseMessage
DeleteArticle(@RequestBody ArticleDto articleDto){ + return ArticleService.deleteArticle(articleDto.getArticleid()); } } diff --git a/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java b/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java index 1b826b0..bc45cc3 100644 --- a/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java +++ b/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java @@ -12,8 +12,42 @@ public class ResponseMessage { this.message = message; this.data = data; } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + // 接口请求成功 public static ResponseMessage success(T data) { return new ResponseMessage(HttpStatus.OK.value(), "success", data); } + + public static ResponseMessage Delete(Boolean delete) { + return new ResponseMessage(HttpStatus.OK.value(), "delete", delete); + } + + public static ResponseMessage Save(Boolean save) { + return new ResponseMessage(HttpStatus.OK.value(), "save", save); + } + } diff --git a/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java b/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java index 9d7c104..dbcd37b 100644 --- a/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java +++ b/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java @@ -1,6 +1,5 @@ package com.qf.myafterprojecy.pojo.dto; -import javax.persistence.Column; public class ArticleDto { private Integer articleid; diff --git a/src/main/java/com/qf/myafterprojecy/service/ArticleService.java b/src/main/java/com/qf/myafterprojecy/service/ArticleService.java index c98b6e3..cbcd2c3 100644 --- a/src/main/java/com/qf/myafterprojecy/service/ArticleService.java +++ b/src/main/java/com/qf/myafterprojecy/service/ArticleService.java @@ -2,23 +2,41 @@ package com.qf.myafterprojecy.service; import com.qf.myafterprojecy.pojo.Article; import com.qf.myafterprojecy.pojo.ResponseMessage; +import com.qf.myafterprojecy.pojo.dto.ArticleDto; import com.qf.myafterprojecy.repository.ArticleRepository; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Collections; + @Service public class ArticleService implements IArticleService{ @Autowired ArticleRepository articleRepository; @Override - public ResponseMessage
getArticleByid(Integer articleid){ - Article articlenew = articleRepository.findById(articleid).orElseThrow(RuntimeException::new); - return ResponseMessage.success(articlenew) ; + public ResponseMessage> getArticleAllByID(ArticleDto articleDto){ + Iterable
Articles = null; + if (articleDto.getArticleid()!=null && articleDto.getArticleid()!=0){ + + Articles = Collections.singleton(articleRepository.findById(articleDto.getArticleid()).orElseThrow(()-> new RuntimeException("没有该文章"))); + }else { + Articles = articleRepository.findAll(); + } + return ResponseMessage.success(Articles) ; } + @Override - public ResponseMessage> getArticleAll(){ - Iterable
Articles = articleRepository.findAll(); - return ResponseMessage.success(Articles); + public ResponseMessage
SaveArticle(ArticleDto article) { + Article articlenew = new Article(); + BeanUtils.copyProperties(article,articlenew); + return ResponseMessage.Save(articleRepository.save(articlenew)!= null); + } + + + @Override + public ResponseMessage
deleteArticle(Integer articleid) { + return ResponseMessage.Delete(articleRepository.existsById(articleid)) ; } } diff --git a/src/main/java/com/qf/myafterprojecy/service/IArticleService.java b/src/main/java/com/qf/myafterprojecy/service/IArticleService.java index b9df6c8..b0a7da4 100644 --- a/src/main/java/com/qf/myafterprojecy/service/IArticleService.java +++ b/src/main/java/com/qf/myafterprojecy/service/IArticleService.java @@ -2,10 +2,32 @@ package com.qf.myafterprojecy.service; import com.qf.myafterprojecy.pojo.Article; import com.qf.myafterprojecy.pojo.ResponseMessage; +import com.qf.myafterprojecy.pojo.dto.ArticleDto; public interface IArticleService { - ResponseMessage
getArticleByid(Integer articleid); + /** + * 根据id查询 如果id为空查询所有 + * + * @param articleDto + * @return ResponseMessage
+ */ + ResponseMessage> getArticleAllByID(ArticleDto articleDto); + + + /** + * 新增(id=0) + * 修改 (id=修改数据的id) + * @param article + * @return ResponseMessage
+ */ + ResponseMessage
SaveArticle(ArticleDto article); + + /** + * 删除 + * @param articleid + * @return ResponseMessage
+ */ + ResponseMessage
deleteArticle(Integer articleid); - ResponseMessage> getArticleAll(); }