feat: validate value positioning statements
parent
0aac26f13b
commit
5b7602bca3
@ -0,0 +1,32 @@
|
|||||||
|
package com.sztzjy.linkCommerce.entity.dto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ProductValuePositioningStepThreeValidationRequest {
|
||||||
|
private List<PositioningStatement> statements;
|
||||||
|
|
||||||
|
public List<PositioningStatement> getStatements() { return statements; }
|
||||||
|
public void setStatements(List<PositioningStatement> statements) { this.statements = statements; }
|
||||||
|
|
||||||
|
public static class PositioningStatement {
|
||||||
|
private Integer id;
|
||||||
|
private String targetUser;
|
||||||
|
private String productName;
|
||||||
|
private String productCategory;
|
||||||
|
private String coreValue;
|
||||||
|
private String differentiationReason;
|
||||||
|
|
||||||
|
public Integer getId() { return id; }
|
||||||
|
public void setId(Integer id) { this.id = id; }
|
||||||
|
public String getTargetUser() { return targetUser; }
|
||||||
|
public void setTargetUser(String targetUser) { this.targetUser = targetUser; }
|
||||||
|
public String getProductName() { return productName; }
|
||||||
|
public void setProductName(String productName) { this.productName = productName; }
|
||||||
|
public String getProductCategory() { return productCategory; }
|
||||||
|
public void setProductCategory(String productCategory) { this.productCategory = productCategory; }
|
||||||
|
public String getCoreValue() { return coreValue; }
|
||||||
|
public void setCoreValue(String coreValue) { this.coreValue = coreValue; }
|
||||||
|
public String getDifferentiationReason() { return differentiationReason; }
|
||||||
|
public void setDifferentiationReason(String differentiationReason) { this.differentiationReason = differentiationReason; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package com.sztzjy.linkCommerce.entity.dto;
|
||||||
|
|
||||||
|
public class ProductValuePositioningStepThreeValidationResult {
|
||||||
|
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.ProductValuePositioningStepThreeValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ProductValuePositioningStepThreeValidationResult;
|
||||||
|
|
||||||
|
public interface ProductValuePositioningStepThreeService {
|
||||||
|
ProductValuePositioningStepThreeValidationResult validate(ProductValuePositioningStepThreeValidationRequest request, JwtUser user);
|
||||||
|
}
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
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.ProductValuePositioningStepThreeValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ProductValuePositioningStepThreeValidationResult;
|
||||||
|
import com.sztzjy.linkCommerce.service.ProductValuePositioningStepThreeService;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ProductValuePositioningStepThreeServiceImpl implements ProductValuePositioningStepThreeService {
|
||||||
|
@Override
|
||||||
|
public ProductValuePositioningStepThreeValidationResult validate(ProductValuePositioningStepThreeValidationRequest request, JwtUser user) {
|
||||||
|
requireStudent(user);
|
||||||
|
List<ProductValuePositioningStepThreeValidationRequest.PositioningStatement> statements = request == null ? null : request.getStatements();
|
||||||
|
if (statements == null || statements.size() != 2) throw badRequest("请填写两个产品价值定位陈述版本");
|
||||||
|
for (int index = 0; index < statements.size(); index++) {
|
||||||
|
ProductValuePositioningStepThreeValidationRequest.PositioningStatement statement = statements.get(index);
|
||||||
|
if (statement == null || statement.getId() == null || statement.getId() != index + 1
|
||||||
|
|| StringUtils.isBlank(statement.getTargetUser()) || StringUtils.isBlank(statement.getProductName())
|
||||||
|
|| StringUtils.isBlank(statement.getProductCategory()) || StringUtils.isBlank(statement.getCoreValue())
|
||||||
|
|| StringUtils.isBlank(statement.getDifferentiationReason())) {
|
||||||
|
throw badRequest("请完整填写两个产品价值定位陈述版本");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ProductValuePositioningStepThreeValidationResult result = new ProductValuePositioningStepThreeValidationResult();
|
||||||
|
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,54 @@
|
|||||||
|
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.ProductValuePositioningStepThreeValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ProductValuePositioningStepThreeValidationResult;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class ProductValuePositioningStepThreeServiceImplTest {
|
||||||
|
private final ProductValuePositioningStepThreeServiceImpl service = new ProductValuePositioningStepThreeServiceImpl();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void acceptsTwoCompleteStatements() {
|
||||||
|
ProductValuePositioningStepThreeValidationResult result = service.validate(request(), student());
|
||||||
|
assertTrue(result.isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsAnIncompleteStatement() {
|
||||||
|
ProductValuePositioningStepThreeValidationRequest request = request();
|
||||||
|
request.getStatements().get(1).setCoreValue(" ");
|
||||||
|
assertThrows(ServiceException.class, () -> service.validate(request, student()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProductValuePositioningStepThreeValidationRequest request() {
|
||||||
|
List<ProductValuePositioningStepThreeValidationRequest.PositioningStatement> statements = new ArrayList<>();
|
||||||
|
for (int index = 1; index <= 2; index++) {
|
||||||
|
ProductValuePositioningStepThreeValidationRequest.PositioningStatement statement = new ProductValuePositioningStepThreeValidationRequest.PositioningStatement();
|
||||||
|
statement.setId(index);
|
||||||
|
statement.setTargetUser("目标用户" + index);
|
||||||
|
statement.setProductName("产品名称" + index);
|
||||||
|
statement.setProductCategory("产品类别" + index);
|
||||||
|
statement.setCoreValue("核心价值" + index);
|
||||||
|
statement.setDifferentiationReason("差异化理由" + index);
|
||||||
|
statements.add(statement);
|
||||||
|
}
|
||||||
|
ProductValuePositioningStepThreeValidationRequest request = new ProductValuePositioningStepThreeValidationRequest();
|
||||||
|
request.setStatements(statements);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JwtUser student() {
|
||||||
|
JwtUser user = new JwtUser();
|
||||||
|
user.setUserId("student-id");
|
||||||
|
user.setRoleId(4);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue