更改用户主页查询逻辑

This commit is contained in:
cyk
2023-07-05 11:23:48 +08:00
parent 404ac0a863
commit ea45ddd7f1
4 changed files with 158 additions and 8 deletions

View File

@@ -0,0 +1,59 @@
package com.hellogithub.dao;
import com.hellogithub.entity.projectEntity;
import com.hellogithub.entity.supportEntity;
import com.hellogithub.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.Connection;
import java.sql.SQLException;
public class supportDao {
public supportEntity selectSupport(int userId,int projectId)
{
int num;
supportEntity supportEntity =null;
try{
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "select * from support where isValid = 1 and userId = ? and projectId = ?";
supportEntity = runner.query(conn, sql,new BeanHandler<>(supportEntity.class) , userId,projectId);
conn.close();
}catch (SQLException e){
throw new RuntimeException(e);
}
return supportEntity;
}
public int insertSupport(int userId,int projectId)
{
int num;
try{
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "insert into support (userId,projectId,isValid) VALUES (?,?,1) ";
num = runner.update(conn, sql, userId,projectId);
conn.close();
}catch (SQLException e){
throw new RuntimeException(e);
}
return num;
}
public int deleteSupport(int userId,int projectId)
{
int num;
try{
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "delete from support where isValid = 1 and userId = ? and projectId =?";
num = runner.update(conn, sql , userId,projectId);
conn.close();
}catch (SQLException e){
throw new RuntimeException(e);
}
return num;
}
}

View File

@@ -0,0 +1,41 @@
package com.hellogithub.entity;
public class supportEntity {
int userId;
int projectId;
int isValid;
int id;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public int getIsValid() {
return isValid;
}
public void setIsValid(int isValid) {
this.isValid = isValid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

View File

@@ -0,0 +1,49 @@
package com.hellogithub.service;
import com.hellogithub.dao.supportDao;
import com.hellogithub.entity.supportEntity;
public class supprotService {
private supportDao supportDao = new supportDao();
public int retNum(int userId,int projectId){
int num ;
supportEntity supportEntity = supportDao.selectSupport(userId,projectId);
if(supportEntity == null)
{
num =0;
}
else{
num = 1;
}
return num ;
}
public String insertSupport(int userId , int projectId)
{
String tip;
int num = supportDao.insertSupport(userId,projectId);
if(num == 1)
{
tip="点赞成功";
}
else{
tip="点赞失败";
}
return tip;
}
public String deleteSupport(int userId , int projectId)
{
String tip;
int num = supportDao.deleteSupport(userId,projectId);
if(num == 1)
{
tip="删除成功";
}
else{
tip="删除失败";
}
return tip;
}
}

View File

@@ -47,20 +47,21 @@ public class userDetailServlet extends HttpServlet {
resp.setContentType("application/json;charset=UTF-8");
PrintWriter writer = resp.getWriter();
HttpSession session = req.getSession();
userEntity userEntity=(userEntity)session.getAttribute("user");
if(userEntity == null){
setResultError("用户暂未登录", writer);
String jsonString="";
String username = req.getParameter("username");
jsonString = JSONObject.toJSONString(userService.selectUserByName(username));
if(jsonString.equals(""))
{
setResultError("查询失败",writer);
}else{
String name = userEntity.getUserName();
Map<String,String> dataMap= new HashMap<>();
int id = userService.selectIdByName(name);
int id = userService.selectIdByName(username);
dataMap.put("comment",commentService.selectByUserId(id));
dataMap.put("project",projectService.retProjectByUserId(id));
String jsonString = JSONObject.toJSONString(dataMap);
jsonString = JSONObject.toJSONString(dataMap);
writer.println(jsonString);
setResultOK("查询失败",writer);
}
writer.close();
}
public void setResult(Integer code, String msg, PrintWriter writer) {