From c2361bbae00063095b55cac02badba0be9d3427a Mon Sep 17 00:00:00 2001 From: johnniang Date: Wed, 3 Apr 2019 09:59:09 +0800 Subject: [PATCH] Complete cc.ryanc.halo.web.controller.portal.api.UserController#getProfile --- .../cc/ryanc/halo/service/UserService.java | 11 +++++++ .../halo/service/impl/UserServiceImpl.java | 16 ++++++++++ .../controller/portal/api/TagController.java | 2 ++ .../controller/portal/api/UserController.java | 31 +++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 src/main/java/cc/ryanc/halo/web/controller/portal/api/UserController.java diff --git a/src/main/java/cc/ryanc/halo/service/UserService.java b/src/main/java/cc/ryanc/halo/service/UserService.java index 526ba783e..356a0401b 100755 --- a/src/main/java/cc/ryanc/halo/service/UserService.java +++ b/src/main/java/cc/ryanc/halo/service/UserService.java @@ -26,8 +26,19 @@ public interface UserService extends CrudService { */ int MAX_LOGIN_TRY = 5; + /** + * Lock minutes. + */ int LOCK_MINUTES = 10; + /** + * Gets current user. + * + * @return an optional user + */ + @NonNull + Optional getCurrentUser(); + /** * Gets user by username. * diff --git a/src/main/java/cc/ryanc/halo/service/impl/UserServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/UserServiceImpl.java index dae3d2dc1..7c903c0b9 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/UserServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/UserServiceImpl.java @@ -18,9 +18,11 @@ import cn.hutool.crypto.digest.BCrypt; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import javax.servlet.http.HttpSession; import java.util.Date; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -44,6 +46,20 @@ public class UserServiceImpl extends AbstractCrudService implemen this.stringCacheStore = stringCacheStore; } + @Override + public Optional getCurrentUser() { + // Find all users + List users = listAll(); + + if (CollectionUtils.isEmpty(users)) { + // Return empty user + return Optional.empty(); + } + + // Return the first user + return Optional.of(users.get(0)); + } + @Override public Optional getByUsername(String username) { return userRepository.findByUsername(username); diff --git a/src/main/java/cc/ryanc/halo/web/controller/portal/api/TagController.java b/src/main/java/cc/ryanc/halo/web/controller/portal/api/TagController.java index ee6d15fcb..d4e10be91 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/portal/api/TagController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/portal/api/TagController.java @@ -6,6 +6,7 @@ import cc.ryanc.halo.model.entity.Tag; import cc.ryanc.halo.service.PostTagService; import cc.ryanc.halo.service.TagService; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -39,6 +40,7 @@ public class TagController { @GetMapping @ApiOperation("Lists tags") public List listTags(@SortDefault(sort = "updateTime", direction = DESC) Sort sort, + @ApiParam("If the param is true, post count of tag will be returned") @RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) { if (more) { return postTagService.listTagWithCountDtos(sort); diff --git a/src/main/java/cc/ryanc/halo/web/controller/portal/api/UserController.java b/src/main/java/cc/ryanc/halo/web/controller/portal/api/UserController.java new file mode 100644 index 000000000..b6e8e611b --- /dev/null +++ b/src/main/java/cc/ryanc/halo/web/controller/portal/api/UserController.java @@ -0,0 +1,31 @@ +package cc.ryanc.halo.web.controller.portal.api; + +import cc.ryanc.halo.model.dto.UserOutputDTO; +import cc.ryanc.halo.service.UserService; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Portal user controller. + * + * @author johnniang + * @date 4/3/19 + */ +@RestController("PortalUserController") +@RequestMapping("/api/users") +public class UserController { + + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } + + @GetMapping("profile") + @ApiOperation("Gets blogger profile") + public UserOutputDTO getProfile() { + return userService.getCurrentUser().map(user -> new UserOutputDTO().convertFrom(user)).get(); + } +}