-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKostoAPIService.js
38 lines (30 loc) · 1.11 KB
/
KostoAPIService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require('express')();
const {Port} = require('./constants');
const cityController = require('./controllers/cityController');
express.get('/getPricesFor/:city',(req,res)=>
{
let cityName = req.params.city.trim();
(async () => {
let city = await cityController.FindCityInDB(cityName);
if(city){
res.send(await cityController.TransformDbDataToView(city, true));
return;
}
else{
let found = await cityController.FindCityOnWebAndInsertInDB(cityName);
if(!found){
console.log('City not found on the internet either.');
res.send('City not found :(');
return;
}
let city = await cityController.FindCityInDB(cityName);
res.send(await cityController.TransformDbDataToView(city, false));
return;
}
})().then(()=>{},(err)=>{
console.log(`Error encoutered : ${err}`);
res.send('City not found :(');
return;
});
});
express.listen(Port, ()=>{console.log(`Server is up on port ${Port};`);});