Skip to content

Commit

Permalink
Merge pull request #53 from PEC-CSS/feat-next-event
Browse files Browse the repository at this point in the history
Feat next event API
  • Loading branch information
harshjohar authored Oct 10, 2023
2 parents 35888ed + 727adca commit 8c7447f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/com/pecacm/backend/controllers/EventsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import com.pecacm.backend.entities.Event;
import com.pecacm.backend.model.EndEventDetails;
import com.pecacm.backend.services.EventService;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@RestController
Expand Down Expand Up @@ -90,4 +95,12 @@ public ResponseEntity<Void> endEvent(@PathVariable Integer eventId, @RequestBody
eventService.endEvent(eventId, endEventDetails);
return ResponseEntity.ok().build();
}

@GetMapping("/next")
@PreAuthorize(Constants.HAS_ANY_ROLE)
public ResponseEntity<Event> nextEvent(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @Nonnull LocalDate date) {
LocalDateTime currDateTime = LocalDateTime.of(date, LocalTime.of(0, 0));
Event nextEvent = eventService.getNextEvent(currDateTime);
return ResponseEntity.ok(nextEvent);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.pecacm.backend.exception;

import com.pecacm.backend.response.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@ControllerAdvice
public class ExceptionResponseHandler {
Expand All @@ -12,4 +14,11 @@ public class ExceptionResponseHandler {
public ResponseEntity<ErrorResponse> handleAcmException(AcmException acmException) {
return ResponseEntity.status(acmException.getStatus()).body(new ErrorResponse(acmException.getStatus().getReasonPhrase(), acmException.getMessage()));
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ErrorResponse> handleConversionException(MethodArgumentTypeMismatchException ex) {
String errorMessage = "Invalid parameter value. " + ex.getName() + " should be of type " + ex.getRequiredType();
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.getReasonPhrase(), errorMessage);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.pecacm.backend.entities.Event;
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;
import java.util.List;

@Repository
public interface EventRepository extends JpaRepository<Event, Integer> {
List<Event> findByBranch(String branch);

List<Event> findAllByEndedFalse();

@Query("SELECT e from Event e WHERE e.startDate > :currDateTime ORDER BY e.startDate ASC LIMIT 1")
Event getNearestEvent(@Param("currDateTime") LocalDateTime currDate);
}
5 changes: 4 additions & 1 deletion src/main/java/com/pecacm/backend/services/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public EventService(EventRepository eventRepository, AttendanceRepository attend
}

// TODO : change all GET events to pageable repositories

public List<Event> getAllEvents() {
return new ArrayList<>(eventRepository.findAll());
}
Expand Down Expand Up @@ -167,4 +166,8 @@ public void endEvent(Integer eventId, EndEventDetails endEventDetails) {
eventRepository.save(event);

}

public Event getNextEvent(LocalDateTime currDateTime) {
return eventRepository.getNearestEvent(currDateTime);
}
}

0 comments on commit 8c7447f

Please sign in to comment.