Skip to content

Commit

Permalink
SMA-36: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejFrnka committed Apr 13, 2024
2 parents cc598ab + 4e1eff0 commit 95c482c
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.sportsmatch.models.Event;
import com.sportsmatch.services.EventService;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ResponseEntity<?> deleteEvent(@PathVariable("id") Long id) {
}

@GetMapping("/upcoming-events")
public ResponseEntity<?> getUpcomingEvents(@RequestBody List<Long> sportsIds) {
public ResponseEntity<?> getUpcomingEvents(@RequestParam List<Long> sportsIds) {
List<EventDTO> listOfEvents = eventService.getEventsBySports(sportsIds);
return ResponseEntity.ok().body(listOfEvents);
}
Expand All @@ -57,7 +58,7 @@ public ResponseEntity<?> getUpcomingEvents(@RequestBody List<Long> sportsIds) {
* @return a list of finished EventHistoryDTO of the logged-in user
*/
@GetMapping("/event-history")
public List<EventHistoryDTO> getEventsHistory(final Pageable pageable) {
public List<EventHistoryDTO> getEventsHistory(@ParameterObject final Pageable pageable) {
return eventService.getEventsHistory(pageable);
}

Expand All @@ -70,7 +71,7 @@ public List<EventHistoryDTO> getEventsHistory(final Pageable pageable) {
* @return a list of Events sorted by distance from the given location. User can filter by sports.
*/
@GetMapping("/nearby")
public List<EventDTO> getNearbyEvents(@RequestBody RequestEventDTO requestEventDTO,
public List<EventDTO> getNearbyEvents(@RequestParam RequestEventDTO requestEventDTO,
final Pageable pageable) {
return eventService.getNearbyEvents(requestEventDTO, pageable);
}
Expand All @@ -84,4 +85,14 @@ public ResponseEntity<?> joinEvent(@PathVariable("id") Long id) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

/**
* This endpoint returns the upcoming matches of the logged-in user.
*
* @return a list of logged-in user's upcoming EventDTOs ordered by date ascending
*/
@GetMapping("/upcoming-matches")
public List<EventDTO> getUpcomingMatches(@ParameterObject final Pageable pageable) {
return eventService.getUsersUpcomingEvents(pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class EventDTO {
private LocalDateTime dateStart;
@NotNull
private LocalDateTime dateEnd;
@NotBlank
private String location;
@NotNull
private Integer minElo;
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public class SportDTO {
public String emoji;

public String backgroundUImageURL;
public Long id;

public SportDTO(String name, String emoji, String backgroundUImageURL) {
public SportDTO(String name, String emoji, String backgroundUImageURL, Long id) {
this.name = name;
this.emoji = emoji;
this.backgroundUImageURL = backgroundUImageURL;
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public SportDTO toDTO(Sport entity) {
.name(entity.getName())
.emoji(entity.getEmoji())
.backgroundUImageURL(entity.getBackgroundImageURL())
.id(entity.getId())
.build();
}

public Sport toEntity(SportDTO sportDTO) {
return Sport.builder()
.name(sportDTO.getName())
.emoji(sportDTO.getEmoji())
.backgroundImageURL(sportDTO.getBackgroundUImageURL())
.build();
.name(sportDTO.getName())
.emoji(sportDTO.getEmoji())
.backgroundImageURL(sportDTO.getBackgroundUImageURL())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ public interface EventRepository extends JpaRepository<Event, Long> {
/**
* Retrieves events filtered by user and finished status.
*
* @param name the logged user's name to filter events by
* @param now the current time to filter events by
* @param id the logged user's id to filter events by
* @param now the current time to filter events by
* @param pageable pagination information (page, size)
* @return a list of events filtered by user and finished status
*/
// =?1 =?2")
@Query("SELECT ep.event FROM EventPlayer ep WHERE ep.player.name = :name AND ep.event.dateEnd < :now")
@Query("SELECT ep.event FROM EventPlayer ep WHERE ep.player.id = :id AND ep.event.dateEnd < :now")
List<Event> findEventsByUser(
@Param("name") String name,
@Param("now") LocalDateTime now,
Pageable pageable
);
@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")
List<Event> findUpcomingEventsByUser(@Param("id") Long id, @Param("now") LocalDateTime now, Pageable pageable);




/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ public void deleteEventFromDatabase(Event eventById) {
* @return a list of EventHistoryDTOs representing the logged-in user's event history$ ../gradlew clean checkStyleMain -info
*/
public List<EventHistoryDTO> getEventsHistory(final Pageable pageable) {
String loggedUserName = userService.getUserFromContext().getName();
String loggedUserName = userService.getUserFromContext().getName();
Long loggedUserId = userService.getUserFromContext().getId();

return eventRepository.findEventsByUser(loggedUserName, LocalDateTime.now(), pageable)
.stream()
.map(event -> eventMapper.toDTO(event, loggedUserName,
checkScoreMatch(event.getPlayers())))
.collect(Collectors.toList());
}
return eventRepository.findEventsByUser(loggedUserId, LocalDateTime.now(), pageable).stream()
.map(event -> eventMapper.toDTO(event, loggedUserName, checkScoreMatch(event.getPlayers())))
.collect(Collectors.toList());
}

/**
* Returns the checked status of the match (check the score is matching or missing).
Expand Down Expand Up @@ -206,4 +205,18 @@ public List<EventDTO> getNearbyEvents(RequestEventDTO requestEventDTO,
return events.stream().map(eventMapper::convertEventToEventDTO)
.collect(Collectors.toList());
}

/**
* Returns the upcoming matches of the logged-in user.
*
* @return a list of logged-in user's upcoming EventDTOs ordered by date ascending
*/
public List<EventDTO> getUsersUpcomingEvents(Pageable pageable) {
User loggedUser = userService.getUserFromContext();
return eventRepository
.findUpcomingEventsByUser(loggedUser.getId(), LocalDateTime.now(), pageable)
.stream()
.map(eventMapper::convertEventToEventDTO)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.sportsmatch.mappers.SportMapper;
import com.sportsmatch.models.Sport;
import com.sportsmatch.repositories.SportRepository;
import com.sportsmatch.services.SportServiceImp;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -46,8 +45,8 @@ void getAllSportsShouldReturnAllSportsWhenRequired() {
List<Sport> sports = Arrays.asList(sport1, sport2);
Page<Sport> sportsPage = new PageImpl<>(sports, pageable, sports.size());

SportDTO sportDTO1 = new SportDTO("Football", footballEmoji, "urlFootball");
SportDTO sportDTO2 = new SportDTO("Basketball", basketballEmoji, "urlBasketball");
SportDTO sportDTO1 = new SportDTO("Football", footballEmoji, "urlFootball", 1L);
SportDTO sportDTO2 = new SportDTO("Basketball", basketballEmoji, "urlBasketball", 2L);

when(sportMapper.toDTO(sport1)).thenReturn(sportDTO1);
when(sportMapper.toDTO(sport2)).thenReturn(sportDTO2);
Expand Down
33 changes: 16 additions & 17 deletions frontend/sportsmatch-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BrowserRouter, Route, Routes } from 'react-router-dom'
import './App.css'
import Home from './pages/Home'
import Test from './pages/Test'
import Login from './pages/Login'
import Signup from './pages/Signup'
import Wrapper from './pages/AppWrapper'
Expand All @@ -12,31 +11,37 @@ import PrivateRoute from './components/PrivateRoute'
import NotFound from './pages/NotFound'
import { OpenAPI } from './generated/api'
import UserInfo from './pages/UserInfo'
import HostEvent from './pages/HostEvent'
import UserRating from './pages/UserRating'
import HostEvent from './pages/HostEvent'

function App() {
OpenAPI.TOKEN = localStorage.getItem('token')!

return (
<BrowserRouter>
<Routes>
<Route
path="/user/:id/ratings"
element={<Wrapper child={<UserRating />} />}
/>
{/* public routes */}
<Route path="/login" element={<Wrapper child={<Login />} />} />
<Route path="/signup" element={<Wrapper child={<Signup />} />} />
<Route
path="/allSports"
path="/"
element={
<Wrapper child={<Index />} acitvateCheckRatingModal={true} />
}
/>
<Route
path="/all-sports"
element={<Wrapper child={<AllSportsList />} />}
/>
<Route path="/*" element={<NotFound />}></Route>

{/* private routes */}
<Route element={<PrivateRoute />}>
<Route path="/app" element={<Wrapper child={<Home />} />} />
<Route path="/user-info" element={<Wrapper child={<UserInfo />} />} />
<Route
path="/index"
element={
<Wrapper child={<Index />} acitvateCheckRatingModal={true} />
}
path="/user/:id/ratings"
element={<Wrapper child={<UserRating />} />}
/>
<Route path="/user" element={<UserPage />}></Route>
<Route path="/user-info" element={<Wrapper child={<UserInfo />} />} />
Expand All @@ -45,12 +50,6 @@ function App() {
element={<Wrapper child={<HostEvent />} />}
/>
</Route>
<Route path="/test" element={<Test />}>
<Route index element={<Test />} />
<Route path=":testId" element={<Test />} />
</Route>
<Route path="/" element={<Wrapper child={<Home />} />} />
<Route path="/*" element={<NotFound />}></Route>
</Routes>
</BrowserRouter>
)
Expand Down
51 changes: 48 additions & 3 deletions frontend/sportsmatch-app/src/components/EventHistoryItem.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react'
import { EventHistoryDTO } from '../generated/api'
import '../styles/EventHistoryItem.css'
import Avatar from './Avatar'
Expand All @@ -7,12 +8,44 @@ interface EventHistoryProps {
}

function EventHistoryItem({ eventHistoryDTO }: EventHistoryProps) {
const [eventStatus, setEventStatus] = useState<string>('')

useEffect(() => {
const matchResult =
eventHistoryDTO.userScore! > eventHistoryDTO.opponentScore!
? 'VICTORY'
: eventHistoryDTO.userScore! == eventHistoryDTO.opponentScore!
? 'DRAW'
: 'DEFEAT'
if (
eventHistoryDTO.status?.includes(EventHistoryDTO.status.MATCH) &&
!eventHistoryDTO.status?.includes(EventHistoryDTO.status.MISMATCH)
) {
setEventStatus(matchResult)
} else if (
eventHistoryDTO.status?.includes(
EventHistoryDTO.status.WAITING_FOR_RATING,
)
) {
const result = matchResult + ' (UNCONFIRMED)'
setEventStatus(result)
} else if (
eventHistoryDTO.status?.includes(EventHistoryDTO.status.MISMATCH)
) {
setEventStatus('SCORE MISMATCH')
}
}, [
eventHistoryDTO.opponentScore,
eventHistoryDTO.status,
eventHistoryDTO.userScore,
])

return (
<>
<div className="container-fluid">
<div className={'row'}>
<div className="event-history-item">
<div className="match-status">VICTORY (uncorfirmed)</div>
<div className="match-status">{eventStatus}</div>
<div className="user-side">
<div className="profile">
<div className="user-avatar">
Expand All @@ -21,15 +54,27 @@ function EventHistoryItem({ eventHistoryDTO }: EventHistoryProps) {
<div className="user-name">You</div>
</div>
<div className="user-score score">
<span>{eventHistoryDTO.userScore}</span>
<span>
{eventHistoryDTO.status?.includes(
EventHistoryDTO.status.MISMATCH,
)
? '?'
: eventHistoryDTO.userScore}
</span>
</div>
</div>
<div className="colon">
<span>:</span>
</div>
<div className="opponent-side">
<div className="opponent-score score">
<span>{eventHistoryDTO.opponentScore}</span>
<span>
{eventHistoryDTO.status?.includes(
EventHistoryDTO.status.MISMATCH,
)
? '?'
: eventHistoryDTO.opponentScore}
</span>
</div>
<div className="profile">
<div className="opponent-avatar">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function JoinEventComponent(p: JoinEventProps) {
<p className="text-center">
You want to join <b>{p.event.sport}</b>
{' at ' +
p.event.location +
p.event.placeDTO?.name +
' on ' +
date.toLocaleDateString('en', { month: 'long' }) +
' ' +
Expand Down Expand Up @@ -90,7 +90,7 @@ export default function JoinEventComponent(p: JoinEventProps) {
<div className="col-12">
<p className="join-event-message">
Your rank is too low/high to join <b>{p.event.sport}</b> at{' '}
{p.event.location}
{p.event.placeDTO?.name}
</p>
</div>
</div>
Expand Down
Loading

0 comments on commit 95c482c

Please sign in to comment.