Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]:添加修改问卷,删除问卷接口,增加相关单元测试 #369

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum RespErrorCode {
SURVEY_TYPE_ERROR(3003, "问卷类型错误"),
SURVEY_NOT_FOUND(3004, "问卷不存在"),
SURVEY_CONTENT_NOT_ALLOW(3005, "存在禁用内容"),
UPDATE_SURVEY_META_ERROR(3006, "修改问卷失败"),
DELETE_SURVEY_ERROR(3007, "删除问卷失败"),
CAPTCHA_INCORRECT(4001, "验证码不正确"),
RESPONSE_SIGN_ERROR(9001, "签名不正确"),
RESPONSE_CURRENT_TIME_NOT_ALLOW(9002, "当前时间不允许提交"),
Expand All @@ -24,6 +26,9 @@ public enum RespErrorCode {
RESPONSE_DATA_DECRYPT_ERROR(9005, "问卷已删除"),
UPLOAD_FILE_ERROR(5001, "上传文件错误");




private final int code;
private final String message;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@ public class Status implements Serializable {
private Long date;

private String status;


public Long getDate() {
return date;
}

public void setDate(Long date) {
this.date = date;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class SurveyMeta extends BaseEntity {
@NotBlank(message = "问卷类型不能为空")
@NotNull(message = "问卷类型不能为空")
private String surveyType;

/**
* 问卷短链
*/
private String surveyPath;
/**
* 问卷标题
*/
Expand All @@ -43,5 +48,14 @@ public class SurveyMeta extends BaseEntity {
*/
private String createFrom;

/**
* 创建人
*/
private String creator;
/**
* 所有者
*/
private String owner;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.xiaojusurvey.engine.common.entity.survey;

import com.xiaojusurvey.engine.common.entity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.data.mongodb.core.mapping.Document;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
* 问卷发布后的配置表
*
* @author [email protected]
* @date: 2024/7/27 13:59
*/
@Document("surveySubmit")
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Data
public class SurveyPublish extends BaseEntity {

/**
* 问卷配置ID
*/
@NotBlank(message = "问卷配置ID不能为空")
@NotNull(message = "问卷配置ID不能为空")
private String pageId;

/**
* 问卷标题
*/
@NotBlank(message = "问卷标题不能为空")
@NotNull(message = "问卷标题不能为空")
private String title;
/**
* 问卷短链
*/
@NotBlank(message = "问卷短链不能为空")
@NotNull(message = "问卷短链不能为空")
private String surveyPath;
/**
* 问卷schema
*/
@NotBlank(message = "问卷schema不能为空")
@NotNull(message = "问卷schema不能为空")
private String code;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.xiaojusurvey.engine.common.enums;

import com.xiaojusurvey.engine.common.entity.Status;

/**
* Survey状态
*
* @author [email protected]
* @Date 2024/7/26 23:32
*/
public enum SurveyStatusEnum {
NEW("new"),
EDITING("editing"),
PAUSING("pausing"),
PUBLISHED("published"),
REMOVED("removed"),
FORCE_REMOVED("FORCE_REMOVED");

private String status;

SurveyStatusEnum(String status) {
this.status = status;
}

public String getStatus() {
return this.status;
}


public static Status getSpecStatus(SurveyStatusEnum statusEnum) {
Status newStatus = new Status();
newStatus.setStatus(statusEnum.getStatus());
newStatus.setDate(System.currentTimeMillis());
return newStatus;
}

}
10 changes: 10 additions & 0 deletions survey-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xiaojusurvey.engine.core.survey;

import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;

/**
* 问卷发布配置服务
*
* @author [email protected]
* @date: 2024/7/27 13:54
*/
public interface SurveyPublishService {


boolean delete(SurveyMeta param);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;

/**
* @Author: LYF
Expand All @@ -12,4 +13,8 @@ public interface SurveyService {
IdResult createSurvey(SurveyMeta surveyMeta);

SurveyMeta getSurveyMeta(String surveyId);

boolean updateMeta(SurveyMetaUpdateParam param);

boolean deleteSurvey(String surveyId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.xiaojusurvey.engine.core.survey.impl;

import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.common.entity.survey.SurveyPublish;
import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
* 问卷发布配置服务实现
*
* @author [email protected]
* @Date 2024/7/27 14:02
*/
@Service
public class SurveyPublishServiceImpl implements SurveyPublishService {
@Resource
private MongoRepository mongoRepository;

public MongoRepository getMongoRepository() {
return mongoRepository;
}

public void setMongoRepository(MongoRepository mongoRepository) {
this.mongoRepository = mongoRepository;
}


@Override
public boolean delete(SurveyMeta param) {
if (StringUtils.hasLength(param.getSurveyPath())) {
SurveyPublish surveyPublish = getBysurveyPath(param.getSurveyPath());
if (surveyPublish != null) {
Status st = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.PUBLISHED);
surveyPublish.setCurStatus(st);
surveyPublish.getStatusList().add(st);
mongoRepository.save(surveyPublish);
}
}
return true;
}

public SurveyPublish getBysurveyPath(String surveyPath) {
Query query = new Query();
query.addCriteria(Criteria.where("surveyPath").is(surveyPath));
SurveyPublish surveyPublish = mongoRepository.findOne(query, SurveyPublish.class);
return surveyPublish;
}


}
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
package com.xiaojusurvey.engine.core.survey.impl;

import com.xiaojusurvey.engine.common.constants.RespErrorCode;
import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum;
import com.xiaojusurvey.engine.common.exception.ServiceException;
import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.repository.MongoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* @Author: LYF
* @CreateTime: 2024-06-05
* @Description: 问卷ServiceImpl
*/
@Service("surveyService")
@Service
@Slf4j
public class SurveyServiceImpl implements SurveyService {

@Resource
private MongoRepository mongoRepository;

@Resource
private SurveyPublishService surveyPublishService;

public MongoRepository getMongoRepository() {
return mongoRepository;
}

public void setMongoRepository(MongoRepository mongoRepository) {
this.mongoRepository = mongoRepository;
}

/**
* 创建问卷
*/
@Override
public IdResult createSurvey(SurveyMeta surveyMeta) {
IdResult idResult = new IdResult();
Status newStatus = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.NEW);
surveyMeta.setCurStatus(newStatus);
List<Status> statusList = Arrays.asList(newStatus);
surveyMeta.setStatusList(statusList);
idResult.setId(mongoRepository.save(surveyMeta).getId());
return idResult;
}
Expand All @@ -37,4 +63,46 @@ public SurveyMeta getSurveyMeta(String surveyId) {
query.addCriteria(Criteria.where("_id").is(surveyId));
return mongoRepository.findOne(query, SurveyMeta.class);
}

@Override
public boolean updateMeta(SurveyMetaUpdateParam param) {
SurveyMeta temp = getSurveyMeta(param.getSurveyId());
Status st = temp.getCurStatus();
if (!st.getStatus().equals(SurveyStatusEnum.NEW.getStatus()) && !st.getStatus().equals(SurveyStatusEnum.EDITING.getStatus())) {
Status newStatus = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.EDITING);
temp.setCurStatus(newStatus);
temp.setTitle(param.getTitle());
temp.setRemark(param.getRemark());
temp.getStatusList().add(newStatus);
}
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(param.getSurveyId()));
Update update = new Update();
update.set("title", param.getTitle()).set("remark", param.getRemark());
update.set("curStatus", temp.getCurStatus());
update.set("statusList", temp.getStatusList());
mongoRepository.updateFirst(query, update, SurveyMeta.class);
return true;
}

@Override
public boolean deleteSurvey(String surveyId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(surveyId));
SurveyMeta meta = mongoRepository.findOne(query, SurveyMeta.class);
if (meta.getCurStatus().getStatus().equals(SurveyStatusEnum.REMOVED.getStatus())) {
log.error("[deleteSurvey] 问卷已删除,不能重复删除,surveyId={}", surveyId);
throw new ServiceException(RespErrorCode.SURVEY_STATUS_TRANSFORM_ERROR.getMessage(), RespErrorCode.SURVEY_STATUS_TRANSFORM_ERROR.getCode()); // 问卷状态转换报错
}
Status newStatus = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.REMOVED);
meta.setCurStatus(newStatus);
meta.getStatusList().add(newStatus);
mongoRepository.save(meta);

//删除问卷回收表
surveyPublishService.delete(meta);
return true;
}


}
Loading