Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments #4

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,43 +1,73 @@
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.ICommentRepository;
import com.makersacademy.acebook.repository.PostRepository;
import com.makersacademy.acebook.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
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.util.List;
import java.util.Optional;

@Controller
public class PostsController {

@Autowired
PostRepository repository;
PostRepository postRepository;

@Autowired
ICommentRepository commentRepository;


@Autowired
UserRepository userRepository;

@GetMapping("/posts")
public String index(Model model) {
Iterable<Post> posts = repository.findAll();
Iterable<Post> posts = postRepository.findAll();
model.addAttribute("posts", posts);
model.addAttribute("post", new Post());
model.addAttribute("comment", new Comment());

return "posts/index";
}

@PostMapping("/posts")
public RedirectView create(@ModelAttribute Post post) {
repository.save(post);
postRepository.save(post);
return new RedirectView("/posts");
}

@PostMapping("/posts-like")
public RedirectView like(@RequestParam("postId") Long postId) {
Optional<Post> postOptional = repository.findById(postId);
Optional<Post> postOptional = postRepository.findById(postId);
Post post = postOptional.get();
System.out.println(post);
post.incrementLikeCount();
repository.save(post);
postRepository.save(post);
return new RedirectView("/posts");
}

@PostMapping("/posts-comments")
public RedirectView comment(@RequestParam("postId") Long postId, @RequestParam("content") String content) {
Optional<Post> postOptional = postRepository.findById(postId);
Post post = postOptional.get();
Comment comment = new Comment();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

String currentPrincipleName = authentication.getName();

comment.setContent(content);
comment.setPost(post);
comment.setUser_id(userRepository.findIdByUsername(currentPrincipleName));
commentRepository.save(comment);
return new RedirectView("/posts");
}

}
49 changes: 49 additions & 0 deletions src/main/java/com/makersacademy/acebook/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.makersacademy.acebook.model;

import javax.persistence.*;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;


@Data
@Entity
@Table(name = "COMMENTS")

public class Comment {



@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; // Todo update Schema to BIGSERIAL and this type to long
@Getter @Setter private String content;
private Long user_id;

@ManyToOne
@JoinColumn(name = "post_id")
private Post post;

public Comment() {}

public Comment(String content) {this.content = content;}

public Post getPost() {return this.post;}

public void setPost(Post post) {
this.post = post;
}

public Long getUser_id() {
return user_id;
}

public void setUser_id(Long user_id) {
this.user_id = user_id;
}

public void setContent(String content) {
this.content = content;
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/makersacademy/acebook/model/Post.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.makersacademy.acebook.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GenerationType;
import javax.persistence.*;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Data
@Entity
@Table(name = "POSTS")
Expand All @@ -23,6 +22,9 @@ public class Post {
private String content;
private Integer likeCount = 0;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Getter @Setter private List<Comment> comments;

// No-args constructor
public Post() {}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/makersacademy/acebook/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class User {
private boolean enabled;

public User() {
this.enabled = TRUE;
}

public User(String username, String password) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.makersacademy.acebook.repository;

import com.makersacademy.acebook.model.Comment;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ICommentRepository extends CrudRepository<Comment, Long> {

@Query(value = "SELECT c FROM Comment c JOIN c.post p WHERE p.id = :postId")
Iterable<Comment> findCommentByPostId(@Param("postId") Long postId);
}


Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.makersacademy.acebook.repository;

import com.makersacademy.acebook.model.Comment;
import com.makersacademy.acebook.model.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

@Query(value = "SELECT u.id from User u WHERE u.username = :username")
Long findIdByUsername(@Param("username") String username);
}
25 changes: 20 additions & 5 deletions src/main/resources/templates/posts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,28 @@ <h1>Posts</h1>
<p><input id="content_create" type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

<ul th:each="post: ${posts}">
<li th:text="${post.content}" />
<li th:text="'Likes: ' + ${post.likeCount}" />
<form th:action="@{/posts-like}" method="post">

<ul th:each="post : ${posts}">
<li>
<p th:text="${post.content}"></p>
<p th:text="'Likes: ' + ${post.likeCount}"></p>
<form th:action="@{/posts-like}" method="post">
<input type="hidden" name="postId" th:value="${post.id}"/>
<button type="submit" th:id="'like_button' + ${post.id}">Like</button>
</form>
<!-- Display comments associated with the post -->
<ul>
<li th:each="comment : ${post.comments}">
<p th:text="${comment.content}"></p>
</li>
</ul>

<form th:action="@{/posts-comments}" method="post">
<input type="hidden" name="postId" th:value="${post.id}"/>
<button type="submit" th:id="'like_button' + ${post.id}">Like</button>
<p>Content: <input id="comment-content" type="text" name="content" /></p>
<button type="submit" th:id="'comment' + ${comment.id}">Comment</button>
</form>

</ul>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void tearDown() {
@Test
public void signInViewPost() {
driver.get("http://localhost:8080/login");
driver.findElement(By.id("username")).sendKeys("Noah");
driver.findElement(By.id("username")).sendKeys("manith2");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.tagName("button")).click();
WebElement element = driver.findElement(By.tagName("ul"));
Expand Down