summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2024-07-08 23:24:23 -0400
committerrealtradam <[email protected]>2024-07-08 23:24:23 -0400
commitd5ab38e1bc8a5519720f413cee6573fbe31da986 (patch)
treeb071e63aeaa037c41fa39eed42499d306ae210d2
parenta681496e1569c0f152d881b17f341127287462fb (diff)
downloadspring-blog-d5ab38e1bc8a5519720f413cee6573fbe31da986.tar.gz
spring-blog-d5ab38e1bc8a5519720f413cee6573fbe31da986.zip
add form submission
-rw-r--r--src/main/java/com/blog/web/controllers/ArticleController.java8
-rw-r--r--src/main/java/com/blog/web/services/ArticleService.java3
-rw-r--r--src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java5
-rw-r--r--src/main/resources/templates/articles/new.html70
4 files changed, 47 insertions, 39 deletions
diff --git a/src/main/java/com/blog/web/controllers/ArticleController.java b/src/main/java/com/blog/web/controllers/ArticleController.java
index 3c86ea3..9d096f5 100644
--- a/src/main/java/com/blog/web/controllers/ArticleController.java
+++ b/src/main/java/com/blog/web/controllers/ArticleController.java
@@ -6,6 +6,8 @@ import com.blog.web.services.ArticleService;
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.PostMapping;
import java.util.List;
@@ -30,4 +32,10 @@ public class ArticleController {
model.addAttribute("article", article);
return "articles/new";
}
+
+ @PostMapping("/articles/new")
+ public String saveArticle(@ModelAttribute("article") Article article) {
+ articleService.saveArticle(article);
+ return "redirect:/articles";
+ }
}
diff --git a/src/main/java/com/blog/web/services/ArticleService.java b/src/main/java/com/blog/web/services/ArticleService.java
index c1b28e6..8d80c8a 100644
--- a/src/main/java/com/blog/web/services/ArticleService.java
+++ b/src/main/java/com/blog/web/services/ArticleService.java
@@ -1,9 +1,12 @@
package com.blog.web.services;
import com.blog.web.dto.ArticleDto;
+import com.blog.web.models.Article;
import java.util.List;
public interface ArticleService {
List<ArticleDto> findAllArticles();
+
+ Article saveArticle(Article article);
}
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 2d3f253..3daf92c 100644
--- a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
+++ b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
@@ -23,6 +23,11 @@ public class ArticleServiceImpl implements ArticleService {
return articles.stream().map(this::mapToArticleDto).collect(Collectors.toList());
}
+ @Override
+ public Article saveArticle(Article article) {
+ return articleRepository.save(article);
+ }
+
private ArticleDto mapToArticleDto(Article article) {
return ArticleDto.builder()
.id(article.getId())
diff --git a/src/main/resources/templates/articles/new.html b/src/main/resources/templates/articles/new.html
index d288e91..afaed02 100644
--- a/src/main/resources/templates/articles/new.html
+++ b/src/main/resources/templates/articles/new.html
@@ -7,60 +7,52 @@
<body layout:fragment="content">
<div class="flex justify-center bg-white p-12">
-<form class="w-full max-w-lg">
+<form th:action="@{/articles/new}" th:object="${article}" method="post" class="w-full max-w-lg">
<div class="flex flex-wrap -mx-3 mb-6">
<div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
- <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
- First Name
+ <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
+ 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" id="grid-first-name" type="text" placeholder="Jane">
+ <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"
+ 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>
</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" for="grid-last-name">
- Last Name
+ <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
+ for="photoUrl">
+ Photo URL
</label>
- <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-last-name" type="text" placeholder="Doe">
+ <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
+ id="photoUrl"
+ type="text"
+ name="photoUrl"
+ th:field="*{photoUrl}"
+ placeholder="Doe">
</div>
</div>
<div class="flex flex-wrap -mx-3 mb-6">
<div class="w-full px-3">
- <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-password">
- Password
+ <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
+ for="content">
+ Content
</label>
- <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-password" type="password" placeholder="******************">
- <p class="text-gray-600 text-xs italic">Make it as long and as crazy as you'd like</p>
+ <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
+ id="content"
+ type="text"
+ name="content"
+ th:field="*{content}"
+ placeholder="Doe">
</div>
</div>
- <div class="flex flex-wrap -mx-3 mb-2">
- <div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
- <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
- City
- </label>
- <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-city" type="text" placeholder="Albuquerque">
- </div>
- <div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
- <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-state">
- State
- </label>
- <div class="relative">
- <select class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-state">
- <option>New Mexico</option>
- <option>Missouri</option>
- <option>Texas</option>
- </select>
- <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
- <svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/></svg>
- </div>
- </div>
- </div>
- <div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
- <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-zip">
- Zip
- </label>
- <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-zip" type="text" placeholder="90210">
- </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>
+
</form>
</div>