增加点赞功能
This commit is contained in:
@@ -14,7 +14,7 @@ import java.util.*;
|
|||||||
public class projectService {
|
public class projectService {
|
||||||
|
|
||||||
|
|
||||||
private static projectDao projectDao = new projectDao();
|
private projectDao projectDao = new projectDao();
|
||||||
private categoryDao categoryDao = new categoryDao();
|
private categoryDao categoryDao = new categoryDao();
|
||||||
/**
|
/**
|
||||||
* 按照期刊号查询
|
* 按照期刊号查询
|
||||||
|
@@ -1,14 +1,15 @@
|
|||||||
package com.hellogithub.service;
|
package com.hellogithub.service;
|
||||||
|
|
||||||
|
import com.hellogithub.dao.UserDao;
|
||||||
import com.hellogithub.dao.supportDao;
|
import com.hellogithub.dao.supportDao;
|
||||||
import com.hellogithub.entity.supportEntity;
|
import com.hellogithub.entity.supportEntity;
|
||||||
|
|
||||||
public class supprotService {
|
public class supportService {
|
||||||
private supportDao supportDao = new supportDao();
|
private supportDao supportDao = new supportDao();
|
||||||
|
private UserDao userDao = new UserDao();
|
||||||
public int retNum(int userId,int projectId){
|
public int retNum(String username,int projectId){
|
||||||
int num ;
|
int num ;
|
||||||
supportEntity supportEntity = supportDao.selectSupport(userId,projectId);
|
supportEntity supportEntity = supportDao.selectSupport(userDao.selectIdByName(username),projectId);
|
||||||
if(supportEntity == null)
|
if(supportEntity == null)
|
||||||
{
|
{
|
||||||
num =0;
|
num =0;
|
||||||
@@ -19,10 +20,10 @@ public class supprotService {
|
|||||||
return num ;
|
return num ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String insertSupport(int userId , int projectId)
|
public String insertSupport(String username, int projectId)
|
||||||
{
|
{
|
||||||
String tip;
|
String tip;
|
||||||
int num = supportDao.insertSupport(userId,projectId);
|
int num = supportDao.insertSupport(userDao.selectIdByName(username),projectId);
|
||||||
if(num == 1)
|
if(num == 1)
|
||||||
{
|
{
|
||||||
tip="点赞成功";
|
tip="点赞成功";
|
||||||
@@ -33,16 +34,16 @@ public class supprotService {
|
|||||||
return tip;
|
return tip;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String deleteSupport(int userId , int projectId)
|
public String deleteSupport(String username, int projectId)
|
||||||
{
|
{
|
||||||
String tip;
|
String tip;
|
||||||
int num = supportDao.deleteSupport(userId,projectId);
|
int num = supportDao.deleteSupport(userDao.selectIdByName(username),projectId);
|
||||||
if(num == 1)
|
if(num == 1)
|
||||||
{
|
{
|
||||||
tip="删除成功";
|
tip="取消成功";
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
tip="删除失败";
|
tip="取消失败";
|
||||||
}
|
}
|
||||||
return tip;
|
return tip;
|
||||||
}
|
}
|
72
src/com/hellogithub/servlet/whetherAddLikeServlet.java
Normal file
72
src/com/hellogithub/servlet/whetherAddLikeServlet.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package com.hellogithub.servlet;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hellogithub.entity.userEntity;
|
||||||
|
import com.hellogithub.service.supportService;
|
||||||
|
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("/whetherAddLike")
|
||||||
|
public class whetherAddLikeServlet extends HttpServlet {
|
||||||
|
private com.hellogithub.service.supportService supportService = new supportService();
|
||||||
|
@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");
|
||||||
|
PrintWriter writer = resp.getWriter();
|
||||||
|
String proId = req.getParameter("proId");
|
||||||
|
HttpSession session = req.getSession();
|
||||||
|
userEntity userEntity= null;
|
||||||
|
userEntity=(userEntity)session.getAttribute("user");
|
||||||
|
if(userEntity == null){
|
||||||
|
setResultError("用户未登录,无法点赞",writer);
|
||||||
|
}else{
|
||||||
|
String tip = supportService.insertSupport(userEntity.getUserName(),Integer.parseInt(proId));
|
||||||
|
writer.println(tip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
74
src/com/hellogithub/servlet/whetherCancelLikeServlet.java
Normal file
74
src/com/hellogithub/servlet/whetherCancelLikeServlet.java
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package com.hellogithub.servlet;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hellogithub.entity.userEntity;
|
||||||
|
import com.hellogithub.service.supportService;
|
||||||
|
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("/whetherCancelLike")
|
||||||
|
public class whetherCancelLikeServlet extends HttpServlet {
|
||||||
|
private com.hellogithub.service.supportService supportService = new supportService();
|
||||||
|
@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");
|
||||||
|
PrintWriter writer = resp.getWriter();
|
||||||
|
String proId = req.getParameter("proId");
|
||||||
|
HttpSession session = req.getSession();
|
||||||
|
userEntity userEntity= null;
|
||||||
|
userEntity=(userEntity)session.getAttribute("user");
|
||||||
|
if(userEntity == null){
|
||||||
|
setResultError("用户未登录,无法点赞",writer);
|
||||||
|
}else{
|
||||||
|
String tip = supportService.deleteSupport(userEntity.getUserName(),Integer.parseInt(proId));
|
||||||
|
writer.println(tip);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
75
src/com/hellogithub/servlet/whetherLikeServlet.java
Normal file
75
src/com/hellogithub/servlet/whetherLikeServlet.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package com.hellogithub.servlet;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hellogithub.entity.userEntity;
|
||||||
|
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 com.hellogithub.service.supportService;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@WebServlet("/whetherLike")
|
||||||
|
public class whetherLikeServlet extends HttpServlet {
|
||||||
|
private supportService supportService = new supportService();
|
||||||
|
@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");
|
||||||
|
PrintWriter writer = resp.getWriter();
|
||||||
|
String proId = req.getParameter("proId");
|
||||||
|
HttpSession session = req.getSession();
|
||||||
|
userEntity userEntity= null;
|
||||||
|
userEntity=(userEntity)session.getAttribute("user");
|
||||||
|
if(userEntity == null)
|
||||||
|
{
|
||||||
|
writer.println(0);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
int num = supportService.retNum(userEntity.getUserName(),Integer.parseInt(proId));
|
||||||
|
writer.println(num);
|
||||||
|
}
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
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