forked from makersacademy/acebook-java-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from davidsholoye/comment_on_posts
Comment on posts
- Loading branch information
Showing
9 changed files
with
175 additions
and
92 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/main/java/com/makersacademy/acebook/controller/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.makersacademy.acebook.controller; | ||
|
||
import com.makersacademy.acebook.model.Comment; | ||
import com.makersacademy.acebook.model.Post; | ||
import com.makersacademy.acebook.model.User; | ||
import com.makersacademy.acebook.repository.CommentRepository; | ||
import com.makersacademy.acebook.repository.PostRepository; | ||
import com.makersacademy.acebook.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
import java.security.Principal; | ||
import java.util.Optional; | ||
|
||
@Controller | ||
public class CommentController { | ||
|
||
|
||
@Autowired | ||
PostRepository postRepository; | ||
|
||
@Autowired | ||
CommentRepository commentRepository; | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@GetMapping("/post/{id}") | ||
public String show(@PathVariable Long id, Model model) { | ||
// get post | ||
Optional<Post> post = postRepository.findById(id); | ||
Post currentPost = post.orElse(null); | ||
model.addAttribute("currentPost", currentPost); | ||
|
||
// create new comment object | ||
Comment commentObj = new Comment(); | ||
model.addAttribute("newComment", commentObj); | ||
|
||
// get list of comments for the post | ||
Iterable<Comment> comments = commentRepository.findAllByPostId(id); | ||
model.addAttribute("comments", comments); | ||
|
||
return "posts/show"; | ||
} | ||
|
||
@PostMapping("/post/addComment") | ||
public String createComment(@RequestParam String comment, @RequestParam Long postId, Principal principal) { | ||
|
||
// Get userID | ||
Optional<User> currentUser = userRepository.findByUsername(principal.getName()); | ||
User principalUser = currentUser.orElse(null); | ||
|
||
System.out.println(comment); | ||
System.out.println(postId); | ||
System.out.println(principalUser.getId()); | ||
|
||
Comment newComment = new Comment(comment, postId, principalUser.getId()); | ||
|
||
commentRepository.save(newComment); | ||
|
||
return "redirect:/post/" + postId; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/makersacademy/acebook/repository/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.makersacademy.acebook.repository; | ||
|
||
import com.makersacademy.acebook.model.Comment; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface CommentRepository extends CrudRepository<Comment, Long> { | ||
public List<Comment> findAllByPostId(Long postId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
<!doctype html> | ||
<html lang="en" xmlns:th="http://www.thymeleaf.org" | ||
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5" > | ||
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Acebook</title> | ||
<meta charset="UTF-8"/> | ||
<title>Post</title> | ||
</head> | ||
<body> | ||
<p th:text="${currentPost.content}"></p> | ||
<form action="#" th:action="@{/post/{id}(id=${currentPost.id})}" th:object="${newComment}" method="post"> | ||
<p>Comment: <input type="text" id="comment" th:field="*{comment}" /></p> | ||
<p><input type="submit" id="addComment" value="Submit" /> <input type="reset" value="Reset" /></p> | ||
|
||
<!-- Form to add a comment to post --> | ||
<form action="#" th:action="@{/post/addComment}" th:object="${newComment}" method="post"> | ||
<input type="hidden" name="postId" th:value="${currentPost.id}"/> | ||
<p>Comment: <input type="text" id="addComment" name="comment" th:field="*{comment}" /></p> | ||
<p><input type="submit" id="submitComment" value="Submit" /> <input type="reset" value="Reset" /></p> | ||
</form> | ||
<ul> | ||
<li>Comment 1</li> | ||
<li>Comment 2</li> | ||
<li>Comment 3</li> | ||
<ul th:each="comment: ${comments}"> | ||
<li th:text="${comment.comment}" ></li> | ||
</ul> | ||
</body> | ||
</html> |
76 changes: 76 additions & 0 deletions
76
src/test/java/com/makersacademy/aceboook/controller/CommentControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.makersacademy.aceboook.controller; | ||
|
||
import com.github.javafaker.Faker; | ||
import com.makersacademy.acebook.Application; | ||
import com.makersacademy.acebook.repository.CommentRepository; | ||
import com.makersacademy.acebook.repository.PostRepository; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.util.List; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
public class CommentControllerTest { | ||
|
||
|
||
@Autowired | ||
PostRepository postRepository; | ||
|
||
@Autowired | ||
CommentRepository commentRepository; | ||
|
||
WebDriver driver; | ||
Faker faker; | ||
|
||
@Before | ||
public void setup() { | ||
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); | ||
driver = new ChromeDriver(); | ||
faker = new Faker(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
driver.close(); | ||
} | ||
|
||
@Test | ||
public void testAddCommentReflectedInCommentsList() { | ||
driver.get("http://localhost:8080/login"); | ||
driver.findElement(By.id("username")).sendKeys("test_user"); | ||
driver.findElement(By.id("password")).sendKeys("password22"); | ||
driver.findElement(By.id("submit")).click(); | ||
driver.findElement(By.id("comment")).click(); | ||
driver.findElement(By.id("addComment")).sendKeys("Here is my new comment!"); | ||
driver.findElement(By.id("submitComment")).click(); | ||
|
||
WebElement ul = driver.findElement(By.tagName("ul"));; | ||
List<WebElement> commentsList = ul.findElements(By.tagName("li")); | ||
String comment = commentsList.get(0).getText(); | ||
|
||
Assert.assertEquals("Here is my new comment!", comment); | ||
|
||
} | ||
|
||
|
||
|
||
// @Test | ||
// public void testPostPageShowsPostAndListOfComments() { | ||
// driver.get("http://localhost:8080/login"); | ||
// driver.findElement(By.id("username")).sendKeys("test_user"); | ||
// driver.findElement(By.id("password")).sendKeys("password22"); | ||
// driver.findElement(By.id("submit")).click(); | ||
// | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters