Skip to content

Commit

Permalink
SMA-22: Add endpoint that returns upcoming events (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johna91 authored Feb 1, 2024
1 parent 5f8d217 commit 91c3322
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public ResponseEntity<?> register(
@PostMapping("/login")
@Tag(name = "Login")
@Operation(
summary = "Login user",
description = "Login a user by providing their email and username.")
summary = "Login user",
description = "Login a user by providing their email and username.")
public ResponseEntity<?> login(
@RequestBody @Valid AuthRequestDTO authRequestDTO, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/event")
public class EventsController {
Expand Down Expand Up @@ -37,4 +39,10 @@ public ResponseEntity<?> deleteEvent(@PathVariable("id") Long id) {
eventService.deleteEventFromDatabase(eventById);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@GetMapping("/upcoming-events")
public ResponseEntity<?> getUpcomingEvents(@RequestBody List<Long> sportsIds) {
List<EventDTO> listOfEvents = eventService.getEventsBySports(sportsIds);
return ResponseEntity.ok().body(listOfEvents);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.sportsmatch.repositories;

import com.sportsmatch.models.Event;

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

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EventRepository extends JpaRepository<Event, Long> {

Optional<Event> findEventById(Long id);

List<Event> findAllBySportIdIn(List<Long> sportId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public List<EventDTO> getAllEvents() {
return eventDTOList;
}

public List<EventDTO> getEventsBySports(List<Long> sportsIds) {
List<EventDTO> eventDTOList = new ArrayList<>();
List<Event> eventListBySport = eventRepository.findAllBySportIdIn(sportsIds);
for (Event event : eventListBySport) {
eventDTOList.add(getEventDTObyEventId(event.getId()));
}
return eventDTOList;
}

public EventPlayer addPlayerToEvent(Long playerId, Long eventId) {
EventPlayer eventPlayer = new EventPlayer();
eventPlayer.setPlayer(
Expand Down

0 comments on commit 91c3322

Please sign in to comment.