Skip to content

Commit

Permalink
feat: delete all documents
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
  • Loading branch information
peterpeterparker committed Nov 19, 2023
1 parent 963687f commit ee35666
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/declarations/satellite/satellite.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export interface _SERVICE {
del_controllers: ActorMethod<[DeleteControllersArgs], Array<[Principal, Controller]>>;
del_custom_domain: ActorMethod<[string], undefined>;
del_doc: ActorMethod<[string, string, DelDoc], undefined>;
del_docs: ActorMethod<[string], undefined>;
del_rule: ActorMethod<[RulesType, string, DelDoc], undefined>;
deposit_cycles: ActorMethod<[DepositCyclesArgs], undefined>;
get_config: ActorMethod<[], Config>;
Expand Down
1 change: 1 addition & 0 deletions src/declarations/satellite/satellite.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export const idlFactory = ({ IDL }) => {
),
del_custom_domain: IDL.Func([IDL.Text], [], []),
del_doc: IDL.Func([IDL.Text, IDL.Text, DelDoc], [], []),
del_docs: IDL.Func([IDL.Text], [], []),
del_rule: IDL.Func([RulesType, IDL.Text, DelDoc], [], []),
deposit_cycles: IDL.Func([DepositCyclesArgs], [], []),
get_config: IDL.Func([], [Config], []),
Expand Down
1 change: 1 addition & 0 deletions src/declarations/satellite/satellite.factory.did.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export const idlFactory = ({ IDL }) => {
),
del_custom_domain: IDL.Func([IDL.Text], [], []),
del_doc: IDL.Func([IDL.Text, IDL.Text, DelDoc], [], []),
del_docs: IDL.Func([IDL.Text], [], []),
del_rule: IDL.Func([RulesType, IDL.Text, DelDoc], [], []),
deposit_cycles: IDL.Func([DepositCyclesArgs], [], []),
get_config: IDL.Func([], [Config], []),
Expand Down
22 changes: 22 additions & 0 deletions src/frontend/src/lib/api/satellites.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,25 @@ export const deleteAsset = async ({
const actor = await getSatelliteActor(satelliteId);
return actor.del_asset(collection, full_path);
};

export const deleteDocs = async ({
satelliteId,
collection
}: {
satelliteId: Principal;
collection: string;
}) => {
const { del_docs } = await getSatelliteActor(satelliteId);
return del_docs(collection);
};

export const deleteAssets = async ({
satelliteId,
collection
}: {
satelliteId: Principal;
collection: string;
}) => {
const { del_assets } = await getSatelliteActor(satelliteId);
return del_assets(collection);
};
6 changes: 3 additions & 3 deletions src/frontend/src/lib/components/assets/Asset.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { RULES_CONTEXT_KEY, type RulesContext } from '$lib/types/rules.context';
import ExternalLink from '$lib/components/ui/ExternalLink.svelte';
import DataHeader from '$lib/components/data/DataHeader.svelte';
import DataDelete from '$lib/components/data/DataDelete.svelte';
import DataKeyDelete from '$lib/components/data/DataKeyDelete.svelte';
const { store, resetData }: DataContext<AssetNoContent> =
getContext<DataContext<AssetNoContent>>(DATA_CONTEXT_KEY);
Expand Down Expand Up @@ -74,10 +74,10 @@
{key ?? ''}

<svelte:fragment slot="actions">
<DataDelete {deleteData}>
<DataKeyDelete {deleteData}>
<svelte:fragment slot="title">{$i18n.asset.delete}</svelte:fragment>
{key}
</DataDelete>
</DataKeyDelete>
</svelte:fragment>
</DataHeader>
</div>
Expand Down
19 changes: 19 additions & 0 deletions src/frontend/src/lib/components/data/DataCollectionDelete.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import type { Principal } from '@dfinity/principal';
import DataDelete from '$lib/components/data/DataDelete.svelte';
export let deleteData: (params: { collection: string; satelliteId: Principal }) => Promise<void>;
</script>

<DataDelete {deleteData}>
<slot name="title" slot="title" />

<p><slot /></p>
</DataDelete>

<style lang="scss">
p {
white-space: initial;
word-break: break-word;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { getContext } from 'svelte';
import { nonNullish } from '@dfinity/utils';
import { fade } from 'svelte/transition';
import DataActions from '$lib/components/data/DataActions.svelte';
const { store }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
Expand All @@ -19,7 +20,9 @@
<div transition:fade>
<DataFilter />
<DataOrder />
<!-- <DataActions>TODO</DataActions>-->
<DataActions>
<slot name="actions" />
</DataActions>
</div>
{/if}
</div>
Expand Down
13 changes: 1 addition & 12 deletions src/frontend/src/lib/components/data/DataDelete.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,5 @@
<Confirmation bind:visible on:junoYes={deleteSelectedData} on:junoNo={close}>
<slot name="title" slot="title" />

<Value>
<svelte:fragment slot="label">{$i18n.collections.key}</svelte:fragment>
<p><slot /></p>
</Value>
<slot />
</Confirmation>

<style lang="scss">
@use '../../styles/mixins/text';
p {
@include text.truncate;
}
</style>
25 changes: 25 additions & 0 deletions src/frontend/src/lib/components/data/DataKeyDelete.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
import { i18n } from '$lib/stores/i18n.store';
import Value from '$lib/components/ui/Value.svelte';
import type { Principal } from '@dfinity/principal';
import DataDelete from '$lib/components/data/DataDelete.svelte';
export let deleteData: (params: { collection: string; satelliteId: Principal }) => Promise<void>;
</script>

<DataDelete {deleteData}>
<slot name="title" slot="title" />

<Value>
<svelte:fragment slot="label">{$i18n.collections.key}</svelte:fragment>
<p><slot /></p>
</Value>
</DataDelete>

<style lang="scss">
@use '../../styles/mixins/text';
p {
@include text.truncate;
}
</style>
6 changes: 3 additions & 3 deletions src/frontend/src/lib/components/docs/DocForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import type { Doc } from '$declarations/satellite/satellite.did';
import type { Principal } from '@dfinity/principal';
import { deleteDoc } from '$lib/api/satellites.api';
import DataDelete from '$lib/components/data/DataDelete.svelte';
import DataKeyDelete from '$lib/components/data/DataKeyDelete.svelte';
const { store, reload }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
const { store: docsStore, resetData }: DataContext<Doc> =
Expand Down Expand Up @@ -131,10 +131,10 @@
{key ?? ''}

<svelte:fragment slot="actions">
<DataDelete {deleteData}>
<DataKeyDelete {deleteData}>
<svelte:fragment slot="title">{$i18n.document.delete}</svelte:fragment>
{key}
</DataDelete>
</DataKeyDelete>
</svelte:fragment>
</DataHeader>
{/if}
Expand Down
22 changes: 21 additions & 1 deletion src/frontend/src/lib/components/docs/Docs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { getContext, setContext } from 'svelte';
import { RULES_CONTEXT_KEY } from '$lib/types/rules.context';
import { isNullish, nonNullish } from '@dfinity/utils';
import { listDocs, satelliteVersion } from '$lib/api/satellites.api';
import { deleteDocs, listDocs, satelliteVersion } from '$lib/api/satellites.api';
import { toasts } from '$lib/stores/toasts.store';
import type { Doc as DocType } from '$declarations/satellite/satellite.did';
import { PAGINATION_CONTEXT_KEY, type PaginationContext } from '$lib/types/pagination.context';
Expand All @@ -18,6 +18,8 @@
import { compare } from 'semver';
import { listDocs008 } from '$lib/api/satellites.deprecated.api';
import IconNew from '$lib/components/icons/IconNew.svelte';
import type { Principal } from '@dfinity/principal';
import DataCollectionDelete from '$lib/components/data/DataCollectionDelete.svelte';
const { store }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
Expand Down Expand Up @@ -82,11 +84,29 @@
})();
const ADD_DOCUMENT_FEATURE = import.meta.env.VITE_DEV_FEATURES === 'true';
/**
* Delete data
*/
let deleteData: (params: { collection: string; satelliteId: Principal }) => Promise<void>;
$: deleteData = async (params: { collection: string; satelliteId: Principal }) => {
await deleteDocs(params);
resetData();
};
</script>

<div class="title">
<DataCollectionHeader>
{$i18n.datastore.documents}

<svelte:fragment slot="actions">
<DataCollectionDelete {deleteData}>
<svelte:fragment slot="title">{$i18n.document.delete_all}</svelte:fragment>
{$i18n.core.are_you_sure}
</DataCollectionDelete>
</svelte:fragment>
</DataCollectionHeader>
</div>

Expand Down
5 changes: 4 additions & 1 deletion src/frontend/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"start": "Start",
"stop": "Stop",
"save": "Save",
"more": "More options"
"more": "More options",
"are_you_sure": "Are you sure? This action is irreversible"
},
"canisters": {
"top_up": "Top-up",
Expand Down Expand Up @@ -282,6 +283,7 @@
"data": "Data",
"no_match": "No document matches your filters.",
"delete": "Delete document?",
"delete_all": "Delete all documents?",
"description": "Description",
"document_submission_success": "Document has been successfully submitted!",
"document_submission_failed": "Document submission failed!"
Expand Down Expand Up @@ -313,6 +315,7 @@
"updated": "Updated",
"no_match": "No asset matches your filters.",
"delete": "Delete asset?",
"delete_all": "Delete all assets?",
"description": "Description",
"full_path": "Full path"
},
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/src/lib/i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"start": "启动",
"stop": "停止",
"save": "Save",
"more": "More options"
"more": "More options",
"are_you_sure": "Are you sure? This action is irreversible"
},
"canisters": {
"top_up": "充值",
Expand Down Expand Up @@ -282,6 +283,7 @@
"data": "数据",
"no_match": "找不到满足条件的文档.",
"delete": "删除文档?",
"delete_all": "Delete all documents?",
"description": "描述",
"document_submission_success": "文件已成功提交!",
"document_submission_failed": "文件提交失败!"
Expand Down Expand Up @@ -313,6 +315,7 @@
"updated": "更新于",
"no_match": "找不到满足条件的资料.",
"delete": "删除资料?",
"delete_all": "Delete all assets?",
"description": "描述",
"full_path": "完整路徑"
},
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/src/lib/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface I18nCore {
stop: string;
save: string;
more: string;
are_you_sure: string;
}

