From eed14995aa2b49e3a62c6c0e843c343088557666 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Sun, 29 Dec 2019 18:35:13 +0800 Subject: [PATCH] feat: add data process apis. --- .../admin/api/DataProcessController.java | 50 ++++++++++++++ .../repository/ThemeSettingRepository.java | 7 ++ .../halo/app/service/DataProcessService.java | 20 ++++++ .../halo/app/service/ThemeSettingService.java | 5 ++ .../service/impl/AttachmentServiceImpl.java | 8 ++- .../service/impl/BaseCommentServiceImpl.java | 4 +- .../app/service/impl/BasePostServiceImpl.java | 12 +++- .../service/impl/DataProcessServiceImpl.java | 65 +++++++++++++++++++ .../app/service/impl/OptionServiceImpl.java | 5 +- .../app/service/impl/PhotoServiceImpl.java | 8 ++- .../service/impl/ThemeSettingServiceImpl.java | 11 +++- 11 files changed, 185 insertions(+), 10 deletions(-) create mode 100644 src/main/java/run/halo/app/controller/admin/api/DataProcessController.java create mode 100644 src/main/java/run/halo/app/service/DataProcessService.java create mode 100644 src/main/java/run/halo/app/service/impl/DataProcessServiceImpl.java diff --git a/src/main/java/run/halo/app/controller/admin/api/DataProcessController.java b/src/main/java/run/halo/app/controller/admin/api/DataProcessController.java new file mode 100644 index 000000000..fccf379c5 --- /dev/null +++ b/src/main/java/run/halo/app/controller/admin/api/DataProcessController.java @@ -0,0 +1,50 @@ +package run.halo.app.controller.admin.api; + +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; +import run.halo.app.service.DataProcessService; +import run.halo.app.service.ThemeSettingService; + +/** + * @author ryanwang + * @date 2019-12-29 + */ +@RestController +@RequestMapping("/api/admin/data/process") +public class DataProcessController { + + private final DataProcessService dataProcessService; + + private final ThemeSettingService themeSettingService; + + public DataProcessController(DataProcessService dataProcessService, + ThemeSettingService themeSettingService) { + this.dataProcessService = dataProcessService; + this.themeSettingService = themeSettingService; + } + + @PutMapping("url/replace") + @ApiOperation("Replace url in all table.") + public void replaceUrl(@RequestParam("oldUrl") String oldUrl, + @RequestParam("newUrl") String newUrl) { + dataProcessService.replaceAllUrl(oldUrl, newUrl); + } + + @DeleteMapping("themes/settings/inactivated") + @ApiOperation("Delete inactivated theme settings.") + public void deleteInactivatedThemeSettings() { + themeSettingService.deleteInactivated(); + } + + @DeleteMapping("tags/unused") + @ApiOperation("Delete unused tags") + public void deleteUnusedTags() { + // TODO + } + + @DeleteMapping("categories/unused") + @ApiOperation("Delete unused categories") + public void deleteUnusedCategories() { + // TODO + } +} diff --git a/src/main/java/run/halo/app/repository/ThemeSettingRepository.java b/src/main/java/run/halo/app/repository/ThemeSettingRepository.java index 8ea8b50e3..9a504c199 100644 --- a/src/main/java/run/halo/app/repository/ThemeSettingRepository.java +++ b/src/main/java/run/halo/app/repository/ThemeSettingRepository.java @@ -42,4 +42,11 @@ public interface ThemeSettingRepository extends BaseRepository findByThemeIdAndKey(@NonNull String themeId, @NonNull String key); + + /** + * Deletes inactivated theme settings. + * + * @param activatedThemeId activated theme id. + */ + void deleteByThemeIdIsNot(@NonNull String activatedThemeId); } diff --git a/src/main/java/run/halo/app/service/DataProcessService.java b/src/main/java/run/halo/app/service/DataProcessService.java new file mode 100644 index 000000000..adf9d253d --- /dev/null +++ b/src/main/java/run/halo/app/service/DataProcessService.java @@ -0,0 +1,20 @@ +package run.halo.app.service; + +import org.springframework.lang.NonNull; + +/** + * Data process service interface. + * + * @author ryanwang + * @date 2019-12-29 + */ +public interface DataProcessService { + + /** + * Replace all url. + * + * @param oldUrl old url must not be null. + * @param newUrl new url must not be null. + */ + void replaceAllUrl(@NonNull String oldUrl, @NonNull String newUrl); +} diff --git a/src/main/java/run/halo/app/service/ThemeSettingService.java b/src/main/java/run/halo/app/service/ThemeSettingService.java index 04816e536..f71c33973 100644 --- a/src/main/java/run/halo/app/service/ThemeSettingService.java +++ b/src/main/java/run/halo/app/service/ThemeSettingService.java @@ -64,4 +64,9 @@ public interface ThemeSettingService { * @return replaced theme settings. */ List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); + + /** + * Delete unused theme setting. + */ + void deleteInactivated(); } diff --git a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java index 4f1bd8d26..775d9139a 100644 --- a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java @@ -188,8 +188,12 @@ public class AttachmentServiceImpl extends AbstractCrudService attachments = listAll(); List replaced = new ArrayList<>(); attachments.forEach(attachment -> { - attachment.setPath(attachment.getPath().replaceAll(oldUrl, newUrl)); - attachment.setThumbPath(attachment.getThumbPath().replaceAll(oldUrl, newUrl)); + if (StringUtils.isNotEmpty(attachment.getPath())) { + attachment.setPath(attachment.getPath().replaceAll(oldUrl, newUrl)); + } + if (StringUtils.isNotEmpty(attachment.getThumbPath())) { + attachment.setThumbPath(attachment.getThumbPath().replaceAll(oldUrl, newUrl)); + } replaced.add(attachment); }); return updateInBatch(replaced); diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index 12cb86733..0ecc9554e 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -589,7 +589,9 @@ public abstract class BaseCommentServiceImpl extend List comments = listAll(); List replaced = new ArrayList<>(); comments.forEach(comment -> { - comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl)); + if (StringUtils.isNotEmpty(comment.getAuthorUrl())) { + comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl)); + } replaced.add(comment); }); List updated = updateInBatch(replaced); diff --git a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java index e4903953a..f1fa768e0 100644 --- a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java @@ -397,9 +397,15 @@ public abstract class BasePostServiceImpl extends Abstrac List posts = listAll(); List replaced = new ArrayList<>(); posts.forEach(post -> { - post.setThumbnail(post.getThumbnail().replaceAll(oldUrl, newUrl)); - post.setOriginalContent(post.getOriginalContent().replaceAll(oldUrl, newUrl)); - post.setFormatContent(post.getFormatContent().replaceAll(oldUrl, newUrl)); + if (StringUtils.isNotEmpty(post.getThumbnail())) { + post.setThumbnail(post.getThumbnail().replaceAll(oldUrl, newUrl)); + } + if (StringUtils.isNotEmpty(post.getOriginalContent())) { + post.setOriginalContent(post.getOriginalContent().replaceAll(oldUrl, newUrl)); + } + if (StringUtils.isNotEmpty(post.getFormatContent())) { + post.setFormatContent(post.getFormatContent().replaceAll(oldUrl, newUrl)); + } replaced.add(post); }); List updated = updateInBatch(replaced); diff --git a/src/main/java/run/halo/app/service/impl/DataProcessServiceImpl.java b/src/main/java/run/halo/app/service/impl/DataProcessServiceImpl.java new file mode 100644 index 000000000..84a5a681e --- /dev/null +++ b/src/main/java/run/halo/app/service/impl/DataProcessServiceImpl.java @@ -0,0 +1,65 @@ +package run.halo.app.service.impl; + +import org.springframework.stereotype.Service; +import run.halo.app.service.*; + +/** + * DataProcessService implementation. + * + * @author ryanwang + * @date 2019-12-29 + */ +@Service +public class DataProcessServiceImpl implements DataProcessService { + + private final PostService postService; + + private final SheetService sheetService; + + private final PostCommentService postCommentService; + + private final SheetCommentService sheetCommentService; + + private final JournalCommentService journalCommentService; + + private final AttachmentService attachmentService; + + private final OptionService optionService; + + private final PhotoService photoService; + + private final ThemeSettingService themeSettingService; + + public DataProcessServiceImpl(PostService postService, + SheetService sheetService, + PostCommentService postCommentService, + SheetCommentService sheetCommentService, + JournalCommentService journalCommentService, + AttachmentService attachmentService, + OptionService optionService, + PhotoService photoService, + ThemeSettingService themeSettingService) { + this.postService = postService; + this.sheetService = sheetService; + this.postCommentService = postCommentService; + this.sheetCommentService = sheetCommentService; + this.journalCommentService = journalCommentService; + this.attachmentService = attachmentService; + this.optionService = optionService; + this.photoService = photoService; + this.themeSettingService = themeSettingService; + } + + @Override + public void replaceAllUrl(String oldUrl, String newUrl) { + postService.replaceUrl(oldUrl, newUrl); + sheetService.replaceUrl(oldUrl, newUrl); + postCommentService.replaceUrl(oldUrl, newUrl); + sheetCommentService.replaceUrl(oldUrl, newUrl); + journalCommentService.replaceUrl(oldUrl, newUrl); + attachmentService.replaceUrl(oldUrl, newUrl); + optionService.replaceUrl(oldUrl, newUrl); + photoService.replaceUrl(oldUrl, newUrl); + themeSettingService.replaceUrl(oldUrl, newUrl); + } +} diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index c627307d5..a0e7ffb3f 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -466,10 +466,13 @@ public class OptionServiceImpl extends AbstractCrudService impl List