feat:添加本地数据库
This commit is contained in:
108
src/services/transactionService.js
Normal file
108
src/services/transactionService.js
Normal file
@@ -0,0 +1,108 @@
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { getDb } from '../lib/sqlite'
|
||||
|
||||
const statusMap = {
|
||||
pending: 0,
|
||||
synced: 1,
|
||||
error: 2,
|
||||
}
|
||||
|
||||
const normalizeStatus = (value) => {
|
||||
if (value === 1) return 'synced'
|
||||
if (value === 2) return 'error'
|
||||
return 'pending'
|
||||
}
|
||||
|
||||
const mapRow = (row) => ({
|
||||
id: row.id,
|
||||
amount: Number(row.amount),
|
||||
merchant: row.merchant,
|
||||
category: row.category || 'Uncategorized',
|
||||
date: row.date,
|
||||
note: row.note || '',
|
||||
syncStatus: normalizeStatus(row.sync_status),
|
||||
})
|
||||
|
||||
export const fetchTransactions = async () => {
|
||||
const db = await getDb()
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT id, amount, merchant, category, date, note, sync_status
|
||||
FROM transactions
|
||||
ORDER BY date DESC
|
||||
`,
|
||||
)
|
||||
return (result?.values || []).map(mapRow)
|
||||
}
|
||||
|
||||
export const insertTransaction = async (payload) => {
|
||||
const db = await getDb()
|
||||
const id = payload.id || uuidv4()
|
||||
const sanitized = {
|
||||
merchant: payload.merchant?.trim() || 'Unknown',
|
||||
category: payload.category?.trim() || 'Uncategorized',
|
||||
note: payload.note?.trim() || '',
|
||||
date: payload.date,
|
||||
amount: Number(payload.amount),
|
||||
syncStatus: statusMap[payload.syncStatus] ?? statusMap.pending,
|
||||
}
|
||||
|
||||
await db.run(
|
||||
`
|
||||
INSERT INTO transactions (id, amount, merchant, category, date, note, sync_status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
[
|
||||
id,
|
||||
sanitized.amount,
|
||||
sanitized.merchant,
|
||||
sanitized.category,
|
||||
sanitized.date,
|
||||
sanitized.note || null,
|
||||
sanitized.syncStatus,
|
||||
],
|
||||
)
|
||||
|
||||
return {
|
||||
id,
|
||||
...sanitized,
|
||||
syncStatus: payload.syncStatus || 'pending',
|
||||
}
|
||||
}
|
||||
|
||||
export const updateTransaction = async (payload) => {
|
||||
const db = await getDb()
|
||||
await db.run(
|
||||
`
|
||||
UPDATE transactions
|
||||
SET amount = ?, merchant = ?, category = ?, date = ?, note = ?, sync_status = ?
|
||||
WHERE id = ?
|
||||
`,
|
||||
[
|
||||
Number(payload.amount),
|
||||
payload.merchant?.trim() || 'Unknown',
|
||||
payload.category?.trim() || 'Uncategorized',
|
||||
payload.date,
|
||||
payload.note?.trim() || null,
|
||||
statusMap[payload.syncStatus] ?? statusMap.pending,
|
||||
payload.id,
|
||||
],
|
||||
)
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT id, amount, merchant, category, date, note, sync_status
|
||||
FROM transactions
|
||||
WHERE id = ?
|
||||
`,
|
||||
[payload.id],
|
||||
)
|
||||
|
||||
const updated = result?.values?.[0]
|
||||
return updated ? mapRow(updated) : null
|
||||
}
|
||||
|
||||
export const deleteTransaction = async (id) => {
|
||||
const db = await getDb()
|
||||
await db.run(`DELETE FROM transactions WHERE id = ?`, [id])
|
||||
}
|
||||
Reference in New Issue
Block a user