Skip to content

Commit

Permalink
完善「编辑页面」
Browse files Browse the repository at this point in the history
- 修改文章接口
  • Loading branch information
slTrust committed Feb 20, 2021
1 parent 9ba29b7 commit 2910e1a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pages/api/v1/posts/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {NextApiHandler} from 'next';
import {withSession} from '../../../../lib/withSession';
import {getDatabaseConnection} from '../../../../lib/getDatabaseConnection';


const Posts: NextApiHandler = withSession(async (req, res) => {
if (req.method === 'PATCH') {
const connection = await getDatabaseConnection();
const {title, content, id} = req.body;
const post = await connection.manager.findOne<Post>('Post', id);
post.title = title;
post.content = content;
const user = req.session.get('currentUser');
if (!user) {
res.statusCode = 401;
res.end();
return;
}
await connection.manager.save(post);
res.json(post);
}

});
export default Posts;
3 changes: 1 addition & 2 deletions pages/posts/[id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ const PostsEdit: NextPage<Props> = (props) => {
<button type="submit">提交</button>
</div>,
submit: {
request: formData => axios.patch(`/api/v1/posts/${id}`, formData),
request: formData => axios.patch(`/api/v1/posts/${id}`, {...formData, id}),
success: () => {
window.alert('提交成功');
window.location.href = '/posts';
}
}
});
Expand Down

0 comments on commit 2910e1a

Please sign in to comment.