mirror of https://github.com/halo-dev/halo
Complete cc.ryanc.halo.web.controller.portal.api.UserController#getProfile
parent
c45185dd6f
commit
c2361bbae0
|
@ -26,8 +26,19 @@ public interface UserService extends CrudService<User, Integer> {
|
|||
*/
|
||||
int MAX_LOGIN_TRY = 5;
|
||||
|
||||
/**
|
||||
* Lock minutes.
|
||||
*/
|
||||
int LOCK_MINUTES = 10;
|
||||
|
||||
/**
|
||||
* Gets current user.
|
||||
*
|
||||
* @return an optional user
|
||||
*/
|
||||
@NonNull
|
||||
Optional<User> getCurrentUser();
|
||||
|
||||
/**
|
||||
* Gets user by username.
|
||||
*
|
||||
|
|
|
@ -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<User, Integer> implemen
|
|||
this.stringCacheStore = stringCacheStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> getCurrentUser() {
|
||||
// Find all users
|
||||
List<User> 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<User> getByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
|
|
|
@ -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<? extends TagOutputDTO> 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);
|
||||
|
|
|
@ -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().<UserOutputDTO>convertFrom(user)).get();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue