Skip to content
Snippets Groups Projects
Commit b6ee5444 authored by Beth Davies's avatar Beth Davies
Browse files

Fix method problems arising from updated users tables

parent b9810d45
Branches
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ public class CommentJdbcRepository implements CommentRepository {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Comment save(Comment comment) {
String sql = "INSERT INTO comment (comment_content, user_id, post_title, created_date) VALUES (?, ?, ?, ?)";
jdbcTemplate.update(sql,
......@@ -35,24 +36,28 @@ public class CommentJdbcRepository implements CommentRepository {
return comment;
}
@Override
public List<Comment> findByPostTitle(String postTitle) {
String sql = "SELECT c.id, c.comment_content, c.user_id, c.post_title, c.created_date, " +
"u.fullname as username, u.email as user_email " +
"FROM comment c " +
"JOIN users u ON c.user_id = u.id " +
"WHERE c.post_title = ?";
return jdbcTemplate.query(sql, new CommentRowMapper(), postTitle);
}
return jdbcTemplate.query(sql, new Object[]{postTitle}, new RowMapper<Comment>() {
private static class CommentRowMapper implements RowMapper<Comment> {
@Override
public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
Comment comment = new Comment();
comment.setId(rs.getInt("comment_id"));
comment.setId(rs.getInt("id"));
comment.setContent(rs.getString("comment_content"));
comment.setUserId(rs.getInt("user_id"));
comment.setPostTitle(rs.getString("post_title"));
comment.setCreatedDate(rs.getTimestamp("created_date").toLocalDateTime());
comment.setUsername(rs.getString("username"));
comment.setUserEmail(rs.getString("user_email"));
return comment;
}
});
}
}
\ No newline at end of file
package polish_community_group_11.polish_community.comments.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import polish_community_group_11.polish_community.comments.models.Comment;
import java.util.List;
public interface CommentRepository extends JpaRepository <Comment, Integer>{
public interface CommentRepository {
Comment save(Comment comment);
List<Comment> findByPostTitle(String postTitle);
}
package polish_community_group_11.polish_community.comments.models;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Entity
@Table(name = "comment")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String content;
@Column(name = "user_id")
private int userId;
@Column(name = "post_title")
private String postTitle;
@Column(name = "user_email")
private String userEmail;
private String username;
@Column(name = "created_date")
private LocalDateTime createdDate;
// Constructor without id for creating new comments
......
......@@ -7,7 +7,7 @@ import polish_community_group_11.polish_community.comments.dao.CommentRepository
import polish_community_group_11.polish_community.comments.models.Comment;
import polish_community_group_11.polish_community.feed.models.Feed;
import polish_community_group_11.polish_community.register.models.User;
import polish_community_group_11.polish_community.register.services.UserRepository;
import polish_community_group_11.polish_community.register.dao.UserRepository;
import java.time.LocalDateTime;
import java.util.List;
......@@ -39,5 +39,6 @@ public class CommentService {
LocalDateTime.now()
);
return commentRepository.save(comment);
}
}
......@@ -2,7 +2,7 @@
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Comment Section</title>
<link rel="stylesheet" href="/static/css/comments/comments.css">
<link rel="stylesheet" th:href="@{/static/css/comments/comments.css}">
</head>
<body>
......
......@@ -5,7 +5,7 @@
<head>
<meta name="viewport" CHARSET="UTF-8" content="width=device-width, initial-scale=1">
<title>POLISH COMMUNITY GROUP</title>
<link rel="stylesheet" href="/css/comments/comments.css">
<link rel="stylesheet" href="/css/layout/layout.css">
</head>
<body>
......@@ -99,7 +99,7 @@
<p class="footerCompanyName">&copy; LUDEK PCG ltd. All rights reserved.</p>
</div>
</footer>
<script th:replace="comments/commentFragment::commentScript"></script>
<script th:replace="~{comments/commentFragment::commentScript}"></script>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment