Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspc v3 syntax #758

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ if-watch = { git = "https://github.com/oscartbeaumont/if-watch", rev = "410e8e1d

mdns-sd = { git = "https://github.com/oscartbeaumont/mdns-sd", rev = "45515a98e9e408c102871abaa5a9bff3bee0cbe8" } # TODO: Do upstream PR

rspc = { git = "https://github.com/oscartbeaumont/rspc", rev = "9cb64def32aef5a987f9f06f727b4160c321b5f8" }
rspc = { git = "https://github.com/oscartbeaumont/rspc", rev = "745f0b210f6d268a4cef5d43b595e88d99ac269f" }
httpz = { git = "https://github.com/oscartbeaumont/httpz", rev = "a5185f2ed2fdefeb2f582dce38a692a1bf76d1d6" }
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sd-file-ext = { path = "../crates/file-ext" }
sd-sync = { path = "../crates/sync" }
sd-p2p = { path = "../crates/p2p", features = ["specta", "serde"] }

rspc = { workspace = true, features = ["uuid", "chrono", "tracing", "unstable"] }
rspc = { workspace = true, features = ["uuid", "chrono", "tracing", "alpha", "unstable"] }
httpz = { workspace = true }
prisma-client-rust = { workspace = true }
specta = { workspace = true }
Expand Down
188 changes: 97 additions & 91 deletions core/src/api/files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
api::utils::library,
invalidate_query,
library::Library,
location::{file_path_helper::MaterializedPath, find_location, LocationError},
object::fs::{
copy::FileCopierJobInit, cut::FileCutterJobInit, decrypt::FileDecryptorJobInit,
Expand All @@ -9,147 +9,153 @@ use crate::{
prisma::{location, object},
};

use rspc::ErrorCode;
use rspc::{alpha::AlphaRouter, ErrorCode};
use serde::Deserialize;
use specta::Type;
use std::path::Path;
use tokio::fs;

use super::{utils::LibraryRequest, RouterBuilder};
use super::{Ctx, R};

pub(crate) fn mount() -> RouterBuilder {
<RouterBuilder>::new()
.library_query("get", |t| {
pub(crate) fn mount() -> AlphaRouter<Ctx> {
R.router()
.procedure("get", {
#[derive(Type, Deserialize)]
pub struct GetArgs {
pub id: i32,
}
t(|_, args: GetArgs, library: Library| async move {
Ok(library
.db
.object()
.find_unique(object::id::equals(args.id))
.include(object::include!({ file_paths media_data }))
.exec()
.await?)
})
R.with2(library())
.query(|(_, library), args: GetArgs| async move {
Ok(library
.db
.object()
.find_unique(object::id::equals(args.id))
.include(object::include!({ file_paths media_data }))
.exec()
.await?)
})
})
.library_mutation("setNote", |t| {
.procedure("setNote", {
#[derive(Type, Deserialize)]
pub struct SetNoteArgs {
pub id: i32,
pub note: Option<String>,
}

t(|_, args: SetNoteArgs, library: Library| async move {
library
.db
.object()
.update(
object::id::equals(args.id),
vec![object::note::set(args.note)],
)
.exec()
.await?;
R.with2(library())
.mutation(|(_, library), args: SetNoteArgs| async move {
library
.db
.object()
.update(
object::id::equals(args.id),
vec![object::note::set(args.note)],
)
.exec()
.await?;

invalidate_query!(library, "locations.getExplorerData");
invalidate_query!(library, "tags.getExplorerData");
invalidate_query!(library, "locations.getExplorerData");
invalidate_query!(library, "tags.getExplorerData");

Ok(())
})
Ok(())
})
})
.library_mutation("setFavorite", |t| {
.procedure("setFavorite", {
#[derive(Type, Deserialize)]
pub struct SetFavoriteArgs {
pub id: i32,
pub favorite: bool,
}

t(|_, args: SetFavoriteArgs, library: Library| async move {
library
.db
.object()
.update(
object::id::equals(args.id),
vec![object::favorite::set(args.favorite)],
)
.exec()
.await?;
R.with2(library())
.mutation(|(_, library), args: SetFavoriteArgs| async move {
library
.db
.object()
.update(
object::id::equals(args.id),
vec![object::favorite::set(args.favorite)],
)
.exec()
.await?;

invalidate_query!(library, "locations.getExplorerData");
invalidate_query!(library, "tags.getExplorerData");
invalidate_query!(library, "locations.getExplorerData");
invalidate_query!(library, "tags.getExplorerData");

Ok(())
})
Ok(())
})
})
.library_mutation("delete", |t| {
t(|_, id: i32, library: Library| async move {
library
.db
.object()
.delete(object::id::equals(id))
.exec()
.await?;
.procedure("delete", {
R.with2(library())
.mutation(|(_, library), id: i32| async move {
library
.db
.object()
.delete(object::id::equals(id))
.exec()
.await?;

invalidate_query!(library, "locations.getExplorerData");
Ok(())
})
invalidate_query!(library, "locations.getExplorerData");
Ok(())
})
})
.library_mutation("encryptFiles", |t| {
t(
|_, args: FileEncryptorJobInit, library: Library| async move {
.procedure("encryptFiles", {
R.with2(library())
.mutation(|(_, library), args: FileEncryptorJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
},
)
})
})
.library_mutation("decryptFiles", |t| {
t(
|_, args: FileDecryptorJobInit, library: Library| async move {
.procedure("decryptFiles", {
R.with2(library())
.mutation(|(_, library), args: FileDecryptorJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
},
)
})
})
.library_mutation("deleteFiles", |t| {
t(|_, args: FileDeleterJobInit, library: Library| async move {
library.spawn_job(args).await.map_err(Into::into)
})
.procedure("deleteFiles", {
R.with2(library())
.mutation(|(_, library), args: FileDeleterJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
})
})
.library_mutation("eraseFiles", |t| {
t(|_, args: FileEraserJobInit, library: Library| async move {
library.spawn_job(args).await.map_err(Into::into)
})
.procedure("eraseFiles", {
R.with2(library())
.mutation(|(_, library), args: FileEraserJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
})
})
.library_mutation("duplicateFiles", |t| {
t(|_, args: FileCopierJobInit, library: Library| async move {
library.spawn_job(args).await.map_err(Into::into)
})
.procedure("duplicateFiles", {
R.with2(library())
.mutation(|(_, library), args: FileCopierJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
})
})
.library_mutation("copyFiles", |t| {
t(|_, args: FileCopierJobInit, library: Library| async move {
library.spawn_job(args).await.map_err(Into::into)
})
.procedure("copyFiles", {
R.with2(library())
.mutation(|(_, library), args: FileCopierJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
})
})
.library_mutation("cutFiles", |t| {
t(|_, args: FileCutterJobInit, library: Library| async move {
library.spawn_job(args).await.map_err(Into::into)
})
.procedure("cutFiles", {
R.with2(library())
.mutation(|(_, library), args: FileCutterJobInit| async move {
library.spawn_job(args).await.map_err(Into::into)
})
})
.library_mutation("renameFile", |t| {
.procedure("renameFile", {
#[derive(Type, Deserialize)]
pub struct RenameFileArgs {
pub location_id: i32,
pub file_name: String,
pub new_file_name: String,
}

t(
|_,
R.with2(library()).mutation(
|(_, library),
RenameFileArgs {
location_id,
file_name,
new_file_name,
}: RenameFileArgs,
library: Library| async move {
}: RenameFileArgs| async move {
let location = find_location(&library, location_id)
.select(location::select!({ path }))
.exec()
Expand Down
Loading