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

Replaced attendance object with transaction #64

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pecacm.backend.constants.Constants;
import com.pecacm.backend.entities.Event;
import com.pecacm.backend.enums.Branch;
import com.pecacm.backend.enums.EventRole;
import com.pecacm.backend.exception.AcmException;
import com.pecacm.backend.model.EndEventDetails;
import com.pecacm.backend.services.EventService;
Expand Down Expand Up @@ -76,11 +77,11 @@ public ResponseEntity<List<Event>> getEventsByBranch(@PathVariable Branch branch

@GetMapping("/user/{userId}")
@PreAuthorize(Constants.HAS_ROLE_MEMBER_AND_ABOVE)
public ResponseEntity<List<Event>> getUserEventsByRole(@PathVariable Integer userId, @RequestParam("role") @Nullable String role) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldnt it be better to get emai from jwt instead of passing user id in url ? @harshjohar 🤔
Rest lgtm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, actually better idea

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed userId from path

if (role == null){
return ResponseEntity.ok(eventService.getUserEvents(userId));
public ResponseEntity<List<Event>> getUserEventsByRole(@PathVariable Integer userId, @RequestParam("role") @Nullable EventRole eventRole) {
if (eventRole == null){
return ResponseEntity.ok(eventService.getUserEventsByRole(userId, EventRole.PARTICIPANT));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to #51 if event role is not provided all events should be returned

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now returning all events

}
return ResponseEntity.ok(eventService.getUserEventsByRole(userId, role));
return ResponseEntity.ok(eventService.getUserEventsByRole(userId, eventRole));
}

@PostMapping
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/com/pecacm/backend/entities/Attendance.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.pecacm.backend.repository;

import com.pecacm.backend.entities.Transaction;
import com.pecacm.backend.enums.EventRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TransactionRepository extends JpaRepository<Transaction, Integer> {
List<Transaction> findByUserIdAndRole(Integer userId, EventRole role);
}
18 changes: 3 additions & 15 deletions src/main/java/com/pecacm/backend/services/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
import com.pecacm.backend.enums.EventRole;
import com.pecacm.backend.exception.AcmException;
import com.pecacm.backend.model.EndEventDetails;
import com.pecacm.backend.repository.AttendanceRepository;
import com.pecacm.backend.repository.EventRepository;
import com.pecacm.backend.repository.TransactionRepository;
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;
Expand All @@ -28,13 +24,11 @@
public class EventService {

private final EventRepository eventRepository;
private final AttendanceRepository attendanceRepository;
private final UserRepository userRepository;
private final TransactionRepository transactionRepository;

public EventService(EventRepository eventRepository, AttendanceRepository attendanceRepository, UserRepository userRepository, TransactionRepository transactionRepository) {
public EventService(EventRepository eventRepository, UserRepository userRepository, TransactionRepository transactionRepository) {
this.eventRepository = eventRepository;
this.attendanceRepository = attendanceRepository;
this.userRepository = userRepository;
this.transactionRepository = transactionRepository;
}
Expand Down Expand Up @@ -65,19 +59,13 @@ 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()));
return events;
}

public List<Event> getUserEventsByRole(Integer userId, String role) {
public List<Event> getUserEventsByRole(Integer userId, EventRole eventRole) {
Optional<User> user = userRepository.findById(userId);
List<Event> events = new ArrayList<>();
if (user.isEmpty()) {
throw new AcmException(ErrorConstants.USER_NOT_FOUND, HttpStatus.NOT_FOUND);
}
attendanceRepository.findByUserIdAndRole(userId, role).forEach(attendance -> events.add(attendance.getEvent()));
transactionRepository.findByUserIdAndRole(userId, eventRole).forEach(transaction -> events.add(transaction.getEvent()));
return events;
}

Expand Down