fix article and advertisement Update()

main
Revers 9 months ago
parent 59a15cca4f
commit 271fe44881

@ -56,7 +56,7 @@ public class CMSAdvertisementController {
@ApiOperation(value = "更新广告", notes = "更新广告") @ApiOperation(value = "更新广告", notes = "更新广告")
@PutMapping( "/{id}") @PutMapping( "/{id}")
public Result update(@ApiParam(value = "广告ID", required = true) @PathVariable String id, public Result update(@ApiParam(value = "广告ID", required = true) @PathVariable String id,
@ApiParam(value = "广告信息", required = true) @RequestBody AdvertisementReqModel updateModel,BindingResult bindingResult){ @ApiParam(value = "广告信息", required = true) @Valid @RequestBody AdvertisementReqModel updateModel,BindingResult bindingResult){
if (bindingResult.hasErrors()) if (bindingResult.hasErrors())
return Result.failed(bindingResult.getFieldError().getDefaultMessage()); return Result.failed(bindingResult.getFieldError().getDefaultMessage());
return adService.update(id, updateModel); return adService.update(id, updateModel);
@ -75,4 +75,10 @@ public class CMSAdvertisementController {
@ApiParam(value = "每页条数", required = true) @PathVariable int size){ @ApiParam(value = "每页条数", required = true) @PathVariable int size){
return adService.search(page, size); return adService.search(page, size);
} }
@ApiOperation(value = "查询前5条首页banner", notes = "查询前5条首页banner")
@GetMapping ("/banner")
public Result<PageResult<AdvertisementRespDTO>> getBanner(){
return adService.search(1,5,"首页banner");
}
} }

