summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--pom.xml4
-rw-r--r--src/main/java/com/blog/web/controllers/ArticleController.java24
-rw-r--r--src/main/java/com/blog/web/dto/ArticleDto.java7
-rw-r--r--src/main/java/com/blog/web/services/ArticleService.java2
-rw-r--r--src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java3
-rw-r--r--src/main/resources/templates/articles/edit.html8
-rw-r--r--src/main/resources/templates/articles/new.html4
7 files changed, 40 insertions, 12 deletions
diff --git a/pom.xml b/pom.xml
index de71b39..7ceafb8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,6 +68,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-validation</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/src/main/java/com/blog/web/controllers/ArticleController.java b/src/main/java/com/blog/web/controllers/ArticleController.java
index 2b32070..0e58877 100644
--- a/src/main/java/com/blog/web/controllers/ArticleController.java
+++ b/src/main/java/com/blog/web/controllers/ArticleController.java
@@ -3,13 +3,16 @@ package com.blog.web.controllers;
import com.blog.web.dto.ArticleDto;
import com.blog.web.models.Article;
import com.blog.web.services.ArticleService;
+import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
+import org.springframework.validation.BindingResult;
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.time.LocalDateTime;
import java.util.List;
@Controller
@@ -35,20 +38,31 @@ public class ArticleController {
}
@PostMapping("/articles/new")
- public String saveArticle(@ModelAttribute("article") Article article) {
- articleService.saveArticle(article);
+ public String saveArticle(@Valid @ModelAttribute("article") ArticleDto articleDto,
+ BindingResult result,
+ Model model) {
+ if(result.hasErrors()) {
+ model.addAttribute("article", articleDto);
+ return "articles/new";
+ }
+ articleService.saveArticle(articleDto);
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);
+ ArticleDto articleDto = articleService.findArticleById(articleId);
+ model.addAttribute("article", articleDto);
return "articles/edit";
}
@PostMapping("/articles/edit/{articleId}")
- public String updateArticle(@PathVariable("articleId") Long articleId, @ModelAttribute("article") ArticleDto article) {
+ public String updateArticle(@PathVariable("articleId") Long articleId,
+ @Valid @ModelAttribute("article") ArticleDto article,
+ BindingResult result) {
+ if(result.hasErrors()) {
+ return "articles/edit";
+ }
article.setId(articleId);
articleService.updateArticle(article);
return "redirect:/articles";
diff --git a/src/main/java/com/blog/web/dto/ArticleDto.java b/src/main/java/com/blog/web/dto/ArticleDto.java
index ffe7926..0c48f62 100644
--- a/src/main/java/com/blog/web/dto/ArticleDto.java
+++ b/src/main/java/com/blog/web/dto/ArticleDto.java
@@ -3,10 +3,12 @@ package com.blog.web.dto;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
+import jakarta.validation.constraints.NotEmpty;
import lombok.Builder;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
+import org.hibernate.validator.constraints.URL;
import java.time.LocalDateTime;
@@ -16,12 +18,15 @@ public class ArticleDto {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
+ @NotEmpty(message = "Article title should not be empty")
private String title;
+ @NotEmpty(message = "Article Photo URL should not be empty")
+ @URL(message = "Article Photo URL should be a URL")
private String photoUrl;
+ @NotEmpty(message = "Article Content should not be empty")
private String content;
@CreationTimestamp
private LocalDateTime createdOn;
@UpdateTimestamp
private LocalDateTime updatedOn;
-
}
diff --git a/src/main/java/com/blog/web/services/ArticleService.java b/src/main/java/com/blog/web/services/ArticleService.java
index 870a290..e2ec309 100644
--- a/src/main/java/com/blog/web/services/ArticleService.java
+++ b/src/main/java/com/blog/web/services/ArticleService.java
@@ -8,7 +8,7 @@ import java.util.List;
public interface ArticleService {
List<ArticleDto> findAllArticles();
- Article saveArticle(Article article);
+ Article saveArticle(ArticleDto article);
ArticleDto findArticleById(long articleId);
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 d950369..b9e11f6 100644
--- a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
+++ b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
@@ -24,7 +24,8 @@ public class ArticleServiceImpl implements ArticleService {
}
@Override
- public Article saveArticle(Article article) {
+ public Article saveArticle(ArticleDto articleDto) {
+ Article article = mapToArticle(articleDto);
return articleRepository.save(article);
}
diff --git a/src/main/resources/templates/articles/edit.html b/src/main/resources/templates/articles/edit.html
index ccfbe85..f4faee9 100644
--- a/src/main/resources/templates/articles/edit.html
+++ b/src/main/resources/templates/articles/edit.html
@@ -15,13 +15,13 @@
for="title">
Title
</label>
- <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white"
+ <input class="appearance-none block w-full bg-gray-200 text-gray-700 border rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white"
id="title"
type="text"
name="title"
th:field="*{title}"
placeholder="Yep">
- <p class="text-red-500 text-xs italic">Please fill out this field.</p>
+ <p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="text-red-500 text-xs italic">Please fill out this field.</p>
</div>
<div class="w-full md:w-1/2 px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
@@ -34,6 +34,7 @@
name="photoUrl"
th:field="*{photoUrl}"
placeholder="Doe">
+ <p th:if="${#fields.hasErrors('photoUrl')}" th:errors="*{photoUrl}" class="text-red-500 text-xs italic">Please fill out this field.</p>
</div>
</div>
<div class="flex flex-wrap -mx-3 mb-6">
@@ -48,11 +49,12 @@
name="content"
th:field="*{content}"
placeholder="Doe">
+ <p th:if="${#fields.hasErrors('content')}" th:errors="*{content}" class="text-red-500 text-xs italic">Please fill out this field.</p>
</div>
</div>
<div class="flex flex-wrap mb-2">
</div>
- <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Create</button>
+ <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Update</button>
</form>
</div>
diff --git a/src/main/resources/templates/articles/new.html b/src/main/resources/templates/articles/new.html
index afaed02..f5924d6 100644
--- a/src/main/resources/templates/articles/new.html
+++ b/src/main/resources/templates/articles/new.html
@@ -20,7 +20,7 @@
name="title"
th:field="*{title}"
placeholder="Yep">
- <p class="text-red-500 text-xs italic">Please fill out this field.</p>
+ <p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="text-red-500 text-xs italic">Please fill out this field.</p>
</div>
<div class="w-full md:w-1/2 px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
@@ -33,6 +33,7 @@
name="photoUrl"
th:field="*{photoUrl}"
placeholder="Doe">
+ <p th:if="${#fields.hasErrors('photoUrl')}" th:errors="*{photoUrl}" class="text-red-500 text-xs italic">Please fill out this field.</p>
</div>
</div>
<div class="flex flex-wrap -mx-3 mb-6">
@@ -47,6 +48,7 @@
name="content"
th:field="*{content}"
placeholder="Doe">
+ <p th:if="${#fields.hasErrors('content')}" th:errors="*{content}" class="text-red-500 text-xs italic">Please fill out this field.</p>
</div>
</div>
<div class="flex flex-wrap mb-2">