Add API router, index
CI/CD Pipeline / test (push) Failing after 1m47s
CI/CD Pipeline / deploy-internal (push) Has been skipped

This commit is contained in:
chris committed 2026-08-28 20:19:35 -04:00
1 parent fea861f042
commit 223ab79d21
4 files changed
+94 -2

No files matched your search

-2
View File
@@ -1,2 +0,0 @@
DATABASE_URL='postgres://chris:DB_d3v%24pw@localhost:5432/booktest'
PORT=8300
+35
View File
@@ -0,0 +1,35 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { buildCategoryTree, CategoryNode } from './tree.utility.ts';
describe('Tree Utility - buildCategoryTree()', () => {
it('should transform a flat list of ltree rows into a nested JSON structure', () => {
const flatNodes: CategoryNode[] = [
{ id: 1, name: 'Root', path: 'Top' },
{ id: 3, name: 'Astronomy', path: 'Top.Science.Astronomy' },
{ id: 2, name: 'Science', path: 'Top.Science' },
{ id: 4, name: 'Gardening', path: 'Top.Hobbies.Gardening' },
{ id: 5, name: 'Hobbies', path: 'Top.Hobbies' }
];
const tree = buildCategoryTree(flatNodes);
// Verify root nodes count
assert.equal(tree.length, 1);
assert.equal(tree[0].name, 'Root');
// Verify second level ("Science" and "Hobbies")
const rootChildren = tree[0].children;
assert.equal(rootChildren.length, 2);
const scienceNode = rootChildren.find(c => c.name === 'Science');
const hobbiesNode = rootChildren.find(c => c.name === 'Hobbies');
assert.ok(scienceNode);
assert.ok(hobbiesNode);
// Verify third level deeply nested child ("Astronomy")
assert.equal(scienceNode.children.length, 1);
assert.equal(scienceNode.children[0].name, 'Astronomy');
});
});
+35
View File
@@ -0,0 +1,35 @@
import { Router, Request, Response } from 'express';
import { CategoryRepository } from './category.repository.ts';
import { buildCategoryTree } from './tree.utility.ts';
export function createCategoryRouter(repository: CategoryRepository): Router {
const router = Router();
/**
* GET /api/categories/tree/:path?
* Fetches all descendants of a path and returns them formatted as a tree.
* If no path parameter is provided, it defaults to the root level.
*/
router.get('/tree/:path?', async (req: Request, params: Response): Promise<void> => {
try {
const rawPath = req.params.path;
const parentPath = Array.isArray(rawPath)
? rawPath[0]
: (rawPath || 'Top'); // default fallback root value in the database
// Fetch flat database rows from our repository
const flatRows = await repository.findAllDescendants(parentPath);
// Format data into nested layout
const nestedTree = buildCategoryTree(flatRows);
params.json({ success: true, data: nestedTree });
} catch (error) {
console.error('Failed to resolve category tree:', error);
params.status(500).json({ success: false, error: 'Internal Server Error' });
}
});
return router;
}
+24
View File
@@ -0,0 +1,24 @@
import express from 'express';
import { Pool } from 'pg';
import { CategoryRepository } from './category.repository.ts';
import { createCategoryRouter } from './category.router.ts';
const app = express();
const port = process.env.API_PORT || 8300;
// Initialize your database pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
app.use(express.json());
// Inject the database pool into the repository layer
const categoryRepository = new CategoryRepository(pool);
// Attach your category endpoints to the Express app
app.use('/api/categories', createCategoryRouter(categoryRepository));
app.listen(port, () => {
console.log(`🚀 API workspace server running smoothly on http://localhost:${port}`);
});