Skip to main content

commit_bridge/repository/
subscription.rs

1//! Repository access for the `subscriptions` table.
2
3use crate::domain::{EventType, TargetRepo};
4use crate::model::{CreateSubscription, Subscription, SubscriptionWithBranch, UpdateSubscription};
5use crate::repository::RepositoryError;
6use async_trait::async_trait;
7
8/// Interface for `subscriptions` table operations.
9#[async_trait]
10pub trait SubscriptionRepository: Send + Sync {
11    /// Creates a new subscription and returns it.
12    async fn create(
13        &self,
14        subscription: &CreateSubscription,
15    ) -> Result<SubscriptionWithBranch, RepositoryError>;
16
17    /// Returns the subscription with the given id.
18    async fn get_by_id(&self, id: i64) -> Result<Option<Subscription>, RepositoryError>;
19
20    /// Returns the subscription with the given id with its branch information.
21    async fn get_by_id_with_branch(
22        &self,
23        id: i64,
24    ) -> Result<Option<SubscriptionWithBranch>, RepositoryError>;
25
26    /// Returns the subscription with the given keys with its branch information.
27    async fn get_by_keys_with_branch(
28        &self,
29        branch_id: i64,
30        target_repo: &TargetRepo,
31        event_type: &EventType,
32    ) -> Result<Option<SubscriptionWithBranch>, RepositoryError>;
33
34    /// Lists some subscriptions.
35    ///
36    /// `last_id` is the last subscription ID that is going to be excluded,
37    /// while `limit` is the number of subscriptions to show.
38    async fn list_paginated(
39        &self,
40        last_id: i64,
41        limit: i64,
42    ) -> Result<Vec<Subscription>, RepositoryError>;
43
44    /// Lists some subscriptions with their branch information.
45    async fn list_paginated_with_branches(
46        &self,
47        last_id: i64,
48        limit: i64,
49    ) -> Result<Vec<SubscriptionWithBranch>, RepositoryError>;
50
51    /// Counts the remaining subscriptions after `last_id`.
52    async fn count_remaining(&self, last_id: i64) -> Result<i64, RepositoryError>;
53
54    /// Updates the subscription with the given id.
55    async fn update(
56        &self,
57        id: i64,
58        subscription: &UpdateSubscription,
59    ) -> Result<Subscription, RepositoryError>;
60
61    /// Deletes the subscription with the given id.
62    async fn delete(&self, id: i64) -> Result<(), RepositoryError>;
63
64    /// Returns the branch ID associated to the given subscription's `id`.
65    async fn get_branch_id_by_subscription_id(
66        &self,
67        id: i64,
68    ) -> Result<Option<i64>, RepositoryError>;
69
70    /// Counts the number of subscriptions associated to the given `branch_id`.
71    async fn count_subscriptions_by_branch_id(
72        &self,
73        branch_id: i64,
74    ) -> Result<i64, RepositoryError>;
75
76    /// Deletes a subscription and cascades deletion to the associated branch if no other subscriptions exist.
77    async fn delete_subscription_and_cascade(&self, id: i64) -> Result<(), RepositoryError>;
78}