Merge remote-tracking branch 'origin/v1' into v1

# Conflicts:
#	src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java
#	src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java
pull/137/head
johnniang 2019-03-14 21:34:34 +08:00
commit e895725a9c
3 changed files with 49 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.config;
import cc.ryanc.halo.config.properties.HaloProperties; import cc.ryanc.halo.config.properties.HaloProperties;
import cc.ryanc.halo.security.resolver.AuthenticationArgumentResolver; import cc.ryanc.halo.security.resolver.AuthenticationArgumentResolver;
import cc.ryanc.halo.factory.StringToEnumConverterFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -9,6 +10,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@ -92,4 +94,9 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
lci.setParamName("lang"); lci.setParamName("lang");
return lci; return lci;
} }
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(new StringToEnumConverterFactory());
}
} }

View File

@ -0,0 +1,35 @@
package cc.ryanc.halo.factory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;
/**
* @author : RYAN0UP
* @date : 2019/3/14
*/
@Component
public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
@SuppressWarnings("unchecked")
@Override
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnumConverter(targetType);
}
private static class StringToEnumConverter<T extends Enum>
implements Converter<String, T> {
private Class<T> enumType;
private StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}
@SuppressWarnings("unchecked")
@Override
public T convert(String source) {
return (T) Enum.valueOf(this.enumType, source.toUpperCase());
}
}
}

View File

@ -8,7 +8,6 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault; import org.springframework.data.web.SortDefault;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -28,7 +27,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
@RequestMapping(value = "/admin/posts") @RequestMapping(value = "/admin/posts")
public class PostController { public class PostController {
private PostService postService; private final PostService postService;
public PostController(PostService postService) { public PostController(PostService postService) {
this.postService = postService; this.postService = postService;
@ -41,23 +40,23 @@ public class PostController {
* @param status post status * @param status post status
* @param page current page * @param page current page
* @param sort sort * @param sort sort
*
* @return template path: admin/admin_post.ftl * @return template path: admin/admin_post.ftl
*/ */
@GetMapping @GetMapping
public String posts(Model model, public String posts(Model model,
@RequestParam(value = "status", defaultValue = "PUBLISHED") PostStatus status, @RequestParam(value = "status", defaultValue = "published") PostStatus status,
@PageableDefault Pageable defaultPageable,
@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "page", defaultValue = "0") Integer page,
@SortDefault.SortDefaults({ @SortDefault.SortDefaults({
@SortDefault(sort = "postPriority", direction = DESC), @SortDefault(sort = "topPriority", direction = DESC),
@SortDefault(sort = "postDate", direction = DESC) @SortDefault(sort = "createTime", direction = DESC)
}) Sort sort) { }) Sort sort) {
final Pageable pageable = PageRequest.of(page, 10, sort); final Pageable pageable = PageRequest.of(page, 10, sort);
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable); final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable);
model.addAttribute("posts", posts); model.addAttribute("posts", posts);
model.addAttribute("publishCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST)); model.addAttribute("publishedCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST));
model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT, PostType.POST)); model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT, PostType.POST));
model.addAttribute("trashCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST)); model.addAttribute("recycleCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST));
model.addAttribute("status", status); model.addAttribute("status", status);
return "admin/admin_post"; return "admin/admin_post";
} }