converted full sql from onboarding flow to prisma

This commit is contained in:
Ren Amamiya
2023-04-03 15:03:07 +07:00
parent 33000979ed
commit 3f87d510ab
14 changed files with 314 additions and 74 deletions

View File

@@ -7,6 +7,7 @@
#[macro_use]
extern crate objc;
use prisma_client_rust::raw;
use tauri::{Manager, WindowEvent};
#[cfg(target_os = "macos")]
use window_ext::WindowExt;
@@ -31,6 +32,11 @@ struct CreateAccountData {
metadata: String,
}
#[derive(Deserialize, Type)]
struct GetFollowData {
account_id: i32,
}
#[derive(Deserialize, Type)]
struct CreateFollowData {
pubkey: String,
@@ -39,6 +45,18 @@ struct CreateFollowData {
account_id: i32,
}
#[derive(Deserialize, Type)]
struct CreateNoteData {
event_id: String,
pubkey: String,
kind: i32,
tags: String,
content: String,
parent_id: String,
parent_comment_id: String,
account_id: i32,
}
#[tauri::command]
#[specta::specta]
async fn get_account(db: DbState<'_>) -> Result<Vec<account::Data>, ()> {
@@ -59,6 +77,16 @@ async fn create_account(db: DbState<'_>, data: CreateAccountData) -> Result<acco
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn get_follows(db: DbState<'_>, data: GetFollowData) -> Result<Vec<follow::Data>, ()> {
db.follow()
.find_many(vec![follow::account_id::equals(data.account_id)])
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_follow(db: DbState<'_>, data: CreateFollowData) -> Result<follow::Data, ()> {
@@ -75,13 +103,68 @@ async fn create_follow(db: DbState<'_>, data: CreateFollowData) -> Result<follow
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_note(db: DbState<'_>, data: CreateNoteData) -> Result<note::Data, ()> {
let event_id = data.event_id.clone();
let content = data.content.clone();
db.note()
.upsert(
note::event_id::equals(event_id),
note::create(
data.event_id,
data.pubkey,
data.kind,
data.tags,
data.content,
data.parent_id,
data.parent_comment_id,
account::id::equals(data.account_id),
vec![],
),
vec![note::content::set(content)],
)
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn get_notes(db: DbState<'_>) -> Result<Vec<note::Data>, ()> {
db._query_raw(raw!("SELECT * FROM Note"))
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn check_note(db: DbState<'_>) -> Result<Vec<note::Data>, ()> {
db.note()
.find_many(vec![])
.take(5)
.exec()
.await
.map_err(|_| ())
}
#[tokio::main]
async fn main() {
let db = PrismaClient::_builder().build().await.unwrap();
#[cfg(debug_assertions)]
ts::export(
collect_types![get_account, create_account, create_follow],
collect_types![
get_account,
create_account,
get_follows,
create_follow,
create_note,
get_notes,
check_note
],
"../src/utils/bindings.ts",
)
.unwrap();
@@ -115,7 +198,11 @@ async fn main() {
.invoke_handler(tauri::generate_handler![
get_account,
create_account,
create_follow
get_follows,
create_follow,
create_note,
get_notes,
check_note
])
.manage(Arc::new(db))
.run(tauri::generate_context!())