Skip to content

Commit

Permalink
Feat:
Browse files Browse the repository at this point in the history
  • Loading branch information
kz44 committed Feb 15, 2024
1 parent c4ea517 commit 6c5dda2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sportsmatch.controllers;

import com.sportsmatch.dtos.EventDTO;
import com.sportsmatch.dtos.EventHistoryDTO;
import com.sportsmatch.models.Event;
import com.sportsmatch.services.EventService;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -56,7 +55,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) {
return eventService.getEventsHistory(pageable);
public ResponseEntity<?> getEventsHistory(final Pageable pageable) {
return ResponseEntity.ok(eventService.getEventsHistory(pageable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sportsmatch.dtos.EventHistoryDTO;
import com.sportsmatch.models.Event;
import com.sportsmatch.models.EventPlayer;
import com.sportsmatch.models.User;
import com.sportsmatch.services.EventService;
import com.sportsmatch.services.UserService;
import org.modelmapper.ModelMapper;
Expand All @@ -16,17 +17,12 @@
@Component
public class EventMapper {

private final UserService userService;
private final EventService eventService;

private final UserMapper userMapper;
private ModelMapper modelMapper;

@Autowired
public EventMapper(ModelMapper modelMapper, UserService userService, EventService eventService, UserMapper userMapper) {
public EventMapper(ModelMapper modelMapper, UserMapper userMapper) {
this.modelMapper = modelMapper;
this.userService = userService;
this.eventService = eventService;
this.userMapper = userMapper;
this.modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
Expand Down Expand Up @@ -55,17 +51,17 @@ public Event convertEventDTOtoEvent(EventDTO eventDTO) {
* @param event to be converted
* @return an EventHistoryDTO based on the given Event
*/
public EventHistoryDTO toDTO(Event event) {
public EventHistoryDTO toDTO(Event event, String loggedUsername, String status) {

// Get the logged-in EventPlayer
EventPlayer loggedPlayer = event.getPlayers().stream()
.filter(p -> p.getPlayer().getName().equals(userService.getUserFromTheSecurityContextHolder().getName()))
.filter(p -> p.getPlayer().getName().equals(loggedUsername))
.findFirst()
.orElse(null);

// Get the opponent EventPlayer
EventPlayer opponentPlayer = event.getPlayers().stream()
.filter(p -> !p.getPlayer().getName().equals(userService.getUserFromTheSecurityContextHolder().getName()))
.filter(p -> !p.getPlayer().getName().equals(loggedUsername))
.findFirst()
.orElse(null);

Expand All @@ -75,7 +71,7 @@ public EventHistoryDTO toDTO(Event event) {
.opponentScore((opponentPlayer != null) ? opponentPlayer.getMyScore() : null)
.opponent((opponentPlayer != null) ? userMapper.toDTO(opponentPlayer.getPlayer()) : null)
.dateOfTheMatch(event.getDateEnd())
.status(eventService.checkScoreMatch(event.getPlayers()))
.status(status)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.sportsmatch.repositories;

import com.sportsmatch.models.Event;
import com.sportsmatch.models.EventPlayer;
import com.sportsmatch.models.User;
import org.springframework.data.domain.Pageable;
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.time.LocalDateTime;
Expand All @@ -21,12 +23,17 @@ public interface EventRepository extends JpaRepository<Event, Long> {
/**
* Retrieves events filtered by user and finished status.
*
* @param user the user to filter events by
* @param name the logged user's name 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
*/
@Query("SELECT e FROM Event e JOIN e.players ep WHERE ep.player = ?1 AND e.dateEnd <= ?2")
List<Event> findEventsByUser(User user, LocalDateTime now, Pageable pageable);
// =?1 =?2")

@Query("SELECT ep.event FROM EventPlayer ep WHERE ep.player.name = :name AND ep.event.dateEnd < :now")
List<Event> findEventsByUser(
@Param("name") String name,
@Param("now") LocalDateTime now,
Pageable pageable
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class EventService {
private SportRepository sportRepository;
private EventPlayerRepository eventPlayerRepository;


public Event getEventById(Long id) {
return eventRepository
.findEventById(id)
Expand Down Expand Up @@ -108,9 +109,11 @@ public void deleteEventFromDatabase(Event eventById) {
* @return a list of EventHistoryDTOs representing the logged-in user's event history
*/
public List<EventHistoryDTO> getEventsHistory(final Pageable pageable) {
return eventRepository.findEventsByUser(userService.getUserFromTheSecurityContextHolder(), LocalDateTime.now(), pageable)
String loggedUserName = userService.getUserFromTheSecurityContextHolder().getName();

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

Expand Down

0 comments on commit 6c5dda2

Please sign in to comment.