From 32195042c7e3fd75f25323b9036a2481e48fd6f8 Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 11 Jul 2024 19:18:30 -0400 Subject: add editing to articles --- .../blog/web/controllers/ArticleController.java | 20 +++++++ .../java/com/blog/web/services/ArticleService.java | 4 ++ .../blog/web/services/impl/ArticleServiceImpl.java | 23 ++++++++ src/main/resources/templates/articles/edit.html | 61 ++++++++++++++++++++++ src/main/resources/templates/index.html | 1 + src/main/resources/templates/layout.html | 53 +++++++++++++++++++ src/main/resources/templates/post.html | 49 ----------------- 7 files changed, 162 insertions(+), 49 deletions(-) create mode 100644 src/main/resources/templates/articles/edit.html diff --git a/src/main/java/com/blog/web/controllers/ArticleController.java b/src/main/java/com/blog/web/controllers/ArticleController.java index 9d096f5..2b32070 100644 --- a/src/main/java/com/blog/web/controllers/ArticleController.java +++ b/src/main/java/com/blog/web/controllers/ArticleController.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @@ -38,4 +39,23 @@ public class ArticleController { articleService.saveArticle(article); return "redirect:/articles"; } + + @GetMapping("/articles/edit/{articleId}") + public String editArticleForm(@PathVariable("articleId") long articleId, Model model) { + ArticleDto article = articleService.findArticleById(articleId); + model.addAttribute("article", article); + return "articles/edit"; + } + + @PostMapping("/articles/edit/{articleId}") + public String updateArticle(@PathVariable("articleId") Long articleId, @ModelAttribute("article") ArticleDto article) { + article.setId(articleId); + articleService.updateArticle(article); + return "redirect:/articles"; + } + + @GetMapping("/articles") + public String getArticles() { + return "redirect:/"; + } } diff --git a/src/main/java/com/blog/web/services/ArticleService.java b/src/main/java/com/blog/web/services/ArticleService.java index 8d80c8a..870a290 100644 --- a/src/main/java/com/blog/web/services/ArticleService.java +++ b/src/main/java/com/blog/web/services/ArticleService.java @@ -9,4 +9,8 @@ public interface ArticleService { List findAllArticles(); Article saveArticle(Article article); + + ArticleDto findArticleById(long articleId); + + void updateArticle(ArticleDto articleDto); } diff --git a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java index 3daf92c..dd8bade 100644 --- a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java +++ b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java @@ -28,6 +28,29 @@ public class ArticleServiceImpl implements ArticleService { return articleRepository.save(article); } + @Override + public ArticleDto findArticleById(long articleId) { + Article article = articleRepository.findById(articleId).get(); + return mapToArticleDto(article); + } + + @Override + public void updateArticle(ArticleDto articleDto) { + Article article = mapToArticle(articleDto); + } + + private Article mapToArticle(ArticleDto articleDto) { + Article article = Article.builder() + .id(articleDto.getId()) + .title(articleDto.getTitle()) + .photoUrl(articleDto.getPhotoUrl()) + .content(articleDto.getContent()) + .createdOn(articleDto.getCreatedOn()) + .updatedOn(articleDto.getUpdatedOn()) + .build(); + return article; + } + private ArticleDto mapToArticleDto(Article article) { return ArticleDto.builder() .id(article.getId()) diff --git a/src/main/resources/templates/articles/edit.html b/src/main/resources/templates/articles/edit.html new file mode 100644 index 0000000..ccfbe85 --- /dev/null +++ b/src/main/resources/templates/articles/edit.html @@ -0,0 +1,61 @@ + + + + +
+
+ +
+
+ + +

Please fill out this field.

+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 611ef92..ed98605 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -108,6 +108,7 @@
Lorem ipsum dolor sit amet.

+ Edit
diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html index 3a93a87..407ecd9 100644 --- a/src/main/resources/templates/layout.html +++ b/src/main/resources/templates/layout.html @@ -26,6 +26,57 @@ + + + + +
@@ -37,6 +88,8 @@
+ +