Init commit!

This commit is contained in:
Jennie Robinson Faber 2025-08-22 18:36:16 +01:00
commit 086d682592
34 changed files with 19249 additions and 0 deletions

View 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'
})
}
})

View 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'
})
}
})

View 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'
})
}
})

View 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'
})
}
})

View 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'
})
}
})