点赞
This commit is contained in:
@@ -70,5 +70,19 @@ public class commentDao {
|
||||
}
|
||||
return commentEntityList;
|
||||
}
|
||||
public int addLikeNum(int commentId)
|
||||
{
|
||||
int num ;
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql = "update comment set likeNum = likeNum +1 where commentId = ? and isValid = 1";
|
||||
num = runner.update(conn, sql,commentId);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return num ;
|
||||
}
|
||||
}
|
||||
|
@@ -63,4 +63,16 @@ public class commentService {
|
||||
|
||||
return JSONObject.toJSONString(commentEntityList);
|
||||
}
|
||||
public String addLikeNum(int commentId){
|
||||
String tip="";
|
||||
int num = commentDao.addLikeNum(commentId);
|
||||
if(num == 1)
|
||||
{
|
||||
tip="点赞成功";
|
||||
}
|
||||
else{
|
||||
tip= "点赞失败";
|
||||
}
|
||||
return tip;
|
||||
}
|
||||
}
|
||||
|
76
src/com/hellogithub/servlet/addLikeServlet.java
Normal file
76
src/com/hellogithub/servlet/addLikeServlet.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package com.hellogithub.servlet;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.entity.userEntity;
|
||||
import com.hellogithub.service.commentService;
|
||||
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 jakarta.servlet.http.HttpSession;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
|
||||
@WebServlet("/addLike")
|
||||
public class addLikeServlet extends HttpServlet {
|
||||
|
||||
private commentService commentService = new commentService();
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req,resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("text/html; charset=utf-8");
|
||||
// 设置响应头允许ajax跨域访问
|
||||
String curOrigin = req.getHeader("Origin");
|
||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
//
|
||||
String proId = req.getParameter("id");
|
||||
PrintWriter writer = resp.getWriter();
|
||||
HttpSession session =req.getSession();
|
||||
userEntity userEntity=(userEntity)session.getAttribute("user");
|
||||
if(userEntity ==null)
|
||||
{
|
||||
setResultError("用户未登录", writer);
|
||||
}
|
||||
else{
|
||||
commentService.addLikeNum(Integer.parseInt(proId));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void setResultError(String msg, PrintWriter writer) {
|
||||
setResult(500, msg, writer);
|
||||
}
|
||||
|
||||
public void setResultOK(String msg, PrintWriter writer) {
|
||||
setResult(200, msg, writer);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user