Refactor the rest controllers

pull/98/head
johnniang 2019-02-21 00:47:10 +08:00
parent e88126fd0b
commit 135719607e
3 changed files with 19 additions and 16 deletions

View File

@ -1,5 +1,6 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.exception.NotFoundException;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
@ -150,13 +151,16 @@ public class ApiPostController {
* @return JsonResult
*/
@GetMapping(value = "/{postId}")
public JsonResult posts(@PathVariable(value = "postId") Long postId) {
public Post posts(@PathVariable(value = "postId") Long postId) {
final Post post = postService.findByPostId(postId, PostTypeEnum.POST_TYPE_POST.getDesc());
if (null != post) {
postService.cacheViews(post.getPostId());
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), post);
} else {
return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
if (post == null) {
throw new NotFoundException("Post with id: " + postId + " was not found").setErrorData(postId);
}
// Cache views
postService.cacheViews(post.getPostId());
return post;
}
}

View File

@ -1,5 +1,6 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.exception.NotFoundException;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.ResponseStatusEnum;
@ -78,12 +79,13 @@ public class ApiTagController {
* @return JsonResult
*/
@GetMapping(value = "/{tagUrl}")
public JsonResult tags(@PathVariable("tagUrl") String tagUrl) {
public Tag tags(@PathVariable("tagUrl") String tagUrl) {
final Tag tag = tagService.findByTagUrl(tagUrl);
if (null != tag) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), tag);
} else {
return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
if (tag == null) {
throw new NotFoundException("Tag with url: " + tagUrl + " was not found");
}
return tag;
}
}

View File

@ -1,8 +1,6 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.ResponseStatusEnum;
import cc.ryanc.halo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@ -47,8 +45,7 @@ public class ApiUserController {
* @return JsonResult
*/
@GetMapping
public JsonResult user() {
final User user = userService.findUser();
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), user);
public User user() {
return userService.findUser();
}
}