Skip to content

Commit 73d8f59

Browse files
committed
chore: request database 4 test
1 parent e75f950 commit 73d8f59

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

server/db/notes.db

0 Bytes
Binary file not shown.

web/src/components/NoteCard/index.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import React from 'react';
2-
import { Card } from 'antd';
2+
import { Card, Button } from 'antd';
33
import styles from './NoteCard.module.scss';
44

55
interface NoteCardProps {
66
title: string;
77
content: string;
88
date: string;
9+
onDelete: () => void;
910
}
1011

11-
const NoteCard: React.FC<NoteCardProps> = ({ title, content, date }) => {
12+
const NoteCard: React.FC<NoteCardProps> = ({ title, content, date, onDelete }) => {
1213
return (
1314
<Card className={styles.noteCard} hoverable>
1415
<h3>{title}</h3>
1516
<p>{content}</p>
16-
<small>{date}</small>
17+
<small>{new Date(date).toLocaleString()}</small>
18+
<Button type="link" danger onClick={onDelete}>
19+
删除
20+
</Button>
1721
</Card>
1822
);
1923
};

web/src/components/NoteList/index.tsx

+20-24
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import NoteCard from '../NoteCard';
3+
import { fetchNotes, deleteNote } from '../../utils/api';
34
import styles from './NoteList.module.scss';
45

56
interface Note {
67
id: number;
78
title: string;
89
content: string;
9-
date: string;
10+
created_at: string;
1011
}
1112

12-
const notes: Note[] = [
13-
{
14-
id: 1,
15-
title: '工作笔记',
16-
content: '今天完成了项目需求分析...',
17-
date: '2023-10-01',
18-
},
19-
{
20-
id: 2,
21-
title: '学习笔记',
22-
content: '学习了 React Hooks 的使用...',
23-
date: '2023-10-02',
24-
},
25-
{
26-
id: 3,
27-
title: '生活笔记',
28-
content: '周末去公园散步,放松心情...',
29-
date: '2023-10-03',
30-
},
31-
];
32-
3313
const NoteList: React.FC = () => {
14+
const [notes, setNotes] = useState<Note[]>([]);
15+
16+
useEffect(() => {
17+
const loadNotes = async () => {
18+
const data = await fetchNotes();
19+
setNotes(data);
20+
};
21+
loadNotes();
22+
}, []);
23+
24+
const handleDelete = async (id: number) => {
25+
await deleteNote(id);
26+
setNotes(notes.filter((note) => note.id !== id));
27+
};
28+
3429
return (
3530
<div className={styles.noteList}>
3631
{notes.map((note) => (
3732
<NoteCard
3833
key={note.id}
3934
title={note.title}
4035
content={note.content}
41-
date={note.date}
36+
date={note.created_at}
37+
onDelete={() => handleDelete(note.id)}
4238
/>
4339
))}
4440
</div>

web/src/utils/api.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import axios from 'axios';
2+
3+
const API_BASE_URL = 'http://localhost:9999/api/notes';
4+
5+
export const fetchNotes = async () => {
6+
const response = await axios.get(API_BASE_URL);
7+
return response.data;
8+
};
9+
10+
export const createNote = async (title: string, content: string) => {
11+
const response = await axios.post(API_BASE_URL, { title, content });
12+
return response.data;
13+
};
14+
15+
export const deleteNote = async (id: number) => {
16+
const response = await axios.delete(`${API_BASE_URL}/${id}`);
17+
return response.data;
18+
};

0 commit comments

Comments
 (0)