👽 代码优化

pull/21/head
ruibaby 2018-07-19 00:41:15 +08:00
parent 0c3d0e2327
commit e6fc40ca1a
6 changed files with 91 additions and 7 deletions

View File

@ -84,6 +84,15 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*/
Post findPostByPostUrlAndPostType(String postUrl, String postType);
/**
*
*
* @param postId
* @param postType post or page
* @return Post
*/
Post findPostByPostIdAndPostType(Long postId, String postType);
/**
*
*

View File

@ -117,6 +117,15 @@ public interface PostService {
*/
Optional<Post> findByPostId(Long postId);
/**
*
*
* @param postId postId
* @param postType postType
* @return Post
*/
Post findByPostId(Long postId, String postType);
/**
*
*

View File

@ -192,6 +192,17 @@ public class PostServiceImpl implements PostService {
return postRepository.findById(postId);
}
/**
*
*
* @param postId postId
* @return Post
*/
@Override
public Post findByPostId(Long postId, String postType) {
return postRepository.findPostByPostIdAndPostType(postId, postType);
}
/**
*
*

View File

@ -0,0 +1,54 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.ResponseStatus;
import cc.ryanc.halo.service.OptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author : RYAN0UP
* @date : 2018/7/19
*/
@RestController
@RequestMapping(value = "/api/options")
public class ApiOptionController {
@Autowired
private OptionsService optionsService;
/**
*
*
* @return JsonResult
*/
@GetMapping
public JsonResult options() {
Map<String, String> options = optionsService.findAllOptions();
//去掉隐私元素
options.remove(BlogProperties.MAIL_SMTP_HOST.getProp());
options.remove(BlogProperties.MAIL_FROM_NAME.getProp());
options.remove(BlogProperties.MAIL_SMTP_PASSWORD.getProp());
options.remove(BlogProperties.MAIL_SMTP_USERNAME.getProp());
options.remove(BlogProperties.MAIL_SMTP_USERNAME.getProp());
return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), options);
}
/**
*
*
* @param optionName
* @return JsonResult
*/
@GetMapping(value = "/{optionName}")
public JsonResult option(@PathVariable(value = "optionName") String optionName) {
String optionValue = optionsService.findOneOption(optionName);
return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), optionValue);
}
}

View File

@ -25,11 +25,12 @@ public class ApiPageController {
/**
*
*
* @param postId postId
* @return JsonResult
*/
@GetMapping(value = "/{postUrl}")
public JsonResult pages(@PathVariable(value = "postUrl") String postUrl) {
Post post = postService.findByPostUrl(postUrl, PostType.POST_TYPE_PAGE.getDesc());
@GetMapping(value = "/{postId}")
public JsonResult pages(@PathVariable(value = "postId") Long postId) {
Post post = postService.findByPostId(postId, PostType.POST_TYPE_PAGE.getDesc());
if (null != post) {
return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), post);
} else {

View File

@ -66,12 +66,12 @@ public class ApiPostController {
/**
*
*
* @param postUrl
* @param postId
* @return JsonResult
*/
@GetMapping(value = "/{postUrl}")
public JsonResult posts(@PathVariable(value = "postUrl") String postUrl) {
Post post = postService.findByPostUrl(postUrl, PostType.POST_TYPE_POST.getDesc());
@GetMapping(value = "/{postId}")
public JsonResult posts(@PathVariable(value = "postId") Long postId) {
Post post = postService.findByPostId(postId, PostType.POST_TYPE_POST.getDesc());
if (null != post) {
return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), post);
} else {