Skip to content

Commit

Permalink
Merge pull request #15 from davidsholoye/comment_on_posts
Browse files Browse the repository at this point in the history
Comment on posts
  • Loading branch information
NatalieJClark authored Nov 22, 2023
2 parents 6c460a9 + 20beaf1 commit 8df87ee
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 92 deletions.
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;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.makersacademy.acebook.model.Friend;
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;
Expand Down Expand Up @@ -56,6 +57,9 @@ public String index(Model model, Principal principal) {

@PostMapping("/posts")
public RedirectView create(@ModelAttribute Post post, Principal principal) {
System.out.println("HEREEEEEEE");
System.out.println("HEREEEEEEE");
System.out.println(post);
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
post.setTimestamp(Timestamp.valueOf(timeStamp));
Optional<User> currentUser = userRepository.findByUsername(principal.getName());
Expand All @@ -65,40 +69,4 @@ public RedirectView create(@ModelAttribute Post post, Principal principal) {
return new RedirectView("/posts");
}

@GetMapping("/post/{id}")
public String show(@PathVariable Long id, Model model) {

Optional<Post> post = postRepository.findById(id);
Post currentPost = post.orElse(null);
model.addAttribute("currentPost", currentPost);

Comment newComment = new Comment();
model.addAttribute("newComment", newComment);

return "posts/show";
}

@PostMapping("/post/{id}")
public ModelAndView createComment(@PathVariable Long id, @ModelAttribute Comment comment, Principal principal) {

// to make a new comment for a post:
// comment content, post_id, user_id
comment.setPostId(id);

Optional<User> currentUser = userRepository.findByUsername(principal.getName());
User principalUser = currentUser.orElse(null);
comment.setUserId(principalUser.getId());








ModelAndView modelAndView = new ModelAndView("/post/{id}");
modelAndView.addObject("comment", new Comment());
return modelAndView;

}
}
18 changes: 5 additions & 13 deletions src/main/java/com/makersacademy/acebook/model/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import lombok.Data;
import lombok.Getter;

import javax.persistence.*;

Expand All @@ -15,12 +16,15 @@ public class Comment {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Getter
private String comment;
@Getter
private Long postId;
@Getter
private Long userId;

// @ManyToOne(cascade = CascadeType.ALL)
// @JoinColumn(name = "id")
// @JoinColumn(name = "post_id")
// private Post post;

public Comment() {};
Expand All @@ -31,26 +35,14 @@ public Comment(String comment, Long postId, Long userId) {
this.userId = userId;
}

public String getComment() {
return this.comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public Long getPostId() {
return this.postId;
}

public void setPostId(Long postId) {
this.postId = postId;
}

public Long getUserId() {
return this.userId;
}

public void setUserId(Long UserId) {
this.userId = UserId;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/makersacademy/acebook/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Post {
private Long userId;

// @Getter
// @OneToMany(fetch = FetchType.LAZY, mappedBy = "postId", cascade = CascadeType.ALL)
// @OneToMany(fetch = FetchType.LAZY, mappedBy = "post", cascade = CascadeType.ALL)
// private List<Comment> commentList;

public Post() {}
Expand Down
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);
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/posts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ <h1>Posts</h1>
Signed in as <span sec:authentication="name"></span>
</div>

<!--Make a new post-->
<form action="#" th:action="@{/posts}" th:object="${newPost}" method="post">
<p>Content: <input type="text" id="content" th:field="*{content}" /></p>
<p><input type="submit" id="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

<ul th:each="map, stat : ${postsAndPosters}">
<ul th:each="entry : ${map.entrySet()}">
<li>
Expand Down
25 changes: 11 additions & 14 deletions src/main/resources/templates/posts/show.html
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>
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();
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,5 @@ public void testCommentButtonNavigatesToPostPageFromPosts() {

}

@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 commentsUL = driver.findElement(By.tagName("ul"));
List<WebElement> commentsLI = commentsUL.findElements(By.tagName("li"));
String comment = commentsLI.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();
//
// }
}

0 comments on commit 8df87ee

Please sign in to comment.