Skip to content

DO NOT MERGE -- add feedback #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
833b287
seperated data tables into locations and events
Morriden May 18, 2020
a04b02a
completed some of the create tables section and corrected syntax
Morriden May 18, 2020
e521dda
table created
Morriden May 18, 2020
fe904c4
completed load, drop, and create tables
Morriden May 18, 2020
729a63c
postman shows data
Morriden May 18, 2020
fca42a5
adding end points for getting random planet and spaceship choice
brammerl May 18, 2020
350a459
Merge pull request #1 from 3rd-git-from-the-hub/lucia
brammerl May 18, 2020
52c1010
adding get event by planet_id
brammerl May 19, 2020
f02df42
changed database structure
Morriden May 19, 2020
3033185
database changes
Morriden May 19, 2020
3ba4881
repush
Morriden May 19, 2020
0842106
testing put and get methods for user/ship
brammerl May 20, 2020
a7bf201
Merge pull request #3 from 3rd-git-from-the-hub/luciadev
brammerl May 20, 2020
f9556e2
updated tables
Morriden May 20, 2020
cd5824e
completed base game
Morriden May 21, 2020
a504701
comimt message
brammerl May 21, 2020
b5f8b8e
deleting post for adding ship to user table
brammerl May 21, 2020
236d53b
Merge pull request #4 from 3rd-git-from-the-hub/luciadev
brammerl May 21, 2020
1425b8d
loggedinuser endpoints
hunterdanielson May 21, 2020
c40735c
Merge branch 'dev' of https://github.com/3rd-git-from-the-hub/space-g…
hunterdanielson May 21, 2020
c4a36bb
pathing
hunterdanielson May 21, 2020
7bc544c
pushing to dev
Morriden May 21, 2020
7f8304d
Merge branch 'dev' of https://github.com/3rd-git-from-the-hub/space-g…
Morriden May 21, 2020
bd7e7f9
pushing up db
Morriden May 21, 2020
95eb5df
finished backend databases
Morriden May 22, 2020
b97a253
fixed table varchar
Morriden May 22, 2020
cfda440
commiting changes
brammerl May 22, 2020
2d0b46f
Merge branch 'dev' of https://github.com/3rd-git-from-the-hub/space-g…
brammerl May 22, 2020
d31e811
completed project
Morriden May 22, 2020
9c72a2d
testing color
brammerl May 22, 2020
28500cb
Merge branch 'dev' of https://github.com/3rd-git-from-the-hub/space-g…
brammerl May 22, 2020
2e9d5b1
add feedback
dpcairns May 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .env-example

This file was deleted.

14 changes: 0 additions & 14 deletions data/animals.js

This file was deleted.

43 changes: 37 additions & 6 deletions data/create-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,50 @@ async function run() {
await client.connect();

// run a query to create tables
// Wow!! Look at this data model! So much complexity. My only concern: is it necessary to track the loggedinuser in its own table?
await client.query(`
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(256) NOT NULL,
hash VARCHAR(512) NOT NULL
);
CREATE TABLE animals (
id SERIAL PRIMARY KEY NOT NULL,
hash VARCHAR(512) NOT NULL,
user_ship VARCHAR(512)

);
CREATE TABLE loggedinuser (
id SERIAL PRIMARY KEY,
name VARCHAR(512) NOT NULL,
cool_factor INTEGER NOT NULL,
score INTEGER NOT NULL,
owner_id INTEGER NOT NULL REFERENCES users(id)
);

CREATE TABLE locations (
id SERIAL PRIMARY KEY NOT NULL,
location_name VARCHAR(512) NOT NULL,
location_type VARCHAR(512) NOT NULL,
location_image VARCHAR(512) NOT NULL,
location_description VARCHAR(512) NOT NULL,
event_id INTEGER NOT NULL UNIQUE
);
CREATE TABLE events (
id SERIAL PRIMARY KEY NOT NULL,
planet_id INTEGER REFERENCES locations(event_id),
event_name VARCHAR(512) NOT NULL,
event_image VARCHAR(1000) NOT NULL,
event_description VARCHAR(2000) NOT NULL,
event_choices VARCHAR(5000) ARRAY NOT NULL
);
CREATE TABLE shipchoices (
id SERIAL PRIMARY KEY NOT NULL,
ship_name VARCHAR(512) NOT NULL,
ship_fuel INTEGER NOT NULL,
ship_hull INTEGER NOT NULL,
base_combat INTEGER NOT NULL,
base_diplomacy INTEGER NOT NULL,
base_science INTEGER NOT NULL,
used_item_slots INTEGER NOT NULL,
max_item_slots INTEGER NOT NULL
);
`);
`); // Generally, the SQL standard would have put ship_choices in snake case

console.log('create tables complete', getEmoji(), getEmoji(), getEmoji());
}
Expand Down
5 changes: 4 additions & 1 deletion data/drop-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ async function run() {

await client.query(`
DROP TABLE IF EXISTS users CASCADE;
DROP TABLE IF EXISTS animals;
DROP TABLE IF EXISTS locations CASCADE;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS shipchoices;
DROP TABLE IF EXISTS loggedinuser;
`);

console.log(' drop tables complete', getEmoji(), getEmoji(), getEmoji());
Expand Down
342 changes: 342 additions & 0 deletions data/events.js

Large diffs are not rendered by default.

35 changes: 29 additions & 6 deletions data/load-seed-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const client = require('../lib/client');
// import our seed data:
const animals = require('./animals.js');
const locations = require('./locations.js');
const usersData = require('./users.js');
const events = require('./events.js');
const shipChoices = require('./shipchoices.js');
const { getEmoji } = require('../lib/emoji.js');

run();
Expand All @@ -11,6 +13,8 @@ async function run() {
try {
await client.connect();



const users = await Promise.all(
usersData.map(user => {
return client.query(`
Expand All @@ -21,16 +25,35 @@ async function run() {
[user.email, user.hash]);
})
);

