[Feature] 新增问卷列表查询接口及相关单元测试 (#377)

* feat: 1,添加修改问卷,删除问卷接口,修改创建问卷 部分逻辑
2,添加修改问卷,删除问卷接口单元测试

* feat: 1,添加修改问卷,删除问卷接口,修改创建问卷 部分逻辑
2,添加修改问卷,删除问卷接口单元测试

* style:代码格式化调整

* style:代码格式化调整

* feat:新增发布问卷接口,同时修改了创建问卷接口

* feat:新增问卷列表查询接口及相关单元测试

* feat:修改变量名

---------

Co-authored-by: kui <likui@jiuhy.cn>
This commit is contained in:
likui63 2024-08-05 14:18:49 +08:00 committed by GitHub
parent c2ccdca147
commit 92f11f81a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 369 additions and 65 deletions

View File

@ -0,0 +1,14 @@
package com.xiaojusurvey.engine.common.constants;
/**
* Survey 常量
*
* @author likui63@163.com
* @Date 2024/8/3 12:24
*/
public class SurveyConstant {
public static final String OPT_OR = "$or";
public static final String OPT_NE = "$ne";
public static final String OPT_REGEX = "$regex";
}

View File

@ -57,5 +57,14 @@ public class SurveyMeta extends BaseEntity {
*/ */
private String owner; private String owner;
/**
* 所有者ID
*/
private String ownerId;
/**
* 空间
*/
private String workspaceId;
} }

View File

@ -2,7 +2,9 @@ package com.xiaojusurvey.engine.core.survey;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.core.reslut.IdResult; import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam; import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;
/** /**
* @Author: LYF * @Author: LYF
@ -26,6 +28,12 @@ public interface SurveyService {
*/ */
boolean publishSurvey(String surveyId); boolean publishSurvey(String surveyId);
/**
* 获取问卷列表
* @param param
* @return
*/
SurveyListVO getSurveyList(SurveyListParam param);
} }

View File

@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.core.survey.dto;
import lombok.Data;
import java.io.Serializable;
/**
* 查询基础条件
*
* @author likui63@163.com
* @Date 2024/8/1 21:14
*/
@Data
public class BaseQuery implements Serializable {
private static final long serialVersionUID = 3378327499951251208L;
private int pageSize = 10;
private int curPage = 1;
}

View File

@ -0,0 +1,36 @@
package com.xiaojusurvey.engine.core.survey.dto;
import lombok.Data;
/**
* TODO
*
* @author likui63@163.com
* @Date 2024/8/1 20:59
*/
@Data
public class FilterItem {
/**
* 比较符
*/
private String comparator;
private FilterCondition condition;
@Data
public static class FilterCondition {
/**
* 字段名
*/
private String field;
/**
* 比较符
*/
private String comparator;
/**
* 筛选条件
*/
private String value;
}
}

View File

@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.core.survey.dto;
import lombok.Data;
/**
* 排序条件
*
* @author likui63@163.com
* @Date 2024/8/1 20:59
*/
@Data
public class OrderItem {
/**
* 字段名
*/
private String field;
/**
* 升序降序 1-升序-1-降序
*/
private int value = 1;
}

View File

