mirror of https://github.com/halo-dev/halo
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.javapull/137/head
commit
e895725a9c
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.config;
|
|||
|
||||
import cc.ryanc.halo.config.properties.HaloProperties;
|
||||
import cc.ryanc.halo.security.resolver.AuthenticationArgumentResolver;
|
||||
import cc.ryanc.halo.factory.StringToEnumConverterFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.PropertySource;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
|
@ -92,4 +94,9 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
|
|||
lci.setParamName("lang");
|
||||
return lci;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addConverterFactory(new StringToEnumConverterFactory());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,7 +8,6 @@ import org.springframework.data.domain.Page;
|
|||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.data.web.SortDefault;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
@ -28,7 +27,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
|
|||
@RequestMapping(value = "/admin/posts")
|
||||
public class PostController {
|
||||
|
||||
private PostService postService;
|
||||
private final PostService postService;
|
||||
|
||||
public PostController(PostService postService) {
|
||||
this.postService = postService;
|
||||
|
@ -41,23 +40,23 @@ public class PostController {
|
|||
* @param status post status
|
||||
* @param page current page
|
||||
* @param sort sort
|
||||
*
|
||||
* @return template path: admin/admin_post.ftl
|
||||
*/
|
||||
@GetMapping
|
||||
public String posts(Model model,
|
||||
@RequestParam(value = "status", defaultValue = "PUBLISHED") PostStatus status,
|
||||
@PageableDefault Pageable defaultPageable,
|
||||
@RequestParam(value = "status", defaultValue = "published") PostStatus status,
|
||||
@RequestParam(value = "page", defaultValue = "0") Integer page,
|
||||
@SortDefault.SortDefaults({
|
||||
@SortDefault(sort = "postPriority", direction = DESC),
|
||||
@SortDefault(sort = "postDate", direction = DESC)
|
||||
@SortDefault(sort = "topPriority", direction = DESC),
|
||||
@SortDefault(sort = "createTime", direction = DESC)
|
||||
}) Sort sort) {
|
||||
final Pageable pageable = PageRequest.of(page, 10, sort);
|
||||
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable);
|
||||
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("trashCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST));
|
||||
model.addAttribute("recycleCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST));
|
||||
model.addAttribute("status", status);
|
||||
return "admin/admin_post";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue