Skip to content

Commit

Permalink
feat: add edit functions to refs
Browse files Browse the repository at this point in the history
  • Loading branch information
xnought committed Nov 28, 2023
1 parent fac83b1 commit f4875fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions backend/src/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ class EditBody(CamelModel):
old_name: str # so we can identify the exact row we need to change
new_name: str
new_content: str | None = None
new_refs: str | None = None
24 changes: 18 additions & 6 deletions backend/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,33 @@ def edit_protein_entry(body: EditBody):
os.rename(pdb_file_name(body.old_name), pdb_file_name(body.new_name))

with Database() as db:
# if we have content/markdown, then update it, otherwise just update the name
if body.new_content is not None:
if body.new_name != body.old_name:
db.execute(
"""UPDATE proteins SET name = %s, content = %s WHERE name = %s""",
"""UPDATE proteins SET name = %s WHERE name = %s""",
[
body.new_name,
body.old_name,
],
)

if body.new_content is not None:
db.execute(
"""UPDATE proteins SET content = %s WHERE name = %s""",
[
str_to_bytea(body.new_content),
body.old_name,
],
)
else:

if body.new_refs is not None:
db.execute(
"""UPDATE proteins SET name = %s WHERE name = %s""",
[body.new_name, body.old_name],
"""UPDATE proteins SET refs = %s WHERE name = %s""",
[
str_to_bytea(body.new_refs),
body.old_name,
],
)

except Exception:
return UploadError.WRITE_ERROR

Expand Down
33 changes: 21 additions & 12 deletions frontend/src/routes/edit/[proteinName]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import { goto } from "$app/navigation";
import { onMount } from "svelte";
import { formatProteinName, humanReadableProteinName } from "$lib/format";
import ArticleEditor from "$lib/ArticleEditor.svelte";
// key difference, here we get the information, then populate it in the upload form that can be edited
// and reuploaded/edited
export let data;
let name: string;
let content: string | null;
// store original too so we can see if the user changed/edited the content
let ogContent: string;
let content: string;
let ogRefs: string;
let refs: string;
let uploadError: UploadError | undefined;
let entry: ProteinEntry | null = null;
let error = false;
Expand All @@ -26,7 +33,12 @@
error = true;
} else {
name = humanReadableProteinName(entry.name);
content = entry.content!;
content = entry.content ?? "";
ogContent = content; // log original content
refs = entry.refs ?? "";
ogRefs = refs; // log original refs
}
});
</script>
Expand Down Expand Up @@ -54,29 +66,25 @@
{/if}
</div>
<div>
<Label for="content" class="block mb-2">Protein Article</Label>
<Textarea
id="content"
placeholder="Enter markdown..."
rows={10}
bind:value={content}
/>
<ArticleEditor bind:content bind:refs />
</div>
<div>
<Button
on:click={async () => {
if (entry) {
if (
name === humanReadableProteinName(entry.name) &&
content === entry.content
content === ogContent &&
refs === ogRefs
)
return; // no changes no edits!

try {
const err = await Backend.editProteinEntry({
newName: name,
oldName: entry.name,
newContent: content,
newContent: content !== ogContent ? content : undefined,
newRefs: refs !== ogRefs ? refs : undefined,
});
if (err) {
uploadError = err;
Expand All @@ -91,7 +99,8 @@
}
}}
disabled={(name === humanReadableProteinName(entry.name) &&
content === entry.content) ||
content === ogContent &&
refs === ogRefs) ||
name.length === 0}>Edit Protein</Button
>

Expand Down

0 comments on commit f4875fe

Please sign in to comment.