Skip to content

Commit

Permalink
Add bank accounts, prepare its crud
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Jan 20, 2024
1 parent 9f32305 commit 158d55e
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src-tauri/Cargo.lock

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

19 changes: 19 additions & 0 deletions src-tauri/src/commands/bank_account_get_by_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::entities::bank_accounts;
use rusqlite::Connection;
use std::ops::Deref;
use std::sync::Mutex;
use tauri::State;

#[tauri::command]
pub(crate) fn bank_account_get_by_id(conn_state: State<'_, Mutex<Connection>>, id: String) -> String {
let conn = conn_state
.inner()
.lock()
.expect("Could not retrieve database connection");
let conn = conn.deref();

let id_as_u32: u32 = id.parse().unwrap();

serde_json::to_string(&bank_accounts::get_by_id(conn, id_as_u32))
.expect("Could not serialize BankAccount properly")
}
29 changes: 29 additions & 0 deletions src-tauri/src/entities/bank_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ pub(crate) fn find_all(conn: &Connection) -> Vec<BankAccount> {
bank_accounts
}

pub(crate) fn get_by_id(conn: &Connection, id: u32) -> BankAccount {
let mut stmt = conn
.prepare(
"
SELECT
id,
name,
slug,
currency
FROM bank_accounts
WHERE id = :id
",
)
.expect("Could not fetch bank account");

let mut rows = stmt
.query(named_params! {
":id": &id,
})
.expect("Could not execute query to fetch an bank account by id.");

let row = rows
.next()
.expect("Could not retrieve query rows.")
.expect("No bank account found with this ID.");

serde_rusqlite::from_row::<BankAccount>(row).unwrap()
}

pub(crate) fn create(conn: &Connection, bank_account: BankAccount) -> i64 {
if bank_account.id != 0 {
panic!("Cannot create a bank account that already has an ID");
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 @@ -11,6 +11,7 @@ mod migrations;
mod commands {
pub(crate) mod bank_account_create;
pub(crate) mod bank_account_find_all;
pub(crate) mod bank_account_get_by_id;
pub(crate) mod bank_account_update;
pub(crate) mod import_operations;
pub(crate) mod operation_delete;
Expand Down Expand Up @@ -69,6 +70,7 @@ fn main() {
crate::commands::operations_get_triage::operations_get_triage,
crate::commands::operations_get_triage_count::operations_get_triage_count,
crate::commands::bank_account_find_all::bank_account_find_all,
crate::commands::bank_account_get_by_id::bank_account_get_by_id,
crate::commands::bank_account_create::bank_account_create,
crate::commands::bank_account_update::bank_account_update,
crate::commands::tags_get::tags_get,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/crud/Dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DashboardDefinition, UrlAction } from '@orbitale/svelte-admin';
import Home from 'carbon-icons-svelte/lib/Home.svelte';

import OperationCrud from '$lib/crud/cruds/OperationCrud';
import BankAccountsCrud from "$lib/crud/cruds/BankAccountsCrud";

export const dashboard = new DashboardDefinition({
adminConfig: {
Expand All @@ -26,5 +27,6 @@ export const dashboard = new DashboardDefinition({

cruds: [
OperationCrud,
BankAccountsCrud,
]
});
51 changes: 51 additions & 0 deletions src/lib/crud/cruds/BankAccountsCrud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
CallbackStateProcessor,
CallbackStateProvider,
CrudDefinition, Edit,
List,
TextField,
UrlAction,
} from "@orbitale/svelte-admin";
import type {CrudBankAccount} from "@orbitale/svelte-admin/dist/Crud/BankAccounts";
import type {RequestParameters} from "@orbitale/svelte-admin/dist/request";
import {getBankAccountById, getBankAccounts} from "$lib/db/bank_accounts";
import type BankAccount from "$lib/entities/BankAccount";

const baseFields = [
new TextField('name', 'Name'),
new TextField('slug', 'Identifier'),
new TextField('currency', 'Currency'),
];

export default new CrudDefinition<BankAccount>('bank-accounts', {
defaultOperationName: "list",
label: {plural: "BankAccounts", singular: "BankAccount"},
// minStateLoadingTimeMs: 0,

operations: [
new List([...baseFields],
[
new UrlAction('Edit', '/crud/bank-accounts/edit'),
]),
new Edit(baseFields),
],

stateProvider: new CallbackStateProvider<BankAccount>(async (bankAccount: CrudBankAccount, requestParameters: RequestParameters) => {
if (typeof window === 'undefined') {
// SSR, can't call Tauri API then.
return Promise.resolve([]);
}

if (bankAccount.name === 'list') {
const results = await getBankAccounts(requestParameters.page||1);
return Promise.resolve(results);
}

if (bankAccount.name === 'view' || bankAccount.name === 'edit') {
return getBankAccountById(requestParameters.id);
}

return Promise.resolve(null);
}),
stateProcessor: new CallbackStateProcessor<BankAccount>(() => {})
});
Empty file.
Empty file added src/lib/crud/cruds/TagsCrud.ts
Empty file.
Empty file.
37 changes: 12 additions & 25 deletions src/lib/db/bank_accounts.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
// @ts-ignore
import BankAccount from '$lib/entities/BankAccount';
import api_call from '$lib/utils/api_call';
import { writable } from 'svelte/store';
import type { Writable } from 'svelte/store';
import DeserializedOperation from "$lib/db/operations";
import BankAccountsCrud from "$lib/crud/cruds/BankAccountsCrud";

export const bankAccountsStore: Writable<BankAccount[]> = writable();

let bank_accounts_promise: Promise<BankAccount[]> | null = null;

async function getBankAccountPromise(): Promise<Array<BankAccount>> {
if (!bank_accounts_promise) {
bank_accounts_promise = getBankAccounts();
}

return bank_accounts_promise;
}

export async function getBankAccounts(): Promise<Array<BankAccount>> {
if (bank_accounts_promise) {
return await bank_accounts_promise;
}

const res: string = await api_call('bank_account_find_all');

const bank_accounts = JSON.parse(res).map((data: BankAccount) => {
Expand All @@ -44,15 +31,19 @@ export async function getBankAccountsAsChoices(): Promise<Array<{ name: string;
}

export async function getBankAccountById(id: number): Promise<BankAccount | null> {
const bank_accounts = await getBankAccountPromise();
const res: string = await api_call('bank_account_get_by_id', { id: id.toString() });

if (!res) {
throw 'No results from the API';
}

for (const bank_account of bank_accounts) {
if (bank_account.id === id) {
return bank_account;
}
const bank_account: BankAccount = JSON.parse(res);

if (!bank_account) {
throw new Error('Could not deserialize bank account.');
}

return null;
return bank_account;
}

export async function createBankAccount(bank_account: BankAccount): Promise<void> {
Expand All @@ -61,8 +52,6 @@ export async function createBankAccount(bank_account: BankAccount): Promise<void
if (isNaN(+id)) {
throw new Error('Internal error: API returned a non-number ID.');
}

bank_accounts_promise = null;
}

export async function updateBankAccount(bank_account: BankAccount) {
Expand All @@ -75,6 +64,4 @@ export async function updateBankAccount(bank_account: BankAccount) {
name: bank_account.name,
currency: bank_account.currency
});

bank_accounts_promise = null;
}

0 comments on commit 158d55e

Please sign in to comment.