Skip to content

Commit

Permalink
Extend search by intentions, interests and miscInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonononoki committed Dec 12, 2024
1 parent b47aa80 commit 33e6456
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ public class UserSearchRequest {
private int age;
private Date minDateDob;
private Date maxDateDob;
private long intentionId;
private Collection<Long> intentionIds;
private Collection<Long> likeIds;
private Collection<Long> hideIds;
private Collection<Long> blockIds;
private Collection<Long> blockedByIds;
private Collection<Long> genderIds;

private Collection<Integer> miscInfos;
private Collection<String> interests;
}
116 changes: 65 additions & 51 deletions src/main/java/com/nonononoki/alovoa/repo/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.nonononoki.alovoa.entity.User;
import com.nonononoki.alovoa.model.UserSearchRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -15,6 +14,56 @@

public interface UserRepository extends JpaRepository<User, Long> {

String SEARCH_SELECT_QUERY = "SELECT u FROM User u ";
String SEARCH_JOIN_QUERY = "JOIN u.miscInfos misc JOIN u.interests interest ";
String SEARCH_BASE_QUERY = "WHERE u.disabled = FALSE AND u.admin = FALSE AND u.confirmed = TRUE AND u.intention IS NOT NULL AND "
+ "u.locationLatitude IS NOT NULL AND u.locationLongitude IS NOT NULL "
+ "AND u.profilePicture IS NOT NULL "
+ "AND TIMESTAMPDIFF(YEAR, u.dates.dateOfBirth, CURDATE()) + u.preferedMaxAge >= :age AND TIMESTAMPDIFF(YEAR, u.dates.dateOfBirth, CURDATE()) + u.preferedMinAge <= :age AND u.dates.dateOfBirth >= :minDate AND u.dates.dateOfBirth <= :maxDate "
+ "AND u.id NOT IN (:likeIds) AND u.id NOT IN (:likeIds) AND u.id NOT IN (:hideIds) "
+ "AND u.id NOT IN (:blockIds) "
+ "AND u.id NOT IN (:blockedByIds) "
+ "AND u.gender.id IN (:genderIds) ";
String SEARCH_INTENTION_QUERY = "AND u.intention.id IN (:intentionIds) ";
String SEARCH_MISC_INFO_QUERY = "AND misc IN (:miscInfoIds) ";
String SEARCH_INTEREST_QUERY = "AND interest IN (:interests) ";
String SEARCH_LOCATION_QUERY = "AND u.locationLatitude BETWEEN :latitudeFrom AND :latitudeTo AND u.locationLongitude BETWEEN :longitudeFrom AND :longitudeTo ";

@Query(value = SEARCH_SELECT_QUERY + SEARCH_BASE_QUERY + SEARCH_LOCATION_QUERY)
List<User> usersSearchBaseQuery(@Param("age") int age, @Param("minDate") Date minDate, @Param("maxDate") Date maxDate,
@Param("latitudeFrom") Double latitudeFrom, @Param("latitudeTo") Double latitudeTo,
@Param("longitudeFrom") Double longitudeFrom, @Param("longitudeTo") Double longitudeTo,
@Param("likeIds") Collection<Long> likeIds,
@Param("hideIds") Collection<Long> hideIds, @Param("blockIds") Collection<Long> blockIds,
@Param("blockedByIds") Collection<Long> blockedByIds, @Param("genderIds") Collection<Long> genderIds,
Pageable page);

@Query(value = SEARCH_SELECT_QUERY + SEARCH_JOIN_QUERY + SEARCH_BASE_QUERY + SEARCH_LOCATION_QUERY + SEARCH_INTENTION_QUERY + SEARCH_MISC_INFO_QUERY + SEARCH_INTEREST_QUERY)
List<User> usersSearchQuery(@Param("age") int age,
@Param("minDate") Date minDate,
@Param("maxDate") Date maxDate,
@Param("latitudeFrom") Double latitudeFrom,
@Param("latitudeTo") Double latitudeTo,
@Param("longitudeFrom") Double longitudeFrom,
@Param("longitudeTo") Double longitudeTo,
@Param("likeIds") Collection<Long> likeIds,
@Param("hideIds") Collection<Long> hideIds,
@Param("blockIds") Collection<Long> blockIds,
@Param("blockedByIds") Collection<Long> blockedByIds,
@Param("genderIds") Collection<Long> genderIds,
@Param("intentionIds") Collection<Long> intentionIds,
@Param("miscInfoIds") Collection<Integer> miscInfos,
@Param("interests") Collection<String> interests,
Pageable page);

@Query(value = SEARCH_SELECT_QUERY + SEARCH_BASE_QUERY)
List<User> usersSearchIgnoreLocation(@Param("age") int age, @Param("minDate") Date minDate, @Param("maxDate") Date maxDate,
@Param("likeIds") Collection<Long> likeIds,
@Param("hideIds") Collection<Long> hideIds,
@Param("blockIds") Collection<Long> blockIds,
@Param("blockedByIds") Collection<Long> blockedByIds,
@Param("genderIds") Collection<Long> genderIds, Pageable page);

User findByEmail(String email);

User findByUuid(UUID uuid);
Expand All @@ -25,55 +74,28 @@ public interface UserRepository extends JpaRepository<User, Long> {

default List<User> usersSearch(UserSearchRequest request, Pageable page) {
return usersSearchQuery(request.getAge(), request.getMinDateDob(), request.getMaxDateDob(), request.getMinLat(),
request.getMaxLat(), request.getMinLong(), request.getMaxLong(), request.getIntentionId(),
request.getLikeIds(), request.getHideIds(), request.getBlockIds(), request.getGenderIds(), page);
request.getMaxLat(), request.getMinLong(), request.getMaxLong(),
request.getLikeIds(), request.getHideIds(), request.getBlockIds(), request.getBlockedByIds(), request.getGenderIds(),
request.getIntentionIds(),
request.getMiscInfos(),
request.getInterests(),
page);
}

List<User> findByConfirmedIsFalseAndAdminFalseAndDatesCreationDateBefore(Date date);
default List<User> usersBaseSearch(UserSearchRequest request, Pageable page) {
return usersSearchBaseQuery(request.getAge(), request.getMinDateDob(), request.getMaxDateDob(), request.getMinLat(),
request.getMaxLat(), request.getMinLong(), request.getMaxLong(),
request.getLikeIds(), request.getHideIds(), request.getBlockIds(), request.getBlockedByIds(), request.getGenderIds(),
page);
}

@Query(value = "SELECT u FROM User u WHERE u.disabled = FALSE AND u.admin = FALSE AND u.confirmed = TRUE AND u.intention IS NOT NULL AND "
+ "u.locationLatitude IS NOT NULL AND u.locationLongitude IS NOT NULL AND u.profilePicture IS NOT NULL "
+ "AND TIMESTAMPDIFF(YEAR, u.dates.dateOfBirth, CURDATE()) + u.preferedMaxAge >= :age AND TIMESTAMPDIFF(YEAR, u.dates.dateOfBirth, CURDATE()) + u.preferedMinAge <= :age AND u.dates.dateOfBirth >= :minDate AND u.dates.dateOfBirth <= :maxDate "
+ "AND u.locationLatitude BETWEEN :latitudeFrom AND :latitudeTo AND u.locationLongitude BETWEEN :longitudeFrom AND :longitudeTo "
+ "AND CASE WHEN :intentionId < 0 THEN 1=1 ELSE :intentionId = u.intention.id END "
+ "AND u.id NOT IN (:likeIds) AND u.id NOT IN (:likeIds) AND u.id NOT IN (:hideIds) "
+ "AND u.id NOT IN (:blockIds) AND u.gender.id IN (:genderIds)")
List<User> usersSearchQuery(@Param("age") int age, @Param("minDate") Date minDate, @Param("maxDate") Date maxDate,
@Param("latitudeFrom") Double latitudeFrom, @Param("latitudeTo") Double latitudeTo,
@Param("longitudeFrom") Double longitudeFrom, @Param("longitudeTo") Double longitudeTo,
@Param("intentionId") long intentionId, @Param("likeIds") Collection<Long> likeIds,
@Param("hideIds") Collection<Long> hideIds, @Param("blockIds") Collection<Long> blockIds,
@Param("genderIds") Collection<Long> genderIds, Pageable page);
List<User> findByConfirmedIsFalseAndAdminFalseAndDatesCreationDateBefore(Date date);

default List<User> usersSearchAllIgnoreLocation(UserSearchRequest request, Pageable page) {
return usersSearchIgnoreLocation(
request.getAge(), request.getMinDateDob(), request.getMaxDateDob(),
request.getIntentionId(), request.getLikeIds(), request.getHideIds(), request.getBlockIds(),
request.getGenderIds(), page);
}

@Query(value = "SELECT u FROM User u WHERE u.disabled = FALSE AND u.admin = FALSE AND u.confirmed = TRUE AND u.intention IS NOT NULL AND "
+ "u.locationLatitude IS NOT NULL AND u.locationLongitude IS NOT NULL AND u.profilePicture IS NOT NULL "
+ "AND TIMESTAMPDIFF(YEAR, u.dates.dateOfBirth, CURDATE()) + u.preferedMaxAge >= :age AND TIMESTAMPDIFF(YEAR, u.dates.dateOfBirth, CURDATE()) + u.preferedMinAge <= :age AND u.dates.dateOfBirth >= :minDate AND u.dates.dateOfBirth <= :maxDate "
+ "AND CASE WHEN :intentionId < 0 THEN 1=1 ELSE :intentionId = u.intention.id END "
+ "AND u.id NOT IN (:likeIds) AND u.id NOT IN (:likeIds) AND u.id NOT IN (:hideIds) "
+ "AND u.id NOT IN (:blockIds) AND u.gender.id IN (:genderIds)")
List<User> usersSearchIgnoreLocation(@Param("age") int age, @Param("minDate") Date minDate, @Param("maxDate") Date maxDate,
@Param("intentionId") long intentionId, @Param("likeIds") Collection<Long> likeIds,
@Param("hideIds") Collection<Long> hideIds, @Param("blockIds") Collection<Long> blockIds,
@Param("genderIds") Collection<Long> genderIds, Pageable page);

default List<User> usersSearchAllIgnoreLocationAndIntention(UserSearchRequest request, Sort sort) {
return findTop200ByDisabledFalseAndAdminFalseAndConfirmedTrueAndIntentionNotNullAndLocationLatitudeNotNullAndProfilePictureNotNullAndDatesDateOfBirthGreaterThanEqualAndDatesDateOfBirthLessThanEqualAndIdNotInAndIdNotInAndIdNotInAndGenderIdIn(
request.getMinDateDob(), request.getMaxDateDob(), request.getLikeIds(), request.getHideIds(),
request.getBlockIds(), request.getGenderIds(), sort);
}

// almost all, must have complete profile and not blocked
default List<User> usersSearchAllIgnoreAll(UserSearchRequest request, Sort sort) {
return findTop50ByDisabledFalseAndAdminFalseAndConfirmedTrueAndIntentionNotNullAndLocationLatitudeNotNullAndProfilePictureNotNullAndDatesDateOfBirthGreaterThanEqualAndDatesDateOfBirthLessThanEqualAndIdNotInAndIdNotInAndIdNotIn(
request.getMinDateDob(), request.getMaxDateDob(), request.getLikeIds(), request.getHideIds(),
request.getBlockIds(), sort);
request.getLikeIds(), request.getHideIds(), request.getBlockIds(),
request.getBlockedByIds(), request.getGenderIds(), page);
}

