create objects page and routes

This commit is contained in:
Linus Miller 2025-11-24 23:07:31 +01:00
parent 60bdd909a7
commit 5c75af9aaa
9 changed files with 153 additions and 2 deletions

16
.bruno/BRF/Object.bru Normal file
View File

@ -0,0 +1,16 @@
meta {
name: Object
type: http
seq: 3
}
get {
url: {{base_url}}/api/objects/10
body: none
auth: inherit
}
settings {
encodeUrl: true
timeout: 0
}

16
.bruno/BRF/Objects.bru Normal file
View File

@ -0,0 +1,16 @@
meta {
name: Objects
type: http
seq: 4
}
get {
url: {{base_url}}/api/objects
body: none
auth: inherit
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -1,7 +1,7 @@
meta {
name: Result
type: http
seq: 2
seq: 5
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: Results
type: http
seq: 4
seq: 6
}
get {

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
/dump
/sie
# Logs
logs

View File

@ -0,0 +1,31 @@
import { h, type FunctionalComponent } from 'preact'
import { useEffect, useState } from 'preact/hooks'
import rek from 'rek'
interface Props {
objectId: number
}
const Result: FunctionalComponent<Props> = ({ objectId }) => {
const [transactions, setTransactions] = useState([])
useEffect(() => {
rek(`/api/objects/${objectId}`).then(setTransactions)
}, [objectId])
return (
<table>
<tbody>
{transactions.map((transaction) => (
<tr>
<td>{transaction.transactionDate.slice(0, 10)}</td>
<td>{transaction.accountNumber}</td>
<td>{transaction.amount}</td>
</tr>
))}
</tbody>
</table>
)
}
export default Result

View File

@ -0,0 +1,41 @@
import { h } from 'preact'
import { useEffect, useState } from 'preact/hooks'
import rek from 'rek'
import Head from './head.ts'
import Object from './object.tsx'
import s from './results_page.module.scss'
const ObjectsPage = () => {
const [objects, setObjects] = useState([])
const [currentObject, setCurrentObject] = useState(null)
useEffect(() => {
rek('/api/objects').then(setObjects)
}, [])
return (
<section>
<Head>
<title> : Objects</title>
</Head>
<h1>Objects</h1>
<div className={s.objects}>
{objects.map((object) => (
<button onClick={() => setCurrentObject(object)}>
{object.dimensionName}: {object.name}
</button>
))}
</div>
{currentObject ? (
<div>
<h2>{currentObject}</h2>
<Object objectId={currentObject.id} />
</div>
) : null}
</section>
)
}
export default ObjectsPage

View File

@ -1,4 +1,5 @@
import Start from './components/start_page.tsx'
import Objects from './components/objects_page.tsx'
import Results from './components/results_page.tsx'
export default [
@ -8,6 +9,12 @@ export default [
title: 'Start',
component: Start,
},
{
path: '/objects',
name: 'objects',
title: 'Objects',
component: Objects,
},
{
path: '/results',
name: 'results',

View File

@ -1,5 +1,6 @@
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../lib/knex.ts'
import StatusError from '../lib/status_error.ts'
export const FinancialYear = Type.Object({
year: Type.Number(),
@ -18,6 +19,44 @@ const apiRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
},
})
fastify.route({
url: '/objects',
method: 'GET',
handler() {
return knex('object AS o')
.select('o.id', 'o.number', 'o.name', 'd.number AS dimensionNumber', 'd.name AS dimensionName')
.innerJoin('dimension AS d', function () {
this.on('o.dimensionId', '=', 'd.id')
})
},
})
fastify.route({
url: '/objects/:id',
method: 'GET',
schema: {
params: Type.Object({
id: Type.Number(),
}),
},
async handler(req) {
// const object = knex('object').first('*').where('id', req.params.id)
// if (!object) throw new StatusError(404)
console.log(req.params.id)
return knex('transaction AS t')
.select('e.transactionDate', 't.accountNumber', 't.amount')
.innerJoin('transactions_to_objects AS to', function () {
this.on('t.id', 'to.transactionId')
})
.innerJoin('entry AS e', function () {
this.on('e.id', '=', 't.entryId')
})
.where('to.objectId', req.params.id)
},
})
fastify.route({
url: '/results',
method: 'GET',