@ -3,6 +3,7 @@ package com.xiaojusurvey.engine.core.survey.impl;
import java.util.ArrayList; import java.util.ArrayList;
import com.xiaojusurvey.engine.common.constants.RespErrorCode; import com.xiaojusurvey.engine.common.constants.RespErrorCode;
import com.xiaojusurvey.engine.common.constants.SurveyConstant;
import com.xiaojusurvey.engine.common.entity.Status; import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
@ -17,20 +18,29 @@ import com.xiaojusurvey.engine.core.survey.SurveyConfService;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService; import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.core.survey.SurveyService; import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.dto.FilterItem;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam; import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyVO;
import com.xiaojusurvey.engine.core.util.WebUtils; import com.xiaojusurvey.engine.core.util.WebUtils;
import com.xiaojusurvey.engine.repository.MongoRepository; import com.xiaojusurvey.engine.repository.MongoRepository;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @Author: LYF * @Author: LYF
@ -175,5 +185,75 @@ public class SurveyServiceImpl implements SurveyService {
return true; return true;
} }
@Override
public SurveyListVO getSurveyList(SurveyListParam param) {
log.info("[getSurveyList]获取问卷列表 param={}", param);
Query query = buildQuery(param);
List<SurveyMeta> list = mongoRepository.page(query, param.getCurPage() - 1, param.getPageSize(), SurveyMeta.class);
Long count = mongoRepository.count(query, SurveyMeta.class);
SurveyListVO vo = new SurveyListVO();
vo.setCount(count);
List<SurveyVO> data = new ArrayList<>();
if (!CollectionUtils.isEmpty(list)) {
data = list.stream().map(r -> {
SurveyVO surveyVO = new SurveyVO();
BeanUtils.copyProperties(r, surveyVO);
return surveyVO;
}).collect(Collectors.toList());
}
vo.setData(data);
return vo;
}
private Query buildQuery(SurveyListParam param) {
Query query = new Query();
if (param.getOrder() != null) {
List<Sort.Order> orders = new ArrayList<>();
Arrays.stream(param.getOrder()).forEach(r -> {
if (r.getValue() == 1) {
orders.add(new Sort.Order(Sort.Direction.ASC, r.getField()));
} else {
orders.add(new Sort.Order(Sort.Direction.DESC, r.getField()));
}
});
query.with(Sort.by(orders));
}
Criteria criteria = new Criteria();
List<Criteria> listAnd = new ArrayList();
List<Criteria> listOr = new ArrayList();
if (StringUtils.hasLength(param.getWorkspaceId())) {
listAnd.add(Criteria.where("workspaceId").is(param.getWorkspaceId()));
}
if (StringUtils.hasLength(param.getUsername())) {
listAnd.add(Criteria.where("owner").is(param.getUsername()));
}
if (StringUtils.hasLength(param.getUserId())) {
listAnd.add(Criteria.where("ownerId").is(param.getUserId()));
}
if (param.getFilter() != null) {
Arrays.stream(param.getFilter()).forEach(r -> {
FilterItem.FilterCondition ff = r.getCondition();
Criteria crt = null;
if (SurveyConstant.OPT_NE.equals(ff.getComparator())) {
crt = Criteria.where(ff.getField()).ne(ff.getValue());
} else if (SurveyConstant.OPT_REGEX.equals(ff.getComparator())) {
crt = Criteria.where(ff.getField()).regex(ff.getValue());
}
if (StringUtils.hasLength(r.getComparator()) && SurveyConstant.OPT_OR.equals(r.getComparator())) {
listOr.add(crt);
} else {
listOr.add(crt);
}
});
}
if (!listAnd.isEmpty()) {
criteria.andOperator(listAnd);
}
if (!listOr.isEmpty()) {
criteria = criteria.orOperator(listOr);
}
query.addCriteria(criteria);
return query;
}
} }

View File

@ -0,0 +1,37 @@
package com.xiaojusurvey.engine.core.survey.param;
import com.xiaojusurvey.engine.core.survey.dto.BaseQuery;
import com.xiaojusurvey.engine.core.survey.dto.FilterItem;
import com.xiaojusurvey.engine.core.survey.dto.OrderItem;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 问卷查询
*
* @author likui63@163.com
* @Date 2024/8/1 21:05
*/
@Data
public class SurveyListParam extends BaseQuery implements Serializable {
private static final long serialVersionUID = -6294820920444326423L;
private FilterItem[] filter;
private OrderItem[] order;
@NotBlank(message = "空间id不能为空")
private String workspaceId;
/**
* 所有者
*/
private String username;
/**
* 所有者Id
*/
private String userId;
}

View File

@ -0,0 +1,27 @@
package com.xiaojusurvey.engine.core.survey.vo;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 问卷列表
*
* @author likui63@163.com
* @Date 2024/8/1 21:36
*/
@Data
public class SurveyListVO implements Serializable {
private static final long serialVersionUID = 2263256769125151536L;
/**
* 问卷列表
*/
private List<SurveyVO> data;
/**
* 总数量
*/
private Long count;
}

View File

