Skip to content
Permalink
1139228c1a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
208 lines (196 sloc) 5.5 KB
// dependecies
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
// server app setup
const port = 3000;
const app = express();
// database setup
const sqlite3 = require('sqlite3').verbose();
//path.resolve(__dirname, 'db', 'database2.db')
let db = new sqlite3.Database(':memory:');
// body parser setup
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.json());
// database tables setup
db.run(
`
CREATE TABLE IF NOT EXISTS locations (
id INTEGER PRIMARY KEY,
user_email TEXT NOT NULL,
title TEXT NOT NULL,
longitude REAL NOT NULL,
latitude READ NOT NULL
);`,
[],
(err) => {
if (err) console.log(err.message);
}
);
db.run(
`
CREATE TABLE IF NOT EXISTS forecastcards (
id INTEGER PRIMARY KEY,
user_email TEXT NOT NULL,
forecast_date TEXT NOT NULL,
icon TEXT NOT NULL,
forecast_location TEXT NOT NULL,
temperature TEXT NOT NULL,
summary TEXT NOT NULL,
humidity TEXT NOT NULL,
wind_speed TEXT NOT NULL,
wind_direction TEXT NOT NULL,
water_level TEXT NOT NULL
);`,
[],
(err) => {
if (err) console.log(err.message);
}
);
// route: base
app.get('/', (req, res) => {
res.status(200);
res.send('Welcome to a basic express App');
});
// route: locations/:id
// method: GET
// params:
// id: user email to filter locations
// summary: gets all locations for specified user email(id)
// response: json
app.get('/locations/:id', (req, res) => {
const email = req.params.id;
const sql = `SELECT * FROM locations WHERE user_email='${email}';`;
db.all(sql, [], (err, rows) => {
if (err) {
res.status(400).json({ error: err.message });
return;
} else {
res.json(rows);
}
});
});
// route: locations
// method: POST
// summary: insert body data into database table locations
// body example:
// {
// "user_email": "armandas.bark@gmail.com",
// "title": "this is my title",
// "longitude": 1.56666,
// "latitude": -13.1222
// }
app.post('/locations', (req, res) => {
if (req.body) {
const sql = `INSERT INTO locations(user_email,title,longitude,latitude) VALUES ('${req.body.user_email}','${req.body.title}','${req.body.longitude}','${req.body.latitude}');`;
db.run(sql, [], function(err) {
if (err) {
res.status(400).json({ error: err.message });
return;
} else {
res.status(200).send('OK');
}
});
}
});
// route: locations/:id/:title
// method: DELETE
// summary: deletes a database record where user email and location title matches given parameters
// params:
// id: user email to delete matched records
// title: location title to delete matched records
app.delete('/locations/:id/:title', (req, res) => {
const email = req.params.id;
const title = req.params.title;
const sql = `DELETE FROM locations WHERE user_email='${email}' AND title='${title}';`;
if (email && title) {
db.run(sql, [], function(err) {
if (err) {
res.status(500).send('Fail');
console.log(err.message);
} else {
res.status(200).send('OK');
}
});
}
});
// route: forecastcards/:id
// method: GET
// params:
// id: user email to filter forecast cards
// summary: gets all forecast cards for specified user email(id)
// response: json
app.get('/forecastcards/:id', (req, res) => {
const email = req.params.id;
const sql = `SELECT * FROM forecastcards WHERE user_email='${email}';`;
if (email) {
db.all(sql, [], (err, rows) => {
if (err) {
res.status(400).json({ error: err.message });
return;
} else {
res.json(rows);
}
});
} else {
res.status(500).send('No user email attached');
}
});
// route: forecastcards
// method: POST
// summary: insert body data into database table forecastcards
// body example:
// {
// "user_email": "armandas.bark@gmail.com",
// "forecast_date": "this is my title",
// "icon": 1.56666,
// "forecast_location": "London",
// "temperature": "45.4",
// "summary": "sumamrtasdasd",
// "humidity": "45%",
// "wind_speed": "1m/s",
// "wind_direction": "5*",
// "water_level": "5.2m"
// }
app.post('/forecastcards', (req, res) => {
const s = req.body;
if (req.body) {
const sql = `INSERT INTO forecastcards(user_email,forecast_date,icon,forecast_location,temperature,summary,humidity,wind_speed,wind_direction,water_level) VALUES ('${s.user_email}','${s.forecast_date}','${s.icon}','${s.forecast_location}','${s.temperature}','${s.summary}','${s.humidity}','${s.wind_speed}','${s.wind_direction}','${s.water_level}');`;
db.run(sql, [], function(err) {
if (err) {
res.status(500).send('Fail');
console.log(err.message);
} else {
res.status(200).send('OK');
}
});
}
});
// route: forecastcards/:id/:location/:date
// method: DELETE
// summary: deletes a database record where user email and
// location and forecast date matches given parameters
// params:
// id: user email to delete matched records
// title: location title to delete matched records
// date: forecasts card date that has to be deleted
app.delete('/forecastcards/:id/:location/:date', (req, res) => {
const email = req.params.id;
const location = req.params.location;
const date = req.params.date;
const sql = `DELETE FROM forecastcards WHERE user_email='${email}' AND forecast_location='${location}' AND forecast_date='${date}';`;
if (email && location) {
db.run(sql, [], function(err) {
if (err) {
res.status(500).send('Fail');
console.log(err.message);
} else {
res.status(200).send('OK');
}
});
}
});
// at this point everything is set up, thus run server
app.listen(port, () => {
console.log(`Server is booming on port 3000 Visit http://localhost:3000`);
});