-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (87 loc) · 3.33 KB
/
server.js
File metadata and controls
101 lines (87 loc) · 3.33 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require ('dotenv').config();
const bp = require('body-parser');
const express = require('express');
const app = express();
const pg = require('pg');
const fs = require('fs');
const PORT = process.env.PORT;
const cors = require('cors');
const superagent = require('superagent');
const MARS_API_KEY = process.env.MARS_API_KEY;
app.use(cors());
app.use(bp.json());
app.use(bp.urlencoded({extended: true}));
const client = new pg.Client(process.env.DATABASE_URL);
client.connect();
app.get('/api/v1/favorites', (req, res) => {
client.query(`
SELECT id FROM users WHERE name = $1`,[req.query.user])
.then(data =>
client.query(`SELECT url
FROM images
INNER JOIN favorites
ON (favorites.images_id = images.image_id)
WHERE favorites.user_id = $1`, [data.rows[0].id]))
.then(data => res.send(data.rows));
});
app.post('/api/v1/favorites', (req, res) =>{
client.query(`
SELECT id FROM users WHERE name = $1`,[req.body.user])
.then (data => {
if(data.rows.length > 0) {
const userId = data.rows[0].id;
getImages(userId);
} else {
client.query(`INSERT INTO users (name) VALUES ($1) RETURNING id`, [req.body.user], (err,data) => {
getImages(data.rows[0].id);
if (err) console.log(err);
});
}
});
function getImages (userId) {
console.log('getImages', userId);
client.query(`SELECT image_id FROM images WHERE url = $1`, [req.body.url])
.then (data => {
if (data.rows.length > 0){
const imageId = data.rows[0].image_id;
addToFavorites(imageId,userId);
} else {
client.query(`INSERT INTO images (image_id, rover, camera, url) VALUES ($1, $2, $3, $4) RETURNING image_id` , [req.body.image_id, req.body.rover, req.body.camera, req.body.url], (err,data) =>{
addToFavorites(data.rows[0].image_id, userId);
console.log('geturl', data.rows[0].image_id);
if (err) console.log(err);
});
}
})
.catch(err => {
console.log(err);
});
}
function addToFavorites (imageId, userId){
console.log(imageId,userId);
client.query('INSERT INTO favorites (user_id, images_id) VALUES ($1, $2)', [userId, imageId]);
res.send('done');
}
});
app.get('/api/v1/nasa', (req, res) => {
const rover = req.query.rover;
const camera = req.query.camera;
const date = req.query.date;
const nasaUrl = 'https://api.nasa.gov/mars-photos/api/v1/rovers/';
superagent.get(`${nasaUrl}${rover}/photos?earth_date=${date}&camera=${camera}&api_key=${MARS_API_KEY}`)
.end((err, resp) => {
const topPix = resp.body.photos.slice(0,25).map(image => {
const returnImg = {
id: image.id,
rover: image.rover.name,
camera: image.camera.name,
url: image.img_src
};
return returnImg;
});
res.send(topPix);
});
});
app.listen(PORT, () => {
console.log(`Listening on Port ${PORT}`);
});