@ -1,53 +1,56 @@
//package com.xiaojusurvey.engine.core.survey.vo; package com.xiaojusurvey.engine.core.survey.vo;
//
//import com.xiaojusurvey.engine.common.entity.Status; import com.xiaojusurvey.engine.common.entity.Status;
// import lombok.Data;
//import javax.validation.constraints.NotBlank;
//import javax.validation.constraints.NotNull; import javax.validation.constraints.NotBlank;
// import javax.validation.constraints.NotNull;
///** import java.io.Serializable;
// * 问卷信息
// * /**
// * @author likui63@163.com * 问卷信息
// * @Date 2024/7/28 21:57 *
// */ * @author likui63@163.com
//public class SurveyVO { * @Date 2024/7/28 21:57
// */
// private String _id; @Data
// public class SurveyVO implements Serializable {
// private String name;
// /** private static final long serialVersionUID = 6002962715728356197L;
// * 问卷类型
// */ private String name;
// @NotBlank(message = "问卷类型不能为空") /**
// @NotNull(message = "问卷类型不能为空") * 问卷类型
// private String surveyType; */
// @NotBlank(message = "问卷类型不能为空")
// /** @NotNull(message = "问卷类型不能为空")
// * 问卷短链 private String surveyType;
// */
// private String surveyPath; /**
// /** * 问卷短链
// * 问卷标题 */
// */ private String surveyPath;
// private String title; /**
// /** * 问卷标题
// * 当前状态 */
// */ private String title;
// private Status curStatus; /**
// * 当前状态
// /** */
// * 创建时间 private Status curStatus;
// */
// private Long createDate; /**
// * 创建时间
// /** */
// * 创建人 private Long createDate;
// */
// private String creator; /**
// /** * 创建人
// * 所有者 */
// */ private String creator;
// private String owner; /**
// * 所有者
//} */
private String owner;
}

View File

