之前已經(jīng)完成了添加文章和查詢的接口冬骚,接下來繼續(xù)實現(xiàn)以下接口
- 查詢所有
- 更新
- 刪除
Controller 層
在 postApi.java里面添加
@GetMapping("")
public List<Post> all() {
return postService.all();
}
/**
* 更新文章私恬,需要登錄
* @param post 需要修改的內(nèi)容
* @param id 文章 id
* @param currentUser 當前用戶
* @return 更新之后的文章
*/
@LoginRequired
@PutMapping("/{id}")
public Post update(@RequestBody Post post, @PathVariable int id, @CurrentUser User currentUser) {
post.setId(id);
return postService.update(post, currentUser);
}
/**
* 刪除文章,需要登錄
* @param id 文章 id
* @param currentUser 當前登錄用戶
* @return 提示信息
*/
@LoginRequired
@DeleteMapping("/{id}")
public Object delete(@PathVariable int id, @CurrentUser User currentUser) {
postService.delete(id, currentUser);
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "刪除成功");
return jsonObject;
}
在這段代碼里面颠猴,使用了 @LoginRequired
注解保護更新和刪除接口(使用自定義注解實現(xiàn)登錄攔截)洛巢。同樣使用了 @CurrentUser
注解來注入當前登錄用戶(通過自定義注解獲取當前用戶)。
delete
方法請求成功之后返回 { "message": "刪除成功" }
厦滤,和之前寫的請求錯誤時返回的格式一樣援岩。為了避免將來使用我們接口的人感到迷惑,將接口請求錯誤時的響應修改為 { "error": "error message" }
的形式:將 GlobalExceptionHandler.java 掏导、AuthenticationApi.java享怀、PostApi.java、UserApi.java 這幾個文件里面 jsonObject.put("message", "......");
修改為 jsonObject.put("error", "......");
Service 層
在 postService.java 里面添加
public List<Post> all() {
return postMapper.all();
}
public Post update(Post post, User currentUser) {
checkNotNull(post.getId(), "id不能為空");
checkOwner(post.getId(), currentUser);
postMapper.update(post);
return findById(post.getId());
}
private void checkOwner(Integer id, User currentUser) {
Post post = findById(id);
if (!post.getAuthorId().equals(currentUser.getId())) {
throw new RuntimeException("不能刪除或修改別人的文章");
}
}
public void delete(int id, User currentUser) {
checkOwner(id, currentUser);
postMapper.delete(id);
}
只有作者才可以更新自己的文章趟咆,所以添加一個 checkOwner
的私有方法添瓷。
update
方法里面使用了 Google Guava 的 checkNotNull
方法,當前也可以自己寫 if
語句判空值纱,但 Guava 提供很多功能鳞贷,能幫助我們編寫出更優(yōu)雅的代碼。
在 pom.xml 中添加 guava 的依賴
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
更多Guava 相關(guān)的的知識可以看看使用Guava編寫優(yōu)雅代碼
checkNotNull
是 Guava Preconditions
類里面的一個靜態(tài)方法虐唠,需要靜態(tài)導入
import static com.google.common.base.Preconditions.checkNotNull;
mapper
首先將 postMapper
的 findOne
拆成下面這種形式搀愧,select-post
那段 SQL 就可以在 all
方法中復用了
<select id="findOne" resultMap="PostResultMap">
<include refid="select-post" />
<where>
<if test="id!=null">
AND post.id=#{id}
</if>
</where>
</select>
<select id="all" resultMap="PostResultMap">
<include refid="select-post" />
</select>
<sql id="select-post" >
SELECT
post.id,
post.author_id ,
post.title ,
post.content ,
post.create_time ,
post.update_time,
<!-- 作者信息,password 不需要就不查了 -->
`user`.id as author__id,
`user`.`name` as author__name
FROM post
LEFT JOIN `user` ON `user`.id=post.author_id
</sql>
<select id="all" resultMap="PostResultMap">
<include refid="select-post" />
</select>
<update id="update">
UPDATE post
<set>
<if test="title!=null">
title=#{title},
</if>
<if test="content!=null">
content=#{content},
</if>
</set>
where post.id=#{id}
</update>
<delete id="delete">
DELETE FROM post WHERE post.id=#{id}
</delete>
測試
啟動項目疆偿,使用 postman 測試一下三個接口
GET api/post
UPDATE api/post/{id}
DELETE api/post/{id}
查看項目完整代碼
項目地址: https://github.com/hyrijk/spring-boot-blog
克隆項目到本地
git clone https://github.com/hyrijk/spring-boot-blog.git
checkout 到當前版本
git checkout 3594fc7eb5dddeec52ad5bddeedc06678178b5bd
完咱筛。