1. add advertisement add article summary

2. enhance code robustness
main
Revers 9 months ago
parent c006555499
commit a4161efdb2

@ -0,0 +1,33 @@
package com.luoo.music.controller;
import api.PageResult;
import api.Result;
import com.luoo.music.dto.response.cms.ArticleRespDTO;
import com.luoo.music.service.CMSAdvertisementService;
import com.luoo.music.service.CMSArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author Revers.
* @date 2024/02/24 20:53
**/
@RestController
@CrossOrigin
@Api(tags = "雀跃APP广告 APIs")
@RequestMapping("/ad")
public class AdvertisementControoller {
@Autowired
private CMSAdvertisementService adService;
@ApiOperation(value = "广告阅览次数加一", notes = "广告阅览次数加一")
@PostMapping("/{id}")
public Result<PageResult<ArticleRespDTO>> visitAdd(@ApiParam(value = "广告ID", required = true) @PathVariable String id){
return adService.visitAdd(id);
}
}

@ -10,6 +10,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -39,7 +40,10 @@ public class CMSAdvertisementController {
@ApiOperation(value = "添加广告", notes = "添加广告")
@PostMapping("/add")
public Result add(@ApiParam(value = "广告信息", required = true) @Valid @RequestBody AdvertisementReqModel adReqModel){
public Result add(@ApiParam(value = "广告信息", required = true) @Valid @RequestBody AdvertisementReqModel adReqModel, BindingResult bindingResult){
if (bindingResult.hasErrors())
return Result.failed(bindingResult.getFieldError().getDefaultMessage());
return adService.add(adReqModel);
}
@ -52,7 +56,9 @@ public class CMSAdvertisementController {
@ApiOperation(value = "更新广告", notes = "更新广告")
@PutMapping( "/{id}")
public Result update(@ApiParam(value = "广告ID", required = true) @PathVariable String id,
@ApiParam(value = "广告信息", required = true) @RequestBody AdvertisementReqModel updateModel){
@ApiParam(value = "广告信息", required = true) @RequestBody AdvertisementReqModel updateModel,BindingResult bindingResult){
if (bindingResult.hasErrors())
return Result.failed(bindingResult.getFieldError().getDefaultMessage());
return adService.update(id, updateModel);
}

@ -10,6 +10,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -39,7 +40,9 @@ public class CMSArticleController {
@ApiOperation(value = "添加文章", notes = "添加文章")
@PostMapping("/add")
public Result add(@ApiParam(value = "文章信息", required = true) @Valid @RequestBody ArticleAddModel articleAddModel){
public Result add(@ApiParam(value = "文章信息", required = true) @Valid @RequestBody ArticleAddModel articleAddModel, BindingResult bindingResult){
if (bindingResult.hasErrors())
return Result.failed(bindingResult.getFieldError().getDefaultMessage());
return articleService.add(articleAddModel);
}
@ -52,7 +55,9 @@ public class CMSArticleController {
@ApiOperation(value = "更新文章", notes = "更新文章")
@PutMapping( "/{id}")
public Result update(@ApiParam(value = "文章ID", required = true) @PathVariable String id,
@ApiParam(value = "文章信息", required = true) @RequestBody ArticleAddModel updateModel){
@ApiParam(value = "文章信息", required = true) @RequestBody ArticleAddModel updateModel, BindingResult bindingResult){
if (bindingResult.hasErrors())
return Result.failed(bindingResult.getFieldError().getDefaultMessage());
return articleService.update(id, updateModel);
}

@ -3,8 +3,10 @@ package com.luoo.music.dto.request.cms;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
/**
* @author Revers.
@ -15,34 +17,42 @@ import javax.validation.constraints.NotBlank;
@ApiModel(value = "增加或修改广告对象")
public class AdvertisementReqModel {
@Length(min = 1, max = 100, message = "广告名称长度在1-100之间")
@NotBlank(message = "广告名称必填")
@ApiModelProperty(value = "广告名称")
private String name;
@Length(min = 1, max = 255, message = "广告位置长度在1-255之间")
@NotBlank(message = "广告位置必填")
@ApiModelProperty(value = "广告位置")
private String location;
@Length(min = 1, max = 100, message = "广告图片路径长度在1-100之间")
@NotBlank(message = "广告图片必填")
@ApiModelProperty(value = "广告图片路径")
private String image;
@Pattern(regexp="(^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})", message="开始时间格式异常")
@NotBlank(message = "开始时间必填")
@ApiModelProperty(value = "开始时间yyyy-MM-dd HH:mm:ss")
private String startTime;
@Pattern(regexp="(^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})", message="到期时间格式异常")
@NotBlank(message = "到期时间必填")
@ApiModelProperty(value = "到期时间时间yyyy-MM-dd HH:mm:ss")
private String endTime;
@Length(min = 1, max = 20, message = "跳转类型长度在1-20之间")
@NotBlank(message = "跳转类型必填")
@ApiModelProperty(value = "跳转类型")
private String type;
@Length(min = 1, max = 100, message = "跳转地址长度在1-100之间")
@NotBlank(message = "跳转地址必填")
@ApiModelProperty(value = "跳转地址")
private String url;
@Pattern(regexp="(^[0-1])", message="是否显示广告异常")
@NotBlank(message = "广告显示必填")
@ApiModelProperty(value = "是否显示广告0:否1:是")
private String isDisplay;
@ -50,6 +60,7 @@ public class AdvertisementReqModel {
@ApiModelProperty(value = "备注")
private String content;
@Pattern(regexp="(^[0-9]{0,20})", message="是否自动推送异常")
@ApiModelProperty(value = "发布人ID")
private String userId;

@ -3,8 +3,10 @@ package com.luoo.music.dto.request.cms;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
/**
* @author Revers.
@ -15,25 +17,31 @@ import javax.validation.constraints.NotBlank;
@ApiModel(value = "增加或修改文章对象")
public class ArticleAddModel {
@Length(min = 1, max = 100, message = "文章标题长度在1-100之间")
@NotBlank(message = "文章标题必填")
@ApiModelProperty(value = "文章标题")
private String title;
@Length(min = 1, max = 255, message = "文章标题长度在1-255之间")
@NotBlank(message = "文章类型必填")
@ApiModelProperty(value = "文章类型")
private String type;
@Length(min = 1, max = 100, message = "文章封面路径长度在1-100之间")
@NotBlank(message = "文章封面必填")
@ApiModelProperty(value = "文章封面路径")
private String image;
@Pattern(regexp="(^[0-1])", message="是否定时发布异常")
@NotBlank(message = "发布方式必填")
@ApiModelProperty(value = "是否定时发布0:否1:是")
private String isScheduled;
@Pattern(regexp="(^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})", message="定时发布时间格式异常")
@ApiModelProperty(value = "定时发布时间yyyy-MM-dd HH:mm:ss")
private String pubTime;
@Pattern(regexp="(^[0-9]{0,20})", message="是否自动推送异常")
@ApiModelProperty(value = "发布人ID")
private String userId;
@ -41,10 +49,12 @@ public class ArticleAddModel {
@ApiModelProperty(value = "文章详情")
private String content;
@Pattern(regexp="(^[0-1])", message="是否允许评论异常")
@NotBlank(message = "评论设置必填")
@ApiModelProperty(value = "是否允许评论0:否1:是")
private String allowCommit;
@Pattern(regexp="(^[0-1])", message="是否自动推送异常")
@NotBlank(message = "自动推送必填")
@ApiModelProperty(value = "是否自动推送0:否1:是")
private String autoPush;

@ -69,6 +69,10 @@ public class Advertisement implements Serializable {
* 广
*/
private String content;
/**
* 广
*/
private String summary;
/**
*

@ -66,6 +66,7 @@ public class CMSAdvertisementService {
ad.setUrl(paramAdd.getUrl());
ad.setDisplay(paramAdd.getIsDisplay());
ad.setContent(paramAdd.getContent());
ad.setSummary(contentToSummary(paramAdd.getContent()));
ad.setVisits(0l);
ad.setIsDeleted("0");
@ -147,8 +148,10 @@ public class CMSAdvertisementService {
if(StringUtils.isNotBlank(param.getIsDisplay()))
ad.setDisplay(param.getIsDisplay());
if(StringUtils.isNotBlank(param.getContent()))
if(StringUtils.isNotBlank(param.getContent())) {
ad.setContent(param.getContent());
ad.setSummary(contentToSummary(param.getContent()));
}
return Result.success();
}
@ -200,4 +203,27 @@ public class CMSAdvertisementService {
};
}
private String contentToSummary(String content){
String originalString = content;
String[] parts = originalString.split("<.*?>");
StringBuilder cleanedStringBuilder = new StringBuilder();
for (String part : parts) {
cleanedStringBuilder.append(part);
}
String cleanedString = cleanedStringBuilder.toString();
return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString;
}
public Result visitAdd(String id){
Advertisement advertisement = adDao.findById(id).get();
if (!Objects.isNull(advertisement)) {
advertisement.setVisits(advertisement.getVisits()+1);
adDao.save(advertisement);
return Result.success();
}
return Result.failed("文章不存在");
}
}

@ -57,7 +57,7 @@ public class CMSArticleService {
article.setTitle(paramAdd.getTitle());
article.setType(paramAdd.getType());
article.setImage(image);
//article.setSummary();
article.setSummary(contentToSummary(paramAdd.getContent()));
article.setContent(paramAdd.getContent());
article.setVisits(0l);
article.setComment(0l);
@ -167,7 +167,7 @@ public class CMSArticleService {
article.setType(param.getType());
if (StringUtils.isNotBlank(param.getContent())) {
//article.setSummary();
article.setSummary(contentToSummary(param.getContent()));
article.setContent(param.getContent());
}
@ -228,4 +228,17 @@ public class CMSArticleService {
return builder.and(predicateList.toArray(new Predicate[predicateList.size()]));
};
}
private String contentToSummary(String content){
String originalString = content;
String[] parts = originalString.split("<.*?>");
StringBuilder cleanedStringBuilder = new StringBuilder();
for (String part : parts) {
cleanedStringBuilder.append(part);
}
String cleanedString = cleanedStringBuilder.toString();
return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString;
}
}

Loading…
Cancel
Save