From a4161efdb24cfa1cdf1a3380fdaaa2be7156551f Mon Sep 17 00:00:00 2001 From: Revers <0x00stone@gmail.com> Date: Sat, 24 Feb 2024 21:27:28 +0800 Subject: [PATCH] 1. add advertisement add article summary 2. enhance code robustness --- .../controller/AdvertisementControoller.java | 33 +++++++++++++++++++ .../CMSAdvertisementController.java | 10 ++++-- .../controller/CMSArticleController.java | 9 +++-- .../request/cms/AdvertisementReqModel.java | 11 +++++++ .../dto/request/cms/ArticleAddModel.java | 10 ++++++ .../com/luoo/music/pojo/Advertisement.java | 4 +++ .../service/CMSAdvertisementService.java | 28 +++++++++++++++- .../luoo/music/service/CMSArticleService.java | 17 ++++++++-- 8 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/controller/AdvertisementControoller.java diff --git a/luoo_music/src/main/java/com/luoo/music/controller/AdvertisementControoller.java b/luoo_music/src/main/java/com/luoo/music/controller/AdvertisementControoller.java new file mode 100644 index 0000000..57dcbe6 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/controller/AdvertisementControoller.java @@ -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> visitAdd(@ApiParam(value = "广告ID", required = true) @PathVariable String id){ + return adService.visitAdd(id); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/controller/CMSAdvertisementController.java b/luoo_music/src/main/java/com/luoo/music/controller/CMSAdvertisementController.java index 6e2ae41..470f919 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/CMSAdvertisementController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/CMSAdvertisementController.java @@ -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); } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/CMSArticleController.java b/luoo_music/src/main/java/com/luoo/music/controller/CMSArticleController.java index e79249f..d3266f9 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/CMSArticleController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/CMSArticleController.java @@ -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); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/cms/AdvertisementReqModel.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/AdvertisementReqModel.java index eb1eb75..ecd68b8 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/cms/AdvertisementReqModel.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/AdvertisementReqModel.java @@ -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; diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/cms/ArticleAddModel.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/ArticleAddModel.java index 7ae4e9e..8eed24f 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/cms/ArticleAddModel.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/ArticleAddModel.java @@ -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; diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/Advertisement.java b/luoo_music/src/main/java/com/luoo/music/pojo/Advertisement.java index e88f3a5..8d668cd 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/Advertisement.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/Advertisement.java @@ -69,6 +69,10 @@ public class Advertisement implements Serializable { * 广告备注 */ private String content; + /** + * 广告备注 + */ + private String summary; /** * 浏览量 diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java index f0efcaf..47f261d 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java @@ -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("文章不存在"); + } + } diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java index dd42fcb..3b23933 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java @@ -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; + } }