feat: add a categorised main /all invitations

This commit is contained in:
sBubshait 2025-07-22 09:56:45 +03:00
parent a5215b126b
commit 5f6f3a5f0a
4 changed files with 85 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import online.wesal.wesal.dto.ApiResponse; import online.wesal.wesal.dto.ApiResponse;
import online.wesal.wesal.dto.CategorizedInvitationsResponse;
import online.wesal.wesal.dto.CreateInvitationRequest; import online.wesal.wesal.dto.CreateInvitationRequest;
import online.wesal.wesal.dto.InvitationResponse; import online.wesal.wesal.dto.InvitationResponse;
import online.wesal.wesal.service.InvitationService; import online.wesal.wesal.service.InvitationService;
@ -80,12 +81,15 @@ public class InvitationController {
} }
} }
@GetMapping("/available") @GetMapping("/all")
@Operation(summary = "Get available invitations", description = "Get all invitations with available space") @Operation(summary = "Get all categorized invitations", description = "Get invitations in three categories: created, accepted, and available")
public ResponseEntity<ApiResponse<List<InvitationResponse>>> getAvailableInvitations() { public ResponseEntity<ApiResponse<CategorizedInvitationsResponse>> getAllCategorizedInvitations(Authentication authentication) {
try { try {
List<InvitationResponse> invitations = invitationService.getAvailableInvitations(); String userEmail = authentication.getName();
return ResponseEntity.ok(ApiResponse.success(invitations)); CategorizedInvitationsResponse response = invitationService.getAllCategorizedInvitations(userEmail);
return ResponseEntity.ok(ApiResponse.success(response));
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later")); return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
} }

View File

@ -0,0 +1,44 @@
package online.wesal.wesal.dto;
import java.util.List;
public class CategorizedInvitationsResponse {
private List<InvitationResponse> created;
private List<InvitationResponse> accepted;
private List<InvitationResponse> available;
public CategorizedInvitationsResponse() {}
public CategorizedInvitationsResponse(List<InvitationResponse> created,
List<InvitationResponse> accepted,
List<InvitationResponse> available) {
this.created = created;
this.accepted = accepted;
this.available = available;
}
public List<InvitationResponse> getCreated() {
return created;
}
public void setCreated(List<InvitationResponse> created) {
this.created = created;
}
public List<InvitationResponse> getAccepted() {
return accepted;
}
public void setAccepted(List<InvitationResponse> accepted) {
this.accepted = accepted;
}
public List<InvitationResponse> getAvailable() {
return available;
}
public void setAvailable(List<InvitationResponse> available) {
this.available = available;
}
}

View File

@ -10,6 +10,12 @@ import java.util.List;
@Repository @Repository
public interface InvitationRepository extends JpaRepository<Invitation, Long> { public interface InvitationRepository extends JpaRepository<Invitation, Long> {
@Query("SELECT i FROM Invitation i WHERE i.currentAttendees < i.maxParticipants ORDER BY i.id ASC") @Query("SELECT i FROM Invitation i WHERE i.currentAttendees < i.maxParticipants AND (i.dateTime IS NULL OR i.dateTime > CURRENT_TIMESTAMP) ORDER BY i.id ASC")
List<Invitation> findAvailableInvitationsOrderByCreationDate(); List<Invitation> findAvailableInvitationsOrderByCreationDate();
@Query("SELECT i FROM Invitation i WHERE i.creator.id = :userId ORDER BY i.id DESC")
List<Invitation> findByCreatorIdOrderByCreationDateDesc(Long userId);
@Query("SELECT i FROM Invitation i JOIN Attendee a ON a.invitation.id = i.id WHERE a.user.id = :userId ORDER BY i.id DESC")
List<Invitation> findAcceptedInvitationsByUserIdOrderByCreationDateDesc(Long userId);
} }

View File

@ -1,5 +1,6 @@
package online.wesal.wesal.service; package online.wesal.wesal.service;
import online.wesal.wesal.dto.CategorizedInvitationsResponse;
import online.wesal.wesal.dto.CreateInvitationRequest; import online.wesal.wesal.dto.CreateInvitationRequest;
import online.wesal.wesal.dto.InvitationResponse; import online.wesal.wesal.dto.InvitationResponse;
import online.wesal.wesal.entity.Attendee; import online.wesal.wesal.entity.Attendee;
@ -68,6 +69,30 @@ public class InvitationService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public CategorizedInvitationsResponse getAllCategorizedInvitations(String userEmail) {
User user = userRepository.findByEmail(userEmail)
.orElseThrow(() -> new RuntimeException("Authentication error. Please log in again"));
List<InvitationResponse> created = invitationRepository.findByCreatorIdOrderByCreationDateDesc(user.getId())
.stream()
.map(this::mapToResponse)
.collect(Collectors.toList());
List<InvitationResponse> accepted = invitationRepository.findAcceptedInvitationsByUserIdOrderByCreationDateDesc(user.getId())
.stream()
.map(this::mapToResponse)
.collect(Collectors.toList());
List<InvitationResponse> available = invitationRepository.findAvailableInvitationsOrderByCreationDate()
.stream()
.filter(invitation -> !invitation.getCreator().getId().equals(user.getId()))
.filter(invitation -> !attendeeRepository.existsByInvitationIdAndUserId(invitation.getId(), user.getId()))
.map(this::mapToResponse)
.collect(Collectors.toList());
return new CategorizedInvitationsResponse(created, accepted, available);
}
@Transactional @Transactional
public void acceptInvitation(Long invitationId, String userEmail) { public void acceptInvitation(Long invitationId, String userEmail) {
User user = userRepository.findByEmail(userEmail) User user = userRepository.findByEmail(userEmail)