Initial Commit
This commit is contained in:
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
0
backend/api/controllers/.gitkeep
Normal file
0
backend/api/controllers/.gitkeep
Normal file
83
backend/api/controllers/tasks.ts
Normal file
83
backend/api/controllers/tasks.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import { Request, Response, Router } from 'express';
|
||||
import { DeleteResult, getManager, UpdateResult } from 'typeorm';
|
||||
import { Task } from '../entities/Task';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/tasks', async (_req: Request, res: Response) => {
|
||||
try {
|
||||
const taskRepository = getManager().getRepository(Task);
|
||||
await taskRepository.find({
|
||||
order: { id: 'DESC' },
|
||||
}).then((tasks: Task[]) => {
|
||||
res.status(200).send(tasks);
|
||||
}).catch((error) => {
|
||||
res.status(400).send(error);
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).send(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/tasks', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const taskRepository = getManager().getRepository(Task);
|
||||
const task: Task = new Task();
|
||||
task.title = req.body.title;
|
||||
task.done = req.body.done;
|
||||
task.dueDate = req.body.dueDate;
|
||||
|
||||
await taskRepository.save(task).then((result: Task) => {
|
||||
return res.status(200).send({
|
||||
message: `Task successfully created`,
|
||||
response: result,
|
||||
});
|
||||
}).catch((error) => {
|
||||
res.status(400).send(error);
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).send(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/tasks/:id', (req: Request, res: Response) => {
|
||||
try {
|
||||
const taskRepository = getManager().getRepository(Task);
|
||||
const task: Task = new Task();
|
||||
task.title = req.body.title;
|
||||
task.done = req.body.done;
|
||||
task.dueDate = req.body.dueDate;
|
||||
taskRepository.update(
|
||||
req.params.id,
|
||||
task,
|
||||
).then((updatedTask: UpdateResult) => {
|
||||
res.status(200).send({
|
||||
message: `Task successfully updated`,
|
||||
response: updatedTask,
|
||||
});
|
||||
}).catch((error) => {
|
||||
res.status(400).send(error);
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).send(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/tasks/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const taskRepository = getManager().getRepository(Task);
|
||||
const taskEntity = await taskRepository.findOne(req.params.id);
|
||||
if (taskEntity) {
|
||||
taskRepository.delete(req.params.id).then((result: DeleteResult) => {
|
||||
res.status(200).send({
|
||||
message: `Task successfully deleted`,
|
||||
response: result,
|
||||
});
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send(error);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
36
backend/api/entities/Task.ts
Normal file
36
backend/api/entities/Task.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity, PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Task {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Column({
|
||||
default: false,
|
||||
})
|
||||
done: boolean;
|
||||
|
||||
@Column()
|
||||
dueDate: Date;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
|
||||
columns: {
|
||||
updatedAt: {
|
||||
updateDate: true,
|
||||
},
|
||||
};
|
||||
}
|
0
backend/api/migrations/.gitkeep
Normal file
0
backend/api/migrations/.gitkeep
Normal file
37
backend/api/migrations/1548630212872-MigrationTask.ts
Normal file
37
backend/api/migrations/1548630212872-MigrationTask.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
|
||||
|
||||
export class MigrationTask1548630212872 implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.createTable(new Table({
|
||||
name: 'task',
|
||||
columns: [
|
||||
{
|
||||
name: 'id',
|
||||
type: 'int',
|
||||
isPrimary: true,
|
||||
}, {
|
||||
name: 'title',
|
||||
type: 'varchar',
|
||||
}, {
|
||||
name: 'done',
|
||||
type: 'boolean',
|
||||
}, {
|
||||
name: 'dueDate',
|
||||
type: 'datetime',
|
||||
}, {
|
||||
name: 'createdAt',
|
||||
type: 'datetime',
|
||||
}, {
|
||||
name: 'updatedAt',
|
||||
type: 'datetime',
|
||||
},
|
||||
],
|
||||
}), true);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
console.log(queryRunner);
|
||||
}
|
||||
|
||||
}
|
8
backend/api/router.ts
Normal file
8
backend/api/router.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Router } from 'express';
|
||||
import taskRouter from './controllers/tasks';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use(taskRouter);
|
||||
|
||||
export default router;
|
29
backend/api/server.ts
Normal file
29
backend/api/server.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import * as bodyParser from 'body-parser';
|
||||
import * as express from 'express';
|
||||
import * as cors from 'cors';
|
||||
import 'reflect-metadata';
|
||||
import { Connection, createConnection } from 'typeorm';
|
||||
import router from './router';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors());
|
||||
app.use(bodyParser.json());
|
||||
app.use((_req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
|
||||
next();
|
||||
});
|
||||
app.use('/api', router);
|
||||
app.set('port', 2000);
|
||||
|
||||
createConnection().then(async (connection: Connection) => {
|
||||
app.listen(app.get('port'), () => {
|
||||
console.log(`Node API is running at http://localhost:${app.get('port')} in ${app.get('env')} mode`);
|
||||
console.log(`Connected to your ${connection.options.type} database`);
|
||||
console.log('Press CTRL-C to stop\n');
|
||||
});
|
||||
}).catch((error) => console.error(`TypeORM connection ${error}`));
|
||||
|
||||
export default app;
|
0
backend/api/subscribers/.gitkeep
Normal file
0
backend/api/subscribers/.gitkeep
Normal file
11
backend/nodemon.json
Normal file
11
backend/nodemon.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"ignore": [
|
||||
"**/*.test.ts",
|
||||
"node_modules"
|
||||
],
|
||||
"watch": [
|
||||
"api"
|
||||
],
|
||||
"exec": "npm start",
|
||||
"ext": "ts"
|
||||
}
|
23
backend/ormconfig.json
Normal file
23
backend/ormconfig.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"type": "postgres",
|
||||
"host": "localhost",
|
||||
"port": 5432,
|
||||
"username": "db",
|
||||
"password": "db",
|
||||
"database": "db",
|
||||
"synchronize": true,
|
||||
"entities": [
|
||||
"api/entities/*.ts"
|
||||
],
|
||||
"subscribers": [
|
||||
"api/subscribers/*.ts"
|
||||
],
|
||||
"migrations": [
|
||||
"api/migrations/*.ts"
|
||||
],
|
||||
"cli": {
|
||||
"entitiesDir": "api/entities",
|
||||
"migrationsDir": "api/migrations",
|
||||
"subscribersDir": "api/subscribers"
|
||||
}
|
||||
}
|
17748
backend/package-lock.json
generated
Normal file
17748
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
49
backend/package.json
Normal file
49
backend/package.json
Normal file
@ -0,0 +1,49 @@
|
||||
{
|
||||
"name": "microfrontend-api",
|
||||
"author": "Billy Lando <billy_lando@yahoo.com>",
|
||||
"version": "0.0.1",
|
||||
"description": "ExpressJS, NodeJS microfrontend API",
|
||||
"scripts": {
|
||||
"dev": "nodemon",
|
||||
"start": "ts-node --inspect=5858 -r tsconfig-paths/register api/server.ts",
|
||||
"test": "npx jest --watchAll",
|
||||
"migration:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate -n",
|
||||
"migration:run": "ts-node ./node_modules/typeorm/cli.js migration:run",
|
||||
"migration:revert": "ts-node ./node_modules/typeorm/cli.js migration:revert"
|
||||
},
|
||||
"jest": {
|
||||
"transform": {
|
||||
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
|
||||
},
|
||||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)",
|
||||
"moduleFileExtensions": [
|
||||
"ts",
|
||||
"tsx",
|
||||
"js",
|
||||
"json"
|
||||
]
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.18.3",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.16.4",
|
||||
"mysql": "^2.16.0",
|
||||
"pg": "^8.16.0",
|
||||
"typeorm": "^0.2.25"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.17.0",
|
||||
"@types/cors": "^2.8.18",
|
||||
"@types/express": "^4.16.0",
|
||||
"@types/jest": "^23.3.13",
|
||||
"@types/supertest": "^2.0.7",
|
||||
"jest": "^24.5.0",
|
||||
"nodemon": "^1.18.9",
|
||||
"supertest": "^3.4.2",
|
||||
"ts-jest": "^23.10.5",
|
||||
"ts-node": "^7.0.1",
|
||||
"tsconfig-paths": "^3.7.0",
|
||||
"typescript": "^3.2.2"
|
||||
}
|
||||
}
|
0
backend/test/.gitkeep
Normal file
0
backend/test/.gitkeep
Normal file
69
backend/test/api/controllers/tasks.test.ts
Normal file
69
backend/test/api/controllers/tasks.test.ts
Normal file
@ -0,0 +1,69 @@
|
||||
|
||||
import supertest = require('supertest');
|
||||
import { Task } from '../../../api/entities/Task';
|
||||
import app from '../../../api/server';
|
||||
import { initConnection, stopConnection } from '../../helper';
|
||||
|
||||
describe('/api/tasks', () => {
|
||||
let task: Task;
|
||||
beforeAll(async () => {
|
||||
task = {
|
||||
id: 1,
|
||||
title: 'Buy a new NodeJS book',
|
||||
dueDate: new Date('Januar 29, 2019 00:00:00'),
|
||||
done: false,
|
||||
} as Task;
|
||||
await initConnection();
|
||||
});
|
||||
|
||||
it('should return a 200 response for /api/tasks', () => {
|
||||
return supertest(app).get('/api/tasks')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
it('should return a 404 response for /api/lorem', () => {
|
||||
return supertest(app).get('/api/lorem').then((response) => {
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a 200 response for POST /api/tasks and delete the entry', (done) => {
|
||||
return supertest(app)
|
||||
.post('/api/tasks')
|
||||
.send(task)
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
expect(res.body.message).toBe('Task successfully created');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should UPDATE /api/tasks/1 correctly', (done) => {
|
||||
return supertest(app)
|
||||
.put('/api/tasks/1')
|
||||
.send({
|
||||
dueDate: new Date('Februar 1, 2019 00:00:00'),
|
||||
})
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
expect(res.body.message).toBe('Task successfully updated');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await stopConnection();
|
||||
});
|
||||
});
|
31
backend/test/helper.ts
Normal file
31
backend/test/helper.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Connection, ConnectionOptions, createConnection } from 'typeorm';
|
||||
|
||||
let connection: Connection;
|
||||
let initPromise: Promise<void>;
|
||||
|
||||
const dbConfig = {
|
||||
type: 'mysql',
|
||||
host: 'localhost',
|
||||
port: 3306,
|
||||
username: 'root',
|
||||
password: '',
|
||||
database: '',
|
||||
synchronise: false,
|
||||
entities: [''],
|
||||
} as ConnectionOptions;
|
||||
|
||||
export function initConnection() {
|
||||
if (initPromise) {
|
||||
return initPromise;
|
||||
}
|
||||
|
||||
initPromise = createConnection(dbConfig).then((con) => {
|
||||
connection = con;
|
||||
}).catch((error) => { throw error; });
|
||||
|
||||
return initPromise;
|
||||
}
|
||||
|
||||
export function stopConnection(): Promise<void> {
|
||||
return connection.close();
|
||||
}
|
8
backend/test/index.test.ts
Normal file
8
backend/test/index.test.ts
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
import 'jest';
|
||||
|
||||
describe('Jest Tests', () => {
|
||||
test('Verify Tests Works', () => {
|
||||
expect(true).toBeTruthy();
|
||||
});
|
||||
});
|
30
backend/tsconfig.json
Normal file
30
backend/tsconfig.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "./",
|
||||
"sourceMap": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noImplicitAny": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"target": "es6",
|
||||
"lib": [
|
||||
"es5",
|
||||
"es6"
|
||||
],
|
||||
"paths": {
|
||||
"*": [
|
||||
"node_modules/*",
|
||||
"./types/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
],
|
||||
"include": [
|
||||
"./**/*"
|
||||
]
|
||||
}
|
121
backend/tslint.json
Normal file
121
backend/tslint.json
Normal file
@ -0,0 +1,121 @@
|
||||
{
|
||||
"defaultSeverity": "error",
|
||||
"extends": [
|
||||
"tslint:recommended"
|
||||
],
|
||||
"jsRules": {},
|
||||
"rules": {
|
||||
"arrow-return-shorthand": true,
|
||||
"callable-types": true,
|
||||
"class-name": true,
|
||||
"comment-format": [
|
||||
true,
|
||||
"check-space"
|
||||
],
|
||||
"curly": true,
|
||||
"deprecation": {
|
||||
"severity": "warn"
|
||||
},
|
||||
"eofline": true,
|
||||
"forin": true,
|
||||
"import-spacing": true,
|
||||
"indent": [
|
||||
true,
|
||||
"spaces"
|
||||
],
|
||||
"interface-over-type-literal": true,
|
||||
"label-position": true,
|
||||
"max-line-length": [
|
||||
true,
|
||||
140
|
||||
],
|
||||
"member-access": false,
|
||||
"member-ordering": [
|
||||
true,
|
||||
{
|
||||
"order": [
|
||||
"static-field",
|
||||
"instance-field",
|
||||
"static-method",
|
||||
"instance-method"
|
||||
]
|
||||
}
|
||||
],
|
||||
"no-arg": true,
|
||||
"no-bitwise": true,
|
||||
"no-console": [
|
||||
true,
|
||||
"debug",
|
||||
"info",
|
||||
"time",
|
||||
"timeEnd",
|
||||
"trace"
|
||||
],
|
||||
"no-construct": true,
|
||||
"no-debugger": true,
|
||||
"no-duplicate-super": true,
|
||||
"no-empty": false,
|
||||
"no-empty-interface": true,
|
||||
"no-eval": true,
|
||||
"no-inferrable-types": [
|
||||
true,
|
||||
"ignore-params"
|
||||
],
|
||||
"no-misused-new": true,
|
||||
"no-non-null-assertion": true,
|
||||
"no-redundant-jsdoc": true,
|
||||
"no-shadowed-variable": true,
|
||||
"no-string-literal": false,
|
||||
"no-string-throw": true,
|
||||
"no-switch-case-fall-through": true,
|
||||
"no-trailing-whitespace": true,
|
||||
"no-unnecessary-initializer": true,
|
||||
"no-unused-expression": true,
|
||||
"no-use-before-declare": true,
|
||||
"no-var-keyword": true,
|
||||
"object-literal-sort-keys": false,
|
||||
"one-line": [
|
||||
true,
|
||||
"check-open-brace",
|
||||
"check-catch",
|
||||
"check-else",
|
||||
"check-whitespace"
|
||||
],
|
||||
"prefer-const": true,
|
||||
"quotemark": [
|
||||
true,
|
||||
"single"
|
||||
],
|
||||
"radix": true,
|
||||
"semicolon": [
|
||||
true,
|
||||
"always"
|
||||
],
|
||||
"triple-equals": [
|
||||
true,
|
||||
"allow-null-check"
|
||||
],
|
||||
"typedef-whitespace": [
|
||||
true,
|
||||
{
|
||||
"call-signature": "nospace",
|
||||
"index-signature": "nospace",
|
||||
"parameter": "nospace",
|
||||
"property-declaration": "nospace",
|
||||
"variable-declaration": "nospace"
|
||||
}
|
||||
],
|
||||
"unified-signatures": true,
|
||||
"variable-name": false,
|
||||
"whitespace": [
|
||||
true,
|
||||
"check-branch",
|
||||
"check-decl",
|
||||
"check-operator",
|
||||
"check-separator",
|
||||
"check-type"
|
||||
],
|
||||
"no-output-on-prefix": true
|
||||
},
|
||||
"rulesDirectory": []
|
||||
}
|
Reference in New Issue
Block a user