const user = users[0].rows[0];

await Promise.all(
locations.map(location => {
return client.query(`
INSERT INTO locations (location_name, location_type,location_image, location_description, event_id)
VALUES ($1, $2, $3, $4, $5);
`,
[location.location_name, location.location_type, location.location_image, location.location_description, location.event_id]);
})
);

await Promise.all(
events.map(event => {
return client.query(`
INSERT INTO events (planet_id, event_name, event_image, event_description, event_choices)
VALUES ($1, $2, $3, $4, $5);
`,
[event.planet_id, event.event_name, event.event_image, event.event_description, event.event_choices]);
})
);

await Promise.all(
animals.map(animal => {
shipChoices.map(ship => {
return client.query(`
INSERT INTO animals (name, cool_factor, owner_id)
VALUES ($1, $2, $3);
INSERT INTO shipchoices (ship_name, ship_fuel, ship_hull, base_combat, base_diplomacy, base_science, used_item_slots, max_item_slots)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8);
`,
[animal.name, animal.cool_factor, user.id]);
[ship.ship_name, ship.ship_fuel, ship.ship_hull, ship.base_combat, ship.base_diplomacy, ship.base_science, ship.used_item_slots, ship.max_item_slots]);
})
);

Expand Down
60 changes: 60 additions & 0 deletions data/locations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module.exports = [
{
location_name: 'Gilles C11Y',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2018/08/08/20/23/planet-3593060_960_720.jpg',
location_description: 'Gilles C11Y is a desert of smooth rocks. Spires of sharp stone interrupt the otherwise flat terrain. There are groups of tall, dark orange stalks everywhere. There seems to be large burrows throughout the seemingly deserted planet.',
event_id: 1
},

{
location_name: 'Sulia V',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2017/07/11/21/21/planet-2495089_960_720.jpg',
location_description: 'Dense forests of brown plants cover Sulia V. Large water-filled caves are present just beneath the surface. The geological record shows irregular periods where, for unknown reasons, all oxygen vanished from the atmosphere.',
event_id: 2
},
{
location_name: 'Ipelup VI',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2020/05/04/14/55/sci-fi-5129504_960_720.jpg',
location_description: 'A planet-wide, deep ocean. The planet is home to extremely dangerous apex predators. The stone of the planetary crust is unusually dense and hard.',
event_id: 3
},

{
location_name: 'Selelup II',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2018/08/12/00/48/planet-3599887_960_720.jpg',
location_description: 'Selelup II is a rocky desert type planet that is lit by a gentle sun. Spires of sharp stone interrupt the otherwise flat eterrain. There are signs of a once foreign colony, but now it seems abandoned. There is no water on this now deserted planet.',
event_id: 4
},
{
location_name: 'Laravim VI',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2015/06/26/18/48/mercury-822825_960_720.png',
location_description: 'Laravim VI is an icy desert. Towering columns of hexagonal black stone. Some local plants hover in mid-air, held aloft by gas bags.',
event_id: 5
},
{
location_name: 'Xivon III',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2016/04/13/06/40/mars-1326108_960_720.jpg',
location_description: 'Xivon III is covered in black mud and small rivulets. Deep canyons cut into the planets crust. The local plant life takes the shape of small blue bubbles.',
event_id: 6
},
{
location_name: 'Noxabas I',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2017/11/16/09/32/planet-2953871_960_720.png',
location_description: 'Noxabas I is covered in grey mud and small rivulets. Noxious gases erupt from deep fissures. There are massive flowers in all kinds of colors, sitting atop thick dark green stems.',
event_id: 7
},
{
location_name: 'Yoraporr I',
location_type: 'Planet',
location_image: 'https://cdn.pixabay.com/photo/2018/02/12/21/03/planet-3149121_960_720.jpg',
location_description: 'Yoraporr I is covered in dense jungles of giant dark green geodesic domes cover Yoraporr I. The land is a maze of steep valleys.',
event_id: 8
},
];
32 changes: 32 additions & 0 deletions data/shipchoices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = [
{
ship_name: 'The Reach',
ship_fuel: 4,
ship_hull: 6,
base_combat: 1,
base_diplomacy: 3,
base_science: 2,
used_item_slots: 0,
max_item_slots: 3,
},
{
ship_name: 'The Avenger',
ship_fuel: 4,
ship_hull: 6,
base_combat: 3,
base_diplomacy: 2,
base_science: 1,
used_item_slots: 0,
max_item_slots: 3
},
{
ship_name: 'The Icarus',
ship_fuel: 4,
ship_hull: 6,
base_combat: 1,
base_diplomacy: 2,
base_science: 3,
used_item_slots: 0,
max_item_slots: 3
}
];
4 changes: 2 additions & 2 deletions data/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = [
{
email: 'john@arbuckle.com',
hash: 1234
email: 'testemail@gmail.com',
hash: 1234,
}
];
102 changes: 100 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,116 @@
// I'm grading off the dev branch here. Is that right? Is dev the most updated code? The master branch didn't seem like it
const express = require('express');
const cors = require('cors');
const client = require('./client.js');
const app = express();
const request = require('superagent');

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const ensureAuth = require('./auth/ensure-auth');
const createAuthRoutes = require('./auth/create-auth-routes');

app.get('/animals', async(req, res) => {
const data = await client.query('SELECT * from animals');
const authRoutes = createAuthRoutes({
selectUser(email) {
return client.query(`
SELECT id, email, hash
FROM users
WHERE email = $1;
`,
[email]
).then(result => result.rows[0]);
},
insertUser(user, hash) {
return client.query(`
INSERT into users (email, hash)
VALUES ($1, $2)
RETURNING id, email;
`,
[user.email, hash]
).then(result => result.rows[0]);
}
});

app.use('/auth', authRoutes);
app.use('/api', ensureAuth);

app.get('/color', async(req, res) => {
// nice work getting this endpoint together so quickly!
const data = await request.get('http://thecolorapi.com/scheme?hex=0047AB&rgb=0,71,171&hsl=215,100%,34%&cmyk=100,58,0,33&format=json&mode=analogic&count=6');

res.json(data.body);
});
app.get('/locations', async(req, res) => {
const data = await client.query('SELECT * from locations');

res.json(data.rows);
});

app.get('/events/:eventId', async(req, res) => {
const data = await client.query(`
select * from events where events.planet_id = $1
`, [req.params.eventId]);

res.json(data.rows);
});

app.get('/shipchoices', async(req, res) => {
const data = await client.query('SELECT * from shipchoices');

res.json(data.rows);
});

app.get('/usership/:id', async(req, res) => {
const data = await client.query(`
SELECT shipchoices.id, shipchoices.ship_name, shipchoices.ship_fuel, shipchoices.ship_hull, shipchoices.base_combat, shipchoices.base_diplomacy, shipchoices.base_science, shipchoices.used_item_slots, shipchoices.max_item_slots
FROM shipchoices
WHERE shipchoices.id = $1
`, [req.params.id]);

res.json(data.rows);
});

app.get('/randomPlanet', async(req, res) => {
const data = await client.query(`
SELECT locations.id, locations.location_name, locations.location_type, locations.location_image, locations.location_description, events
FROM locations
JOIN events
on locations.event_id = events.planet_id
`,);

res.json(data.rows);
});


app.post('/api/loggedinuser', async(req, res) => {
const data = await client.query(`
INSERT INTO loggedinuser (name, score, owner_id)
values ($1, $2, $3)
returning *
`, [req.body.name, req.body.score, req.userId]);
res.json(data.rows);
});

app.get('/api/loggedinuser', async(req, res) => {
const data = await client.query(`
SELECT * FROM loggedinuser
WHERE owner_id = $1
`, [req.userId]);
res.json(data.rows);
});

// seems like you could have managed this stuff with req.userId
app.put('/api/loggedinuser', async(req, res) => {
const data = await client.query(`
UPDATE loggedinuser
SET score = score + 1
WHERE owner_id=$1
`, [req.userId]);
res.json(data.rows);
});


app.use(require('./middleware/error'));

module.exports = app;
2 changes: 1 addition & 1 deletion lib/auth/create-auth-routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express');
const bcrypt = require('bcryptjs');
const jwt = require('./jwt');
const client = require('../client').connect();
const client = require('../client');
const ensureAuth = require('./ensure-auth');

function getProfileWithToken(user) {
Expand Down
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Did you end up running postman tests and screenshots of your passing tests?

require('dotenv').config();
require('./lib/client').connect();

Expand Down