diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/controller/UserRemoteApiController.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/controller/UserRemoteApiController.java index e95b8b53..dbd107fd 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/controller/UserRemoteApiController.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/controller/UserRemoteApiController.java @@ -1,16 +1,22 @@ package com.jmsoftware.maf.authcenter.user.controller; +import com.jmsoftware.maf.authcenter.user.entity.GetUserPageListPayload; +import com.jmsoftware.maf.authcenter.user.entity.persistence.User; import com.jmsoftware.maf.authcenter.user.service.UserService; +import com.jmsoftware.maf.common.bean.PageResponseBodyBean; import com.jmsoftware.maf.common.bean.ResponseBodyBean; import com.jmsoftware.maf.common.domain.authcenter.user.GetUserByLoginTokenResponse; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; 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 javax.validation.Valid; + /** *

UserRemoteApiController

*

@@ -19,6 +25,7 @@ * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com * @date 5/10/20 12:36 PM **/ +@Validated @RestController @RequiredArgsConstructor @RequestMapping("/user-remote-api") @@ -31,4 +38,9 @@ public class UserRemoteApiController { public ResponseBodyBean getUserByLoginToken(@PathVariable String loginToken) { return ResponseBodyBean.ofSuccess(userService.getUserByLoginToken(loginToken)); } + + @GetMapping("/users") + public PageResponseBodyBean getUserPageList(@Valid GetUserPageListPayload payload) { + return userService.getUserPageList(payload); + } } diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/GetUserPageListPayload.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/GetUserPageListPayload.java new file mode 100644 index 00000000..c3c3ef8b --- /dev/null +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/GetUserPageListPayload.java @@ -0,0 +1,16 @@ +package com.jmsoftware.maf.authcenter.user.entity; + +import com.jmsoftware.maf.common.bean.PaginationBase; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * Description: GetUserPageList, change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 4:32 PM + **/ +@Data +@EqualsAndHashCode(callSuper = true) +public class GetUserPageListPayload extends PaginationBase { + private String username; +} diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/UserService.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/UserService.java index 3bc2d81d..161ddceb 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/UserService.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/UserService.java @@ -1,8 +1,10 @@ package com.jmsoftware.maf.authcenter.user.service; import com.baomidou.mybatisplus.extension.service.IService; +import com.jmsoftware.maf.authcenter.user.entity.GetUserPageListPayload; import com.jmsoftware.maf.authcenter.user.entity.GetUserStatusPayload; import com.jmsoftware.maf.authcenter.user.entity.persistence.User; +import com.jmsoftware.maf.common.bean.PageResponseBodyBean; import com.jmsoftware.maf.common.domain.authcenter.user.*; import com.jmsoftware.maf.common.exception.SecurityException; import org.springframework.validation.annotation.Validated; @@ -17,8 +19,7 @@ *

