73 lines
1.8 KiB
TypeScript
Executable File
73 lines
1.8 KiB
TypeScript
Executable File
import { serve } from '@hono/node-server'
|
|
import { serveStatic } from '@hono/node-server/serve-static'
|
|
import { Hono } from 'hono'
|
|
import { PrismaClient } from '@prisma/client'
|
|
|
|
const app = new Hono()
|
|
const prisma = new PrismaClient()
|
|
|
|
// API: Alle Units abrufen
|
|
app.get('/api/units', async (c) => {
|
|
const units = await prisma.unit.findMany()
|
|
return c.json(units)
|
|
})
|
|
|
|
// API: Eine Unit erstellen
|
|
app.post('/api/units', async (c) => {
|
|
const body = await c.req.json()
|
|
const unit = await prisma.unit.create({ data: body })
|
|
return c.json(unit)
|
|
})
|
|
|
|
// API: Eine Unit bearbeiten
|
|
app.put('/api/units/:id', async (c) => {
|
|
const id = c.req.param('id')
|
|
const body = await c.req.json()
|
|
const unit = await prisma.unit.update({
|
|
where: { id },
|
|
data: body
|
|
})
|
|
return c.json(unit)
|
|
})
|
|
|
|
// API: Eine Unit löschen
|
|
app.delete('/api/units/:id', async (c) => {
|
|
const id = c.req.param('id')
|
|
await prisma.unit.delete({ where: { id } })
|
|
return c.json({ success: true })
|
|
})
|
|
|
|
// API: Events (Identisch zu Units)
|
|
app.get('/api/events', async (c) => {
|
|
const events = await prisma.event.findMany()
|
|
return c.json(events)
|
|
})
|
|
app.post('/api/events', async (c) => {
|
|
const body = await c.req.json()
|
|
const event = await prisma.event.create({ data: body })
|
|
return c.json(event)
|
|
})
|
|
app.put('/api/events/:id', async (c) => {
|
|
const id = c.req.param('id')
|
|
const body = await c.req.json()
|
|
const event = await prisma.event.update({ where: { id }, data: body })
|
|
return c.json(event)
|
|
})
|
|
app.delete('/api/events/:id', async (c) => {
|
|
const id = c.req.param('id')
|
|
await prisma.event.delete({ where: { id } })
|
|
return c.json({ success: true })
|
|
})
|
|
|
|
// Statische Dateien (Unser Dashboard)
|
|
app.use('/*', serveStatic({ root: './public' }))
|
|
|
|
const port = 3000
|
|
console.log(`Server is running on port ${port}`)
|
|
|
|
serve({
|
|
fetch: app.fetch,
|
|
port,
|
|
hostname: '0.0.0.0'
|
|
})
|