acton-service 0.20.0

Production-ready Rust backend framework with type-enforced API versioning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Handler trait definitions for REST CRUD patterns
//!
//! This module provides generic traits for REST collection handlers using RPITIT
//! (Return Position Impl Trait In Traits), available since Rust 1.75.
//!
//! # Overview
//!
//! - [`CollectionHandler`]: Standard CRUD operations (list, get, create, update, delete)
//! - [`SoftDeleteHandler`]: Extended operations for soft delete support
//!
//! # Example
//!
//! ```rust,ignore
//! use acton_service::handlers::{
//!     ApiError, CollectionHandler, ItemResponse, ListQuery, ListResponse,
//! };
//!
//! struct UserHandler {
//!     repository: UserRepository,
//! }
//!
//! impl CollectionHandler<UserId, User, CreateUser, UpdateUser> for UserHandler {
//!     async fn list(&self, query: ListQuery) -> Result<ListResponse<User>, ApiError> {
//!         let users = self.repository.find_all(&[], None, Some(query.into())).await?;
//!         let total = self.repository.count(&[]).await?;
//!         Ok(ListResponse::new(users, PaginationMeta::new(
//!             query.page_number(),
//!             query.items_per_page(),
//!             total,
//!         )))
//!     }
//!
//!     async fn get(&self, id: &UserId) -> Result<ItemResponse<User>, ApiError> {
//!         let user = self.repository.find_by_id(id).await?
//!             .ok_or_else(|| ApiError::not_found("User", id.to_string()))?;
//!         Ok(ItemResponse::new(user))
//!     }
//!
//!     // ... other methods
//! }
//! ```

use std::future::Future;

use super::error::ApiError;
use super::query::ListQuery;
use super::response::{ItemResponse, ListResponse};

/// Standard REST CRUD handler trait
///
/// This trait defines the standard operations for REST collection endpoints.
/// It uses Rust 1.75+ RPITIT (Return Position Impl Trait In Traits) for ergonomic
/// async trait methods without requiring `async_trait`.
///
/// # Type Parameters
///
/// - `Id`: The identifier type for the entity (e.g., `UserId`, `Uuid`, `i64`)
/// - `Entity`: The full entity type returned from queries
/// - `CreateDto`: The data transfer object for creating new entities
/// - `UpdateDto`: The data transfer object for updating existing entities
///
/// # Example
///
/// ```rust,ignore
/// use acton_service::handlers::{ApiError, CollectionHandler, ItemResponse, ListQuery, ListResponse};
///
/// struct ProductHandler { /* ... */ }
///
/// impl CollectionHandler<ProductId, Product, CreateProduct, UpdateProduct> for ProductHandler {
///     async fn list(&self, query: ListQuery) -> Result<ListResponse<Product>, ApiError> {
///         // Implementation
///         todo!()
///     }
///
///     async fn get(&self, id: &ProductId) -> Result<ItemResponse<Product>, ApiError> {
///         // Implementation
///         todo!()
///     }
///
///     async fn create(&self, dto: CreateProduct) -> Result<ItemResponse<Product>, ApiError> {
///         // Implementation
///         todo!()
///     }
///
///     async fn update(&self, id: &ProductId, dto: UpdateProduct) -> Result<ItemResponse<Product>, ApiError> {
///         // Implementation
///         todo!()
///     }
///
///     async fn delete(&self, id: &ProductId) -> Result<(), ApiError> {
///         // Implementation
///         todo!()
///     }
/// }
/// ```
pub trait CollectionHandler<Id, Entity, CreateDto, UpdateDto>: Send + Sync {
    /// List entities with pagination, filtering, and sorting
    ///
    /// Returns a paginated list of entities matching the query parameters.
    ///
    /// # Arguments
    ///
    /// * `query` - Query parameters for pagination, sorting, and filtering
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let query = ListQuery::new()
    ///     .with_page(1)
    ///     .with_per_page(20)
    ///     .with_sort("created_at".to_string())
    ///     .with_order(SortOrder::Desc);
    ///
    /// let response = handler.list(query).await?;
    /// println!("Found {} items", response.pagination.total);
    /// ```
    fn list(
        &self,
        query: ListQuery,
    ) -> impl Future<Output = Result<ListResponse<Entity>, ApiError>> + Send;

    /// Get a single entity by its identifier
    ///
    /// Returns the entity if found, or a NotFound error if it doesn't exist.
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the entity
    ///
    /// # Errors
    ///
    /// Returns `ApiError` with `NotFound` kind if the entity doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let user = handler.get(&user_id).await?;
    /// println!("Found user: {}", user.data.name);
    /// ```
    fn get(&self, id: &Id) -> impl Future<Output = Result<ItemResponse<Entity>, ApiError>> + Send;

