Skip to content

Commit

Permalink
删除 blog
Browse files Browse the repository at this point in the history
  • Loading branch information
slTrust committed Feb 20, 2021
1 parent 2910e1a commit 976df46
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
6 changes: 6 additions & 0 deletions pages/api/v1/posts/[id].tsx
Expand Up @@ -18,6 +18,12 @@ const Posts: NextApiHandler = withSession(async (req, res) => {
}
await connection.manager.save(post);
res.json(post);
} else if (req.method === 'DELETE') {
const id = req.query.id.toString();
const connection = await getDatabaseConnection();
const result = await connection.manager.delete('Post', id);
res.statusCode = result.affected >= 0 ? 200 : 400;
res.end();
}

});
Expand Down
50 changes: 36 additions & 14 deletions pages/posts/[id].tsx
@@ -1,38 +1,58 @@
import React from 'react';
import React, {useCallback} from 'react';
import {GetServerSideProps, GetServerSidePropsContext, NextPage} from 'next';
import {getDatabaseConnection} from '../../lib/getDatabaseConnection';
import {Post} from '../../src/entity/Post';
import marked from 'marked';
import Link from 'next/link';
import {withSession} from '../../lib/withSession';
import axios from 'axios';
import {useRouter} from 'next/router';

type Props = {
id: number;
post: Post;
currentUser: User | null;
}
const postsShow: NextPage<Props> = (props) => {
const {post, currentUser} = props;
const {post, currentUser, id} = props;
const router = useRouter()
const onRemove = useCallback(() => {
axios.delete(`/api/v1/posts/${id}`).then(() => {
window.alert('删除成功');
router.push('/posts')
}, () => {
window.alert('删除失败');
});
}, [id]);
return (
<>
<div className="wrapper">
<header>
<h1>{post.title}</h1>
{currentUser &&
<p>
<p className="actions">
<Link href="/posts/[id]/edit" as={`/posts/${post.id}/edit`}><a>编辑</a></Link>
<button className="deleteButton" onClick={onRemove}>删除</button>
</p>
}
</header>
<article className="markdown-body" dangerouslySetInnerHTML={{__html: marked(post.content)}}>
</article>
</div>
<style jsx>{`
.actions > *{
margin: 4px;
}
.actions > *:first-child{
margin-left: 0;
}
.wrapper{
max-width: 800px;
margin: 16px auto;
padding: 0 16px;
}
h1{padding-bottom: 16px; border-bottom: 1px solid #666;}
.deleteButton{color:red;background:transparent;border:none;}
`}</style>
</>
);
Expand All @@ -41,14 +61,16 @@ const postsShow: NextPage<Props> = (props) => {
export default postsShow;

export const getServerSideProps: GetServerSideProps<any, { id: string }> = withSession(
async (context: GetServerSidePropsContext) => {
const connection = await getDatabaseConnection();
const post = await connection.manager.findOne('Post', context.params.id);
const currentUser = (context.req as any).session.get('currentUser') || null;
return {
props: {
post: JSON.parse(JSON.stringify(post)),
currentUser
}
};
});
async (context: GetServerSidePropsContext) => {
const connection = await getDatabaseConnection();
const id = context.params.id;
const post = await connection.manager.findOne('Post', id);
const currentUser = (context.req as any).session.get('currentUser') || null;
return {
props: {
id: parseInt(id.toString()),
post: JSON.parse(JSON.stringify(post)),
currentUser
}
};
});

0 comments on commit 976df46

Please sign in to comment.