Skip to content

Commit

Permalink
YEL-159 [feat] 어드민 이름 검색 추가
Browse files Browse the repository at this point in the history
YEL-159 [feat] 어드민 이름 검색 추가
  • Loading branch information
euije authored Sep 9, 2023
2 parents e1d980f + 44cd824 commit fe5a17b
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ public BaseResponse<AdminLoginResponse> postAdminLogin(@RequestBody AdminLoginRe

@GetMapping("/user")
public BaseResponse<AdminUserResponse> getUserAdmin(@AccessTokenUser User user, @RequestParam Integer page,
@Nullable @RequestParam String yelloId) {
val data = yelloId == null
@Nullable @RequestParam String field,
@Nullable @RequestParam String value) {
val data = (field == null && value == null)
? adminService.findUser(user.getId(), PaginationFactory.createPageableLimitTen(page))
: adminService.findUserContaining(user.getId(), PaginationFactory.createPageableLimitTen(page),
yelloId);
field, value);
return BaseResponse.success(READ_USER_ADMIN_SUCCESS, data);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yello.server.domain.admin.exception;

import com.yello.server.global.common.ErrorCode;
import com.yello.server.global.exception.CustomException;

public class UserAdminBadRequestException extends CustomException {

public UserAdminBadRequestException(ErrorCode error) {
super(error, "[UserAdminBadRequestException] " + error.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yello.server.domain.admin.service;

import static com.yello.server.global.common.ErrorCode.DEVICE_TOKEN_CONFLICT_USER_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.USER_ADMIN_BAD_REQUEST_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.USER_ADMIN_NOT_FOUND_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.UUID_CONFLICT_USER_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.YELLOID_CONFLICT_USER_EXCEPTION;
Expand All @@ -13,6 +14,7 @@
import com.yello.server.domain.admin.dto.response.AdminUserContentVO;
import com.yello.server.domain.admin.dto.response.AdminUserDetailResponse;
import com.yello.server.domain.admin.dto.response.AdminUserResponse;
import com.yello.server.domain.admin.exception.UserAdminBadRequestException;
import com.yello.server.domain.admin.exception.UserAdminNotFoundException;
import com.yello.server.domain.admin.repository.UserAdminRepository;
import com.yello.server.domain.authorization.service.TokenProvider;
Expand All @@ -24,6 +26,7 @@
import com.yello.server.domain.user.repository.UserRepository;
import com.yello.server.domain.user.service.UserManager;
import com.yello.server.global.common.dto.EmptyObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -74,16 +77,30 @@ public AdminUserResponse findUser(Long adminId, Pageable page) {
return AdminUserResponse.of(totalCount, list);
}

public AdminUserResponse findUserContaining(Long adminId, Pageable page, String yelloId) {
public AdminUserResponse findUserContaining(Long adminId, Pageable page, String field, String value) {
// exception
final User admin = userRepository.getById(adminId);
userAdminRepository.getByUser(admin);

// logic
final Long totalCount = userRepository.countAllByYelloIdContaining(yelloId);
final List<AdminUserContentVO> list = userRepository.findAllContaining(page, yelloId).stream()
.map(AdminUserContentVO::of)
.toList();
Long totalCount = 0L;
List<AdminUserContentVO> list = new ArrayList<>();

if (field == null || value == null) {
throw new UserAdminBadRequestException(USER_ADMIN_BAD_REQUEST_EXCEPTION);
} else if (field.equals("yelloId")) {
totalCount = userRepository.countAllByYelloIdContaining(value);
list = userRepository.findAllByYelloIdContaining(page, value).stream()
.map(AdminUserContentVO::of)
.toList();
} else if (field.equals("name")) {
totalCount = userRepository.countAllByNameContaining(value);
list = userRepository.findAllByNameContaining(page, value).stream()
.map(AdminUserContentVO::of)
.toList();
} else {
throw new UserAdminBadRequestException(USER_ADMIN_BAD_REQUEST_EXCEPTION);
}

return AdminUserResponse.of(totalCount, list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ List<User> findAllByOtherGroupContainingYelloId(@Param("groupName") String group

Long countAllByYelloIdContaining(String yelloId);

Long countAllByNameContaining(String name);

@Query("select u from User u "
+ "where LOWER(u.yelloId) like LOWER(CONCAT('%', :yelloId, '%'))")
Page<User> findAllByYelloIdContaining(Pageable pageable, @Param("yelloId") String yelloId);

@Query("select u from User u "
+ "where LOWER(u.name) like LOWER(CONCAT('%', :name, '%'))")
Page<User> findAllByNameContaining(Pageable pageable, @Param("name") String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ List<User> findAllByOtherGroupContainingYelloId(String groupName, String keyword

Long countAllByYelloIdContaining(String yelloId);

Long countAllByNameContaining(String name);

Page<User> findAll(Pageable pageable);

Page<User> findAllContaining(Pageable pageable, String yelloId);
Page<User> findAllByYelloIdContaining(Pageable pageable, String yelloId);

Page<User> findAllByNameContaining(Pageable pageable, String name);

void delete(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,26 @@ public Long countAllByYelloIdContaining(String yelloId) {
return userJpaRepository.countAllByYelloIdContaining(yelloId);
}

@Override
public Long countAllByNameContaining(String name) {
return userJpaRepository.countAllByNameContaining(name);
}

@Override
public Page<User> findAll(Pageable pageable) {
return userJpaRepository.findAll(pageable);
}

@Override
public Page<User> findAllContaining(Pageable pageable, String yelloId) {
public Page<User> findAllByYelloIdContaining(Pageable pageable, String yelloId) {
return userJpaRepository.findAllByYelloIdContaining(pageable, yelloId);
}

@Override
public Page<User> findAllByNameContaining(Pageable pageable, String name) {
return userJpaRepository.findAllByNameContaining(pageable, name);
}

@Override
public void delete(User user) {
userJpaRepository.delete(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum ErrorCode {
GOOGLE_INAPP_BAD_REQUEST_EXCEPTION(BAD_REQUEST, "해당 영수증은 취소되었거나, 대기 중인 결제입니다."),
APPLE_IN_APP_BAD_REQUEST_EXCEPTION(BAD_REQUEST, "존재하지 않는 영수증입니다."),
METHOD_ARGUMENT_TYPE_MISMATCH_EXCEPTION(BAD_REQUEST, "입력한 값의 타입이 올바르지 않습니다."),
USER_ADMIN_BAD_REQUEST_EXCEPTION(BAD_REQUEST, "검색 필드가 없습니다."),

/**
* 401 UNAUTHORIZED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

import com.yello.server.domain.admin.exception.UserAdminBadRequestException;
import com.yello.server.domain.admin.exception.UserAdminNotFoundException;
import com.yello.server.domain.authorization.exception.AuthBadRequestException;
import com.yello.server.domain.authorization.exception.CustomAuthenticationException;
Expand Down Expand Up @@ -97,7 +98,8 @@ void handleException(HttpServletRequest request, Exception exception) throws Exc
QuestionException.class,
PurchaseException.class,
GoogleBadRequestException.class,
AppleBadRequestException.class
AppleBadRequestException.class,
UserAdminBadRequestException.class
})
public ResponseEntity<BaseResponse> BadRequestException(CustomException exception) {
return ResponseEntity.status(BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ public Long countAllByYelloIdContaining(String yelloId) {
.size();
}

@Override
public Long countAllByNameContaining(String name) {
return (long) data.stream()
.filter(user -> user.getName().contains(name))
.toList()
.size();
}

@Override
public Page<User> findAll(Pageable pageable) {
final List<User> userList = data.stream()
Expand All @@ -223,7 +231,7 @@ public Page<User> findAll(Pageable pageable) {
}

@Override
public Page<User> findAllContaining(Pageable pageable, String yelloId) {
public Page<User> findAllByYelloIdContaining(Pageable pageable, String yelloId) {
final List<User> userList = data.stream()
.filter(user -> user.getYelloId().contains(yelloId))
.skip(pageable.getOffset())
Expand All @@ -232,6 +240,16 @@ public Page<User> findAllContaining(Pageable pageable, String yelloId) {
return new PageImpl<>(userList);
}

@Override
public Page<User> findAllByNameContaining(Pageable pageable, String name) {
final List<User> userList = data.stream()
.filter(user -> user.getName().contains(name))
.skip(pageable.getOffset())
.limit(pageable.getPageSize())
.toList();
return new PageImpl<>(userList);
}

@Override
public void delete(User user) {
data.remove(user);
Expand Down

0 comments on commit fe5a17b

Please sign in to comment.