feat: 删除主题时可选是否删除配置 (#1105)

* feat: 删除主题时可选是否删除配置

* doc: add java doc for run/halo/app/repository/ThemeSettingRepository.java#deleteByThemeId

Co-authored-by: Ryan Wang <i@ryanc.cc>
pull/1107/head
Wh1te 2020-10-12 22:30:31 +08:00 committed by GitHub
parent 5520baf046
commit 036644bf10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 6 deletions

View File

@ -154,8 +154,9 @@ public class ThemeController {
@DeleteMapping("{themeId}")
@ApiOperation("Deletes a theme")
@DisableOnCondition
public void deleteBy(@PathVariable("themeId") String themeId) {
themeService.deleteTheme(themeId);
public void deleteBy(@PathVariable("themeId") String themeId,
@RequestParam(value = "deleteSettings", defaultValue = "false") Boolean deleteSettings) {
themeService.deleteTheme(themeId, deleteSettings);
}
@PostMapping("upload")

View File

@ -49,4 +49,11 @@ public interface ThemeSettingRepository extends BaseRepository<ThemeSetting, Int
* @param activatedThemeId activated theme id.
*/
void deleteByThemeIdIsNot(@NonNull String activatedThemeId);
/**
* Deletes settings by theme id.
*
* @param themeId theme id.
*/
void deleteByThemeId(String themeId);
}

View File

@ -221,9 +221,10 @@ public interface ThemeService {
/**
* Deletes a theme by key.
*
* @param themeId theme id must not be blank
* @param themeId theme id must not be blank
* @param deleteSettings whether all settings of the specified theme should be deleted.
*/
void deleteTheme(@NonNull String themeId);
void deleteTheme(@NonNull String themeId, @NonNull Boolean deleteSettings);
/**
* Fetches theme configuration.

View File

@ -18,6 +18,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
@ -32,6 +33,7 @@ import run.halo.app.handler.theme.config.support.ThemeProperty;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.support.ThemeFile;
import run.halo.app.repository.ThemeSettingRepository;
import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService;
import run.halo.app.theme.ThemeFileScanner;
@ -77,6 +79,8 @@ public class ThemeServiceImpl implements ThemeService {
private final ApplicationEventPublisher eventPublisher;
private final ThemeSettingRepository themeSettingRepository;
/**
* Activated theme id.
*/
@ -93,7 +97,8 @@ public class ThemeServiceImpl implements ThemeService {
AbstractStringCacheStore cacheStore,
ThemeConfigResolver themeConfigResolver,
RestTemplate restTemplate,
ApplicationEventPublisher eventPublisher) {
ApplicationEventPublisher eventPublisher,
ThemeSettingRepository themeSettingRepository) {
this.optionService = optionService;
this.cacheStore = cacheStore;
this.themeConfigResolver = themeConfigResolver;
@ -101,6 +106,7 @@ public class ThemeServiceImpl implements ThemeService {
themeWorkDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER);
this.eventPublisher = eventPublisher;
this.themeSettingRepository = themeSettingRepository;
}
@Override
@ -255,8 +261,9 @@ public class ThemeServiceImpl implements ThemeService {
}
}
@Transactional
@Override
public void deleteTheme(@NonNull String themeId) {
public void deleteTheme(@NonNull String themeId, @NonNull Boolean deleteSettings) {
// Get the theme property
ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
@ -268,6 +275,10 @@ public class ThemeServiceImpl implements ThemeService {
try {
// Delete the folder
FileUtils.deleteFolder(Paths.get(themeProperty.getThemePath()));
if (deleteSettings) {
// Delete theme settings
themeSettingRepository.deleteByThemeId(themeId);
}
// Delete theme cache
eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
} catch (Exception e) {