* Service of UserPersistence.(UserPersistence) * - * @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com - * @date 5 /10/20 12:31 PM + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 5/10/20 12:31 PM */ @Validated public interface UserService extends IService { @@ -63,4 +64,12 @@ public interface UserService extends IService { * @return the user status */ String getUserStatus(@Valid @NotNull GetUserStatusPayload payload); + + /** + * Gets user page list. + * + * @param payload the payload + * @return the user page list + */ + PageResponseBodyBean getUserPageList(@Valid @NotNull GetUserPageListPayload payload); } diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java index 851f64ea..f6e5326f 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java @@ -2,13 +2,17 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jmsoftware.maf.authcenter.security.service.JwtService; +import com.jmsoftware.maf.authcenter.user.entity.GetUserPageListPayload; import com.jmsoftware.maf.authcenter.user.entity.GetUserStatusPayload; import com.jmsoftware.maf.authcenter.user.entity.persistence.User; import com.jmsoftware.maf.authcenter.user.mapper.UserMapper; import com.jmsoftware.maf.authcenter.user.service.UserService; +import com.jmsoftware.maf.common.bean.PageResponseBodyBean; import com.jmsoftware.maf.common.domain.authcenter.user.*; import com.jmsoftware.maf.common.exception.SecurityException; import lombok.RequiredArgsConstructor; @@ -107,4 +111,15 @@ public String getUserStatus(@Valid @NotNull GetUserStatusPayload payload) { log.info("getHeader: {}", servletRequestAttributes.getRequest().getHeader("X-Username")); return UserStatus.ofValue(payload.getStatus()).getDescription(); } + + @Override + public PageResponseBodyBean getUserPageList(@Valid @NotNull GetUserPageListPayload payload) { + val page = new Page(payload.getCurrentPage(), payload.getPageSize()); + val queryWrapper = Wrappers.lambdaQuery(User.class); + if (StrUtil.isNotBlank(payload.getUsername())) { + queryWrapper.like(User::getUsername, payload.getUsername()); + } + final var page1 = this.page(page, queryWrapper); + return PageResponseBodyBean.ofSuccess(page1.getRecords(), page1.getTotal()); + } } diff --git a/common/src/main/java/com/jmsoftware/maf/common/bean/PageResponseBodyBean.java b/common/src/main/java/com/jmsoftware/maf/common/bean/PageResponseBodyBean.java new file mode 100644 index 00000000..01f961e2 --- /dev/null +++ b/common/src/main/java/com/jmsoftware/maf/common/bean/PageResponseBodyBean.java @@ -0,0 +1,322 @@ +package com.jmsoftware.maf.common.bean; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.json.JSON; +import cn.hutool.json.JSONConfig; +import cn.hutool.json.JSONUtil; +import com.google.common.collect.Lists; +import com.jmsoftware.maf.common.exception.BaseException; +import com.jmsoftware.maf.common.exception.BusinessException; +import lombok.*; +import org.springframework.http.HttpStatus; +import org.springframework.lang.Nullable; + +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +/** + *

PageResponseBodyBean

+ *

+ * Page Response body bean. + * + * @param the response body data type + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 4:24 PM + */ +@Data +@SuppressWarnings("unused") +public class PageResponseBodyBean implements Serializable { + /** + * The constant serialVersionUID. + */ + private static final long serialVersionUID = 4645461634548783641L; + + /** + * The Timestamp. + */ + @Setter(AccessLevel.NONE) + private final LocalDateTime timestamp = LocalDateTime.now(); + /** + * Default status is 200 OK. + */ + private Integer status = HttpStatus.OK.value(); + /** + * The Message. Default: 200 OK. + */ + private String message = HttpStatus.OK.getReasonPhrase(); + /** + * The List. + */ + private Collection list = Collections.emptyList(); + /** + * The Total. + */ + private long total; + + /** + *

Respond to client with IUniversalStatus (status may be OK or other).

+ *

ATTENTION:

+ *

This method CANNOT be used by controller or service or other class, only provided for Exception controller + * .

+ * + * @param the type parameter + * @param status IUniversalStatus + * @return response body for ExceptionControllerAdvice javax.servlet.http.HttpServletResponse, Exception) + */ + public static PageResponseBodyBean ofStatus(@NonNull final HttpStatus status) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setStatus(status.value()); + responseBodyBean.setMessage(status.getReasonPhrase()); + return responseBodyBean; + } + + /** + * Of status response body bean. + * + * @param the type parameter + * @param status the status + * @param message the message + * @return the response body bean + */ + public static PageResponseBodyBean ofStatus(@NonNull final HttpStatus status, + @NonNull final String message) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setStatus(status.value()); + responseBodyBean.setMessage(message); + return responseBodyBean; + } + + /** + *

Respond to client with IUniversalStatus and data.

+ *

ATTENTION:

+ *

This method CANNOT be used by controller or service or other class, only provided for Exception controller + * .

+ * + * @param the response body data type + * @param status IUniversalStatus + * @param data data to be responded to client + * @param total the total + * @return response body for ExceptionControllerAdvice + */ + public static PageResponseBodyBean ofStatus(@NonNull final HttpStatus status, + final Collection data, + final long total) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setStatus(status.value()); + responseBodyBean.setMessage(status.getReasonPhrase()); + responseBodyBean.setList(data); + responseBodyBean.setTotal(total); + return responseBodyBean; + } + + /** + *

Highly customizable response. Status might be any HttpStatus' code value.

+ *

ATTENTION:

+ *

This method CANNOT be used by controller or service or other class, only provided for Exception controller + * .

+ * + * @param the response body data type + * @param status status code + * @param message message to be responded + * @param data data to be responded + * @param total the total + * @return response body for ExceptionControllerAdvice + */ + public static PageResponseBodyBean ofStatus(@NonNull final Integer status, + @NonNull final String message, + final Collection data, + final long total) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setStatus(status); + responseBodyBean.setMessage(message); + responseBodyBean.setList(data); + responseBodyBean.setTotal(total); + return responseBodyBean; + } + + /** + *

Highly customizable response. Status might be any HttpStatus' code value.

+ *

ATTENTION:

+ *

This method CANNOT be used in ExceptionControllerAdvice.

+ * + * @param the response body data type + * @param status status code + * @param message message to be responded + * @param data data to be responded + * @param total the total + * @return response body + * @throws BaseException the base exception + */ + public static PageResponseBodyBean setResponse(@NonNull final Integer status, + @NonNull final String message, + final Collection data, + final long total) + throws BaseException { + if (!HttpStatus.valueOf(status).is2xxSuccessful()) { + throw new BaseException(status, message, data); + } + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setStatus(status); + responseBodyBean.setMessage(message); + responseBodyBean.setList(data); + responseBodyBean.setTotal(total); + return responseBodyBean; + } + + /** + * Respond null data, and status is OK. + * + * @param the response body data type + * @return response body + */ + public static PageResponseBodyBean ofSuccess() { + return new PageResponseBodyBean<>(); + } + + /** + * Respond data and status is OK. + * + * @param the response body data type + * @param data data to be responded to client. + * @param total the total + * @return response body + */ + public static PageResponseBodyBean ofSuccess(final Collection data, + final long total) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setList(data); + responseBodyBean.setTotal(total); + return responseBodyBean; + } + + /** + * Respond a message and status is OK. + * + * @param the response body data type + * @param message message to be responded + * @return response body + */ + public static PageResponseBodyBean ofSuccess(@NonNull final String message) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setMessage(message); + return responseBodyBean; + } + + /** + * Respond data, message and status is OK. + * + * @param the response body data type + * @param data data to be responded + * @param message message to be responded + * @param total the total + * @return response body + */ + public static PageResponseBodyBean ofSuccess(final Collection data, + @NonNull final String message, + final long total) { + PageResponseBodyBean responseBodyBean = new PageResponseBodyBean<>(); + responseBodyBean.setMessage(message); + responseBodyBean.setList(data); + responseBodyBean.setTotal(total); + return responseBodyBean; + } + + /** + * Respond a message and status is FAILURE(464). + * + * @param the response body data type + * @param message message to be responded. + * @return response body + * @throws BusinessException the business exception + */ + public static PageResponseBodyBean ofFailure(@NonNull final String message) throws BusinessException { + throw new BusinessException(message); + } + + /** + * Respond a message and status is FAILURE(464). + * + * @param the response body data type + * @param data data to be responded + * @return response body + * @throws BusinessException the business exception + */ + public static PageResponseBodyBean ofFailure(final Collection data) throws BusinessException { + throw new BusinessException(data); + } + + /** + * Respond data and message, and status if FAILURE(464). + * + * @param the response body data type + * @param data data to be responded + * @param message message to be responded + * @return response body + * @throws BusinessException the business exception + */ + public static PageResponseBodyBean ofFailure(final Collection data, + @NonNull final String message) throws BusinessException { + throw new BusinessException(data, message); + } + + /** + * Respond an ERROR(500). + * + * @param the response body data type + * @return response body + * @throws BaseException the base exception + */ + public static PageResponseBodyBean ofError() throws BaseException { + return setResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), + null, 0); + } + + /** + * Respond a custom error. + * + * @param the response body data type + * @param status Error status, not OK(200) + * @return response body + * @throws BaseException the base exception + */ + public static PageResponseBodyBean ofError(@NonNull final HttpStatus status) + throws BaseException { + return setResponse(status.value(), status.getReasonPhrase(), null, 0); + } + + /** + * Response an exception. + * + * @param the response body data type + * @param Sub class of {@link BaseException} + * @param throwable exception + * @return the response body bean + * @throws BaseException the base exception + */ + public static PageResponseBodyBean ofException(@NonNull final BaseThrowable throwable) + throws BaseException { + throw throwable; + } + + /** + * Of json. + * + * @param the type parameter + * @param message the message + * @param data the data + * @param total the total + * @param status the status + * @return the json + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/22/2020 10:16 AM + */ + public static JSON of(@NonNull String message, + @Nullable Collection data, + long total, @NonNull Integer status) { + val responseBodyBean = PageResponseBodyBean.ofStatus(status, message, data, total); + val config = new JSONConfig(); + config.setIgnoreNullValue(false).setDateFormat("yyyy-MM-dd HH:mm:ss"); + return JSONUtil.parse(responseBodyBean, config); + } +} diff --git a/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java b/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java index 180b931f..3717b9ee 100644 --- a/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java +++ b/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java @@ -1,5 +1,6 @@ package com.jmsoftware.maf.common.bean; +import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; @@ -49,10 +50,26 @@ public class PaginationBase { @JsonIgnore private String orderByStatement; + @JsonIgnore public String getOrderByStatement() { if (!StrUtil.isBlank(orderBy)) { return String.format("%s %s %s", "ORDER BY", orderBy, orderRule); } return orderByStatement; } + + /** + * Has next page boolean. + * + * @param pageResponseBodyBean the page response body bean + * @return the boolean + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 4:37 PM + */ + public boolean hasNextPage(@NotNull PageResponseBodyBean pageResponseBodyBean) { + if (NumberUtil.compare(pageResponseBodyBean.getTotal(), (long) currentPage * pageSize) > 0) { + currentPage += 1; + return true; + } + return false; + } } diff --git a/common/src/main/java/com/jmsoftware/maf/common/bean/ResponseBodyBean.java b/common/src/main/java/com/jmsoftware/maf/common/bean/ResponseBodyBean.java index 6be4a751..ee811079 100644 --- a/common/src/main/java/com/jmsoftware/maf/common/bean/ResponseBodyBean.java +++ b/common/src/main/java/com/jmsoftware/maf/common/bean/ResponseBodyBean.java @@ -3,7 +3,6 @@ import cn.hutool.json.JSON; import cn.hutool.json.JSONConfig; import cn.hutool.json.JSONUtil; -import com.fasterxml.jackson.annotation.JsonFormat; import com.jmsoftware.maf.common.exception.BaseException; import com.jmsoftware.maf.common.exception.BusinessException; import lombok.*; @@ -34,20 +33,19 @@ public class ResponseBodyBean implements Serializable { * The Timestamp. */ @Setter(AccessLevel.NONE) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") - LocalDateTime timestamp = LocalDateTime.now(); + private final LocalDateTime timestamp = LocalDateTime.now(); /** * Default status is 200 OK. */ - Integer status = HttpStatus.OK.value(); + private Integer status = HttpStatus.OK.value(); /** * The Message. Default: 200 OK. */ - String message = HttpStatus.OK.getReasonPhrase(); + private String message = HttpStatus.OK.getReasonPhrase(); /** * The Data. */ - ResponseBodyDataType data; + private ResponseBodyDataType data; /** *

Respond to client with IUniversalStatus (status may be OK or other).