feat: validate consumer behavior hypotheses
parent
c47616b1ec
commit
1ce291ceef
@ -0,0 +1,23 @@
|
||||
package com.sztzjy.linkCommerce.entity.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ConsumerBehaviorStepOneValidationRequest {
|
||||
private List<Hypothesis> hypotheses;
|
||||
|
||||
public List<Hypothesis> getHypotheses() { return hypotheses; }
|
||||
public void setHypotheses(List<Hypothesis> hypotheses) { this.hypotheses = hypotheses; }
|
||||
|
||||
public static class Hypothesis {
|
||||
private String targetGroup;
|
||||
private String keyFinding;
|
||||
private String coreHypothesis;
|
||||
|
||||
public String getTargetGroup() { return targetGroup; }
|
||||
public void setTargetGroup(String targetGroup) { this.targetGroup = targetGroup; }
|
||||
public String getKeyFinding() { return keyFinding; }
|
||||
public void setKeyFinding(String keyFinding) { this.keyFinding = keyFinding; }
|
||||
public String getCoreHypothesis() { return coreHypothesis; }
|
||||
public void setCoreHypothesis(String coreHypothesis) { this.coreHypothesis = coreHypothesis; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.sztzjy.linkCommerce.entity.dto;
|
||||
|
||||
public class ConsumerBehaviorStepOneValidationResult {
|
||||
private boolean valid;
|
||||
private String message;
|
||||
|
||||
public boolean isValid() { return valid; }
|
||||
public void setValid(boolean valid) { this.valid = valid; }
|
||||
public String getMessage() { return message; }
|
||||
public void setMessage(String message) { this.message = message; }
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.sztzjy.linkCommerce.service;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.dto.ConsumerBehaviorStepOneValidationRequest;
|
||||
import com.sztzjy.linkCommerce.entity.dto.ConsumerBehaviorStepOneValidationResult;
|
||||
|
||||
public interface ConsumerBehaviorStepOneService {
|
||||
ConsumerBehaviorStepOneValidationResult validate(ConsumerBehaviorStepOneValidationRequest request, JwtUser user);
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.exception.handler.ServiceException;
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.dto.ConsumerBehaviorStepOneValidationRequest;
|
||||
import com.sztzjy.linkCommerce.entity.dto.ConsumerBehaviorStepOneValidationResult;
|
||||
import com.sztzjy.linkCommerce.service.ConsumerBehaviorStepOneService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ConsumerBehaviorStepOneServiceImpl implements ConsumerBehaviorStepOneService {
|
||||
@Override
|
||||
public ConsumerBehaviorStepOneValidationResult validate(ConsumerBehaviorStepOneValidationRequest request, JwtUser user) {
|
||||
requireStudent(user);
|
||||
if (request == null || request.getHypotheses() == null || request.getHypotheses().isEmpty()) {
|
||||
throw badRequest("请至少填写一条目标人群与问卷核心假设");
|
||||
}
|
||||
for (ConsumerBehaviorStepOneValidationRequest.Hypothesis hypothesis : request.getHypotheses()) {
|
||||
if (hypothesis == null || StringUtils.isBlank(hypothesis.getTargetGroup())
|
||||
|| StringUtils.isBlank(hypothesis.getKeyFinding()) || StringUtils.isBlank(hypothesis.getCoreHypothesis())) {
|
||||
throw badRequest("请完整填写目标人群、实训8关键发现和问卷核心假设");
|
||||
}
|
||||
}
|
||||
ConsumerBehaviorStepOneValidationResult result = new ConsumerBehaviorStepOneValidationResult();
|
||||
result.setValid(true);
|
||||
result.setMessage("校验通过");
|
||||
return result;
|
||||
}
|
||||
|
||||
private void requireStudent(JwtUser user) {
|
||||
if (user == null || StringUtils.isBlank(user.getUserId())) {
|
||||
throw new ServiceException(HttpStatus.UNAUTHORIZED, "请先登录后再提交");
|
||||
}
|
||||
if (user.getRoleId() != 4) {
|
||||
throw new ServiceException(HttpStatus.FORBIDDEN, "仅学生可提交实训答案");
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceException badRequest(String message) {
|
||||
return new ServiceException(HttpStatus.BAD_REQUEST, message);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue