Init commit!
This commit is contained in:
commit
086d682592
34 changed files with 19249 additions and 0 deletions
29
server/api/transactions/[id].delete.js
Normal file
29
server/api/transactions/[id].delete.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const id = getRouterParam(event, 'id')
|
||||
const collection = await getCollection('transactions')
|
||||
|
||||
// Delete the transaction
|
||||
const result = await collection.deleteOne({ _id: id })
|
||||
|
||||
if (result.deletedCount === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'Transaction not found'
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
deletedCount: result.deletedCount
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting transaction:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to delete transaction'
|
||||
})
|
||||
}
|
||||
})
|
||||
40
server/api/transactions/[id].put.js
Normal file
40
server/api/transactions/[id].put.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const id = getRouterParam(event, 'id')
|
||||
const body = await readBody(event)
|
||||
const collection = await getCollection('transactions')
|
||||
|
||||
// Update document
|
||||
const updateData = {
|
||||
...body,
|
||||
_id: id,
|
||||
id: undefined,
|
||||
updatedAt: new Date()
|
||||
}
|
||||
|
||||
const result = await collection.replaceOne(
|
||||
{ _id: id },
|
||||
updateData
|
||||
)
|
||||
|
||||
if (result.matchedCount === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'Transaction not found'
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
modifiedCount: result.modifiedCount
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating transaction:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to update transaction'
|
||||
})
|
||||
}
|
||||
})
|
||||
42
server/api/transactions/bulk.put.js
Normal file
42
server/api/transactions/bulk.put.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const transactions = await readBody(event)
|
||||
const collection = await getCollection('transactions')
|
||||
|
||||
// Clear existing transactions and insert new ones
|
||||
await collection.deleteMany({})
|
||||
|
||||
if (transactions.length > 0) {
|
||||
// Convert transactions for MongoDB
|
||||
const mongoTransactions = transactions.map(transaction => ({
|
||||
...transaction,
|
||||
_id: transaction.id,
|
||||
id: undefined,
|
||||
date: new Date(transaction.date),
|
||||
endDate: transaction.endDate ? new Date(transaction.endDate) : null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}))
|
||||
|
||||
const result = await collection.insertMany(mongoTransactions)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
insertedCount: result.insertedCount
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
insertedCount: 0
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error bulk updating transactions:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to bulk update transactions'
|
||||
})
|
||||
}
|
||||
})
|
||||
23
server/api/transactions/index.get.js
Normal file
23
server/api/transactions/index.get.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const collection = await getCollection('transactions')
|
||||
const transactions = await collection.find({}).toArray()
|
||||
|
||||
// Convert MongoDB _id to id for frontend compatibility
|
||||
const formattedTransactions = transactions.map(transaction => ({
|
||||
...transaction,
|
||||
id: transaction._id.toString(),
|
||||
_id: undefined
|
||||
}))
|
||||
|
||||
return formattedTransactions
|
||||
} catch (error) {
|
||||
console.error('Error fetching transactions:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to fetch transactions'
|
||||
})
|
||||
}
|
||||
})
|
||||
30
server/api/transactions/index.post.js
Normal file
30
server/api/transactions/index.post.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const body = await readBody(event)
|
||||
const collection = await getCollection('transactions')
|
||||
|
||||
// Convert id to _id for MongoDB
|
||||
const transactionData = {
|
||||
...body,
|
||||
_id: body.id,
|
||||
id: undefined,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}
|
||||
|
||||
const result = await collection.insertOne(transactionData)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
id: result.insertedId.toString()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating transaction:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to create transaction'
|
||||
})
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue