-
Notifications
You must be signed in to change notification settings - Fork 27
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 #147 from lass9436/feature6
[이영민] step-6 댓글, step-7 ajax 댓글 구현
- Loading branch information
Showing
30 changed files
with
1,759 additions
and
238 deletions.
There are no files selected for viewing
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 +1,4 @@ | ||
 | ||
# JSP CAFE | ||
|
||
 | ||
|
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 was deleted.
Oops, something went wrong.
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,65 @@ | ||
package lass9436.comment.model; | ||
|
||
public class Comment { | ||
|
||
private long commentSeq; | ||
private long userSeq; | ||
private long questionSeq; | ||
private String writer; | ||
private String contents; | ||
private String useYn; | ||
|
||
public Comment() {} | ||
|
||
public Comment(long userSeq, long questionSeq, String writer, String contents) { | ||
this.userSeq = userSeq; | ||
this.questionSeq = questionSeq; | ||
this.writer = writer; | ||
this.contents = contents; | ||
this.useYn = "Y"; | ||
} | ||
|
||
public long getCommentSeq() { | ||
return commentSeq; | ||
} | ||
|
||
public void setCommentSeq(long commentSeq) { | ||
this.commentSeq = commentSeq; | ||
} | ||
|
||
public long getUserSeq() { | ||
return userSeq; | ||
} | ||
|
||
public void setUserSeq(long userSeq) { | ||
this.userSeq = userSeq; | ||
} | ||
|
||
public long getQuestionSeq() { | ||
return questionSeq; | ||
} | ||
|
||
public void setQuestionSeq(long questionSeq) { | ||
this.questionSeq = questionSeq; | ||
} | ||
|
||
public String getWriter() { | ||
return writer; | ||
} | ||
|
||
public void setWriter(String writer) { | ||
this.writer = writer; | ||
} | ||
|
||
public String getContents() { | ||
return contents; | ||
} | ||
|
||
public void setContents(String contents) { | ||
this.contents = contents; | ||
} | ||
|
||
public String getUseYn() { return useYn; } | ||
|
||
public void setUseYn(String useYn) { this.useYn = useYn; } | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/lass9436/comment/model/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,15 @@ | ||
package lass9436.comment.model; | ||
|
||
import java.util.List; | ||
|
||
public interface CommentRepository { | ||
Comment save(Comment comment); | ||
|
||
Comment findByCommentSeq(long commentSeq); | ||
|
||
List<Comment> findAll(); | ||
|
||
void deleteByCommentSeq(long commentSeq); | ||
|
||
List<Comment> findByQuestionSeq(long questionSeq); | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/lass9436/comment/model/CommentRepositoryDBImpl.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,135 @@ | ||
package lass9436.comment.model; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import lass9436.config.Database; | ||
|
||
public class CommentRepositoryDBImpl implements CommentRepository { | ||
|
||
@Override | ||
public Comment save(Comment comment) { | ||
String sqlInsert = "INSERT INTO comments (userSeq, questionSeq, writer, contents) VALUES (?, ?, ?, ?)"; | ||
String sqlUpdate = "UPDATE comments SET userSeq = ?, questionSeq = ?, writer = ?, contents = ? WHERE commentSeq = ?"; | ||
boolean isInsert = comment.getCommentSeq() == 0; | ||
|
||
try (Connection conn = Database.getConnection(); | ||
PreparedStatement ps = isInsert ? | ||
conn.prepareStatement(sqlInsert, Statement.RETURN_GENERATED_KEYS) : | ||
conn.prepareStatement(sqlUpdate)) { | ||
|
||
ps.setLong(1, comment.getUserSeq()); | ||
ps.setLong(2, comment.getQuestionSeq()); | ||
ps.setString(3, comment.getWriter()); | ||
ps.setString(4, comment.getContents()); | ||
|
||
if (!isInsert) { | ||
ps.setLong(5, comment.getCommentSeq()); | ||
} | ||
|
||
ps.executeUpdate(); | ||
|
||
if (isInsert) { | ||
try (ResultSet generatedKeys = ps.getGeneratedKeys()) { | ||
if (generatedKeys.next()) { | ||
comment.setCommentSeq(generatedKeys.getLong(1)); | ||
} | ||
} | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return comment; | ||
} | ||
|
||
@Override | ||
public Comment findByCommentSeq(long commentSeq) { | ||
String sql = "SELECT * FROM comments WHERE commentSeq = ? AND useYn = 'Y'"; | ||
Comment comment = null; | ||
|
||
try (Connection conn = Database.getConnection(); | ||
PreparedStatement ps = conn.prepareStatement(sql)) { | ||
ps.setLong(1, commentSeq); | ||
|
||
try (ResultSet rs = ps.executeQuery()) { | ||
if (rs.next()) { | ||
comment = mapRow(rs); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return comment; | ||
} | ||
|
||
@Override | ||
public List<Comment> findAll() { | ||
String sql = "SELECT * FROM comments WHERE useYn = 'Y'"; | ||
List<Comment> comments = new ArrayList<>(); | ||
|
||
try (Connection conn = Database.getConnection(); | ||
PreparedStatement ps = conn.prepareStatement(sql); | ||
ResultSet rs = ps.executeQuery()) { | ||
|
||
while (rs.next()) { | ||
comments.add(mapRow(rs)); | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return comments; | ||
} | ||
|
||
@Override | ||
public void deleteByCommentSeq(long commentSeq) { | ||
String sql = "UPDATE comments SET useYn = 'N' WHERE commentSeq = ?"; | ||
|
||
try (Connection conn = Database.getConnection(); | ||
PreparedStatement ps = conn.prepareStatement(sql)) { | ||
ps.setLong(1, commentSeq); | ||
ps.executeUpdate(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Comment> findByQuestionSeq(long questionSeq) { | ||
String sql = "SELECT * FROM comments WHERE questionSeq = ? AND useYn ='Y'"; | ||
List<Comment> comments = new ArrayList<>(); | ||
|
||
try (Connection conn = Database.getConnection(); | ||
PreparedStatement ps = conn.prepareStatement(sql)) { | ||
ps.setLong(1, questionSeq); | ||
|
||
try (ResultSet rs = ps.executeQuery()) { | ||
while (rs.next()) { | ||
comments.add(mapRow(rs)); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return comments; | ||
} | ||
|
||
private Comment mapRow(ResultSet rs) throws SQLException { | ||
Comment comment = new Comment(); | ||
comment.setCommentSeq(rs.getLong("commentSeq")); | ||
comment.setUserSeq(rs.getLong("userSeq")); | ||
comment.setQuestionSeq(rs.getLong("questionSeq")); | ||
comment.setWriter(rs.getString("writer")); | ||
comment.setContents(rs.getString("contents")); | ||
comment.setUseYn(rs.getString("useYn")); | ||
return comment; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/lass9436/comment/model/CommentRepositoryImpl.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,47 @@ | ||
package lass9436.comment.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class CommentRepositoryImpl implements CommentRepository { | ||
private final Map<Long, Comment> commentStore = new ConcurrentHashMap<>(); | ||
private long sequence = 0L; | ||
|
||
@Override | ||
public Comment save(Comment comment) { | ||
if (comment.getCommentSeq() == 0) { | ||
comment.setCommentSeq(++sequence); | ||
} | ||
commentStore.put(comment.getCommentSeq(), comment); | ||
return comment; | ||
} | ||
|
||
@Override | ||
public Comment findByCommentSeq(long commentSeq) { | ||
return commentStore.get(commentSeq); | ||
} | ||
|
||
@Override | ||
public List<Comment> findAll() { | ||
return new ArrayList<>(commentStore.values()); | ||
} | ||
|
||
@Override | ||
public void deleteByCommentSeq(long commentSeq) { | ||
commentStore.remove(commentSeq); | ||
} | ||
|
||
@Override | ||
public List<Comment> findByQuestionSeq(long questionSeq) { | ||
List<Comment> comments = new ArrayList<>(); | ||
for (Comment comment : commentStore.values()) { | ||
if (comment.getQuestionSeq() == questionSeq) { | ||
comments.add(comment); | ||
} | ||
} | ||
return comments; | ||
} | ||
} | ||
|
67 changes: 67 additions & 0 deletions
67
src/main/java/lass9436/comment/servlet/CommentServlet.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 lass9436.comment.servlet; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.ServletConfig; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.annotation.WebServlet; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lass9436.comment.model.Comment; | ||
import lass9436.comment.model.CommentRepository; | ||
|
||
@WebServlet("/comment") | ||
public class CommentServlet extends HttpServlet { | ||
|
||
private CommentRepository commentRepository; | ||
|
||
@Override | ||
public void init(ServletConfig config) throws ServletException { | ||
super.init(config); | ||
commentRepository = (CommentRepository)config.getServletContext().getAttribute("commentRepository"); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
String contents = req.getParameter("contents"); | ||
long questionSeq = Long.parseLong(req.getParameter("questionSeq")); | ||
String writer = (String) req.getSession(false).getAttribute("userName"); | ||
long userSeq = (long)req.getSession(false).getAttribute("userSeq"); | ||
Comment comment = new Comment(userSeq, questionSeq, writer, contents); | ||
commentRepository.save(comment); | ||
|
||
// JSON 응답 생성 | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String jsonResponse = objectMapper.writeValueAsString(comment); | ||
|
||
// 응답 설정 | ||
resp.setContentType("application/json"); | ||
resp.setCharacterEncoding("UTF-8"); | ||
resp.getWriter().write(jsonResponse); | ||
resp.setStatus(HttpServletResponse.SC_OK); | ||
} | ||
|
||
@Override | ||
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { | ||
long seq = Long.parseLong(req.getParameter("seq")); | ||
Comment comment = commentRepository.findByCommentSeq(seq); | ||
|
||
if (comment == null) { | ||
resp.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
return; | ||
} | ||
if (req.getSession(false) == null){ | ||
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
return; | ||
} | ||
if (comment.getUserSeq() != (long)req.getSession(false).getAttribute("userSeq")){ | ||
resp.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
return; | ||
} | ||
|
||
commentRepository.deleteByCommentSeq(seq); | ||
resp.setStatus(HttpServletResponse.SC_OK); | ||
} | ||
} |
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
Oops, something went wrong.