Uploading the Repo
This commit is contained in:
+1
-18
@@ -1,18 +1 @@
|
||||
# Build and Release Folders
|
||||
bin-debug/
|
||||
bin-release/
|
||||
[Oo]bj/
|
||||
[Bb]in/
|
||||
|
||||
# Other files and folders
|
||||
.settings/
|
||||
|
||||
# Executables
|
||||
*.swf
|
||||
*.air
|
||||
*.ipa
|
||||
*.apk
|
||||
|
||||
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
|
||||
# should NOT be excluded as they contain compiler settings and other important
|
||||
# information for Eclipse / Flash Builder.
|
||||
/node_modules
|
||||
Generated
+2638
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "vscode",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "nodemon --watch ./src/index.ts --exec ts-node src/index.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@types/express": "^4.17.13",
|
||||
"express": "^4.17.3",
|
||||
"pg": "^8.7.3",
|
||||
"ts-node": "^10.6.0",
|
||||
"typeorm": "^0.2.44",
|
||||
"typescript": "^4.6.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
} from "typeorm";
|
||||
import { Client } from "./Client";
|
||||
import { Person } from "./utils/Person";
|
||||
|
||||
@Entity("banker")
|
||||
export class Banker extends Person {
|
||||
@Column({
|
||||
unique: true,
|
||||
length: 10,
|
||||
})
|
||||
employee_number: string;
|
||||
|
||||
@ManyToMany(() => Client)
|
||||
@JoinTable({
|
||||
name: "bankers_clients",
|
||||
joinColumn: {
|
||||
name: "banker",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
inverseJoinColumn: {
|
||||
name: "client",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
})
|
||||
clients: Client[];
|
||||
|
||||
@CreateDateColumn()
|
||||
created_at: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updated_at: Date;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
OneToMany,
|
||||
ManyToMany,
|
||||
} from "typeorm";
|
||||
import { Transaction } from "./Transaction";
|
||||
import { Person } from "./utils/Person";
|
||||
import { Banker } from "./Banker";
|
||||
|
||||
|
||||
@Entity("client")
|
||||
export class Client extends Person {
|
||||
@Column()
|
||||
balance: number;
|
||||
|
||||
@Column({ default: true, name: "active" })
|
||||
is_active: boolean;
|
||||
|
||||
@Column({ type: "simple-json", nullable: true })
|
||||
additional_info: {
|
||||
age: number;
|
||||
hair_color: string;
|
||||
};
|
||||
|
||||
@Column({ type: "simple-array", default: [] })
|
||||
family_members: string[];
|
||||
|
||||
@OneToMany(() => Transaction, (transaction) => transaction.client)
|
||||
transactions: Transaction[];
|
||||
|
||||
@ManyToMany(()=> Banker)
|
||||
banker: Banker[]
|
||||
|
||||
@CreateDateColumn()
|
||||
created_at: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updated_at: Date;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from "typeorm";
|
||||
import { Client } from "./Client";
|
||||
export enum TransactionsTypes {
|
||||
DEPOSIT = "deposit",
|
||||
WITHDRAW = "witdraw",
|
||||
}
|
||||
|
||||
@Entity("transaction")
|
||||
export class Transaction extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column({ type: "enum", enum: TransactionsTypes })
|
||||
type: string;
|
||||
|
||||
@Column({ type: "numeric" })
|
||||
ammount: number;
|
||||
|
||||
@ManyToOne(() => Client, (client) => client.transactions, {
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
@JoinColumn({ name: "client_id" })
|
||||
client: Client;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { BaseEntity, Entity, Column, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity("person")
|
||||
export class Person extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
first_name: string;
|
||||
|
||||
@Column()
|
||||
last_name: string;
|
||||
|
||||
@Column({
|
||||
unique: true,
|
||||
})
|
||||
email: string;
|
||||
|
||||
@Column({ unique: true, length: 10 })
|
||||
card_number: string;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import express from "express";
|
||||
import { createConnection } from "typeorm";
|
||||
import { Banker } from "./entities/Banker";
|
||||
import { Client } from "./entities/Client";
|
||||
import { Transaction } from "./entities/Transaction";
|
||||
import { createClientRouter } from "./routers/create_client";
|
||||
import { createBankerRouter } from "./routers/create_banker";
|
||||
import { createTransactionRouter } from "./routers/create_transaction";
|
||||
import { connectBankerToClientRouter } from "./routers/connect_client_to_banler";
|
||||
import { DeleteClientRouter } from "./routers/delete_client";
|
||||
import { fetchClientRouter } from "./routers/fetch_client";
|
||||
|
||||
const app = express();
|
||||
|
||||
const main = async () => {
|
||||
try {
|
||||
await createConnection({
|
||||
type: "postgres",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
username: "postgres",
|
||||
password: "password",
|
||||
database: "typeorm",
|
||||
entities: [Client, Banker, Transaction],
|
||||
synchronize: true,
|
||||
});
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
app.use(createClientRouter);
|
||||
app.use(createBankerRouter);
|
||||
app.use(createTransactionRouter);
|
||||
app.use(connectBankerToClientRouter);
|
||||
app.use(DeleteClientRouter);
|
||||
app.use(fetchClientRouter);
|
||||
|
||||
app.listen(8080, () => {
|
||||
console.log("Now running on port 8080");
|
||||
});
|
||||
|
||||
console.log("connected to Postgres");
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw new Error("Unable to connect to DB");
|
||||
}
|
||||
};
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,23 @@
|
||||
import express from "express";
|
||||
import { Banker } from "../entities/Banker";
|
||||
import { Client } from "../entities/Client";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/api/banker/:bankerId/client/:clientId", async (req, res) => {
|
||||
const { bankerId, clientId } = req.params;
|
||||
|
||||
const client = await Client.findOne(parseInt(clientId));
|
||||
|
||||
const banker = await Banker.findOne(parseInt(bankerId));
|
||||
|
||||
if (!banker || !client) {
|
||||
return res.json({ msg: "Banker or client not exist" });
|
||||
}
|
||||
banker.clients = [client];
|
||||
await banker.save();
|
||||
|
||||
return res.json({ msg: "banker connceted successfully" });
|
||||
});
|
||||
|
||||
export { router as connectBankerToClientRouter };
|
||||
@@ -0,0 +1,20 @@
|
||||
import express from "express";
|
||||
import { Banker } from "../entities/Banker";
|
||||
|
||||
const router = express.Router();
|
||||
router.post("/api/banker", async (req, res) => {
|
||||
const { firstName, lastName, email, cardNumber, employeeNumber } = req.body;
|
||||
|
||||
const banker = Banker.create({
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
email,
|
||||
card_number: cardNumber,
|
||||
employee_number: employeeNumber,
|
||||
});
|
||||
|
||||
await banker.save();
|
||||
return res.json(banker);
|
||||
});
|
||||
|
||||
export { router as createBankerRouter };
|
||||
@@ -0,0 +1,22 @@
|
||||
import express from "express";
|
||||
import { Client } from "../entities/Client";
|
||||
|
||||
const router = express.Router();
|
||||
router.post("/api/client", async (req, res) => {
|
||||
const { firstName, lastName, email, cardNumber, balance } = req.body;
|
||||
|
||||
const client = Client.create({
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
email,
|
||||
card_number: cardNumber,
|
||||
balance,
|
||||
});
|
||||
|
||||
await client.save();
|
||||
return res.json(client);
|
||||
});
|
||||
|
||||
export { router as createClientRouter };
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import express from "express";
|
||||
import { Client } from "../entities/Client";
|
||||
import { Transaction, TransactionsTypes } from "../entities/Transaction";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/api/client/:clientId/transaction", async (req, res) => {
|
||||
const { clientId } = req.params;
|
||||
const { type, ammount } = req.body;
|
||||
|
||||
const client = await Client.findOne(parseInt(clientId));
|
||||
if (!client) {
|
||||
return res.json({
|
||||
msg: "client not found",
|
||||
});
|
||||
}
|
||||
const transaction = Transaction.create({
|
||||
type,
|
||||
ammount,
|
||||
client,
|
||||
});
|
||||
await transaction.save();
|
||||
if (type === TransactionsTypes.DEPOSIT) {
|
||||
client.balance = client.balance + ammount;
|
||||
} else if (type === TransactionsTypes.WITHDRAW) {
|
||||
client.balance = client.balance - ammount;
|
||||
}
|
||||
await client.save();
|
||||
return res.json({ msg: "transaction successful" });
|
||||
});
|
||||
|
||||
export { router as createTransactionRouter };
|
||||
@@ -0,0 +1,13 @@
|
||||
import express from "express";
|
||||
import { Client } from "../entities/Client";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.delete("/api/client/:clientId", async (req, res) => {
|
||||
const { clientId } = req.params;
|
||||
|
||||
const response = await Client.delete(parseInt(clientId));
|
||||
return res.json(response);
|
||||
});
|
||||
|
||||
export { router as DeleteClientRouter };
|
||||
@@ -0,0 +1,19 @@
|
||||
import express from "express";
|
||||
import { Client } from "../entities/Client";
|
||||
import { createQueryBuilder } from "typeorm";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/api/clients", async (req, res) => {
|
||||
const client = await createQueryBuilder("client")
|
||||
.select("client.first_name")
|
||||
.addSelect("client.last_name")
|
||||
.from(Client, "client")
|
||||
.leftJoinAndSelect("client.transactions", "transactions")
|
||||
.where("client.id= :clientId", { clientId: 2 })
|
||||
.getOne();
|
||||
|
||||
return res.json(client);
|
||||
});
|
||||
|
||||
export { router as fetchClientRouter };
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es6",
|
||||
"es2017",
|
||||
"esnext.asynciterable"
|
||||
],
|
||||
"skipLibCheck": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"moduleResolution": "node",
|
||||
"removeComments": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"resolveJsonModule": true,
|
||||
"baseUrl": "."
|
||||
},
|
||||
"exclude": ["node_modules"],
|
||||
"include": ["./src/**/*.tsx", "./src/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user