Skip to content

Commit

Permalink
SMA-105: connect the places filter with backend (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivamach authored May 3, 2024
1 parent 1321614 commit adcd6b5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public class RequestEventDTO {
private List<String> sportsName = new ArrayList<>();
private double longitude;
private double latitude;
private String placeName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,47 @@ public interface EventRepository extends JpaRepository<Event, Long> {
List<Event> findEventsByUser(
@Param("id") Long id, @Param("now") LocalDateTime now, Pageable pageable);

@Query("SELECT ep.event FROM EventPlayer ep WHERE ep.player.id = :id AND ep.event.dateStart > :now ORDER BY ep.event.dateEnd ASC")
@Query(
"SELECT ep.event FROM EventPlayer ep WHERE ep.player.id = :id AND ep.event.dateStart > :now ORDER BY ep.event.dateEnd ASC")
List<Event> findUpcomingEventsByUser(@Param("id") Long id, @Param("now") LocalDateTime now);




/**
* <p>Finds events near the user location, optionally filtered by sport names.</p>
* Finds events near the user location, optionally filtered by sport names.
*
* <p>This method uses a native SQL query with a Haversine distance calculation to find events ordered by their distance from the user's location.</p>
* <p>This method uses a native SQL query with a Haversine distance calculation to find events
* ordered by their distance from the user's location.
*
* @param userLongitude user's longitude coordinate
* @param userLatitude user's latitude coordinate
* @param sportNames optional, list of sport names to filter events by.
* If null, all events are returned.
* Sport names are not case sensitive.
* @param pageable contains the page and size
* @return list of events filtered by sport names if given, and order by distance from the user's given location
* @param userLatitude user's latitude coordinate
* @param sportNames optional, list of sport names to filter events by. If null, all events are
* returned. Sport names are not case-sensitive.
* @param placeName optional, substring to search name of the place by. If null all places are
* returned. It is not case-sensitive.
* @param now date and time to search events by. Only events starting after this date and time are
* returned.
* @param pageable contains the page and size
* @return list of events filtered by sport names if given, and order by distance from the user's
* given location
*/
@Query(nativeQuery = true, value =
"SELECT e.id, e.date_start, e.date_end, e.min_elo, e.max_elo, e.title, e.is_rank_updated, e.sport_id, e.place_id "
+ "FROM events e "
+ "JOIN sports s ON e.sport_id = s.id "
+ "JOIN places p ON e.place_id = p.id "
+ "WHERE (:sportNames IS NULL OR LOWER(s.name) IN(:sportNames)) "
+ "ORDER BY ( "
+ " 6371 * acos( " // Haversine distance calculation
+ " cos(radians(p.latitude)) * cos(radians(:latitude)) * "
+ " cos(radians(:longitude) - radians(p.longitude)) + "
+ " sin(radians(p.latitude)) * sin(radians(:latitude))) "
+ " ) ASC;")
List<Event> findNearbyEvents(@Param("longitude") final double userLongitude,
@Param("latitude") final double userLatitude,
@Param("sportNames") final List<String> sportNames,
Pageable pageable);
@Query(
nativeQuery = true,
value =
"SELECT e.id, e.date_start, e.date_end, e.min_elo, e.max_elo, e.title, e.is_rank_updated, e.sport_id, e.place_id "
+ "FROM events e "
+ "JOIN sports s ON e.sport_id = s.id "
+ "JOIN places p ON e.place_id = p.id "
+ "WHERE (LOWER(p.name) LIKE %:placeName%) AND (:sportNames IS NULL OR LOWER(s.name) IN(:sportNames)) AND (e.date_start > :now)"
+ "ORDER BY ( "
+ " 6371 * acos( " // Haversine distance calculation
+ " cos(radians(p.latitude)) * cos(radians(:latitude)) * "
+ " cos(radians(:longitude) - radians(p.longitude)) + "
+ " sin(radians(p.latitude)) * sin(radians(:latitude))) "
+ " ) ASC;")
List<Event> findNearbyEvents(
@Param("longitude") final double userLongitude,
@Param("latitude") final double userLatitude,
@Param("sportNames") final List<String> sportNames,
@Param("placeName") final String placeName,
@Param("now") final LocalDateTime now,
Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ public List<EventDTO> getNearbyEvents(RequestEventDTO requestEventDTO, final Pag
requestEventDTO.getLongitude(),
requestEventDTO.getLatitude(),
sportNamesWithLowerCase,
requestEventDTO.getPlaceName() == null
? ""
: requestEventDTO.getPlaceName().toLowerCase(),
LocalDateTime.now(),
pageable);

return events.stream().map(eventMapper::convertEventToEventDTO).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function HostEventComponent() {
undefined,
undefined,
undefined,
undefined,
999,
)
if (!Array.isArray(response)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class EventsControllerService {
* @param sportsName
* @param longitude
* @param latitude
* @param placeName
* @param page Zero-based page index (0..N)
* @param size The size of the page to be returned
* @param sort Sorting criteria in the format: property,(asc|desc). Default sort order is ascending. Multiple sort criteria are supported.
Expand All @@ -112,6 +113,7 @@ export class EventsControllerService {
sportsName?: Array<string>,
longitude?: number,
latitude?: number,
placeName?: string,
page?: number,
size: number = 20,
sort?: Array<string>,
Expand All @@ -123,6 +125,7 @@ export class EventsControllerService {
'sportsName': sportsName,
'longitude': longitude,
'latitude': latitude,
'placeName': placeName,
'page': page,
'size': size,
'sort': sort,
Expand Down
27 changes: 11 additions & 16 deletions frontend/sportsmatch-app/src/pages/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function MainPage() {
selectedSports,
0,
0,
searchQuery,
page,
size,
)
Expand All @@ -70,7 +71,7 @@ export default function MainPage() {
}
// call the method
fetchData()
}, [selectedSports, page])
}, [selectedSports, page, searchQuery])

// handle join event pop up after cliking on the event
const handleEventSelection = (e: EventDTO) => {
Expand Down Expand Up @@ -140,21 +141,15 @@ export default function MainPage() {
{filteredEvent.length === 0 ? (
<LoadingSpinner />
) : (
filteredEvent
.filter((e) =>
e.placeDTO.name
.toLowerCase()
.includes(searchQuery.toLowerCase()),
)
.map((event, index) => (
<div
className="nearby-events"
key={index}
onClick={() => handleEventSelection(event)}
>
<SportEvent event={event} />
</div>
))
filteredEvent.map((event, index) => (
<div
className="nearby-events"
key={index}
onClick={() => handleEventSelection(event)}
>
<SportEvent event={event} />
</div>
))
)}
</div>
</div>
Expand Down

0 comments on commit adcd6b5

Please sign in to comment.