diff --git a/src/main/java/cc/ryanc/halo/model/dto/LinkOutputDTO.java b/src/main/java/cc/ryanc/halo/model/dto/LinkOutputDTO.java new file mode 100644 index 000000000..0e156881a --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/dto/LinkOutputDTO.java @@ -0,0 +1,25 @@ +package cc.ryanc.halo.model.dto; + +import cc.ryanc.halo.model.dto.base.OutputConverter; +import cc.ryanc.halo.model.entity.Link; +import lombok.Data; + +/** + * Link output dto. + * + * @author : RYAN0UP + * @date : 2019/3/21 + */ +@Data +public class LinkOutputDTO implements OutputConverter { + + private Integer id; + + private String name; + + private String url; + + private String description; + + private String team; +} diff --git a/src/main/java/cc/ryanc/halo/service/LinkService.java b/src/main/java/cc/ryanc/halo/service/LinkService.java index 943360c3d..9d6ea89aa 100755 --- a/src/main/java/cc/ryanc/halo/service/LinkService.java +++ b/src/main/java/cc/ryanc/halo/service/LinkService.java @@ -1,7 +1,12 @@ package cc.ryanc.halo.service; +import cc.ryanc.halo.model.dto.LinkOutputDTO; import cc.ryanc.halo.model.entity.Link; import cc.ryanc.halo.service.base.CrudService; +import org.springframework.data.domain.Sort; +import org.springframework.lang.NonNull; + +import java.util.List; /** * Link service. @@ -10,4 +15,11 @@ import cc.ryanc.halo.service.base.CrudService; */ public interface LinkService extends CrudService { + /** + * List link dtos. + * + * @param sort sort + * @return all links + */ + List listDtos(@NonNull Sort sort); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java index 5bae9761d..0ccbd3c0b 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java @@ -1,10 +1,16 @@ package cc.ryanc.halo.service.impl; +import cc.ryanc.halo.model.dto.LinkOutputDTO; import cc.ryanc.halo.model.entity.Link; import cc.ryanc.halo.repository.LinkRepository; import cc.ryanc.halo.service.LinkService; import cc.ryanc.halo.service.base.AbstractCrudService; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.util.List; +import java.util.stream.Collectors; /** * LinkService implementation class @@ -21,4 +27,17 @@ public class LinkServiceImpl extends AbstractCrudService implemen super(linkRepository); this.linkRepository = linkRepository; } + + /** + * List link dtos. + * + * @param sort sort + * @return all links + */ + @Override + public List listDtos(Sort sort) { + Assert.notNull(sort, "Sort info must not be null"); + + return listAll(sort).stream().map(link -> (LinkOutputDTO) new LinkOutputDTO().convertFrom(link)).collect(Collectors.toList()); + } } diff --git a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java b/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java index 04acc2b14..f2c8c508b 100644 --- a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java +++ b/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java @@ -33,7 +33,7 @@ public class ThemeUtils { themes.addAll(getThemesByPath(getInternalThemesPath(), true)); themes.addAll(getThemesByPath(getUsersThemesPath(), false)); } catch (Exception e) { - throw new RuntimeException("Themes scan failed",e); + throw new RuntimeException("Themes scan failed", e); } return themes; } @@ -77,7 +77,7 @@ public class ThemeUtils { } } } catch (Exception e) { - throw new RuntimeException("Themes scan failed",e); + throw new RuntimeException("Themes scan failed", e); } return themes; } @@ -108,7 +108,7 @@ public class ThemeUtils { * @return File */ public static File getThemesPath(String themeName) throws FileNotFoundException { - return isInternal(themeName)?getInternalThemesPath():getUsersThemesPath(); + return isInternal(themeName) ? getInternalThemesPath() : getUsersThemesPath(); } /** @@ -139,7 +139,7 @@ public class ThemeUtils { } } } catch (Exception e) { - throw new RuntimeException("Failed to get theme template",e); + throw new RuntimeException("Failed to get theme template", e); } return templates; } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/LinkController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/LinkController.java new file mode 100644 index 000000000..3fb811e58 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/LinkController.java @@ -0,0 +1,39 @@ +package cc.ryanc.halo.web.controller.admin.api; + +import cc.ryanc.halo.model.dto.LinkOutputDTO; +import cc.ryanc.halo.service.LinkService; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.SortDefault; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * Link Controller + * + * @author : RYAN0UP + * @date : 2019/3/21 + */ +@RestController +@RequestMapping(value = "/admin/api/links") +public class LinkController { + + private final LinkService linkService; + + public LinkController(LinkService linkService) { + this.linkService = linkService; + } + + /** + * List all links + * + * @param sort sort + * @return List + */ + @GetMapping + public List listLinks(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort) { + return linkService.listDtos(sort); + } +} diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java new file mode 100644 index 000000000..3f2bb04e9 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java @@ -0,0 +1,69 @@ +package cc.ryanc.halo.web.controller.admin.api; + +import cc.ryanc.halo.model.enums.BlogProperties; +import cc.ryanc.halo.model.support.Theme; +import cc.ryanc.halo.service.OptionService; +import cc.ryanc.halo.utils.ThemeUtils; +import cc.ryanc.halo.web.controller.content.base.BaseContentController; +import freemarker.template.Configuration; +import freemarker.template.TemplateModelException; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static cc.ryanc.halo.model.support.HaloConst.OPTIONS; + +/** + * @author : RYAN0UP + * @date : 2019/3/20 + */ +@RestController +@RequestMapping("/admin/api/themes") +public class ThemeController { + + private final OptionService optionService; + + private Configuration configuration; + + public ThemeController(OptionService optionService, + Configuration configuration) { + this.optionService = optionService; + this.configuration = configuration; + } + + /** + * List all themes + * + * @return themes + */ + @GetMapping + @ApiOperation("List all themes") + public List listAll() { + return ThemeUtils.getThemes(); + } + + /** + * Active theme + * + * @param themeName theme name + * @throws TemplateModelException TemplateModelException + */ + @GetMapping(value = "active") + @ApiOperation("Active theme") + public void active(@RequestParam(name = "themeName", defaultValue = "anatole") String themeName) throws TemplateModelException { + Map properties = new HashMap<>(1); + properties.put(BlogProperties.THEME, themeName); + optionService.saveProperties(properties); + BaseContentController.THEME = themeName; + OPTIONS.clear(); + OPTIONS = optionService.listOptions(); + configuration.setSharedVariable("themeName", themeName); + configuration.setSharedVariable("options", OPTIONS); + } +}