@ -1,6 +1,7 @@
package com.xiaojusurvey.engine.core.survey.impl; package com.xiaojusurvey.engine.core.survey.impl;
import com.xiaojusurvey.engine.common.entity.BaseEntity;
import com.xiaojusurvey.engine.common.entity.Status; import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
@ -9,7 +10,10 @@ import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum;
import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyConfService;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService; import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.core.survey.dto.FilterItem;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam; import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;
import com.xiaojusurvey.engine.core.util.WebUtils; import com.xiaojusurvey.engine.core.util.WebUtils;
import com.xiaojusurvey.engine.repository.MongoRepository; import com.xiaojusurvey.engine.repository.MongoRepository;
import org.junit.Assert; import org.junit.Assert;
@ -21,6 +25,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic; import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.internal.matchers.Any;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -55,10 +60,10 @@ public class SurveyServiceImplTest {
SurveyMetaUpdateParam updateParam; SurveyMetaUpdateParam updateParam;
SurveyMeta surveyMeta; SurveyMeta surveyMeta;
String surveyid = "111111111111111111"; String surveyid = "111111111111111111";
@Before @Before
public void before(){ public void before() {
surveyMeta = new SurveyMeta(); surveyMeta = new SurveyMeta();
surveyMeta.setId(surveyid); surveyMeta.setId(surveyid);
surveyMeta.setTitle("title"); surveyMeta.setTitle("title");
@ -80,29 +85,29 @@ public class SurveyServiceImplTest {
updateParam.setTitle("新时代"); updateParam.setTitle("新时代");
updateParam.setRemark("这是一个测试"); updateParam.setRemark("这是一个测试");
Mockito.when(mongoRepository.findOne(Mockito.any(),Mockito.any())).thenReturn(surveyMeta); Mockito.when(mongoRepository.findOne(Mockito.any(), Mockito.any())).thenReturn(surveyMeta);
boolean flag = surveyService.updateMeta(updateParam); boolean flag = surveyService.updateMeta(updateParam);
Assert.assertEquals(true,flag); Assert.assertEquals(true, flag);
} }
@Test @Test
public void deleteSurveyTest() { public void deleteSurveyTest() {
Mockito.when(mongoRepository.findOne(Mockito.any(),Mockito.any())).thenReturn(surveyMeta); Mockito.when(mongoRepository.findOne(Mockito.any(), Mockito.any())).thenReturn(surveyMeta);
Mockito.when(surveyPublishService.delete(Mockito.any())).thenReturn(true); Mockito.when(surveyPublishService.delete(Mockito.any())).thenReturn(true);
boolean flag = surveyService.deleteSurvey(surveyid); boolean flag = surveyService.deleteSurvey(surveyid);
Assert.assertEquals(true,flag); Assert.assertEquals(true, flag);
} }
@Test @Test
public void publishSurveyTest(){ public void publishSurveyTest() {
SurveyConf conf = new SurveyConf(); SurveyConf conf = new SurveyConf();
Mockito.when(surveyConfService.getSurveyConfBySurveyId(Mockito.any())).thenReturn(conf); Mockito.when(surveyConfService.getSurveyConfBySurveyId(Mockito.any())).thenReturn(conf);
Mockito.when(mongoRepository.findOne(Mockito.any(),Mockito.eq(SurveyMeta.class))).thenReturn(surveyMeta); Mockito.when(mongoRepository.findOne(Mockito.any(), Mockito.eq(SurveyMeta.class))).thenReturn(surveyMeta);
Mockito.when(mongoRepository.save(Mockito.any())).thenReturn(null); Mockito.when(mongoRepository.save(Mockito.any())).thenReturn(null);
Mockito.when(surveyPublishService.save(Mockito.any())).thenReturn(true); Mockito.when(surveyPublishService.save(Mockito.any())).thenReturn(true);
Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(null); Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(null);
@ -114,7 +119,29 @@ public class SurveyServiceImplTest {
user.setUsername("ttt"); user.setUsername("ttt");
Mockito.when(req.getAttribute("user")).thenReturn(user); Mockito.when(req.getAttribute("user")).thenReturn(user);
boolean falg = surveyService.publishSurvey(surveyid); boolean falg = surveyService.publishSurvey(surveyid);
Assert.assertTrue("成功",falg == true); Assert.assertTrue("成功", falg == true);
}
@Test
public void getListTest() {
SurveyListParam param = new SurveyListParam();
param.setWorkspaceId("1");
FilterItem item = new FilterItem();
// item.setComparator("$regex");
FilterItem.FilterCondition condition = new FilterItem.FilterCondition();
condition.setComparator("$regex");
condition.setField("title");
condition.setValue("newTime3");
item.setCondition(condition);
param.setFilter(new FilterItem[]{item});
List<BaseEntity> list = new ArrayList<>();
Mockito.when(mongoRepository.page(Mockito.any(), Mockito.anyInt(), Mockito.anyInt(), Mockito.any())).thenReturn(list);
Mockito.when(mongoRepository.count(Mockito.any(), Mockito.any())).thenReturn(1L);
SurveyListVO vo = surveyService.getSurveyList(param);
Assert.assertTrue("成功", vo.getCount() == 1);
} }

View File

@ -13,9 +13,11 @@ import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyConfService;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyService; import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam; import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -146,4 +148,11 @@ public class SurveyController {
} }
@PostMapping("/getList")
public RpcResult getList(@RequestBody @Validated SurveyListParam param) {
log.info("[publishSurvey] 发布问卷,param={}", param);
SurveyListVO surveyListVO = surveyService.getSurveyList(param);
return RpcResultUtil.createSuccessResult(surveyListVO);
}
} }

View File

@ -9,8 +9,10 @@ import com.xiaojusurvey.engine.config.BannerDataConfig;
import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyConfService;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyService; import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -97,6 +99,16 @@ public class SurveyControllerTest {
Mockito.when(surveyService.publishSurvey(Mockito.any())).thenReturn(true); Mockito.when(surveyService.publishSurvey(Mockito.any())).thenReturn(true);
surveyController.publishSurvey("11111"); surveyController.publishSurvey("11111");
}
@Test
public void getListTest() {
SurveyListVO vo = new SurveyListVO();
Mockito.when(surveyService.getSurveyList(Mockito.any())).thenReturn(vo);
RpcResult rs = surveyController.getList(new SurveyListParam());
Assert.assertEquals(vo,rs.getData());
} }
} }