Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarberio83 committed Feb 2, 2020
0 parents commit 8a8d27c
Show file tree
Hide file tree
Showing 14 changed files with 1,353 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
node_modules/
package-lock.json
**/*.map
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node dist/server.js
1,000 changes: 1,000 additions & 0 deletions data/cars.json

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions dist/controllers/carController.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions dist/models/car.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions dist/router/carRouter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions dist/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions dist/utils/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Arima-Exercise",
"version": "1.0.0",
"description": "Arima exercise API",
"main": "index.js",
"scripts": {
"ts": "tsc -w",
"dev": "nodemon ./dist/server.js",
"start": "tsc && node ./dist/server.js"
},
"author": "Juan Barberio",
"license": "ISC",
"dependencies": {
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.21.2",
"jw-paginate": "^1.0.4",
"mongoose": "^5.8.10",
"morgan": "^1.9.1"
},
"devDependencies": {
"@types/compression": "^1.0.1",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.2",
"@types/helmet": "0.0.45",
"@types/mongoose": "^5.7.0",
"@types/morgan": "^1.7.37",
"@types/node": "^13.5.3",
"nodemon": "^2.0.2",
"typescript": "^3.7.5"
}
}
48 changes: 48 additions & 0 deletions src/controllers/carController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Request, Response } from 'express';
import { Car } from '../models/car';
import paginate = require('jw-paginate');

export class CarController {

public async getCars(req: Request, res: Response): Promise<void> {
const page = (parseInt(req.query.page)) ? parseInt(req.query.page) : 1;
const size = (parseInt(req.query.size)) ? parseInt(req.query.size) : 20;

let filterDate = {};
if (req.query.sDate && req.query.eDate) {
filterDate = {
"date": {
"$gte": new Date(req.query.sDate),
"$lte": new Date(req.query.eDate)
}
};
}

await Car.countDocuments(filterDate, (error, totalCount) => {
if (error) {
this._errorFetchingData(res);
} else {
Car.find(filterDate, function (error, data) {
if (error) {
this._errorFetchingData(res);
} else {
const pager = paginate(totalCount, page, size);
res.json({
error: false,
pager: pager,
data: data
});
}
}).skip(size * (page - 1)).limit(size);
}
});
}

private _errorFetchingData(res) : void{
res.json({
error: true,
message: "Error fetching data"
});
}

}
13 changes: 13 additions & 0 deletions src/models/car.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Schema, model } from 'mongoose';

const CarSchema = new Schema({
brand: { type: String, required: true },
model: { type: String, required: true },
price: { type: Number, required: true },
date: { type: Date, required: true },
color: { type: String, required: true },
km: { type: Number, required: true },
vin: { type: String, required: true }
});

export const Car = model('Car', CarSchema);
22 changes: 22 additions & 0 deletions src/router/carRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Router } from 'express';
import * as carController from "../controllers/carController";

class CarRouter {

public router: Router;
private carService: carController.CarController;

constructor() {
this.carService = new carController.CarController();
this.router = Router();
this._routes();
}

private _routes() {
this.router.get('/', this.carService.getCars);
}

}

const carRouter = new CarRouter();
export default carRouter.router;
56 changes: 56 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as compression from 'compression';
import * as cors from 'cors';
import * as express from 'express';
import * as helmet from 'helmet';
import * as mongoose from 'mongoose';
import * as morgan from 'morgan';
import CarRouter from './router/carRouter';


class Server {

public app: express.Application;

constructor() {
this.app = express();
this._setConfig();
this._setMongoConfig();
this._setRoutes();
}

public start(): void {
this.app.listen(this.app.get('port'), () => {
console.log('Listening on port', this.app.get('port'));
})
}

private _setConfig(): void {
this.app.set('port', process.env.PORT || 3000);
this.app.use(morgan('dev'));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use(helmet());
this.app.use(compression());
this.app.use(cors());
}

private _setMongoConfig(): void {
const MONGODB_URL = 'mongodb+srv://arima:[email protected]/arimaexercisedb?retryWrites=true&w=majority';
//const MONGODB_URL = 'mongodb://localhost:27017/arimaexercisedb';
mongoose.set('useFindAndModify', true)
mongoose.connect(MONGODB_URL || process.env.MONGODB_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
}).then(db => console.log("Arima exercice database connected!"))
}

private _setRoutes(): void {
const router: express.Router = express.Router();
this.app.use('/api/cars', CarRouter);
}

}

const server = new Server();
server.start();
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"sourceMap": true,
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"baseUrl": "./src"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}

0 comments on commit 8a8d27c

Please sign in to comment.