feat: create /accept endpoint to accept invitations
This commit is contained in:
parent
cf373c9be3
commit
a5215b126b
@ -90,4 +90,25 @@ public class InvitationController {
|
||||
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/accept")
|
||||
@Operation(summary = "Accept invitation", description = "Accept an invitation by ID")
|
||||
public ResponseEntity<ApiResponse<Void>> acceptInvitation(
|
||||
@RequestParam Long id,
|
||||
Authentication authentication) {
|
||||
|
||||
if (id == null || id <= 0) {
|
||||
return ResponseEntity.badRequest().body(ApiResponse.error("Valid invitation ID is required"));
|
||||
}
|
||||
|
||||
try {
|
||||
String userEmail = authentication.getName();
|
||||
invitationService.acceptInvitation(id, userEmail);
|
||||
return ResponseEntity.ok(new ApiResponse<>(true));
|
||||
} catch (RuntimeException e) {
|
||||
return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AttendeeRepository extends JpaRepository<Attendee, Long> {
|
||||
|
||||
boolean existsByInvitationIdAndUserId(Long invitationId, Long userId);
|
||||
}
|
||||
@ -2,9 +2,11 @@ package online.wesal.wesal.service;
|
||||
|
||||
import online.wesal.wesal.dto.CreateInvitationRequest;
|
||||
import online.wesal.wesal.dto.InvitationResponse;
|
||||
import online.wesal.wesal.entity.Attendee;
|
||||
import online.wesal.wesal.entity.Invitation;
|
||||
import online.wesal.wesal.entity.Tag;
|
||||
import online.wesal.wesal.entity.User;
|
||||
import online.wesal.wesal.repository.AttendeeRepository;
|
||||
import online.wesal.wesal.repository.InvitationRepository;
|
||||
import online.wesal.wesal.repository.TagRepository;
|
||||
import online.wesal.wesal.repository.UserRepository;
|
||||
@ -28,6 +30,9 @@ public class InvitationService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private AttendeeRepository attendeeRepository;
|
||||
|
||||
@Transactional
|
||||
public InvitationResponse createInvitation(CreateInvitationRequest request, String userEmail) {
|
||||
User creator = userRepository.findByEmail(userEmail)
|
||||
@ -63,6 +68,33 @@ public class InvitationService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void acceptInvitation(Long invitationId, String userEmail) {
|
||||
User user = userRepository.findByEmail(userEmail)
|
||||
.orElseThrow(() -> new RuntimeException("Authentication error. Please log in again"));
|
||||
|
||||
Invitation invitation = invitationRepository.findById(invitationId)
|
||||
.orElseThrow(() -> new RuntimeException("This invitation does not exist"));
|
||||
|
||||
if (invitation.getCreator().getId().equals(user.getId())) {
|
||||
throw new RuntimeException("You cannot accept your own invitation");
|
||||
}
|
||||
|
||||
if (invitation.getCurrentAttendees() >= invitation.getMaxParticipants()) {
|
||||
throw new RuntimeException("This invitation is already full");
|
||||
}
|
||||
|
||||
if (attendeeRepository.existsByInvitationIdAndUserId(invitationId, user.getId())) {
|
||||
throw new RuntimeException("You have already accepted this invitation");
|
||||
}
|
||||
|
||||
Attendee attendee = new Attendee(invitation, user);
|
||||
attendeeRepository.save(attendee);
|
||||
|
||||
invitation.setCurrentAttendees(invitation.getCurrentAttendees() + 1);
|
||||
invitationRepository.save(invitation);
|
||||
}
|
||||
|
||||
private InvitationResponse mapToResponse(Invitation invitation) {
|
||||
InvitationResponse response = new InvitationResponse();
|
||||
response.setId(invitation.getId());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user