should update article
This commit is contained in:
parent
ca2683abab
commit
6c41584abe
@ -1,25 +1,37 @@
|
||||
package io.spring.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
import io.spring.api.exception.NoAuthorizationException;
|
||||
import io.spring.api.exception.ResourceNotFoundException;
|
||||
import io.spring.application.AuthorizationService;
|
||||
import io.spring.application.article.ArticleData;
|
||||
import io.spring.application.article.ArticleQueryService;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.user.User;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/articles/{slug}")
|
||||
public class ArticleApi {
|
||||
private ArticleQueryService articleQueryService;
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Autowired
|
||||
public ArticleApi(ArticleQueryService articleQueryService) {
|
||||
public ArticleApi(ArticleQueryService articleQueryService, ArticleRepository articleRepository) {
|
||||
this.articleQueryService = articleQueryService;
|
||||
this.articleRepository = articleRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ -27,4 +39,30 @@ public class ArticleApi {
|
||||
@AuthenticationPrincipal User user) {
|
||||
return articleQueryService.findBySlug(slug, user).map(ResponseEntity::ok).orElseThrow(ResourceNotFoundException::new);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ResponseEntity<ArticleData> updateArticle(@PathVariable("slug") String slug,
|
||||
@AuthenticationPrincipal User user,
|
||||
@Valid @RequestBody UpdateArticleParam updateArticleParam) {
|
||||
return articleRepository.findBySlug(slug).map(article -> {
|
||||
if (!AuthorizationService.canUpdateArticle(user, article)) {
|
||||
throw new NoAuthorizationException();
|
||||
}
|
||||
article.update(
|
||||
updateArticleParam.getTitle(),
|
||||
updateArticleParam.getDescription(),
|
||||
updateArticleParam.getBody());
|
||||
articleRepository.save(article);
|
||||
return ResponseEntity.ok(articleQueryService.findBySlug(slug, user).get());
|
||||
}).orElseThrow(ResourceNotFoundException::new);
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@JsonRootName("article")
|
||||
class UpdateArticleParam {
|
||||
private String title = "";
|
||||
private String body = "";
|
||||
private String description = "";
|
||||
}
|
||||
|
@ -41,8 +41,6 @@ public class ArticlesApi {
|
||||
}
|
||||
|
||||
Article article = new Article(
|
||||
articleRepository.toSlug(
|
||||
newArticleParam.getTitle()),
|
||||
newArticleParam.getTitle(),
|
||||
newArticleParam.getDescription(),
|
||||
newArticleParam.getBody(),
|
||||
|
@ -0,0 +1,8 @@
|
||||
package io.spring.api.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public class NoAuthorizationException extends RuntimeException {
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package io.spring.application;
|
||||
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.user.User;
|
||||
|
||||
public class AuthorizationService {
|
||||
public static boolean canUpdateArticle(User user, Article article) {
|
||||
return user.getId().equals(article.getUserId());
|
||||
}
|
||||
}
|
@ -26,9 +26,9 @@ public class Article {
|
||||
private DateTime createdAt;
|
||||
private DateTime updatedAt;
|
||||
|
||||
public Article(String slug, String title, String description, String body, String[] tagList, String userId) {
|
||||
public Article(String title, String description, String body, String[] tagList, String userId) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
this.slug = slug;
|
||||
this.slug = toSlug(title);
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.body = body;
|
||||
@ -38,4 +38,20 @@ public class Article {
|
||||
this.updatedAt = new DateTime();
|
||||
}
|
||||
|
||||
public void update(String title, String description, String body) {
|
||||
if (!"".equals(title)) {
|
||||
this.title = title;
|
||||
this.slug = toSlug(title);
|
||||
}
|
||||
if (!"".equals(description)) {
|
||||
this.description = description;
|
||||
}
|
||||
if (!"".equals(body)) {
|
||||
this.body = body;
|
||||
}
|
||||
}
|
||||
|
||||
private String toSlug(String title) {
|
||||
return title.toLowerCase().replace(' ', '-');
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,10 @@ package io.spring.core.article;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ArticleRepository {
|
||||
String toSlug(String title);
|
||||
|
||||
void save(Article article);
|
||||
|
||||
Optional<Article> findById(String id);
|
||||
|
||||
Optional<Article> findBySlug(String slug);
|
||||
}
|
||||
|
@ -18,4 +18,8 @@ public interface ArticleMapper {
|
||||
void insertTag(@Param("tag") Tag tag);
|
||||
|
||||
void insertArticleTagRelation(@Param("articleId") String articleId, @Param("tagId") String tagId);
|
||||
|
||||
Article findBySlug(@Param("slug") String slug);
|
||||
|
||||
void update(@Param("article") Article article);
|
||||
}
|
||||
|
@ -16,12 +16,15 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSlug(String title) {
|
||||
return title.toLowerCase().replace(' ', '-');
|
||||
public void save(Article article) {
|
||||
if (articleMapper.findById(article.getId()) == null) {
|
||||
createNew(article);
|
||||
} else {
|
||||
articleMapper.update(article);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Article article) {
|
||||
private void createNew(Article article) {
|
||||
articleMapper.insert(article);
|
||||
for (Tag tag : article.getTags()) {
|
||||
if (!articleMapper.findTag(tag.getName())) {
|
||||
@ -35,4 +38,9 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
||||
public Optional<Article> findById(String id) {
|
||||
return Optional.ofNullable(articleMapper.findById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Article> findBySlug(String slug) {
|
||||
return Optional.ofNullable(articleMapper.findBySlug(slug));
|
||||
}
|
||||
}
|
||||
|
@ -19,27 +19,45 @@
|
||||
<insert id="insertArticleTagRelation">
|
||||
insert into article_tags (article_id, tag_id) values(#{articleId}, #{tagId})
|
||||
</insert>
|
||||
<select id="findById" resultMap="article">
|
||||
<update id="update">
|
||||
update articles
|
||||
<set>
|
||||
<if test="article.title != ''">title = #{article.title},</if>
|
||||
<if test="article.title != ''">slug = #{article.slug},</if>
|
||||
<if test="article.description != ''">description = #{article.description},</if>
|
||||
<if test="article.body != ''">body = #{article.body}</if>
|
||||
</set>
|
||||
where id = #{article.id}
|
||||
</update>
|
||||
<sql id="selectArticle">
|
||||
select
|
||||
A.id articleId,
|
||||
A.slug articleSlug,
|
||||
A.title articleTitle,
|
||||
A.description articleDescription,
|
||||
A.body articleBody,
|
||||
A.user_id articleUserId,
|
||||
A.created_at articleCreatedAt,
|
||||
A.updated_at articleUpdatedAt,
|
||||
T.id tagId,
|
||||
T.name tagName
|
||||
from articles A
|
||||
left join article_tags AT on A.id = AT.article_id
|
||||
left join tags T on T.id = AT.tag_id
|
||||
A.id articleId,
|
||||
A.slug articleSlug,
|
||||
A.title articleTitle,
|
||||
A.description articleDescription,
|
||||
A.body articleBody,
|
||||
A.user_id articleUserId,
|
||||
A.created_at articleCreatedAt,
|
||||
A.updated_at articleUpdatedAt,
|
||||
T.id tagId,
|
||||
T.name tagName
|
||||
from articles A
|
||||
left join article_tags AT on A.id = AT.article_id
|
||||
left join tags T on T.id = AT.tag_id
|
||||
</sql>
|
||||
|
||||
<select id="findById" resultMap="article">
|
||||
<include refid="selectArticle"/>
|
||||
where A.id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="findTag" resultType="java.lang.Boolean">
|
||||
select count(*) from tags where name = #{tagName}
|
||||
</select>
|
||||
<select id="findBySlug" resultMap="article">
|
||||
<include refid="selectArticle"/>
|
||||
where A.slug = #{slug}
|
||||
</select>
|
||||
|
||||
<resultMap id="article" type="io.spring.core.article.Article">
|
||||
<id column="articleId" property="id"/>
|
||||
|
@ -6,6 +6,7 @@ import io.spring.application.article.ArticleQueryService;
|
||||
import io.spring.application.profile.ProfileData;
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.user.User;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -22,7 +23,6 @@ import java.util.Optional;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
@ -64,8 +64,6 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
||||
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
||||
|
||||
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
|
||||
|
||||
ArticleData articleData = new ArticleData(
|
||||
"123",
|
||||
slug,
|
||||
@ -102,14 +100,11 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
@Test
|
||||
public void should_get_error_message_with_wrong_parameter() throws Exception {
|
||||
String title = "How to train your dragon";
|
||||
String slug = "how-to-train-your-dragon";
|
||||
String description = "Ever wonder how?";
|
||||
String body = "";
|
||||
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
||||
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
||||
|
||||
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "Token " + token)
|
||||
@ -122,21 +117,10 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
|
||||
}
|
||||
|
||||
private HashMap<String, Object> prepareParam(final String title, final String description, final String body, final String[] tagList) {
|
||||
return new HashMap<String, Object>() {{
|
||||
put("article", new HashMap<String, Object>() {{
|
||||
put("title", title);
|
||||
put("description", description);
|
||||
put("body", body);
|
||||
put("tagList", tagList);
|
||||
}});
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_read_article_success() throws Exception {
|
||||
String slug = "test-new-article";
|
||||
Article article = new Article(slug, "Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId());
|
||||
Article article = new Article("Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId());
|
||||
|
||||
DateTime time = new DateTime();
|
||||
ArticleData articleData = new ArticleData(
|
||||
@ -172,4 +156,100 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_update_article_content_success() throws Exception {
|
||||
String title = "new-title";
|
||||
String body = "new body";
|
||||
String description = "new description";
|
||||
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
|
||||
|
||||
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId());
|
||||
|
||||
DateTime time = new DateTime();
|
||||
ArticleData articleData = new ArticleData(
|
||||
article.getId(),
|
||||
article.getSlug(),
|
||||
article.getTitle(),
|
||||
article.getDescription(),
|
||||
article.getBody(),
|
||||
false,
|
||||
0,
|
||||
time,
|
||||
time,
|
||||
Arrays.asList("joda"),
|
||||
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
|
||||
|
||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "Token " + token)
|
||||
.body(updateParam)
|
||||
.when()
|
||||
.put("/articles/{slug}", article.getSlug())
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("article.slug", equalTo(articleData.getSlug()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_403_if_not_author_to_update_article() throws Exception {
|
||||
String title = "new-title";
|
||||
String body = "new body";
|
||||
String description = "new description";
|
||||
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
|
||||
|
||||
User anotherUser = new User("test@test.com", "test", "123123", "", "");
|
||||
|
||||
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, anotherUser.getId());
|
||||
|
||||
DateTime time = new DateTime();
|
||||
ArticleData articleData = new ArticleData(
|
||||
article.getId(),
|
||||
article.getSlug(),
|
||||
article.getTitle(),
|
||||
article.getDescription(),
|
||||
article.getBody(),
|
||||
false,
|
||||
0,
|
||||
time,
|
||||
time,
|
||||
Arrays.asList("joda"),
|
||||
new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false));
|
||||
|
||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "Token " + token)
|
||||
.body(updateParam)
|
||||
.when()
|
||||
.put("/articles/{slug}", article.getSlug())
|
||||
.then()
|
||||
.statusCode(403);
|
||||
}
|
||||
|
||||
private HashMap<String, Object> prepareUpdateParam(final String title, final String body, final String description) {
|
||||
return new HashMap<String, Object>() {{
|
||||
put("article", new HashMap<String, Object>() {{
|
||||
put("title", title);
|
||||
put("body", body);
|
||||
put("description", description);
|
||||
}});
|
||||
}};
|
||||
}
|
||||
|
||||
private HashMap<String, Object> prepareParam(final String title, final String description, final String body, final String[] tagList) {
|
||||
return new HashMap<String, Object>() {{
|
||||
put("article", new HashMap<String, Object>() {{
|
||||
put("title", title);
|
||||
put("description", description);
|
||||
put("body", body);
|
||||
put("tagList", tagList);
|
||||
}});
|
||||
}};
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ public class ArticleQueryServiceTest {
|
||||
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
||||
userRepository.save(user);
|
||||
|
||||
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
articleRepository.save(article);
|
||||
|
||||
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
|
||||
|
@ -17,6 +17,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@MybatisTest
|
||||
@ -29,17 +30,18 @@ public class MyBatisArticleRepositoryTest {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private User user;
|
||||
private Article article;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
||||
userRepository.save(user);
|
||||
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_create_and_fetch_article_success() throws Exception {
|
||||
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
articleRepository.save(article);
|
||||
Optional<Article> optional = articleRepository.findById(article.getId());
|
||||
assertThat(optional.isPresent(), is(true));
|
||||
@ -47,4 +49,19 @@ public class MyBatisArticleRepositoryTest {
|
||||
assertThat(optional.get().getTags().contains(new Tag("java")), is(true));
|
||||
assertThat(optional.get().getTags().contains(new Tag("spring")), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_update_and_fetch_article_success() throws Exception {
|
||||
articleRepository.save(article);
|
||||
|
||||
String newTitle = "new test 2";
|
||||
article.update(newTitle, "", "");
|
||||
articleRepository.save(article);
|
||||
System.out.println(article.getSlug());
|
||||
Optional<Article> optional = articleRepository.findBySlug(article.getSlug());
|
||||
assertThat(optional.isPresent(), is(true));
|
||||
Article fetched = optional.get();
|
||||
assertThat(fetched.getTitle(), is(newTitle));
|
||||
assertThat(fetched.getBody(), not(""));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user