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; 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.domain.Post;
import cc.ryanc.halo.model.dto.JsonResult; import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum; import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
@ -150,13 +151,16 @@ public class ApiPostController {
* @return JsonResult * @return JsonResult
*/ */
@GetMapping(value = "/{postId}") @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()); final Post post = postService.findByPostId(postId, PostTypeEnum.POST_TYPE_POST.getDesc());
if (null != post) {
if (post == null) {
throw new NotFoundException("Post with id: " + postId + " was not found").setErrorData(postId);
}
// Cache views
postService.cacheViews(post.getPostId()); postService.cacheViews(post.getPostId());
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), post);
} else { return post;
return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
}
} }
} }

View File

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

View File

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