feat: create /available endpoint for invitations to get all available invitations
This commit is contained in:
parent
ff87f161df
commit
cf373c9be3
@ -10,6 +10,7 @@ import online.wesal.wesal.service.InvitationService;
|
|||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -78,4 +79,15 @@ public class InvitationController {
|
|||||||
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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/available")
|
||||||
|
@Operation(summary = "Get available invitations", description = "Get all invitations with available space")
|
||||||
|
public ResponseEntity<ApiResponse<List<InvitationResponse>>> getAvailableInvitations() {
|
||||||
|
try {
|
||||||
|
List<InvitationResponse> invitations = invitationService.getAvailableInvitations();
|
||||||
|
return ResponseEntity.ok(ApiResponse.success(invitations));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -2,8 +2,14 @@ package online.wesal.wesal.repository;
|
|||||||
|
|
||||||
import online.wesal.wesal.entity.Invitation;
|
import online.wesal.wesal.entity.Invitation;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
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")
|
||||||
|
List<Invitation> findAvailableInvitationsOrderByCreationDate();
|
||||||
}
|
}
|
||||||
@ -12,7 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class InvitationService {
|
public class InvitationService {
|
||||||
@ -54,6 +56,13 @@ public class InvitationService {
|
|||||||
.map(this::mapToResponse);
|
.map(this::mapToResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<InvitationResponse> getAvailableInvitations() {
|
||||||
|
return invitationRepository.findAvailableInvitationsOrderByCreationDate()
|
||||||
|
.stream()
|
||||||
|
.map(this::mapToResponse)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
private InvitationResponse mapToResponse(Invitation invitation) {
|
private InvitationResponse mapToResponse(Invitation invitation) {
|
||||||
InvitationResponse response = new InvitationResponse();
|
InvitationResponse response = new InvitationResponse();
|
||||||
response.setId(invitation.getId());
|
response.setId(invitation.getId());
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user