    /// Create a new entity
    ///
    /// Returns the created entity with any generated fields (e.g., ID, timestamps).
    ///
    /// # Arguments
    ///
    /// * `dto` - The data for creating the new entity
    ///
    /// # Errors
    ///
    /// Returns `ApiError` with:
    /// - `ValidationFailed` if the input data is invalid
    /// - `AlreadyExists` if a duplicate entity would be created
    /// - `Conflict` if the operation violates constraints
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let dto = CreateUser { name: "Alice".to_string(), email: "alice@example.com".to_string() };
    /// let response = handler.create(dto).await?;
    /// println!("Created user with ID: {}", response.data.id);
    /// ```
    fn create(
        &self,
        dto: CreateDto,
    ) -> impl Future<Output = Result<ItemResponse<Entity>, ApiError>> + Send;

    /// Update an existing entity
    ///
    /// Returns the updated entity.
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the entity to update
    /// * `dto` - The data for updating the entity
    ///
    /// # Errors
    ///
    /// Returns `ApiError` with:
    /// - `NotFound` if the entity doesn't exist
    /// - `ValidationFailed` if the input data is invalid
    /// - `Conflict` if the operation violates constraints
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let dto = UpdateUser { name: Some("Alice Smith".to_string()), email: None };
    /// let response = handler.update(&user_id, dto).await?;
    /// println!("Updated user: {}", response.data.name);
    /// ```
    fn update(
        &self,
        id: &Id,
        dto: UpdateDto,
    ) -> impl Future<Output = Result<ItemResponse<Entity>, ApiError>> + Send;

    /// Delete an entity by its identifier (hard delete)
    ///
    /// Permanently removes the entity from the system.
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the entity to delete
    ///
    /// # Errors
    ///
    /// Returns `ApiError` with:
    /// - `NotFound` if the entity doesn't exist
    /// - `Conflict` if the entity cannot be deleted due to constraints
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// handler.delete(&user_id).await?;
    /// println!("User deleted");
    /// ```
    fn delete(&self, id: &Id) -> impl Future<Output = Result<(), ApiError>> + Send;
}

/// Extended handler trait for soft delete support
///
/// Soft delete is useful for GDPR compliance, audit trails, and data recovery.
/// Entities are marked as deleted rather than being removed from the database.
///
/// # Type Parameters
///
/// Same as [`CollectionHandler`].
///
/// # Example
///
/// ```rust,ignore
/// use acton_service::handlers::{SoftDeleteHandler, CollectionHandler};
///
/// impl SoftDeleteHandler<UserId, User, CreateUser, UpdateUser> for UserHandler {
///     async fn soft_delete(&self, id: &UserId) -> Result<(), ApiError> {
///         // Mark user as deleted
///         self.repository.soft_delete(id).await?;
///         Ok(())
///     }
///
///     async fn restore(&self, id: &UserId) -> Result<ItemResponse<User>, ApiError> {
///         // Restore soft-deleted user
///         self.repository.restore(id).await?;
///         let user = self.repository.find_by_id(id).await?
///             .ok_or_else(|| ApiError::not_found("User", id.to_string()))?;
///         Ok(ItemResponse::new(user))
///     }
///
///     async fn list_with_deleted(&self, query: ListQuery) -> Result<ListResponse<User>, ApiError> {
///         // List all users including soft-deleted ones
///         todo!()
///     }
/// }
/// ```
pub trait SoftDeleteHandler<Id, Entity, CreateDto, UpdateDto>:
    CollectionHandler<Id, Entity, CreateDto, UpdateDto>
{
    /// Mark an entity as deleted without removing it from the database
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the entity to soft delete
    ///
    /// # Errors
    ///
    /// Returns `ApiError` with `NotFound` if the entity doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// handler.soft_delete(&user_id).await?;
    /// // User is now marked as deleted but still in the database
    /// ```
    fn soft_delete(&self, id: &Id) -> impl Future<Output = Result<(), ApiError>> + Send;

    /// Restore a soft-deleted entity
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the entity to restore
    ///
    /// # Errors
    ///
    /// Returns `ApiError` with `NotFound` if the entity doesn't exist or isn't deleted.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let response = handler.restore(&user_id).await?;
    /// println!("Restored user: {}", response.data.name);
    /// ```
    fn restore(
        &self,
        id: &Id,
    ) -> impl Future<Output = Result<ItemResponse<Entity>, ApiError>> + Send;

    /// List all entities including soft-deleted ones
    ///
    /// Useful for admin interfaces or audit views.
    ///
    /// # Arguments
    ///
    /// * `query` - Query parameters for pagination, sorting, and filtering
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let all_users = handler.list_with_deleted(ListQuery::default()).await?;
    /// println!("Total users (including deleted): {}", all_users.pagination.total);
    /// ```
    fn list_with_deleted(
        &self,
        query: ListQuery,
    ) -> impl Future<Output = Result<ListResponse<Entity>, ApiError>> + Send;
}

