Skip to content

Commit

Permalink
Merge pull request #30 from neily2nd/main
Browse files Browse the repository at this point in the history
Delete post works, comments show, but delete comment and edit comment are still being worked on.
  • Loading branch information
Umi007 authored Jun 13, 2024
2 parents 4af8ed0 + 56ceee5 commit f7abfc0
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 107 deletions.
23 changes: 20 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin>
</plugins>
</build>

Expand Down Expand Up @@ -107,10 +107,27 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.22.0</version> <!-- Use the latest version available -->
<version>4.0.0</version> <!-- Use the latest version available -->
<scope>test</scope> <!-- Ensure Mockito is only included in test scope -->
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<!-- Exclude conflicting dependencies if needed -->
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>


</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ public class CommentsController {
@Autowired
CommentRepository commentRepository;

@GetMapping("/posts/{postId}/comments")
public String index(@PathVariable Long postId, Model model) {
List<Comment> comments = commentRepository.findByPostId(postId);
return "posts/index";
}


@PostMapping("/posts/{postId}/comments")
public RedirectView createComment(@PathVariable Long postId, @ModelAttribute Comment comment, Model model) {
public RedirectView createComment(@PathVariable Long postId, @ModelAttribute Comment comment) {
Optional<Post> postOptional = postRepository.findById(postId);

if (postOptional.isPresent()) {
Expand All @@ -53,13 +60,6 @@ public RedirectView createComment(@PathVariable Long postId, @ModelAttribute Com
comment.setUser(user);
commentRepository.save(comment);

// Fetch all comments for the current post
List<Comment> comments = commentRepository.findByPostId(postId);

// Add the post and comments to the model
model.addAttribute("post", post);
model.addAttribute("comments", comments);

// Redirect to the posts page to show updated comments
return new RedirectView("/posts", true, false);
} else {
Expand All @@ -74,6 +74,7 @@ public RedirectView deleteComment(@PathVariable Long postId, @PathVariable Long
return new RedirectView("/posts", true, false);
}


@PostMapping("/posts/{postId}/comments/{commentId}/edit")
public RedirectView editComment(@PathVariable Long postId, @PathVariable Long commentId, @ModelAttribute Comment comment) {
Optional<Comment> optionalComment = commentRepository.findById(commentId);
Expand All @@ -92,3 +93,4 @@ public RedirectView editComment(@PathVariable Long postId, @PathVariable Long co
}



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

import com.makersacademy.acebook.model.Comment;
import com.makersacademy.acebook.model.Like;
import com.makersacademy.acebook.model.Post;
import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.CommentRepository;
import com.makersacademy.acebook.repository.LikeRepository;
import com.makersacademy.acebook.repository.PostRepository;
import com.makersacademy.acebook.repository.UserRepository;
Expand All @@ -19,8 +21,7 @@
import org.springframework.web.servlet.view.RedirectView;

import java.security.Principal;
import java.util.Date;
import java.util.Optional;
import java.util.*;


@Controller
Expand All @@ -31,6 +32,7 @@ public class PostsController {

@Autowired
UserRepository userRepository;
CommentRepository commentRepository;

@Autowired
LikeRepository likeRepository;
Expand All @@ -43,8 +45,11 @@ public class PostsController {
public String index(Model model, @AuthenticationPrincipal Principal principal) {
Iterable<Post> posts = postRepository.findAllByOrderByCreatedAtDesc();



// Iterable<Post> posts = repository.findAll();
model.addAttribute("posts", posts);

// Get current user information
User currentUser = userRepository.findByUsername(principal.getName());
model.addAttribute("currentUser", currentUser);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/makersacademy/acebook/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
@Table(name = "POSTS")
public class Post {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -29,13 +28,13 @@ public class Post {
@JoinColumn(name = "user_id")
private User user;


@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments;

// No post No like, one post can have many likes
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
// set ensures element uniqueness? but has problem , so changed to list
private List<Like> likes = new ArrayList<>();

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ CREATE TABLE likes (
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (post_id) REFERENCES posts(id)
);

10 changes: 10 additions & 0 deletions src/main/resources/db/migration/V12__create_comments_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS comments;
CREATE TABLE comments (
id bigserial PRIMARY KEY,
content TEXT NOT NULL,
post_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (post_id) REFERENCES posts(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
47 changes: 23 additions & 24 deletions src/main/resources/templates/posts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,10 @@ <h1>Posts</h1>
</div>

<!-- List of posts -->
<ul>

<ul th:if="${not #lists.isEmpty(posts)}">
<!-- Iterate over each post and display its content -->
<li th:each="post : ${posts}">
<div class="post-content">

<!-- Post Content -->
<p class="post-title">
<strong>Title:</strong>
Expand All @@ -298,43 +296,32 @@ <h1>Posts</h1>
<strong>Created At:</strong>
<span th:text="${#dates.format(post.createdAt, 'yyyy-MM-dd HH:mm')}"></span>
</p>

<!-- Display comments for the current post -->
<p class="post-comments">
<ul>
<li th:each="comment : ${comments}">
<div class="comment-content">
<!-- Comment Content -->
<p><strong>Comment:</strong> <span th:text="${comment.content}"></span></p>
<p><strong>Posted by:</strong> <span th:text="${comment.user.username}"></span></p>
<p><strong>Posted At:</strong> <span th:text="${#dates.format(comment.createdAt, 'yyyy-MM-dd HH:mm')}"></span></p>
</div>
</li>
<div>
<li th:each="comment : ${post.comments}">
<p th:text="${comment.content}"></p>
<p th:text="${comment.user.username}"></p>
<p th:text="${comment.createdAt}"></p>
</li>
</div>
</ul>
</p>
<!-- Edit comment form -->

<!-- Form to add new comment -->
<form th:action="@{/posts/{postId}/comments(postId=${post.id})}" method="post">
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
<input type="text" name="content" placeholder="Add a comment..." />
<button type="submit">Add Comment</button>
</form>











<p>Likes: <span th:text="${#lists.size(post.likes)}"></span></p>
<form th:action="@{/posts/{id}/like(id=${post.id})}" method="post">
<button type="submit" th:text="${#lists.contains(post.likes, currentUser) ? 'Unlike' : 'Like'}"></button>
</form>
<hr>


<div th:if="${post.user.id == currentUser.id}">
<!-- Flash messages -->
<div id="flash-message-[[${post.id}]]" class="flash-message" th:if="${message}">
Expand Down Expand Up @@ -378,3 +365,15 @@ <h1>Posts</h1>
</script>
</body>
</html>












32 changes: 32 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/CommentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.makersacademy.acebook.model;


import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CommentTest {




@Test
public void testCommentCreation() {
Post post = new Post();
User user = new User();
Comment comment = new Comment();
comment.setContent("Test content");
comment.setPost(post);
comment.setUser(user);

assertNotNull(comment);
assertEquals("Test content", comment.getContent());
}





}

Loading

0 comments on commit f7abfc0

Please sign in to comment.