should update article
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user