Server upload works

This commit is contained in:
2026-06-01 15:36:53 +03:00
parent 8e18e82202
commit c0bd558233
10 changed files with 405 additions and 13 deletions
+1
View File
@@ -2,4 +2,5 @@ pub(crate) mod words;
pub(crate) mod card_sets;
pub(crate) mod card_stats;
pub(crate) mod voice;
pub(crate) mod settings;
+69
View File
@@ -0,0 +1,69 @@
use rusqlite::fallible_iterator::FallibleIterator;
use rusqlite::Connection;
pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
let mut stmt = connection.prepare("SELECT value FROM settings WHERE id = ?1").unwrap();
let iter = stmt.query_map((key,), |row| {
row.get(0)
}).unwrap();
for row in iter {
if let Ok(value) = row {
return Some(value);
}
return None;
}
None
}
pub fn set_setting(key: String, value: String, connection: &Connection) {
let current = get_settings_list(connection);
if current.contains(&key) {
update_settings(key, value, connection);
}else {
create_settings(key, value, connection);
}
}
pub fn delete_settings(key: String, connection: &Connection) {
connection
.execute("DELETE FROM settings WHERE id = ?1", (&key,))
.unwrap_or_else(|e| {
println!("{}", e);
0
});
}
fn create_settings(key: String, value: String, connection: &Connection) {
connection
.execute(
"INSERT into settings (id, value) VALUES (?1, ?2)",
(key, value),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
}
fn update_settings(key: String, value: String, connection: &Connection) {
connection
.execute(
"update settings
set value = ?2
where id = ?1",
(key, value),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});}
fn get_settings_list(connection: &Connection) -> Vec<String> {
let mut stmt = connection.prepare("SELECT id FROM settings").unwrap();
let iter = stmt.query_map((), |row| {
row.get(0)
}).unwrap();
iter.map(|row| { row.unwrap() }).collect()
}
+15 -5
View File
@@ -16,6 +16,16 @@ pub fn create_db() {
}
fn create_tables(conn: &Connection) {
conn.execute(
"create table settings
(
id text primary key,
value text
);",
(),
)
.unwrap_or_else(|e| 0);
conn.execute(
"create table card_set
(
@@ -28,7 +38,7 @@ fn create_tables(conn: &Connection) {
);",
(),
)
.unwrap();
.unwrap_or_else(|e| 0);
conn.execute(
"create table word_group
(
@@ -38,7 +48,7 @@ fn create_tables(conn: &Connection) {
);",
(),
)
.unwrap();
.unwrap_or_else(|e| 0);
conn.execute(
"create table words
(
@@ -55,7 +65,7 @@ fn create_tables(conn: &Connection) {
);",
(),
)
.unwrap();
.unwrap_or_else(|e| 0);
conn.execute(
"create table card_stats
(
@@ -72,13 +82,13 @@ fn create_tables(conn: &Connection) {
);",
(),
)
.unwrap();
.unwrap_or_else(|e| 0);
conn.execute(
"insert into word_group (name)
values (\"Слова\");",
(),
)
.unwrap();
.unwrap_or_else(|e| 0);
}
pub fn add_word(word: &mut WordData, connection: &Connection) {