|
1 | 1 | use serde::{Deserialize, Serialize}; |
2 | 2 | use utoipa::IntoParams; |
3 | 3 |
|
| 4 | +use crate::types::api::error::APIError; |
| 5 | + |
4 | 6 | /// Query parameters for fetching and filtering events. |
5 | 7 | #[derive(Debug, Serialize, Deserialize, IntoParams)] |
6 | 8 | #[serde(deny_unknown_fields)] |
7 | 9 | pub(crate) struct EventQuery { |
8 | | - /// Opaque cursor for pagination. If provided, all filter params are ignored. |
| 10 | + /// Opaque cursor for pagination - returned in the endpoint response. |
| 11 | + /// This parameter is mutually exclusive with all other parameters, |
| 12 | + /// and will return bad request if any other parameter is set. |
9 | 13 | #[param(required = false, nullable = false)] |
10 | 14 | #[serde(skip_serializing_if = "Option::is_none")] |
11 | 15 | pub next_cursor: Option<String>, |
@@ -70,17 +74,48 @@ pub(crate) struct EventQuery { |
70 | 74 | pub include_args: bool, |
71 | 75 | } |
72 | 76 |
|
73 | | -#[derive(Debug, Serialize, Deserialize)] |
74 | | -pub(crate) struct EventCursorPosition { |
75 | | - pub(crate) block_number: u64, |
76 | | - pub(crate) block_hash_hex: String, |
77 | | - pub(crate) index: u32, |
78 | | -} |
79 | | - |
80 | | -#[derive(Debug, Serialize, Deserialize)] |
81 | | -pub(crate) struct EventCursorPayload { |
82 | | - pub(crate) cursor_position: EventCursorPosition, |
83 | | - pub(crate) query: EventQuery, |
| 77 | +impl EventQuery { |
| 78 | + pub(crate) fn validate_next_cursor_mutually_exclusive(&self) -> Result<(), APIError> { |
| 79 | + if self.next_cursor.is_some() { |
| 80 | + let mut other_fields = Vec::new(); |
| 81 | + if self.page_size.is_some() { |
| 82 | + other_fields.push("page_size"); |
| 83 | + } |
| 84 | + if self.min_block_number.is_some() { |
| 85 | + other_fields.push("min_block_number"); |
| 86 | + } |
| 87 | + if self.max_block_number.is_some() { |
| 88 | + other_fields.push("max_block_number"); |
| 89 | + } |
| 90 | + if self.min_block_timestamp.is_some() { |
| 91 | + other_fields.push("min_block_timestamp"); |
| 92 | + } |
| 93 | + if self.max_block_timestamp.is_some() { |
| 94 | + other_fields.push("max_block_timestamp"); |
| 95 | + } |
| 96 | + if self.min_spec_version.is_some() { |
| 97 | + other_fields.push("min_spec_version"); |
| 98 | + } |
| 99 | + if self.max_spec_version.is_some() { |
| 100 | + other_fields.push("max_spec_version"); |
| 101 | + } |
| 102 | + if self.pallet_name.is_some() { |
| 103 | + other_fields.push("pallet_name"); |
| 104 | + } |
| 105 | + if self.event_name.is_some() { |
| 106 | + other_fields.push("event_name"); |
| 107 | + } |
| 108 | + if !other_fields.is_empty() { |
| 109 | + return Err(APIError::BadRequest( |
| 110 | + format!( |
| 111 | + "No other parameter should not be set when next_cursor is set. Please remove these parameters and try again: {}", |
| 112 | + other_fields.join(", ") |
| 113 | + ) |
| 114 | + )); |
| 115 | + } |
| 116 | + } |
| 117 | + Ok(()) |
| 118 | + } |
84 | 119 | } |
85 | 120 |
|
86 | 121 | /// Query parameters for fetching and filtering events within a block. |
|
0 commit comments