106 lines
3.1 KiB
Java
106 lines
3.1 KiB
Java
package com.xubx.springboot_01demo.controller;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.xubx.springboot_01demo.dto.blog.addBlogDto;
|
||
import com.xubx.springboot_01demo.pojo.Blogs;
|
||
import com.xubx.springboot_01demo.service.BlogService;
|
||
import com.xubx.springboot_01demo.utils.api.Result;
|
||
import io.swagger.annotations.Api;
|
||
import io.swagger.annotations.ApiOperation;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.cache.annotation.CacheEvict;
|
||
import org.springframework.cache.annotation.Cacheable;
|
||
import org.springframework.http.HttpStatus;
|
||
import org.springframework.http.ResponseEntity;
|
||
import org.springframework.validation.BindingResult;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import javax.annotation.Resource;
|
||
import javax.validation.Valid;
|
||
import java.util.List;
|
||
|
||
@RestController //注解标识这是一个控制器类
|
||
@CrossOrigin //加上CrossOrigin可解决跨域问题
|
||
@Slf4j
|
||
@Api(tags = "博客接口")
|
||
@RequestMapping("/blog")
|
||
public class BlogsController {
|
||
@Resource
|
||
BlogService blogService;
|
||
|
||
/**
|
||
* 获取所有博客
|
||
*/
|
||
@GetMapping("/getBlogs")
|
||
@ApiOperation("获取所有博客")
|
||
public ResponseEntity<List<Blogs>> getBlogs() {
|
||
log.info("获取所有博客");
|
||
return ResponseEntity.ok(blogService.findAllBlogs());
|
||
}
|
||
|
||
/**
|
||
* 获取博客详情
|
||
*
|
||
* @param id
|
||
* @return
|
||
*/
|
||
@GetMapping("/getBlogDetail")
|
||
@ApiOperation("获取博客详情")
|
||
public ResponseEntity<Blogs> getBlogDetail(@RequestParam("blogId") int id) {
|
||
log.info("获取博客详情,ID: {}", id);
|
||
Blogs blog = blogService.findByIdBlogs(id);
|
||
if (blog == null) {
|
||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||
}
|
||
return ResponseEntity.ok(blog);
|
||
}
|
||
|
||
/**
|
||
* 新增博客
|
||
*
|
||
* @param blogs
|
||
*/
|
||
@PostMapping("/addBlog")
|
||
@ApiOperation("新增博客")
|
||
public ResponseEntity<String> addBlog(@Valid @RequestBody addBlogDto blogs, BindingResult bindingResult) {
|
||
log.info("新增博客入参:{}", JSONObject.toJSONString(blogs));
|
||
|
||
if (bindingResult.hasErrors()) {
|
||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(bindingResult.getFieldError().getDefaultMessage());
|
||
}
|
||
try {
|
||
blogService.addBlogs(blogs);
|
||
} catch (Exception e) {
|
||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("博客新增失败");
|
||
}
|
||
|
||
return ResponseEntity.status(HttpStatus.CREATED).body("博客新增成功");
|
||
}
|
||
|
||
/**
|
||
* 修改博客
|
||
*
|
||
* @param blogs
|
||
*/
|
||
@PostMapping("/updateBlog")
|
||
@ApiOperation("修改博客")
|
||
public ResponseEntity<String> updateBlog(@RequestBody Blogs blogs) {
|
||
blogService.updateBlogs(blogs);
|
||
return ResponseEntity.ok("博客更新成功");
|
||
}
|
||
|
||
/**
|
||
* 删除博客
|
||
*
|
||
* @param id
|
||
*/
|
||
@GetMapping("/deleteBlog")
|
||
@ApiOperation("删除博客")
|
||
public ResponseEntity<String> deleteBlog(@RequestParam("blogId") int id) {
|
||
blogService.deleteBlogs(id);
|
||
return ResponseEntity.ok("博客删除成功");
|
||
}
|
||
|
||
|
||
}
|