Skip to content

Commit

Permalink
๐Ÿš€ ๐Ÿ’ธ
Browse files Browse the repository at this point in the history
  • Loading branch information
edudepetris committed Sep 23, 2018
0 parents commit 1938fef
Show file tree
Hide file tree
Showing 7 changed files with 2,444 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# OS X
.DS_Store*
Icon?
._*

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini

# Linux
.directory
*~


# npm
node_modules
package-lock.json
*.log
*.gz


# Coveralls
coverage

# Benchmarking
benchmarks/graphs

# editor
.vscode

dist/
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "eldolar",
"version": "1.0.0",
"description": "bank exchange by eldolar",
"main": "app.js",
"license": "MIT",
"dependencies": {
"@types/express": "^4.16.0",
"concurrently": "^4.0.1",
"express": "^4.16.3",
"mongoose": "^5.2.17",
"nodemon": "^1.18.4"
},
"scripts": {
"watch-ts": "tsc -w",
"watch-node": "nodemon dist/app.js",
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript, Node\" -c \"yello.bold, cyan.bold\" \"yarn run watch-ts\" \"yarn run watch-node\""
},
"devDependencies": {
"@types/mongoose": "^5.2.17"
}
}
28 changes: 28 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as express from "express";
import * as bodyParse from "body-parser";
import * as mongoose from 'mongoose';
import ExchangeRouter from './routers/exchangeRouter'

const app = express();

// set up mongoose
const URI: string = "mongodb://127.0.0.1:27017/eldolar";
mongoose.connect(process.env.MONGODB_URI || URI, (err: any) => {
if (err) {
console.log(err.message)
} else {
console.log("Successfully Connected")
}
})

// config
app.use(bodyParse.json());
app.set("port", 3000);

// routers
app.use('/api/v1/exchanges', ExchangeRouter)

// start
app.listen(app.get("port"), () => {
console.log("App is running on localhost:%d", app.get("port"));
})
16 changes: 16 additions & 0 deletions src/models/exchange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Schema, model } from 'mongoose';
import { Decimal128 } from 'bson';

export const ExchangeSchema = new Schema({
createdAt: { type: Date, required: true },
banks: [
{
name: String,
buy: { type: Decimal128, default: 0 },
sell: { type: Decimal128, default: 0 }
}
]
});

const Exchange = model("Exchange", ExchangeSchema);
export default Exchange;
33 changes: 33 additions & 0 deletions src/routers/ExchangeRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Router, Request, Response } from 'express';
import Exchange from '../models/exchange';

const router: Router = Router();

// - GET /exchanges # returns all exchanges
function allExchanges(req: Request, res: Response) : void {
Exchange.find({})
.then(exchanges => {
res.send(exchanges)
})
.catch(err => {
res.send(err)
})
}

// - POST /exchanges # insert new one
function addExchange(req: Request, res: Response) : void {
let exchange = new Exchange(req.body);

exchange.save()
.then(data => {
res.send(data)
})
.catch(err => {
res.send(err)
})
}

router.get('/', allExchanges);
router.post('/', addExchange);

export default router;
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"target": "es6",
"preserveConstEnums": true,
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Loading

0 comments on commit 1938fef

Please sign in to comment.