diff --git a/.env b/.env deleted file mode 100644 index ef7ff9f..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -DATABASE_URL='postgres://chris:DB_d3v%24pw@localhost:5432/booktest' -PORT=8300 \ No newline at end of file diff --git a/api/src/category.router.test.ts b/api/src/category.router.test.ts new file mode 100644 index 0000000..9aad1a1 --- /dev/null +++ b/api/src/category.router.test.ts @@ -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'); + }); +}); diff --git a/api/src/category.router.ts b/api/src/category.router.ts new file mode 100644 index 0000000..40ccdf1 --- /dev/null +++ b/api/src/category.router.ts @@ -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 => { + 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; +} diff --git a/api/src/index.ts b/api/src/index.ts new file mode 100644 index 0000000..923f2e0 --- /dev/null +++ b/api/src/index.ts @@ -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}`); +});