-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8a8d27c
Showing
14 changed files
with
1,353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.vscode | ||
node_modules/ | ||
package-lock.json | ||
**/*.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node dist/server.js |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |