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
|
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use crate::storage::db::{
insert::{Committable, Operations},
subscription::{Subscription, Subscriptions},
};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use sqlx::query;
#[derive(Debug, Serialize, Deserialize)]
pub(crate) enum Operation {
Add(Subscription),
Remove(Subscription),
}
impl Committable for Operation {
async fn commit(self, txn: &mut sqlx::SqliteConnection) -> Result<()> {
match self {
Operation::Add(subscription) => {
let url = subscription.url.as_str();
query!(
"
INSERT INTO subscriptions (
name,
url
) VALUES (?, ?);
",
subscription.name,
url
)
.execute(txn)
.await?;
println!(
"Subscribed to '{}' at '{}'",
subscription.name, subscription.url
);
Ok(())
}
Operation::Remove(subscription) => {
let output = query!(
"
DELETE FROM subscriptions
WHERE name = ?
",
subscription.name,
)
.execute(txn)
.await?;
assert_eq!(
output.rows_affected(),
1,
"The removed subscription query did effect more (or less) than one row. This is a bug."
);
println!(
"Unsubscribed from '{}' at '{}'",
subscription.name, subscription.url
);
Ok(())
}
}
}
}
impl Subscription {
pub(crate) fn add(self, ops: &mut Operations<Operation>) {
ops.push(Operation::Add(self));
}
pub(crate) fn remove(self, ops: &mut Operations<Operation>) {
ops.push(Operation::Remove(self));
}
}
impl Subscriptions {
pub(crate) fn remove(self, ops: &mut Operations<Operation>) {
for sub in self.0.into_values() {
ops.push(Operation::Remove(sub));
}
}
}
|