fix: 添加 validation 依赖,修复参数校验不生效的 bug

11111
winter 1 year ago
parent a2d75a1c3d
commit 18cfb50a46

@ -54,6 +54,11 @@
<artifactId>spring-cloud-azure-starter-data-cosmos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>

@ -35,7 +35,7 @@ public class R<T> implements Serializable {
}
public static <T> R<T> error(String msg) {
return new R<>(ErrorCode.SYSTEM_ERROR);
return new R<>(ErrorCode.SYSTEM_ERROR.getCode(), null, msg);
}
public static <T> R<T> error(Integer code, String msg) {

@ -1,11 +1,7 @@
package cn.teammodel.controller;
import cn.teammodel.common.R;
import cn.teammodel.model.dto.Appraise.AppraiseVoteDto;
import cn.teammodel.model.dto.Appraise.DeleteNodeDto;
import cn.teammodel.model.dto.Appraise.GetEvaluateTreeDto;
import cn.teammodel.model.dto.Appraise.InsertNodeDto;
import cn.teammodel.model.dto.Appraise.UpdateNodeDto;
import cn.teammodel.model.dto.Appraise.*;
import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.service.EvaluationService;
import io.swagger.annotations.ApiOperation;
@ -55,10 +51,17 @@ public class AppraiseController {
@PostMapping("vote")
@ApiOperation(value = "给某个学生评价(投票)")
public R<String> vote(@Valid @RequestBody AppraiseVoteDto appraiseVoteDto) {
public R<String> vote(@RequestBody @Valid AppraiseVoteDto appraiseVoteDto) {
evaluationService.vote(appraiseVoteDto);
return R.success("评价成功");
}
@PostMapping("findVoteRecord")
@ApiOperation(value = "多条件查询学生评价(投票)")
public R<String> findVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto) {
evaluationService.findVoteRecord(findVoteRecordDto);
return R.success("评价成功");
}
}

@ -2,8 +2,12 @@ package cn.teammodel.dao;
import cn.teammodel.model.entity.appraise.AppraiseRecord;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author winter
* @create 2023-11-20 14:39 <br/>
@ -21,4 +25,6 @@ public interface AppraiseRecordRepository extends CosmosRepository<AppraiseRecor
*
*/
AppraiseRecord findAppraiseRecordByTargetIdAndAcademicYearIdAndCode(String targetId, String academicYearId, String code);
@Query("select c.id, c.praiseCount, c.score from Student as c where c.targetId = @targetId and c.academicYearId = @academicYearId and c.code = @code")
List<AppraiseRecord> findScoreAndPraise(@Param("targetId") String targetId,@Param("academicYearId") String academicYearId,@Param("code") String code);
}

@ -3,6 +3,7 @@ package cn.teammodel.model.dto.Appraise;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
@ -12,9 +13,13 @@ import javax.validation.constraints.NotNull;
@Data
public class AppraiseVoteDto {
@NotNull
@ApiModelProperty(value = "评价对象为班级 或 学生")
@ApiModelProperty(value = "评价对象 Id")
private String targetId;
@ApiModelProperty(value = "评价对象所处班级 Id")
@NotBlank
private String classId;
@NotNull
@ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class")
private String targetType;

@ -0,0 +1,33 @@
package cn.teammodel.model.dto.Appraise;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* @author winter
* @create 2023-11-28 16:16
*/
@Data
public class FindVoteRecordDto {
@NotNull
@ApiModelProperty(value = "班级或学生 id")
private String targetId;
@NotNull
@ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class")
private String targetType;
// 班级 id
/**
* id
*/
@NotNull
@ApiModelProperty(value = "评价项唯一 id", required = true)
private String appraiseId;
@ApiModelProperty(value = "学段 id,用于拿到 semesterId", required = true)
@NotNull
private String periodId;
}

@ -24,6 +24,10 @@ public class AppraiseRecord extends BaseItem {
*/
private String periodId;
/**
* id
*/
private String classId;
/**
* ( id: -semesterId -> 2023-{semesterId})
*/
private String academicYearId;

@ -16,7 +16,7 @@ import java.time.LocalDateTime;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AppraiseRecordItem {
private String id;
private String appraiseNodeId;
private AppraiseTreeNode appraiseNode;
/**
* , null
*/

@ -47,4 +47,6 @@ public interface EvaluationService {
*
*/
void vote(AppraiseVoteDto appraiseVoteDto);
void findVoteRecord(FindVoteRecordDto findVoteRecordDto);
}

@ -181,6 +181,9 @@ public class EvaluationServiceImpl implements EvaluationService {
this.collectNodesToDelete(node.getId(), nodes, nodesToDelete);
}
}
if (ObjectUtils.isEmpty(nodesToDelete)) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "删除节点不存在");
}
nodes.removeAll(nodesToDelete);
return buildTree(appraiseRepository.save(appraise));
@ -189,6 +192,7 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override
public void vote(AppraiseVoteDto appraiseVoteDto) {
String targetId = appraiseVoteDto.getTargetId();
String classId = appraiseVoteDto.getClassId();
boolean spread = appraiseVoteDto.isSpread();
String targetType = appraiseVoteDto.getTargetType();
String appraiseId = appraiseVoteDto.getAppraiseId();
@ -197,6 +201,7 @@ public class EvaluationServiceImpl implements EvaluationService {
User loginUser = SecurityUtil.getLoginUser();
String schoolId = loginUser.getSchoolId();
// 判断评价对象是否合法
if (!targetType.equals(TARGET_STUDENT) && !targetType.equals(TARGET_CLASS)) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "不受支持的评价对象");
}
@ -223,7 +228,7 @@ public class EvaluationServiceImpl implements EvaluationService {
// 初始化新的评价节点
AppraiseRecordItem item = new AppraiseRecordItem();
item.setId(UUID.randomUUID().toString());
item.setAppraiseNodeId(appraiseId);
item.setAppraiseNode(appraiseTreeNode);
item.setCreator(loginUser.getName());
item.setCreatorId(loginUser.getId());
item.setCreateTime(LocalDateTime.now());
@ -239,6 +244,7 @@ public class EvaluationServiceImpl implements EvaluationService {
record.setTargetType(TARGET_CLASS);
}
record.setTargetId(targetId);
record.setClassId(classId);
record.setAcademicYearId(academicYearId);
record.setPraiseCount(appraiseTreeNode.isPraise() ? 1 : -1);
record.setScore(ObjectUtils.isEmpty(appraiseTreeNode.getScore()) ? 0 : appraiseTreeNode.getScore());
@ -263,6 +269,11 @@ public class EvaluationServiceImpl implements EvaluationService {
}
}
@Override
public void findVoteRecord(FindVoteRecordDto findVoteRecordDto) {
}
/**
* id id ()
*/

@ -2,6 +2,7 @@ package cn.teammodel.utils;
import cn.teammodel.common.ErrorCode;
import cn.teammodel.config.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import java.util.List;
@ -10,12 +11,14 @@ import java.util.List;
* @author winter
* @create 2023-11-29 15:41
*/
@Slf4j
public class RepositoryUtil {
/**
* ,listlist1,
*/
public static <T> T findOne(List<T> list) {
if (ObjectUtils.isEmpty(list) || list.size() > 1) {
log.error("查询结果为空或数量 > 1");
throw new ServiceException(ErrorCode.PARAMS_ERROR);
}
return list.get(0);

@ -11,6 +11,7 @@ import cn.teammodel.model.entity.appraise.AppraiseRecord;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import cn.teammodel.service.EvaluationService;
import cn.teammodel.service.impl.EvaluationServiceImpl;
import cn.teammodel.utils.RepositoryUtil;
import com.azure.cosmos.models.PartitionKey;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
import org.junit.jupiter.api.Test;
@ -89,10 +90,11 @@ class TeamModelExtensionApplicationTests {
//System.out.println(nodeById);
//System.out.println(schoolRepository.findSchoolByIdAndCode("hbcn", "Base", Semester.class));
AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByStudentIdAndAcademicYearIdAndCode("fakeStudentId1",
List<AppraiseRecord> record = appraiseRecordRepository.findScoreAndPraise("fakeStudentId1",
"2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017",
String.format(PK.PK_APPRAISE_RECORD,"habook")
);
System.out.println(RepositoryUtil.findOne(record));
}
@Test
public void testUpdate() {

Loading…
Cancel
Save