feat: add data process apis.

pull/471/head^2
ruibaby 2019-12-29 18:35:13 +08:00
parent 305d907531
commit eed14995aa
11 changed files with 185 additions and 10 deletions

View File

@ -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
}
}

View File

@ -42,4 +42,11 @@ public interface ThemeSettingRepository extends BaseRepository<ThemeSetting, Int
*/
@NonNull
Optional<ThemeSetting> findByThemeIdAndKey(@NonNull String themeId, @NonNull String key);
/**
* Deletes inactivated theme settings.
*
* @param activatedThemeId activated theme id.
*/
void deleteByThemeIdIsNot(@NonNull String activatedThemeId);
}

View File

@ -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);
}

View File

@ -64,4 +64,9 @@ public interface ThemeSettingService {
* @return replaced theme settings.
*/
List<ThemeSetting> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
/**
* Delete unused theme setting.
*/
void deleteInactivated();
}

View File

@ -188,8 +188,12 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
List<Attachment> attachments = listAll();
List<Attachment> 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);

View File

@ -589,7 +589,9 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
List<COMMENT> comments = listAll();
List<COMMENT> 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<COMMENT> updated = updateInBatch(replaced);

View File

@ -397,9 +397,15 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
List<POST> posts = listAll();
List<POST> 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<POST> updated = updateInBatch(replaced);

View File

@ -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);
}
}

View File

@ -466,10 +466,13 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
List<Option> options = listAll();
List<Option> replaced = new ArrayList<>();
options.forEach(option -> {
option.setValue(option.getValue().replaceAll(oldUrl, newUrl));
if (StringUtils.isNotEmpty(option.getValue())) {
option.setValue(option.getValue().replaceAll(oldUrl, newUrl));
}
replaced.add(option);
});
List<Option> updated = updateInBatch(replaced);
publishOptionUpdatedEvent();
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
}

View File

@ -116,8 +116,12 @@ public class PhotoServiceImpl extends AbstractCrudService<Photo, Integer> implem
List<Photo> photos = listAll();
List<Photo> replaced = new ArrayList<>();
photos.forEach(photo -> {
photo.setThumbnail(photo.getThumbnail().replace(oldUrl, newUrl));
photo.setUrl(photo.getUrl().replaceAll(oldUrl, newUrl));
if (StringUtils.isNotEmpty(photo.getThumbnail())) {
photo.setThumbnail(photo.getThumbnail().replace(oldUrl, newUrl));
}
if (StringUtils.isNotEmpty(photo.getUrl())) {
photo.setUrl(photo.getUrl().replaceAll(oldUrl, newUrl));
}
replaced.add(photo);
});
List<Photo> updated = updateInBatch(replaced);

View File

@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.exception.ServiceException;
@ -165,12 +166,20 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
List<ThemeSetting> themeSettings = listAll();
List<ThemeSetting> replaced = new ArrayList<>();
themeSettings.forEach(themeSetting -> {
themeSetting.setValue(themeSetting.getValue().replaceAll(oldUrl, newUrl));
if (StringUtils.isNotEmpty(themeSetting.getValue())) {
themeSetting.setValue(themeSetting.getValue().replaceAll(oldUrl, newUrl));
}
replaced.add(themeSetting);
});
return updateInBatch(replaced);
}
@Override
@Transactional
public void deleteInactivated() {
themeSettingRepository.deleteByThemeIdIsNot(themeService.getActivatedThemeId());
}
/**
* Gets config item map. (key: item name, value: item)
*