Skip to content

Commit

Permalink
#119 fix: 유저 find 기능 수정
Browse files Browse the repository at this point in the history
- findAllByUserToken()
- findAllByUserId()
  • Loading branch information
rivkode committed Sep 5, 2024
1 parent 9efed07 commit 28fcc4e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.seoultech.synergybe.domain.email;

import com.seoultech.synergybe.domain.common.RandomNumber;
import com.seoultech.synergybe.system.utils.RedisUtil;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void updateUserInfo(
String name,
String major
) {
if (userToken == null || email == null || name == null || major == null) throw new NullPointerException();
this.email = this.email.updateEmail(email);
this.name = this.name.updateName(name);
this.major = this.major.updateMajor(major);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public ResponseEntity<GetUserAccountResponse> getUser(@PathVariable("userId") St
@Operation(summary = "검색어를 포함하는 유자", description = "검색어를 포함하는 유저가 반환됩니다.")
@GetMapping
public ResponseEntity<Page<User>> searchAllPosts(@RequestParam("search") String search, @PageableDefault(size = 15) Pageable pageable) {
log.info(">> keyword : {}", search);

return ResponseEntity.status(HttpStatus.OK).body(userService.searchAllUsers(search, pageable));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

@Query(value = "SELECT * FROM user WHERE user_id IN :userIds", nativeQuery = true)
List<User> findAllByUserId(@Param("userIds") List<Long> userIds);

Page<User> findAll(Specification<User> spec, Pageable pageable);

boolean existsByEmail(UserEmail email);

Optional<User> findByUserToken(String userToken);

@Query(value = "SELECT * FROM user WHERE user_id IN :userToken", nativeQuery = true)
List<User> findAllByUserToken(@Param("userToken") List<String> userToken);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.seoultech.synergybe.domain.user.repository;

import com.seoultech.synergybe.domain.user.User;
import com.seoultech.synergybe.domain.user.dto.response.GetUserAccountResponse;

public interface UserRepositoryCustom {
GetUserAccountResponse findUserAccountByEmail(String email);
import java.util.List;

public interface UserRepositoryCustom {
User findByEmail(String email);
List<User> findAllByUserToken(List<String> userToken);
List<User> findAllByUserId(List<Long> userIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.seoultech.synergybe.domain.user.User;
import com.seoultech.synergybe.domain.user.dto.response.GetUserAccountResponse;
import com.seoultech.synergybe.domain.user.dto.response.QGetUserAccountResponse;
import com.seoultech.synergybe.domain.user.vo.UserEmail;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.seoultech.synergybe.domain.user.QUser.user;

@RequiredArgsConstructor
@Repository
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public GetUserAccountResponse findUserAccountByEmail(String email) {
// User user1 = queryFactory
// .select(user)
// .from(user)
// .where(user.email.eq)
// .fetchOne();
GetUserAccountResponse getUserAccountResponse = GetUserAccountResponse.builder().build();
return getUserAccountResponse;
}

@Override
public User findByEmail(String email) {
UserEmail userEmail = new UserEmail(email);
Expand All @@ -34,4 +23,20 @@ public User findByEmail(String email) {
.where(user.email.eq(userEmail))
.fetchOne();
}

@Override
public List<User> findAllByUserToken(List<String> userToken) {
return queryFactory
.selectFrom(user)
.where(user.userToken.in(userToken))
.fetch();
}

@Override
public List<User> findAllByUserId(List<Long> userIds) {
return queryFactory
.selectFrom(user)
.where(user.id.in(userIds))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ public Predicate toPredicate(Root<User> userRoot, CriteriaQuery<?> query, Criter

@Transactional
public void updateMyInfo(
String userId,
String userToken,
String email,
String name,
String major
) {
User user = getUserByToken(userId);
User user = getUserByToken(userToken);
user.updateUserInfo(email, name, major);
}

Expand Down Expand Up @@ -251,7 +251,7 @@ public void generateAccessTokenByRefreshToken(HttpServletRequest request, HttpSe
public List<Long> getUserIds(List<String> userToken) {
List<User> users = userRepository.findAllByUserToken(userToken);

List<Long> userIds = users.stream().map(user -> user.getId()).toList();
List<Long> userIds = users.stream().map(User::getId).toList();
return userIds;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class TokenGeneratorUUIDTest {
void generateLongId() {
List<Long> longList = new ArrayList<>();

// test start
long startTime = System.currentTimeMillis();

for (int i = 0; i < 10000; i++) {
Expand All @@ -44,15 +43,13 @@ void generateLongId() {
void generateStringId() {
List<String> stringList = new ArrayList<>();

// test start
long startTime = System.currentTimeMillis();

for (int i = 0; i < 10000; i++) {
String stringId = tokenGenerator.generateToken(IdPrefix.POST);
stringList.add(stringId);
}

// test end
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;

Expand Down

0 comments on commit 28fcc4e

Please sign in to comment.