Added post deletion

This commit is contained in:
Xodrium 2023-02-11 02:11:45 -05:00
parent abceb346e3
commit 6c205a0ad3
7 changed files with 110 additions and 21 deletions

View file

View file

@ -3,7 +3,7 @@
import PostButton from '$lib/components/PostButton.svelte';
import PostBody from '$lib/components/PostBody.svelte';
export let success, username, content, upvotes, downvotes, id;
export let success, username, content, upvotes, downvotes, id, isAuthor;
let query = (id) ? `/post/${id}` : '';
@ -24,6 +24,20 @@
downvotes = j.data.down;
})
}
function deletePost(v) {
fData = (new FormData());
fData.append('id',id);
fetch('/api/postDelete', {
method: 'POST',
body: fData
}).then(async x => {
window.location.href = '/';
})
}
</script>
<style>
@ -66,17 +80,25 @@
<span slot="footer">
<PostButton
clickFunc={() => vote('up')}
data={upvotes * 1}
data={upvotes * 1 + ' Yes'}
icon='/upvote.svg'
/>
<PostButton
clickFunc={() => vote('down')}
data={downvotes * 1}
data={downvotes * 1 + ' No'}
icon='/downvote.svg'
/>
{#if isAuthor}
<PostButton
clickFunc={() => deletePost()}
data={'Delete'}
icon='/delete.svg'
/>
{/if}
{#if id}
<PostButton
href='/post/{id}'
data={'View'}
icon='/view.svg'
/>
{/if}

View file

@ -3,7 +3,8 @@ const rowCount = 5;
const AUTH_ACTIONS = [
'postCreate',
'fileCreate',
'vote'
'vote',
'postDelete'
];
const fileSizeLimit = 1024*1024*16;
@ -100,6 +101,8 @@ backend.register = async ({user, pass, pass2}) => {
passHash
])
await updateUser({user: user[0].username});
return { success: 'Successfully created account.', location: '/'};
}
@ -155,7 +158,18 @@ backend.postCreate = async ({content, user}) => {
return {'success': 'Your post has been broadcasted!', 'href': `/post/${id}` };
}
backend.postGet = async ({id}) => {
backend.postDelete = async ({id, user}) => {
await db.run('DELETE FROM post WHERE username = ? AND id = ?', [
user,
id
])
return {'success': 'Your post has been deleted!', 'href': `/post/${id}` };
}
backend.postGet = async ({id, cookies }) => {
var posts = await db.all('SELECT * from post WHERE id = ?', [
id
])
@ -164,7 +178,9 @@ backend.postGet = async ({id}) => {
return {'success': 'Post does not exist.'}
}
return {data: posts[0]};
var user = (await backend.token({cookies})).data;
return {data: posts[0], isAuthor: posts[0].username == user};
}
backend.userGet = async ({user}) => {

View file

@ -8,7 +8,7 @@ export async function load({ fetch, params, url }) {
await new Promise(resolve => setTimeout(resolve, 100));
const res = await fetch(`/api/postGet?id=${id}`);
const postJson = (await res.json()).data;
const postJson = (await res.json());
console.log(postJson);

View file

@ -4,12 +4,12 @@
/** @type {import('./$types').PageData} */
export let data;
</script>
<Post
success={data.success}
username={data.username}
content={data.content}
upvotes={data.upvotes}
downvotes={data.downvotes}
id={data.id}
success={data.data.success}
username={data.data.username}
content={data.data.content}
upvotes={data.data.upvotes}
downvotes={data.data.downvotes}
id={data.data.id}
isAuthor={data.isAuthor}
></Post>

View file

@ -32,14 +32,15 @@
<h2>Posts</h2>
{#each data.postJson.data as post}
{#each data.postJson as data}
<Post
success={post.success}
username={post.username}
content={post.content}
upvotes={post.upvotes}
downvotes={post.downvotes}
id={post.id}
success={data.data.success}
username={data.data.username}
content={data.data.content}
upvotes={data.data.upvotes}
downvotes={data.data.downvotes}
id={data.data.id}
isAuthor={data.isAuthor}
></Post>
{/each}