From 621782a3a811692b2d5e7dc6b3c9f80dcf5de3cb Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 7 Feb 2021 15:18:04 -0700 Subject: [PATCH] WIP: Implement backend in express Signed-off-by: William Brawner --- src/server/budget/controller.ts | 29 +++++++++++++++++++++++ src/server/budget/model.ts | 8 +++++++ src/server/budget/repository.ts | 4 ++++ src/server/categories/controller.ts | 25 ++++++++++++++++++++ src/server/db/index.ts | 16 +++++++++++++ src/server/db/migrations.ts | 0 src/server/index.ts | 34 +++++++++++++++++++++++++++ src/server/permissions/controller.ts | 25 ++++++++++++++++++++ src/server/transactions/controller.ts | 25 ++++++++++++++++++++ src/server/tsconfig.json | 20 ++++++++++++++++ src/server/tslint.json | 0 src/server/users/controller.ts | 25 ++++++++++++++++++++ src/server/utils.ts | 7 ++++++ 13 files changed, 218 insertions(+) create mode 100644 src/server/budget/controller.ts create mode 100644 src/server/budget/model.ts create mode 100644 src/server/budget/repository.ts create mode 100644 src/server/categories/controller.ts create mode 100644 src/server/db/index.ts create mode 100644 src/server/db/migrations.ts create mode 100644 src/server/index.ts create mode 100644 src/server/permissions/controller.ts create mode 100644 src/server/transactions/controller.ts create mode 100644 src/server/tsconfig.json create mode 100644 src/server/tslint.json create mode 100644 src/server/users/controller.ts create mode 100644 src/server/utils.ts diff --git a/src/server/budget/controller.ts b/src/server/budget/controller.ts new file mode 100644 index 0000000..a906d83 --- /dev/null +++ b/src/server/budget/controller.ts @@ -0,0 +1,29 @@ +import express from 'express'; + +const router = express.Router() + +router.get('/', (req, res) => { + +}); + +router.post('/', (req, res) => { + +}); + +router.get('/:id', (req, res) => { + +}); + +router.get('/:id/balance', (req, res) => { + +}); + +router.put('/:id', (req, res) => { + +}); + +router.delete('/:id', (req, res) => { + +}); + +export { router }; \ No newline at end of file diff --git a/src/server/budget/model.ts b/src/server/budget/model.ts new file mode 100644 index 0000000..9c524a3 --- /dev/null +++ b/src/server/budget/model.ts @@ -0,0 +1,8 @@ +import { randomId } from '../utils'; + +export class Budget { + id: string = randomId(); + name: string; + description: string; + currencyCode: string; +} diff --git a/src/server/budget/repository.ts b/src/server/budget/repository.ts new file mode 100644 index 0000000..6622373 --- /dev/null +++ b/src/server/budget/repository.ts @@ -0,0 +1,4 @@ + +export class BudgetRepository { + +} \ No newline at end of file diff --git a/src/server/categories/controller.ts b/src/server/categories/controller.ts new file mode 100644 index 0000000..e297e82 --- /dev/null +++ b/src/server/categories/controller.ts @@ -0,0 +1,25 @@ +import express from 'express'; + +const router = express.Router() + +router.get('/', (req, res) => { + +}); + +router.post('/', (req, res) => { + +}); + +router.get('/:id', (req, res) => { + +}); + +router.put('/:id', (req, res) => { + +}); + +router.delete('/:id', (req, res) => { + +}); + +export { router }; \ No newline at end of file diff --git a/src/server/db/index.ts b/src/server/db/index.ts new file mode 100644 index 0000000..c429932 --- /dev/null +++ b/src/server/db/index.ts @@ -0,0 +1,16 @@ +import path from 'path'; +import sqlite3 from 'sqlite3'; +import * as migrations from './migrations'; + + +export function db(dataDir: string): sqlite3.Database { + const db = new sqlite3.Database(path.join(dataDir, "twigs.db"), (err) => { + if (err != null) { + console.error("Failed to open db"); + console.error(err); + throw 'Failed to open db'; + } + + }); + return db +} \ No newline at end of file diff --git a/src/server/db/migrations.ts b/src/server/db/migrations.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/server/index.ts b/src/server/index.ts new file mode 100644 index 0000000..f3d947c --- /dev/null +++ b/src/server/index.ts @@ -0,0 +1,34 @@ +import express from 'express'; +import { router as budgetRouter } from './budget/controller' +import { router as categoryRouter } from './categories/controller' +import { router as permissionsRouter } from './permissions/controller' +import { router as transactionRouter } from './transactions/controller' +import { router as userRouter } from './users/controller' + +const port = process.env.PORT || 3000; +const app = express(); + +const dataDir = process.env.TWIGS_DATA || __dirname; + +app.use(express.static(__dirname + '/public')); + +// app.get('/', (req, res) => { +// console.log('hit: /'); +// res.send('test'); +// }) + +app.use('/api/budgets', budgetRouter); +app.use('/api/categories', categoryRouter); +app.use('/api/permissions', permissionsRouter); +app.use('/api/transactions', transactionRouter); +app.use('/api/users', userRouter); + +app.get('/*', (req, res) => { + res.sendFile(__dirname + '/public/index.html'); +}); + + +app.listen(port, () => { + console.log(`Twigs server listening at http://localhost:${port}`) + console.log(`Serving static content from ${__dirname}/public`) +}); \ No newline at end of file diff --git a/src/server/permissions/controller.ts b/src/server/permissions/controller.ts new file mode 100644 index 0000000..e297e82 --- /dev/null +++ b/src/server/permissions/controller.ts @@ -0,0 +1,25 @@ +import express from 'express'; + +const router = express.Router() + +router.get('/', (req, res) => { + +}); + +router.post('/', (req, res) => { + +}); + +router.get('/:id', (req, res) => { + +}); + +router.put('/:id', (req, res) => { + +}); + +router.delete('/:id', (req, res) => { + +}); + +export { router }; \ No newline at end of file diff --git a/src/server/transactions/controller.ts b/src/server/transactions/controller.ts new file mode 100644 index 0000000..e297e82 --- /dev/null +++ b/src/server/transactions/controller.ts @@ -0,0 +1,25 @@ +import express from 'express'; + +const router = express.Router() + +router.get('/', (req, res) => { + +}); + +router.post('/', (req, res) => { + +}); + +router.get('/:id', (req, res) => { + +}); + +router.put('/:id', (req, res) => { + +}); + +router.delete('/:id', (req, res) => { + +}); + +export { router }; \ No newline at end of file diff --git a/src/server/tsconfig.json b/src/server/tsconfig.json new file mode 100644 index 0000000..1cf1ee5 --- /dev/null +++ b/src/server/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "target": "es6", + "noImplicitAny": true, + "moduleResolution": "node", + "sourceMap": true, + "outDir": "../../dist", + "baseUrl": ".", + "paths": { + "*": [ + "node_modules/*" + ] + } + }, + "include": [ + "**/*" + ] +} diff --git a/src/server/tslint.json b/src/server/tslint.json new file mode 100644 index 0000000..e69de29 diff --git a/src/server/users/controller.ts b/src/server/users/controller.ts new file mode 100644 index 0000000..e297e82 --- /dev/null +++ b/src/server/users/controller.ts @@ -0,0 +1,25 @@ +import express from 'express'; + +const router = express.Router() + +router.get('/', (req, res) => { + +}); + +router.post('/', (req, res) => { + +}); + +router.get('/:id', (req, res) => { + +}); + +router.put('/:id', (req, res) => { + +}); + +router.delete('/:id', (req, res) => { + +}); + +export { router }; \ No newline at end of file diff --git a/src/server/utils.ts b/src/server/utils.ts new file mode 100644 index 0000000..b391a58 --- /dev/null +++ b/src/server/utils.ts @@ -0,0 +1,7 @@ +import { randomInt } from 'crypto'; + +const CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + +export function randomId(length = 32): string { + return Array.from(new Array(length), () => { CHARACTERS[randomInt(CHARACTERS.length)] }).join(''); +} \ No newline at end of file