#[cfg(test)]
mod tests {
    use super::*;

    // Compile-time tests to ensure traits can be implemented
    // Actual implementations would be tested in integration tests.

    #[derive(Debug, Clone)]
    struct MockId(String);

    #[derive(Debug, Clone)]
    struct MockEntity {
        id: String,
        name: String,
    }

    #[derive(Debug, Clone)]
    struct MockCreate {
        name: String,
    }

    #[derive(Debug, Clone)]
    struct MockUpdate {
        name: Option<String>,
    }

    struct MockHandler;

    impl CollectionHandler<MockId, MockEntity, MockCreate, MockUpdate> for MockHandler {
        async fn list(&self, query: ListQuery) -> Result<ListResponse<MockEntity>, ApiError> {
            let data = vec![MockEntity {
                id: "1".to_string(),
                name: "Test".to_string(),
            }];
            let pagination = super::super::response::PaginationMeta::new(
                query.page_number(),
                query.items_per_page(),
                1,
            );
            Ok(ListResponse::new(data, pagination))
        }

        async fn get(&self, id: &MockId) -> Result<ItemResponse<MockEntity>, ApiError> {
            Ok(ItemResponse::new(MockEntity {
                id: id.0.clone(),
                name: "Test".to_string(),
            }))
        }

        async fn create(&self, dto: MockCreate) -> Result<ItemResponse<MockEntity>, ApiError> {
            Ok(ItemResponse::new(MockEntity {
                id: "new".to_string(),
                name: dto.name,
            }))
        }

        async fn update(
            &self,
            id: &MockId,
            dto: MockUpdate,
        ) -> Result<ItemResponse<MockEntity>, ApiError> {
            Ok(ItemResponse::new(MockEntity {
                id: id.0.clone(),
                name: dto.name.unwrap_or_else(|| "unchanged".to_string()),
            }))
        }

        async fn delete(&self, _id: &MockId) -> Result<(), ApiError> {
            Ok(())
        }
    }

    impl SoftDeleteHandler<MockId, MockEntity, MockCreate, MockUpdate> for MockHandler {
        async fn soft_delete(&self, _id: &MockId) -> Result<(), ApiError> {
            Ok(())
        }

        async fn restore(&self, id: &MockId) -> Result<ItemResponse<MockEntity>, ApiError> {
            Ok(ItemResponse::new(MockEntity {
                id: id.0.clone(),
                name: "Restored".to_string(),
            }))
        }

        async fn list_with_deleted(
            &self,
            query: ListQuery,
        ) -> Result<ListResponse<MockEntity>, ApiError> {
            self.list(query).await
        }
    }

    #[tokio::test]
    async fn test_mock_handler_list() {
        let handler = MockHandler;
        let query = ListQuery::new().with_page(1).with_per_page(20);
        let result = handler.list(query).await;
        assert!(result.is_ok());
        let response = result.unwrap();
        assert_eq!(response.data.len(), 1);
        assert_eq!(response.pagination.total, 1);
    }

    #[tokio::test]
    async fn test_mock_handler_get() {
        let handler = MockHandler;
        let id = MockId("123".to_string());
        let result = handler.get(&id).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().data.id, "123");
    }

    #[tokio::test]
    async fn test_mock_handler_create() {
        let handler = MockHandler;
        let dto = MockCreate {
            name: "New Entity".to_string(),
        };
        let result = handler.create(dto).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().data.name, "New Entity");
    }

    #[tokio::test]
    async fn test_mock_handler_update() {
        let handler = MockHandler;
        let id = MockId("123".to_string());
        let dto = MockUpdate {
            name: Some("Updated".to_string()),
        };
        let result = handler.update(&id, dto).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().data.name, "Updated");
    }

    #[tokio::test]
    async fn test_mock_handler_delete() {
        let handler = MockHandler;
        let id = MockId("123".to_string());
        let result = handler.delete(&id).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_mock_soft_delete_handler() {
        let handler = MockHandler;
        let id = MockId("123".to_string());
        let result = handler.soft_delete(&id).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_mock_restore_handler() {
        let handler = MockHandler;
        let id = MockId("123".to_string());
        let result = handler.restore(&id).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().data.name, "Restored");
    }

    #[tokio::test]
    async fn test_mock_list_with_deleted() {
        let handler = MockHandler;
        let query = ListQuery::default();
        let result = handler.list_with_deleted(query).await;
        assert!(result.is_ok());
    }
}