Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added api to retrieve events between 2 respective dates #54

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/main/java/com/pecacm/backend/controllers/EventsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.pecacm.backend.constants.Constants;
import com.pecacm.backend.entities.Event;
import com.pecacm.backend.enums.Branch;
import com.pecacm.backend.exception.AcmException;
import com.pecacm.backend.model.EndEventDetails;
import com.pecacm.backend.services.EventService;
import jakarta.annotation.Nonnull;
Expand Down Expand Up @@ -31,9 +33,22 @@ public EventsController(EventService eventService) {

@GetMapping
@PreAuthorize(Constants.HAS_ANY_ROLE)
public ResponseEntity<List<Event>> getAllEvents() {
public ResponseEntity<List<Event>> getAllEvents(@RequestParam @Nullable LocalDate eventsFrom, @RequestParam @Nullable LocalDate eventsTill) {

if (eventsFrom==null){
eventsFrom = LocalDate.now().minusYears(99);
}
if (eventsTill==null){
eventsTill = LocalDate.now();
}

if (eventsFrom.isAfter(eventsTill)) {
throw new AcmException("eventsFrom Date must be <= eventsTill Date", HttpStatus.BAD_REQUEST);
}
// TODO : Return pageable response
List<Event> events = eventService.getAllEvents();

List<Event> events = eventService.getEventsBetweenTwoTimestamps(eventsFrom, eventsTill);

return ResponseEntity.ok(events);
}

Expand All @@ -53,7 +68,7 @@ public ResponseEntity<Event> getSingleEvent(@PathVariable Integer eventId){

@GetMapping("/branches/{branch}")
@PreAuthorize(Constants.HAS_ANY_ROLE)
public ResponseEntity<List<Event>> getEventsByBranch(@PathVariable String branch){
public ResponseEntity<List<Event>> getEventsByBranch(@PathVariable Branch branch){
// TODO : Return pageable response
List<Event> events = eventService.getEventsByBranch(branch);
return ResponseEntity.ok(events);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.pecacm.backend.repository;

import com.pecacm.backend.entities.Event;
import com.pecacm.backend.enums.Branch;
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> findByBranch(Branch 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);
List<Event> findAllByStartDateGreaterThanEqualAndEndDateLessThanEqual(LocalDateTime startDate, LocalDateTime endDate);
}
30 changes: 16 additions & 14 deletions src/main/java/com/pecacm/backend/services/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.pecacm.backend.entities.Event;
import com.pecacm.backend.entities.Transaction;
import com.pecacm.backend.entities.User;
import com.pecacm.backend.enums.Branch;
import com.pecacm.backend.enums.EventRole;
import com.pecacm.backend.exception.AcmException;
import com.pecacm.backend.model.EndEventDetails;
Expand All @@ -13,10 +14,13 @@
import com.pecacm.backend.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.cglib.core.Predicate;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

Expand All @@ -28,7 +32,6 @@ public class EventService {
private final UserRepository userRepository;
private final TransactionRepository transactionRepository;

@Autowired
public EventService(EventRepository eventRepository, AttendanceRepository attendanceRepository, UserRepository userRepository, TransactionRepository transactionRepository) {
this.eventRepository = eventRepository;
this.attendanceRepository = attendanceRepository;
Expand All @@ -37,8 +40,13 @@ public EventService(EventRepository eventRepository, AttendanceRepository attend
}

// TODO : change all GET events to pageable repositories
public List<Event> getAllEvents() {
return new ArrayList<>(eventRepository.findAll());

public List<Event> getEventsBetweenTwoTimestamps(LocalDate eventsFrom, LocalDate eventsTill) {
return eventRepository
.findAllByStartDateGreaterThanEqualAndEndDateLessThanEqual(
eventsFrom.atStartOfDay(),
eventsTill.plusDays(1).atStartOfDay()
);
}

public List<Event> getOngoingEvents() {
Expand All @@ -53,15 +61,13 @@ public Event getSingleEvent(Integer eventId) {
return event.get();
}

public List<Event> getEventsByBranch(String branch) {
public List<Event> getEventsByBranch(Branch branch) {
return eventRepository.findByBranch(branch);
}

public List<Event> getUserEvents(Integer userId) {
List<Event> events = new ArrayList<>();
attendanceRepository.findByUserId(userId).forEach(
attendance -> events.add(attendance.getEvent())
);
attendanceRepository.findByUserId(userId).forEach(attendance -> events.add(attendance.getEvent()));
return events;
}

Expand All @@ -71,9 +77,7 @@ public List<Event> getUserEventsByRole(Integer userId, String role) {
if (user.isEmpty()) {
throw new AcmException(ErrorConstants.USER_NOT_FOUND, HttpStatus.NOT_FOUND);
}
attendanceRepository.findByUserIdAndRole(userId, role).forEach(
attendance -> events.add(attendance.getEvent())
);
attendanceRepository.findByUserIdAndRole(userId, role).forEach(attendance -> events.add(attendance.getEvent()));
return events;
}

Expand Down Expand Up @@ -105,11 +109,9 @@ public void deleteEvent(Integer eventId) {
@Transactional
public void endEvent(Integer eventId, EndEventDetails endEventDetails) {

Event event = eventRepository.findById(eventId).orElseThrow(() ->
new AcmException("Event not found", HttpStatus.NOT_FOUND)
);
Event event = eventRepository.findById(eventId).orElseThrow(() -> new AcmException("Event not found", HttpStatus.NOT_FOUND));

if(event.isEnded()) {
if (event.isEnded()) {
throw new AcmException("Event has already ended", HttpStatus.BAD_REQUEST);
}

Expand Down