Add PostController

pull/137/head
johnniang 2019-03-19 18:32:15 +08:00
parent 4a6df0bf64
commit 7898b777db
10 changed files with 120 additions and 18 deletions

View File

@ -67,6 +67,15 @@ public class SwaggerConfiguration {
.enable(!haloProperties.getDocDisabled()); .enable(!haloProperties.getDocDisabled());
} }
@Bean
public Docket haloAdminApi() {
log.debug("Doc disabled: [{}]", haloProperties.getDocDisabled());
return buildApiDocket("cc.ryanc.halo.admin",
"cc.ryanc.halo.web.controller.admin",
"/api/admin/**")
.enable(!haloProperties.getDocDisabled());
}
@Bean @Bean
SecurityConfiguration security() { SecurityConfiguration security() {
return SecurityConfigurationBuilder.builder() return SecurityConfigurationBuilder.builder()

View File

@ -1,12 +1,14 @@
package cc.ryanc.halo.listener; package cc.ryanc.halo.listener;
import cc.ryanc.halo.config.properties.HaloProperties; import cc.ryanc.halo.config.properties.HaloProperties;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.support.HaloConst; import cc.ryanc.halo.model.support.HaloConst;
import cc.ryanc.halo.model.support.Theme; import cc.ryanc.halo.model.support.Theme;
import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.ThemeUtils; import cc.ryanc.halo.utils.ThemeUtils;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cc.ryanc.halo.web.controller.support.PageJacksonSerializer;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import freemarker.template.TemplateModelException; import freemarker.template.TemplateModelException;
@ -23,6 +25,9 @@ import java.io.FileNotFoundException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
* <pre> * <pre>
* *
@ -79,12 +84,8 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
* Get active theme * Get active theme
*/ */
private void getActiveTheme() { private void getActiveTheme() {
final String themeValue = optionService.getByKey("theme"); BaseContentController.THEME = optionService.getByProperty(BlogProperties.THEME).orElse(DEFAULT_THEME_NAME);
if (StrUtil.isNotEmpty(themeValue) && !StrUtil.equals(themeValue, null)) {
BaseContentController.THEME = themeValue;
} else {
BaseContentController.THEME = "anatole";
}
try { try {
configuration.setSharedVariable("themeName", BaseContentController.THEME); configuration.setSharedVariable("themeName", BaseContentController.THEME);
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
@ -114,7 +115,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
// Get server port // Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port"); String serverPort = applicationContext.getEnvironment().getProperty("server.port");
String blogUrl = null; String blogUrl = OPTIONS.get(BlogProperties.BLOG_URL);
if (StrUtil.isNotBlank(blogUrl)) { if (StrUtil.isNotBlank(blogUrl)) {
blogUrl = StrUtil.removeSuffix(blogUrl, "/"); blogUrl = StrUtil.removeSuffix(blogUrl, "/");

View File

@ -1,5 +1,6 @@
package cc.ryanc.halo.model.enums; package cc.ryanc.halo.model.enums;
import com.fasterxml.jackson.annotation.JsonValue;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -13,7 +14,7 @@ import java.util.stream.Stream;
public interface ValueEnum<T> { public interface ValueEnum<T> {
/** /**
* Convert value to corresponding enum. * Converts value to corresponding enum.
* *
* @param enumType enum type * @param enumType enum type
* @param value database value * @param value database value
@ -33,9 +34,10 @@ public interface ValueEnum<T> {
} }
/** /**
* Get enum value. * Gets enum value.
* *
* @return enum value * @return enum value
*/ */
@JsonValue
T getValue(); T getValue();
} }

View File

@ -17,6 +17,12 @@ import java.util.Map;
*/ */
public class HaloConst { public class HaloConst {
/**
* Default theme name.
*/
public final static String DEFAULT_THEME_NAME = "anatole";
/** /**
* version constant * version constant
*/ */

View File

@ -4,8 +4,10 @@ import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.service.base.CrudService; import cc.ryanc.halo.service.base.CrudService;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* Option service. * Option service.
@ -46,8 +48,36 @@ public interface OptionService extends CrudService<Option, Integer> {
/** /**
* Get option by key * Get option by key
* *
* @param key key * @param key option key must not be blank
* @return String * @return option value or null
*/ */
String getByKey(String key); @Nullable
String getByKeyOfNullable(@NonNull String key);
/**
* Get option by key
*
* @param key option key must not be blank
* @return an optional option value
*/
@NonNull
Optional<String> getByKey(@NonNull String key);
/**
* Gets option by blog property.
*
* @param property blog property must not be null
* @return an option value
*/
@Nullable
String getByPropertyOfNullable(@NonNull BlogProperties property);
/**
* Gets option by blog property.
*
* @param property blog property must not be null
* @return an optional option value
*/
@NonNull
Optional<String> getByProperty(@NonNull BlogProperties property);
} }

View File

@ -12,6 +12,7 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* OptionService implementation class * OptionService implementation class
@ -103,7 +104,26 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
* @return String * @return String
*/ */
@Override @Override
public String getByKey(String key) { public String getByKeyOfNullable(String key) {
return optionRepository.findByOptionKey(key).map(Option::getOptionValue).orElse(null); return getByKey(key).orElse(null);
}
@Override
public Optional<String> getByKey(String key) {
Assert.hasText(key, "Option key must not be blank");
return optionRepository.findByOptionKey(key).map(Option::getOptionValue);
}
@Override
public String getByPropertyOfNullable(BlogProperties property) {
return getByProperty(property).orElse(null);
}
@Override
public Optional<String> getByProperty(BlogProperties property) {
Assert.notNull(property, "Blog property must not be null");
return getByKey(property.getValue());
} }
} }

View File

@ -0,0 +1,32 @@
package cc.ryanc.halo.web.controller.admin.api;
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.service.PostService;
import org.springframework.data.domain.Page;
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;
/**
* Post controller.
*
* @author johnniang
* @date 3/19/19
*/
@RestController
@RequestMapping("/api/admin/posts")
public class PostController {
private final PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
@GetMapping("latest")
public Page<PostSimpleOutputDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
return postService.pageLatest(top);
}
}

View File

@ -3,6 +3,8 @@ package cc.ryanc.halo.web.controller.content.base;
import cc.ryanc.halo.logging.Logger; import cc.ryanc.halo.logging.Logger;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
/** /**
* Content base Controller * Content base Controller
* *
@ -14,7 +16,7 @@ public abstract class BaseContentController {
/** /**
* Default theme * Default theme
*/ */
public static String THEME = "anatole"; public static String THEME = DEFAULT_THEME_NAME;
protected Logger log = Logger.getLogger(getClass()); protected Logger log = Logger.getLogger(getClass());

View File

@ -6,6 +6,7 @@ import cc.ryanc.halo.model.support.HaloConst;
import cc.ryanc.halo.utils.ThemeUtils; import cc.ryanc.halo.utils.ThemeUtils;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import cn.hutool.json.JSONObject;
import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -48,7 +49,7 @@ public class CommonController implements ErrorController {
final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY); final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
log.error("Error path: [{}], status: [{}]", request.getRequestURI(), statusCode); log.error("Error path: [{}], status: [{}]", getErrorPath(), statusCode);
// Get the exception // Get the exception
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception"); Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");

View File

@ -29,6 +29,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS; import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
@ -42,8 +43,6 @@ import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
@RequestMapping("/installations") @RequestMapping("/installations")
public class InstallController { public class InstallController {
private final static String DEFAULT_THEME_NAME = "anatole";
private final UserService userService; private final UserService userService;
private final CategoryService categoryService; private final CategoryService categoryService;