feat: validate consumer behavior survey report
parent
fc5b3e7b96
commit
3143f136e1
@ -0,0 +1,20 @@
|
|||||||
|
package com.sztzjy.linkCommerce.entity.dto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ConsumerBehaviorStepFourValidationRequest {
|
||||||
|
private List<ReportRow> rows;
|
||||||
|
|
||||||
|
public List<ReportRow> getRows() { return rows; }
|
||||||
|
public void setRows(List<ReportRow> rows) { this.rows = rows; }
|
||||||
|
|
||||||
|
public static class ReportRow {
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
public String getTitle() { return title; }
|
||||||
|
public void setTitle(String title) { this.title = title; }
|
||||||
|
public String getContent() { return content; }
|
||||||
|
public void setContent(String content) { this.content = content; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package com.sztzjy.linkCommerce.entity.dto;
|
||||||
|
|
||||||
|
public class ConsumerBehaviorStepFourValidationResult {
|
||||||
|
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.ConsumerBehaviorStepFourValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ConsumerBehaviorStepFourValidationResult;
|
||||||
|
|
||||||
|
public interface ConsumerBehaviorStepFourService {
|
||||||
|
ConsumerBehaviorStepFourValidationResult validate(ConsumerBehaviorStepFourValidationRequest request, JwtUser user);
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
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.ConsumerBehaviorStepFourValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ConsumerBehaviorStepFourValidationResult;
|
||||||
|
import com.sztzjy.linkCommerce.service.ConsumerBehaviorStepFourService;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ConsumerBehaviorStepFourServiceImpl implements ConsumerBehaviorStepFourService {
|
||||||
|
private static final int REQUIRED_REPORT_ROWS = 5;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConsumerBehaviorStepFourValidationResult validate(ConsumerBehaviorStepFourValidationRequest request, JwtUser user) {
|
||||||
|
requireStudent(user);
|
||||||
|
List<ConsumerBehaviorStepFourValidationRequest.ReportRow> rows = request == null ? null : request.getRows();
|
||||||
|
if (rows == null || rows.size() != REQUIRED_REPORT_ROWS) {
|
||||||
|
throw badRequest("问卷调研报告需填写 5 个报告模块");
|
||||||
|
}
|
||||||
|
for (ConsumerBehaviorStepFourValidationRequest.ReportRow row : rows) {
|
||||||
|
if (row == null || StringUtils.isBlank(row.getTitle()) || StringUtils.isBlank(row.getContent())) {
|
||||||
|
throw badRequest("请完整填写每个报告模块的标题和内容");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ConsumerBehaviorStepFourValidationResult result = new ConsumerBehaviorStepFourValidationResult();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
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.ConsumerBehaviorStepFourValidationRequest;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class ConsumerBehaviorStepFourServiceImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void acceptsFiveCompletedReportModulesForStudent() {
|
||||||
|
ConsumerBehaviorStepFourServiceImpl service = new ConsumerBehaviorStepFourServiceImpl();
|
||||||
|
|
||||||
|
assertTrue(service.validate(validRequest(), student()).isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsIncompleteReportRows() {
|
||||||
|
ConsumerBehaviorStepFourServiceImpl service = new ConsumerBehaviorStepFourServiceImpl();
|
||||||
|
ConsumerBehaviorStepFourValidationRequest request = validRequest();
|
||||||
|
request.getRows().get(2).setContent(" ");
|
||||||
|
|
||||||
|
assertThrows(ServiceException.class, () -> service.validate(request, student()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsNonStudent() {
|
||||||
|
ConsumerBehaviorStepFourServiceImpl service = new ConsumerBehaviorStepFourServiceImpl();
|
||||||
|
|
||||||
|
assertThrows(ServiceException.class, () -> service.validate(validRequest(), teacher()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConsumerBehaviorStepFourValidationRequest validRequest() {
|
||||||
|
ConsumerBehaviorStepFourValidationRequest request = new ConsumerBehaviorStepFourValidationRequest();
|
||||||
|
request.setRows(Arrays.asList(row("调研背景", "说明调研背景与目的"), row("调研方法", "说明样本与渠道"), row("决策因素", "分析购买决策因素"), row("群体差异", "分析不同群体差异"), row("结论建议", "提出产品建议")));
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConsumerBehaviorStepFourValidationRequest.ReportRow row(String title, String content) {
|
||||||
|
ConsumerBehaviorStepFourValidationRequest.ReportRow row = new ConsumerBehaviorStepFourValidationRequest.ReportRow();
|
||||||
|
row.setTitle(title);
|
||||||
|
row.setContent(content);
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JwtUser student() {
|
||||||
|
JwtUser user = new JwtUser();
|
||||||
|
user.setUserId("stu-1");
|
||||||
|
user.setRoleId(4);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JwtUser teacher() {
|
||||||
|
JwtUser user = new JwtUser();
|
||||||
|
user.setUserId("teacher-1");
|
||||||
|
user.setRoleId(2);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue