Commit 73a24985 authored by tangtuo's avatar tangtuo

修改保存nft的接口

parent 6d09c43b
......@@ -5,7 +5,10 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
/**
......@@ -18,24 +21,32 @@ import java.util.Date;
@TableName("tb_nft")
public class Nft extends BaseEntity {
@NotNull(message = "类目id不能为空")
@ApiModelProperty("类目id")
private Integer categoryId;
@ApiModelProperty("用户id")
private Integer userId;
@NotBlank(message = "名称不能为空")
@Length(max = 20, message = "名称最大长度为20")
@ApiModelProperty("名称")
private String name;
@ApiModelProperty("封面")
private String cover;
@NotBlank(message = "作者不能为空")
@Length(max = 20, message = "作者最大长度为20")
@ApiModelProperty("作者")
private String author;
@NotBlank(message = "主题不能为空")
@ApiModelProperty("主题,多个用逗号,隔开")
private String theme;
@NotBlank(message = "简介不能为空")
@Length(max = 500, message = "简介最大长度为500")
@ApiModelProperty("简介")
private String synopsis;
......@@ -45,9 +56,11 @@ public class Nft extends BaseEntity {
@ApiModelProperty("文件地址--用户选择不存档的情况下为空")
private String fileUrl;
@NotBlank(message = "作品哈希不能为空")
@ApiModelProperty("文件hash")
private String fileHash;
@NotNull(message = "平台存档不能为空")
@ApiModelProperty("平台存档 0-不存档 1-加密存档")
private Integer isArchives;
......
......@@ -5,6 +5,7 @@ import com.fzm.common.exception.GlobalException;
import com.fzm.common.model.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
......@@ -24,11 +25,6 @@ import java.util.stream.Collectors;
@RestControllerAdvice
public class GlobalExceptionHandler {
@Resource
private HttpServletResponse response;
@Resource
private HttpServletRequest request;
@ExceptionHandler(value = GlobalException.class)
public ResponseModel<String> handlerGlobalException(GlobalException globalException) {
......@@ -53,4 +49,15 @@ public class GlobalExceptionHandler {
collect(Collectors.joining("、"));
return ResponseModel.fail(ResultCode.VALIDATE_FAILED, errorMsg);
}
@ExceptionHandler(value = {BindException.class})
public ResponseModel<String> handleBindException(BindException e) {
log.error(e.getMessage(), e);
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
String errorMsg = allErrors.
stream().
map(DefaultMessageSourceResolvable::getDefaultMessage).
collect(Collectors.joining("、"));
return ResponseModel.fail(ResultCode.VALIDATE_FAILED, errorMsg);
}
}
......@@ -78,8 +78,10 @@ public class NftServiceImpl extends ServiceImpl<NftMapper, Nft> implements NftSe
@Override
public NftDto saveNft(Nft nft) {
User user = userService.getById(JwtUtil.getUserIdFromToken(request.getHeader("Authorization")));
Integer userId = JwtUtil.getUserIdFromToken(request.getHeader("Authorization"));
nft.setUserId(userId);
save(nft);
User user = userService.getById(userId);
NftDto nftDto = new NftDto();
// 获取用户的钱包地址
String wallet = user.getWallet();
......
......@@ -70,48 +70,12 @@ public class NftController {
@Authentication
@PostMapping("/save")
@ApiOperation(value = "nft基本信息保存(基本信息和加密上链两个步骤)")
public ResponseModel<NftDto> save(@ApiParam(value = "类目id", required = true) Integer categoryId,
@ApiParam(value = "名称", required = true) String name,
@ApiParam(value = "作者", required = true) String author,
@ApiParam(value = "主题", required = true) String theme,
@ApiParam(value = "简介", required = true) String synopsis,
@ApiParam(value = "文件") MultipartFile file,
@ApiParam(value = "文件hash", required = true) String fileHash,
@ApiParam(value = "平台存档 0-不存档 1-加密存档", required = true) Integer isArchives,
@ApiParam(value = "授权阅读 0-不需要授权 1-需要授权") Integer isGrant) throws IOException {
if (StringUtils.isAnyBlank(name, author, theme, synopsis, fileHash) || categoryId == null || isArchives == null) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "缺失必填参数");
public ResponseModel<NftDto> save(@Validated @ModelAttribute Nft nft) {
if (SystemConstant.BOOLEAN_DATA_TRUE.equals(nft.getIsArchives())) {
if (StringUtils.isAnyBlank(nft.getFileUrl(), nft.getName()) || nft.getIsGrant() == null) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "当选择平台存档时,文件名和文件路径必传");
}
if (name.length() > 20) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "作品名称长度不能大于20");
}
if (author.length() > 20) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "作者长度不能大于20");
}
if (synopsis.length() > 500) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "简介长度不能大于500");
}
Nft nft = new Nft();
// 当平台存档选择加密存档时,文件必传
if (SystemConstant.BOOLEAN_DATA_TRUE.equals(isArchives)) {
if (file == null || isGrant == null) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "当选择加密存档的时候,文件和授权阅读不能为空");
}
// 判断文件的hash是否被篡改过
if (!SecureUtil.md5(file.getInputStream()).equals(fileHash)) {
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "文件hash和文件内容不匹配");
}
String fileUrl = ossUtil.putEncryptObject(file);
nft.setFileUrl(fileUrl).setIsGrant(isGrant).setFileName(file.getOriginalFilename());
}
nft.setCategoryId(categoryId)
.setUserId(JwtUtil.getUserIdFromToken(request.getHeader("Authorization")))
.setName(name)
.setAuthor(author)
.setTheme(theme)
.setSynopsis(synopsis)
.setFileHash(fileHash)
.setIsArchives(isArchives);
return ResponseModel.success(nftService.saveNft(nft));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment