Skip to content

Commit

Permalink
Backend supports upserting, deleting identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
phildenhoff committed Dec 17, 2024
1 parent 11c8ce8 commit 61c22f9
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 17 deletions.
Binary file modified bun.lockb
Binary file not shown.
20 changes: 10 additions & 10 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src-tauri/libcalibre/src/api/books.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ impl BooksHandler {
.or(Err(()))
}

pub fn delete_book_identifier(&mut self, book_id: i32, identifier_id: i32) -> Result<(), ()> {
use crate::schema::identifiers::dsl::*;
let mut connection = self.client.lock().unwrap();

diesel::delete(identifiers.filter(book.eq(book_id).and(id.eq(identifier_id))))
.execute(&mut *connection)
.map(|_| ())
.or(Err(()))
}

// === === ===
// Descriptions
// === === ===
Expand Down
13 changes: 13 additions & 0 deletions src-tauri/libcalibre/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::dtos::library::NewLibraryFileDto;
use crate::dtos::library::UpdateLibraryEntryDto;
use crate::entities::book_file::NewBookFile;
use crate::Book;
use crate::UpsertBookIdentifier;
use chrono::DateTime;
use chrono::Utc;
use sanitise_file_name::sanitise;
Expand Down Expand Up @@ -332,6 +333,18 @@ impl CalibreClient {
.collect::<Result<Vec<BookFile>, ClientError>>()
}

// === Identifiers ===

pub fn upsert_book_identifier(&mut self, update: UpsertBookIdentifier) -> Result<i32, ()> {
self.client_v2.books().upsert_book_identifier(update)
}

pub fn delete_book_identifier(&mut self, book_id: i32, identifier_id: i32) -> Result<(), ()> {
self.client_v2
.books()
.delete_book_identifier(book_id, identifier_id)
}

/// Updates the library's ID to a new UUID.
///
/// You probably do not need this method, unless you're creating a new
Expand Down
8 changes: 4 additions & 4 deletions src-tauri/libcalibre/src/entities/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub struct UpdateBookData {

#[derive(Deserialize, Default, Debug)]
pub struct UpsertBookIdentifier {
pub(crate) book_id: i32,
pub(crate) id: Option<i32>,
pub(crate) label: String,
pub(crate) value: String,
pub book_id: i32,
pub id: Option<i32>,
pub label: String,
pub value: String,
}
3 changes: 2 additions & 1 deletion src-tauri/libcalibre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use diesel::SqliteConnection;
use std::sync::{Arc, Mutex};

pub use entities::{
author::Author, book::Book, book_aggregate::BookWithAuthorsAndFiles, book_file::BookFile,
author::Author, book::Book, book::UpsertBookIdentifier,
book_aggregate::BookWithAuthorsAndFiles, book_file::BookFile,
};

pub struct ClientV2 {
Expand Down
49 changes: 47 additions & 2 deletions src-tauri/src/libs/calibre/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use libcalibre::client::CalibreClient;
use libcalibre::dtos::book::UpdateBookDto;
use libcalibre::dtos::library::UpdateLibraryEntryDto;
use libcalibre::mime_type::MIMETYPE;
use libcalibre::UpsertBookIdentifier;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -107,15 +108,59 @@ pub fn clb_cmd_create_book(library_root: String, md: ImportableBookMetadata) {
}
}

#[tauri::command]
#[specta::specta]
pub fn clb_cmd_upsert_book_identifier(
library_root: String,
book_id: String,
label: String,
value: String,
existing_id: Option<i32>,
) -> Result<(), ()> {
match libcalibre::util::get_db_path(&library_root) {
None => Err(()),
Some(database_path) => {
let mut calibre = CalibreClient::new(database_path);

let book_id_int = book_id.parse::<i32>().unwrap();
let result = calibre.upsert_book_identifier(UpsertBookIdentifier {
book_id: book_id_int,
id: existing_id,
label,
value,
});

result.map(|_| ()).map_err(|_| ())
}
}
}

#[tauri::command]
#[specta::specta]
pub fn clb_cmd_delete_book_identifier(
library_root: String,
book_id: String,
identifier_id: i32,
) -> Result<(), ()> {
match libcalibre::util::get_db_path(&library_root) {
None => Err(()),
Some(database_path) => {
let mut calibre = CalibreClient::new(database_path);

let book_id_int = book_id.parse::<i32>().unwrap();
let result = calibre.delete_book_identifier(book_id_int, identifier_id);
result.map(|_| ()).map_err(|_| ())
}
}
}

#[derive(Serialize, Deserialize, specta::Type, Debug)]
pub struct BookUpdate {
pub author_id_list: Option<Vec<String>>,
pub title: Option<String>,
pub timestamp: Option<NaiveDateTime>,
pub publication_date: Option<NaiveDateTime>,
pub is_read: Option<bool>,
// pub tags: Option<String>,
// pub ext_id_list: Option<Vec<String>>,
}

impl BookUpdate {
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fn run_tauri_backend() -> std::io::Result<()> {
libs::calibre::clb_cmd_create_book,
libs::calibre::clb_cmd_create_library,
libs::calibre::clb_cmd_update_book,
libs::calibre::clb_cmd_upsert_book_identifier,
libs::calibre::clb_cmd_delete_book_identifier,
libs::calibre::clb_query_importable_file_metadata,
libs::calibre::clb_query_is_file_importable,
libs::calibre::clb_query_is_path_valid_library,
Expand Down
16 changes: 16 additions & 0 deletions src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ try {
else return { status: "error", error: e as any };
}
},
async clbCmdUpsertBookIdentifier(libraryRoot: string, bookId: string, label: string, value: string, existingId: number | null) : Promise<__Result__<null, null>> {
try {
return { status: "ok", data: await TAURI_INVOKE("plugin:tauri-specta|clb_cmd_upsert_book_identifier", { libraryRoot, bookId, label, value, existingId }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async clbCmdDeleteBookIdentifier(libraryRoot: string, bookId: string, identifierId: number) : Promise<__Result__<null, null>> {
try {
return { status: "ok", data: await TAURI_INVOKE("plugin:tauri-specta|clb_cmd_delete_book_identifier", { libraryRoot, bookId, identifierId }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async clbQueryImportableFileMetadata(file: ImportableFile) : Promise<{ file_type: ImportableBookType; title: string; author_names: string[] | null; identifier: string | null; publisher: string | null; language: string | null; tags: string[]; path: string; publication_date: string | null; file_contains_cover: boolean } | null> {
return await TAURI_INVOKE("plugin:tauri-specta|clb_query_importable_file_metadata", { file });
},
Expand Down

0 comments on commit 61c22f9

Please sign in to comment.