interface I18nCanisters {
Expand Down Expand Up @@ -298,6 +299,7 @@ interface I18nDocument {
data: string;
no_match: string;
delete: string;
delete_all: string;
description: string;
document_submission_success: string;
document_submission_failed: string;
Expand Down Expand Up @@ -331,6 +333,7 @@ interface I18nAsset {
updated: string;
no_match: string;
delete: string;
delete_all: string;
description: string;
full_path: string;
}
Expand Down
1 change: 1 addition & 0 deletions src/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ service : () -> {
);
del_custom_domain : (text) -> ();
del_doc : (text, text, DelDoc) -> ();
del_docs : (text) -> ();
del_rule : (RulesType, text, DelDoc) -> ();
deposit_cycles : (DepositCyclesArgs) -> ();
get_config : () -> (Config);
Expand Down
18 changes: 18 additions & 0 deletions src/satellite/src/db/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,21 @@ fn assert_write_permission(

Ok(())
}

pub fn delete_docs(collection: &CollectionKey) -> Result<(), String> {
delete_docs_impl(collection)
}

fn delete_docs_impl(collection: &CollectionKey) -> Result<(), String> {
let rule = get_state_rule(collection)?;

let keys: Vec<Key> = get_state_docs(collection, &rule)?.iter()
.map(|(key, _)| key.clone())
.collect();

for key in keys {
delete_state_doc(collection, &key, &rule)?;
}

Ok(())
}
14 changes: 12 additions & 2 deletions src/satellite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod storage;
mod types;

use crate::controllers::store::get_admin_controllers;
use crate::db::store::{delete_doc, get_doc as get_doc_store, get_docs, insert_doc};
use crate::db::store::{delete_doc, get_doc as get_doc_store, get_docs, insert_doc, delete_docs};
use crate::db::types::interface::{DelDoc, SetDoc};
use crate::db::types::state::Doc;
use crate::guards::caller_is_admin_controller;
Expand All @@ -31,7 +31,7 @@ use crate::storage::store::{
commit_batch, create_batch, create_chunk, delete_asset, delete_assets, delete_domain,
get_config as get_storage_config, get_content_chunks, get_custom_domains, get_public_asset,
init_certified_assets, list_assets as list_assets_store, set_config as set_storage_config,
set_domain,
set_domain
};
use crate::storage::types::config::StorageConfigRewrites;
use crate::storage::types::domain::{CustomDomains, DomainName};
Expand Down Expand Up @@ -186,6 +186,16 @@ fn list_docs(collection: CollectionKey, filter: ListParams) -> ListResults<Doc>
}
}

#[update(guard = "caller_is_admin_controller")]
fn del_docs(collection: CollectionKey) {
let result = delete_docs(&collection);

match result {
Ok(_) => (),
Err(error) => trap(&["Documents cannot be deleted: ", &error].join("")),
}
}

/// Rules

#[query(guard = "caller_is_admin_controller")]
Expand Down

0 comments on commit ee35666

Please sign in to comment.