allsource-core 0.19.1

High-performance event store core built in Rust
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use crate::{
    application::dto::{
        ArticleDto, CreateArticleRequest, CreateArticleResponse, ListArticlesResponse,
        UpdateArticleRequest, UpdateArticleResponse,
    },
    domain::{
        entities::PaywallArticle,
        repositories::ArticleRepository,
        value_objects::{ArticleId, CreatorId, TenantId},
    },
    error::Result,
};
use std::sync::Arc;

/// Use Case: Create Article
///
/// Creates a new paywalled article for a creator.
///
/// Responsibilities:
/// - Validate input (DTO validation)
/// - Check for duplicate URL
/// - Create domain PaywallArticle entity (with domain validation)
/// - Persist via repository
/// - Return response DTO
pub struct CreateArticleUseCase {
    repository: Arc<dyn ArticleRepository>,
}

impl CreateArticleUseCase {
    pub fn new(repository: Arc<dyn ArticleRepository>) -> Self {
        Self { repository }
    }

    pub async fn execute(&self, request: CreateArticleRequest) -> Result<CreateArticleResponse> {
        // Check for duplicate URL
        if self.repository.url_exists(&request.url).await? {
            return Err(crate::error::AllSourceError::ValidationError(
                "Article with this URL already exists".to_string(),
            ));
        }

        // Parse IDs
        let article_id = ArticleId::new(request.article_id)?;
        let tenant_id = TenantId::new(request.tenant_id)?;
        let creator_id = CreatorId::parse(&request.creator_id)?;

        // Create domain article entity
        let mut article = if request.is_draft.unwrap_or(false) {
            PaywallArticle::new_draft(
                article_id,
                tenant_id,
                creator_id,
                request.title,
                request.url,
                request.price_cents,
            )?
        } else {
            PaywallArticle::new(
                article_id,
                tenant_id,
                creator_id,
                request.title,
                request.url,
                request.price_cents,
            )?
        };

        // Set optional fields
        if let Some(description) = request.description {
            article.update_description(Some(description));
        }

        if let Some(reading_time) = request.estimated_reading_time_minutes {
            article.update_reading_time(Some(reading_time));
        }

        if let Some(preview) = request.preview_content {
            article.update_preview(Some(preview))?;
        }

        // Persist via repository
        self.repository.save(&article).await?;

        Ok(CreateArticleResponse {
            article: ArticleDto::from(&article),
        })
    }
}

/// Use Case: Update Article
///
/// Updates an existing article's information.
pub struct UpdateArticleUseCase {
    repository: Arc<dyn ArticleRepository>,
}

impl UpdateArticleUseCase {
    pub fn new(repository: Arc<dyn ArticleRepository>) -> Self {
        Self { repository }
    }

    pub async fn execute(
        &self,
        mut article: PaywallArticle,
        request: UpdateArticleRequest,
    ) -> Result<UpdateArticleResponse> {
        // Update title if provided
        if let Some(title) = request.title {
            article.update_title(title)?;
        }

        // Update URL if provided
        if let Some(url) = request.url {
            // Check for duplicate URL (if different from current)
            if article.url() != url && self.repository.url_exists(&url).await? {
                return Err(crate::error::AllSourceError::ValidationError(
                    "Another article with this URL already exists".to_string(),
                ));
            }
            article.update_url(url)?;
        }

        // Update price if provided
        if let Some(price_cents) = request.price_cents {
            article.update_price(price_cents)?;
        }

        // Update description if provided
        if let Some(description) = request.description {
            article.update_description(Some(description));
        }

        // Update reading time if provided
        if let Some(reading_time) = request.estimated_reading_time_minutes {
            article.update_reading_time(Some(reading_time));
        }

        // Update preview if provided
        if let Some(preview) = request.preview_content {
            article.update_preview(Some(preview))?;
        }

        // Persist changes
        self.repository.save(&article).await?;

        Ok(UpdateArticleResponse {
            article: ArticleDto::from(&article),
        })
    }
}

/// Use Case: Publish Article
///
/// Publishes a draft article, making it available for purchase.
pub struct PublishArticleUseCase;

impl PublishArticleUseCase {
    pub fn execute(mut article: PaywallArticle) -> Result<ArticleDto> {
        article.publish()?;
        Ok(ArticleDto::from(&article))
    }
}

/// Use Case: Archive Article
///
/// Archives an article, preventing new purchases while preserving existing access.
pub struct ArchiveArticleUseCase;

impl ArchiveArticleUseCase {
    pub fn execute(mut article: PaywallArticle) -> Result<ArticleDto> {
        article.archive();
        Ok(ArticleDto::from(&article))
    }
}

/// Use Case: Restore Article
///
/// Restores an archived article, making it available for purchase again.
pub struct RestoreArticleUseCase;

impl RestoreArticleUseCase {
    pub fn execute(mut article: PaywallArticle) -> Result<ArticleDto> {
        article.restore()?;
        Ok(ArticleDto::from(&article))
    }
}

/// Use Case: Delete Article
///
/// Marks an article as deleted (soft delete).
pub struct DeleteArticleUseCase;

impl DeleteArticleUseCase {
    pub fn execute(mut article: PaywallArticle) -> Result<ArticleDto> {
        article.delete();
        Ok(ArticleDto::from(&article))
    }
}

/// Use Case: Record Article Purchase
///
/// Records a purchase for analytics tracking.
pub struct RecordArticlePurchaseUseCase;

impl RecordArticlePurchaseUseCase {
    pub fn execute(mut article: PaywallArticle, amount_cents: u64) -> Result<ArticleDto> {
        article.record_purchase(amount_cents);
        Ok(ArticleDto::from(&article))
    }
}

/// Use Case: List Articles
///
/// Returns a list of articles.
pub struct ListArticlesUseCase;

impl ListArticlesUseCase {
    pub fn execute(articles: &[PaywallArticle]) -> ListArticlesResponse {
        let article_dtos: Vec<ArticleDto> = articles.iter().map(ArticleDto::from).collect();
        let count = article_dtos.len();

        ListArticlesResponse {
            articles: article_dtos,
            count,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::{entities::ArticleStatus, repositories::ArticleQuery};
    use async_trait::async_trait;
    use std::sync::Mutex;

    struct MockArticleRepository {
        articles: Mutex<Vec<PaywallArticle>>,
    }

    impl MockArticleRepository {
        fn new() -> Self {
            Self {
                articles: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl ArticleRepository for MockArticleRepository {
        async fn save(&self, article: &PaywallArticle) -> Result<()> {
            let mut articles = self.articles.lock().unwrap();
            // Update if exists, otherwise insert
            if let Some(pos) = articles.iter().position(|a| a.id() == article.id()) {
                articles[pos] = article.clone();
            } else {
                articles.push(article.clone());
            }
            Ok(())
        }

        async fn find_by_id(&self, id: &ArticleId) -> Result<Option<PaywallArticle>> {
            let articles = self.articles.lock().unwrap();
            Ok(articles.iter().find(|a| a.id() == id).cloned())
        }

        async fn find_by_url(&self, url: &str) -> Result<Option<PaywallArticle>> {
            let articles = self.articles.lock().unwrap();
            Ok(articles.iter().find(|a| a.url() == url).cloned())
        }

        async fn find_by_creator(
            &self,
            creator_id: &CreatorId,
            _limit: usize,
            _offset: usize,
        ) -> Result<Vec<PaywallArticle>> {
            let articles = self.articles.lock().unwrap();
            Ok(articles
                .iter()
                .filter(|a| a.creator_id() == creator_id)
                .cloned()
                .collect())
        }

        async fn find_by_tenant(
            &self,
            _tenant_id: &TenantId,
            _limit: usize,
            _offset: usize,
        ) -> Result<Vec<PaywallArticle>> {
            Ok(Vec::new())
        }

        async fn find_active_by_creator(
            &self,
            _creator_id: &CreatorId,
            _limit: usize,
            _offset: usize,
        ) -> Result<Vec<PaywallArticle>> {
            Ok(Vec::new())
        }

        async fn find_by_status(
            &self,
            _status: ArticleStatus,
            _limit: usize,
            _offset: usize,
        ) -> Result<Vec<PaywallArticle>> {
            Ok(Vec::new())
        }

        async fn count(&self) -> Result<usize> {
            Ok(self.articles.lock().unwrap().len())
        }

        async fn count_by_creator(&self, _creator_id: &CreatorId) -> Result<usize> {
            Ok(0)
        }

        async fn count_by_status(&self, _status: ArticleStatus) -> Result<usize> {
            Ok(0)
        }

        async fn delete(&self, _id: &ArticleId) -> Result<bool> {
            Ok(false)
        }

        async fn query(&self, _query: &ArticleQuery) -> Result<Vec<PaywallArticle>> {
            Ok(Vec::new())
        }

        async fn find_top_by_revenue(
            &self,
            _creator_id: Option<&CreatorId>,
            _limit: usize,
        ) -> Result<Vec<PaywallArticle>> {
            Ok(Vec::new())
        }

        async fn find_recent(
            &self,
            _creator_id: Option<&CreatorId>,
            _limit: usize,
        ) -> Result<Vec<PaywallArticle>> {
            Ok(Vec::new())
        }
    }

    #[tokio::test]
    async fn test_create_article() {
        let repo = Arc::new(MockArticleRepository::new());
        let use_case = CreateArticleUseCase::new(repo.clone());

        let request = CreateArticleRequest {
            article_id: "test-article".to_string(),
            tenant_id: "test-tenant".to_string(),
            creator_id: uuid::Uuid::new_v4().to_string(),
            title: "My Test Article".to_string(),
            url: "https://blog.example.com/article".to_string(),
            price_cents: 50,
            description: Some("A great article".to_string()),
            estimated_reading_time_minutes: Some(5),
            preview_content: None,
            is_draft: None,
        };

        let response = use_case.execute(request).await;
        assert!(response.is_ok());

        let response = response.unwrap();
        assert_eq!(response.article.title, "My Test Article");
        assert_eq!(response.article.price_cents, 50);
        assert!(response.article.is_purchasable);
    }

    #[tokio::test]
    async fn test_create_draft_article() {
        let repo = Arc::new(MockArticleRepository::new());
        let use_case = CreateArticleUseCase::new(repo.clone());

        let request = CreateArticleRequest {
            article_id: "draft-article".to_string(),
            tenant_id: "test-tenant".to_string(),
            creator_id: uuid::Uuid::new_v4().to_string(),
            title: "Draft Article".to_string(),
            url: "https://blog.example.com/draft".to_string(),
            price_cents: 100,
            description: None,
            estimated_reading_time_minutes: None,
            preview_content: None,
            is_draft: Some(true),
        };

        let response = use_case.execute(request).await.unwrap();
        assert!(!response.article.is_purchasable);
        assert_eq!(
            response.article.status,
            crate::application::dto::ArticleStatusDto::Draft
        );
    }

    #[tokio::test]
    async fn test_create_article_duplicate_url() {
        let repo = Arc::new(MockArticleRepository::new());
        let use_case = CreateArticleUseCase::new(repo.clone());

        // First article
        let request1 = CreateArticleRequest {
            article_id: "article-1".to_string(),
            tenant_id: "test-tenant".to_string(),
            creator_id: uuid::Uuid::new_v4().to_string(),
            title: "Article 1".to_string(),
            url: "https://blog.example.com/article".to_string(),
            price_cents: 50,
            description: None,
            estimated_reading_time_minutes: None,
            preview_content: None,
            is_draft: None,
        };
        use_case.execute(request1).await.unwrap();

        // Second article with same URL
        let request2 = CreateArticleRequest {
            article_id: "article-2".to_string(),
            tenant_id: "test-tenant".to_string(),
            creator_id: uuid::Uuid::new_v4().to_string(),
            title: "Article 2".to_string(),
            url: "https://blog.example.com/article".to_string(),
            price_cents: 50,
            description: None,
            estimated_reading_time_minutes: None,
            preview_content: None,
            is_draft: None,
        };

        let result = use_case.execute(request2).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_publish_article() {
        let article_id = ArticleId::new("test-article".to_string()).unwrap();
        let tenant_id = TenantId::new("test-tenant".to_string()).unwrap();
        let creator_id = CreatorId::new();

        let article = PaywallArticle::new_draft(
            article_id,
            tenant_id,
            creator_id,
            "Draft".to_string(),
            "https://example.com".to_string(),
            50,
        )
        .unwrap();

        assert!(!article.is_purchasable());

        let result = PublishArticleUseCase::execute(article);
        assert!(result.is_ok());
        assert!(result.unwrap().is_purchasable);
    }

    #[test]
    fn test_archive_and_restore() {
        let article_id = ArticleId::new("test-article".to_string()).unwrap();
        let tenant_id = TenantId::new("test-tenant".to_string()).unwrap();
        let creator_id = CreatorId::new();

        let article = PaywallArticle::new(
            article_id,
            tenant_id,
            creator_id,
            "Article".to_string(),
            "https://example.com".to_string(),
            50,
        )
        .unwrap();

        // Archive
        let archived = ArchiveArticleUseCase::execute(article).unwrap();
        assert_eq!(
            archived.status,
            crate::application::dto::ArticleStatusDto::Archived
        );
        assert!(!archived.is_purchasable);

        // Restore (need to recreate since we don't have the entity)
        let article_id = ArticleId::new("test-article-2".to_string()).unwrap();
        let tenant_id = TenantId::new("test-tenant".to_string()).unwrap();
        let creator_id = CreatorId::new();

        let mut article = PaywallArticle::new(
            article_id,
            tenant_id,
            creator_id,
            "Article".to_string(),
            "https://example.com/2".to_string(),
            50,
        )
        .unwrap();
        article.archive();

        let restored = RestoreArticleUseCase::execute(article).unwrap();
        assert_eq!(
            restored.status,
            crate::application::dto::ArticleStatusDto::Active
        );
        assert!(restored.is_purchasable);
    }

    #[test]
    fn test_list_articles() {
        let tenant_id = TenantId::new("test-tenant".to_string()).unwrap();
        let creator_id = CreatorId::new();

        let articles = vec![
            PaywallArticle::new(
                ArticleId::new("article-1".to_string()).unwrap(),
                tenant_id.clone(),
                creator_id,
                "Article 1".to_string(),
                "https://example.com/1".to_string(),
                50,
            )
            .unwrap(),
            PaywallArticle::new(
                ArticleId::new("article-2".to_string()).unwrap(),
                tenant_id,
                creator_id,
                "Article 2".to_string(),
                "https://example.com/2".to_string(),
                100,
            )
            .unwrap(),
        ];

        let response = ListArticlesUseCase::execute(&articles);
        assert_eq!(response.count, 2);
        assert_eq!(response.articles.len(), 2);
    }
}