core/db: add get_list_by_id() method

pull/1/head
Manos Pitsidianakis 2022-05-15 09:46:08 +03:00
parent c5fb32a5ca
commit 0d2b4316e2
2 changed files with 30 additions and 5 deletions

View File

@ -242,11 +242,11 @@ fn run_app(opt: Opt) -> Result<()> {
}
List { list_id, cmd } => {
let db = Database::open_or_create_db()?;
let mut lists = db.list_lists()?;
let list = if let Some(pos) = lists.iter().position(|l| l.id == list_id) {
lists.remove(pos)
} else {
return Err(format!("No list with id {} was found", list_id).into());
let list = match db.get_list_by_id(&list_id)? {
Some(v) => v,
None => {
return Err(format!("No list with id {} was found", list_id).into());
}
};
use ListCommand::*;
match cmd {

View File

@ -83,6 +83,31 @@ impl Database {
Ok(ret)
}
pub fn get_list_by_id<S: AsRef<str>>(&self, id: S) -> Result<Option<DbVal<MailingList>>> {
let id = id.as_ref();
let mut stmt = self
.connection
.prepare("SELECT * FROM mailing_lists WHERE id = ?;")?;
let ret = stmt
.query_row([&id], |row| {
let pk = row.get("pk")?;
Ok(DbVal(
MailingList {
pk,
name: row.get("name")?,
id: row.get("id")?,
address: row.get("address")?,
description: row.get("description")?,
archive_url: row.get("archive_url")?,
},
pk,
))
})
.optional()?;
Ok(ret)
}
pub fn create_list(&self, new_val: MailingList) -> Result<DbVal<MailingList>> {
let mut stmt = self
.connection