@ -55,7 +55,7 @@ public class CMSArticleController {
@ApiOperation(value = "更新文章", notes = "更新文章") @ApiOperation(value = "更新文章", notes = "更新文章")
@PutMapping( "/{id}") @PutMapping( "/{id}")
public Result update(@ApiParam(value = "文章ID", required = true) @PathVariable String id, public Result update(@ApiParam(value = "文章ID", required = true) @PathVariable String id,
@ApiParam(value = "文章信息", required = true) @RequestBody ArticleAddModel updateModel, BindingResult bindingResult){ @ApiParam(value = "文章信息", required = true) @Valid @RequestBody ArticleAddModel updateModel, BindingResult bindingResult){
if (bindingResult.hasErrors()) if (bindingResult.hasErrors())
return Result.failed(bindingResult.getFieldError().getDefaultMessage()); return Result.failed(bindingResult.getFieldError().getDefaultMessage());
return articleService.update(id, updateModel); return articleService.update(id, updateModel);

@ -34,9 +34,16 @@ public class AdvertisementRespDTO {
private String userId; private String userId;
@ApiModelProperty(value = "发布作者昵称") @ApiModelProperty(value = "发布作者昵称")
private String userName; private String userName;
@ApiModelProperty(value = "编辑日期,格式为: yyyy.MM.dd") @ApiModelProperty(value = "编辑日期,格式为: yyyy-MM-dd HH:mm:ss")
private String date; private String date;
@ApiModelProperty(value = "开始日期,格式为: yyyy-MM-dd HH:mm:ss")
private String startTime;
@ApiModelProperty(value = "结束日期,格式为: yyyy-MM-dd HH:mm:ss")
private String endTime;
public static AdvertisementRespDTO convertPojo(Advertisement ad) { public static AdvertisementRespDTO convertPojo(Advertisement ad) {
AdvertisementRespDTO response = new AdvertisementRespDTO(); AdvertisementRespDTO response = new AdvertisementRespDTO();
@ -53,7 +60,9 @@ public class AdvertisementRespDTO {
response.setContent(ad.getContent()); response.setContent(ad.getContent());
response.setUserId(ad.getUserId()); response.setUserId(ad.getUserId());
response.setUserName(ad.getUserName()); response.setUserName(ad.getUserName());
response.setDate(ad.getCreateTime().toString()); response.setDate(ad.getCreateTime().format(Constants.formatter));
response.setStartTime(ad.getStartTime().format(Constants.formatter));
response.setEndTime(ad.getEndTime().format(Constants.formatter));
return response; return response;
} }
} }

@ -32,13 +32,13 @@ public class ArticleRespDTO implements Serializable {
private String userId; private String userId;
@ApiModelProperty(value = "发布作者昵称") @ApiModelProperty(value = "发布作者昵称")
private String userName; private String userName;
@ApiModelProperty(value = "编辑日期,格式为: yyyy.MM.dd") @ApiModelProperty(value = "编辑日期,格式为: yyyy-MM-dd HH:mm:ss")
private String date; private String date;
@ApiModelProperty(value = "是否定时发布 否0 是1") @ApiModelProperty(value = "是否定时发布 否0 是1")
private String isScheduled; private String isScheduled;
@ApiModelProperty(value = "发布时间,格式为: yyyy.MM.dd") @ApiModelProperty(value = "发布时间,格式为: yyyy-MM-dd HH:mm:ss")
private String pubTime; private String pubTime;
@ApiModelProperty(value = "是否允许评论 否0 是1") @ApiModelProperty(value = "是否允许评论 否0 是1")
@ -67,7 +67,7 @@ public class ArticleRespDTO implements Serializable {
response.setContent(article.getContent()); response.setContent(article.getContent());
response.setUserId(article.getUserId()); response.setUserId(article.getUserId());
response.setUserName(article.getUserName()); response.setUserName(article.getUserName());
response.setDate(article.getPubTime().toString()); response.setDate(article.getUpdateTime().format(Constants.formatter));
response.setCommitsNum(article.getComment()); response.setCommitsNum(article.getComment());
response.setVistisNum(article.getVisits()); response.setVistisNum(article.getVisits());
response.setIsScheduled(article.getIsScheduled()); response.setIsScheduled(article.getIsScheduled());

@ -140,32 +140,17 @@ public class CMSAdvertisementService {
} }
} }
if (StringUtils.isNotBlank(param.getName()))
ad.setName(param.getName()); ad.setName(param.getName());
if (StringUtils.isNotBlank(param.getLocation()))
ad.setLocation(param.getLocation()); ad.setLocation(param.getLocation());
if (StringUtils.isNotBlank(param.getStartTime()))
ad.setStartTime(LocalDateTime.parse(param.getStartTime(), Constants.formatter)); ad.setStartTime(LocalDateTime.parse(param.getStartTime(), Constants.formatter));
if (StringUtils.isNotBlank(param.getEndTime()))
ad.setEndTime(LocalDateTime.parse(param.getEndTime(), Constants.formatter)); ad.setEndTime(LocalDateTime.parse(param.getEndTime(), Constants.formatter));
if(StringUtils.isNotBlank(param.getType()))
ad.setType(param.getType()); ad.setType(param.getType());
if(StringUtils.isNotBlank(param.getUrl()))
ad.setUrl(param.getUrl()); ad.setUrl(param.getUrl());
if(StringUtils.isNotBlank(param.getIsDisplay()))
ad.setDisplay(param.getIsDisplay()); ad.setDisplay(param.getIsDisplay());
if(StringUtils.isNotBlank(param.getContent())) {
ad.setContent(param.getContent()); ad.setContent(param.getContent());
ad.setSummary(contentToSummary(param.getContent())); ad.setSummary(contentToSummary(param.getContent()));
}
adDao.save(ad);
return Result.success(); return Result.success();
} }
@ -186,9 +171,13 @@ public class CMSAdvertisementService {
} }
public Result<PageResult<AdvertisementRespDTO>> search(int page, int size) { public Result<PageResult<AdvertisementRespDTO>> search(int page, int size) {
return search(page,size,null);
}
public Result<PageResult<AdvertisementRespDTO>> search(int page, int size,String location) {
List<AdvertisementRespDTO> result = new ArrayList<>(); List<AdvertisementRespDTO> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(page - 1, size); PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Advertisement> AdSpecification = buildSearchSpecification(); Specification<Advertisement> AdSpecification = buildSearchSpecification(location);
Page<Advertisement> AdPage = adDao.findAll(AdSpecification,pageRequest); Page<Advertisement> AdPage = adDao.findAll(AdSpecification,pageRequest);
long totalElements = AdPage.getTotalElements(); long totalElements = AdPage.getTotalElements();
@ -201,12 +190,19 @@ public class CMSAdvertisementService {
} }
return Result.success(new PageResult<>(totalElements, result)); return Result.success(new PageResult<>(totalElements, result));
} }
private Specification<Advertisement> buildSearchSpecification() { private Specification<Advertisement> buildSearchSpecification(String location) {
return (Root<Advertisement> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> { return (Root<Advertisement> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
List<Predicate> predicateList = new ArrayList<Predicate>(); List<Predicate> predicateList = new ArrayList<Predicate>();
Predicate isDeleted = builder.equal(root.get("isDeleted"), "0"); Predicate isDeleted = builder.equal(root.get("isDeleted"), "0");
Predicate isDisplay = builder.equal(root.get("display"), "1"); Predicate isDisplay = builder.equal(root.get("display"), "1");
predicateList.add(builder.and(isDeleted, isDisplay)); Predicate predicate = builder.and(isDeleted, isDisplay);
if(location !=null){
Predicate locationPredicate = builder.equal(root.get("location"), location);
predicate.getExpressions().add(locationPredicate);
}
predicateList.add(predicate);
// LocalDateTime now = LocalDateTime.now(); // LocalDateTime now = LocalDateTime.now();
@ -217,6 +213,7 @@ public class CMSAdvertisementService {
}; };
} }
private String contentToSummary(String content){ private String contentToSummary(String content){
String originalString = content; String originalString = content;
String[] parts = originalString.split("<.*?>"); String[] parts = originalString.split("<.*?>");

@ -54,6 +54,24 @@ public class CMSArticleService {
} }
} }
Article article = buildArticle(paramAdd,image);
Example<Article> example = Example.of(article, ExampleMatcher.matching()
.withIgnorePaths("id")
.withIgnorePaths("image")
.withIgnorePaths("pubTime")
.withIgnorePaths("createTime")
.withIgnorePaths("updateTime")
);
if(!articleDao.exists(example)) {
articleDao.save(article);
}
return Result.success();
}
private Article buildArticle(ArticleAddModel paramAdd,String image){
Article article = new Article(); Article article = new Article();
article.setId(String.valueOf(idWorker.nextId())); article.setId(String.valueOf(idWorker.nextId()));
article.setTitle(paramAdd.getTitle()); article.setTitle(paramAdd.getTitle());
@ -102,20 +120,8 @@ public class CMSArticleService {
article.setIsPublish("1"); article.setIsPublish("1");
article.setPubTime(LocalDateTime.now()); article.setPubTime(LocalDateTime.now());
}*/ }*/
return article;
Example<Article> example = Example.of(article, ExampleMatcher.matching()
.withIgnorePaths("id")
.withIgnorePaths("image")
.withIgnorePaths("pubTime")
.withIgnorePaths("createTime")
.withIgnorePaths("updateTime")
);
if(!articleDao.exists(example)) {
articleDao.save(article);
}
return Result.success();
} }
private String moveArticleImage(String srcKey, String image) { private String moveArticleImage(String srcKey, String image) {
@ -156,7 +162,6 @@ public class CMSArticleService {
if (!optional.isPresent()) { if (!optional.isPresent()) {
return Result.failed("找不到期刊: " + id); return Result.failed("找不到期刊: " + id);
} }
Article article = optional.get(); Article article = optional.get();
//如果图片路径存在 temp/ 则为新图片 //如果图片路径存在 temp/ 则为新图片
@ -173,29 +178,22 @@ public class CMSArticleService {
} }
} }
if (StringUtils.isNotBlank(param.getTitle()))
article.setTitle(param.getTitle()); article.setTitle(param.getTitle());
if (StringUtils.isNotBlank(param.getType()))
article.setType(param.getType()); article.setType(param.getType());
if (StringUtils.isNotBlank(param.getContent())) {
article.setSummary(contentToSummary(param.getContent())); article.setSummary(contentToSummary(param.getContent()));
article.setContent(param.getContent()); article.setContent(param.getContent());
}
if (StringUtils.isNotBlank(param.getAllowCommit()))
article.setAllowCommit(param.getAllowCommit()); article.setAllowCommit(param.getAllowCommit());
if (StringUtils.isNotBlank(param.getAutoPush()))
article.setAutoPush(param.getAutoPush()); article.setAutoPush(param.getAutoPush());
if(StringUtils.isNotBlank(param.getIsScheduled()))
article.setIsScheduled(param.getIsScheduled()); article.setIsScheduled(param.getIsScheduled());
if(StringUtils.isNotBlank(param.getPubTime()))
article.setPubTime(LocalDateTime.parse(param.getPubTime(), Constants.formatter)); article.setPubTime(LocalDateTime.parse(param.getPubTime(), Constants.formatter));
articleDao.save(article);
//TODO: 发布方式 //TODO: 发布方式
return Result.success(); return Result.success();

Loading…
Cancel
Save