default List<User> usersDonate(Date minDate, Date maxDate) {
Expand All @@ -90,14 +112,6 @@ default List<User> adminSearch() {

List<User> findTop100ByDisabledFalseAndAdminFalseAndConfirmedTrueAndIntentionNotNullAndLocationLatitudeNotNullAndProfilePictureNotNullOrderByDatesCreationDateDesc();

List<User> findTop200ByDisabledFalseAndAdminFalseAndConfirmedTrueAndIntentionNotNullAndLocationLatitudeNotNullAndProfilePictureNotNullAndDatesDateOfBirthGreaterThanEqualAndDatesDateOfBirthLessThanEqualAndIdNotInAndIdNotInAndIdNotInAndGenderIdIn(
Date min, Date max, Collection<Long> likeIds, Collection<Long> hideIds, Collection<Long> blockIds,
Collection<Long> genderIds, Sort sort);

List<User> findTop50ByDisabledFalseAndAdminFalseAndConfirmedTrueAndIntentionNotNullAndLocationLatitudeNotNullAndProfilePictureNotNullAndDatesDateOfBirthGreaterThanEqualAndDatesDateOfBirthLessThanEqualAndIdNotInAndIdNotInAndIdNotIn(
Date min, Date max, Collection<Long> likeIds, Collection<Long> hideIds, Collection<Long> blockIds,
Sort sort);

// users donate
List<User> findTop20ByDisabledFalseAndAdminFalseAndConfirmedTrueAndIntentionNotNullAndLocationLatitudeNotNullAndProfilePictureNotNullAndDatesDateOfBirthGreaterThanEqualAndDatesDateOfBirthLessThanEqualOrderByTotalDonationsDesc(
Date minDate, Date maxDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public MessageDtoListModel messageUpdate(@PathVariable long convoId, @PathVariab
}
}

@GetMapping("/search/users")
@PostMapping("/search/users")
public SearchDto searchUsers(Model model, @RequestBody SearchService.SearchParams params) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException, UnsupportedEncodingException, AlovoaException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public Model getUsersModel(Model model, Double latitude, Double longitude, int d
model = new ConcurrentModel();
}
model.addAttribute("dto", searchService.searchComplete(SearchService.SearchParams.builder()
.latitude(Optional.ofNullable(latitude)).longitude(Optional.ofNullable(longitude))
.distance(Optional.of(distance)).sort(Optional.of(sortId)).build()));
.latitude(latitude).longitude(longitude)
.distance(distance).sort(sortId).build()));
model.addAttribute("currUser", authService.getCurrentUser(true));
return model;
}
Expand Down
Loading

0 comments on commit 33e6456

Please sign in to comment.