Skip to content
This repository was archived by the owner on Feb 15, 2021. It is now read-only.
Open

Jeff #16

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
981056c
commit b4 merge w rob
jefftangx Nov 9, 2017
ac9709f
Designed front page with react and modal for login/register
Nov 9, 2017
0a11455
Merge branch 'frontend' of github.com:tangsauce/react-redux-express-t…
jefftangx Nov 9, 2017
3378818
Changed login button look
Nov 9, 2017
b727bc2
Merge branch 'jeff' of https://github.com/tangsauce/react-redux-expre…
Nov 9, 2017
dcf042d
Login testing
Nov 9, 2017
963e85a
Merge branch 'frontend' of github.com:tangsauce/react-redux-express-t…
jefftangx Nov 9, 2017
607c3cc
begin refactoring home
jefftangx Nov 9, 2017
ce2cfa9
getting the hash router to work
Nov 10, 2017
5f38482
getting the hash router to work
Nov 10, 2017
4f441ad
comits before merge w rob
jefftangx Nov 10, 2017
cbd9a20
Day 1 done but with shitty code
Nov 10, 2017
6d32488
rob day1
jefftangx Nov 10, 2017
ea53ffa
fix conflicts
jefftangx Nov 10, 2017
c8af19c
Fixed the passport sessions issue
Nov 10, 2017
c9c03b4
sidebar2 works!!!!
jefftangx Nov 10, 2017
4df0421
added keys to feed
jefftangx Nov 10, 2017
fcdbe5f
fin day 1, for real this time
jefftangx Nov 10, 2017
8fb519f
get front end for unique post
jefftangx Nov 10, 2017
a25eaa5
commit b4 merge rob recursion
jefftangx Nov 10, 2017
97d46ca
Recursion for the comment query
Nov 10, 2017
63f191b
Merge branch 'frontend' of github.com:tangsauce/steemit into jeff
jefftangx Nov 10, 2017
095e39f
rage quit
jefftangx Nov 11, 2017
c907ead
Upvoting
Nov 11, 2017
402709c
push to master
jefftangx Nov 11, 2017
a290b56
Merge pull request #1 from tangsauce/jeff
jefftangx Nov 11, 2017
1a3086a
Merge branch 'master' into jeff
jefftangx Nov 11, 2017
bc23041
refactor a bit of backend
jefftangx Nov 11, 2017
642e13a
refactor front end a lil
jefftangx Nov 11, 2017
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
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"__DEV__": true
},
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 8,
# sourceType: 'module',
ecmaFeatures: {
"experimentalObjectRestSpread": true,
Expand Down Expand Up @@ -123,7 +123,7 @@
*/
"indent": [
2,
4,
2,
{ "SwitchCase": 1 }
], // http://eslint.org/docs/rules/indent
"brace-style": [
Expand Down
44 changes: 44 additions & 0 deletions backend/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require('express');
const router = express.Router();
const {User} = require('./models');
const axios = require('axios');

module.exports = (passport) => {
router.post('/register', (req, res) => {
User.create({username: req.body.username, password: req.body.password})
.then(user => {
// axios.post('/login', {user})
// .then(resp => {console.log(resp)})
// .catch(err => {console.log(err)})
res.json({user});
})
.catch(err => {
res.json({err});
});
});

router.post('/login', passport.authenticate('local'), (req, res) => {
res.json({user: req.user});
});

router.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});

router.get('/:username', (req, res) => {
User.findOne({where: {username: req.params.username}})
.then(user => {
res.json({user});
})
.catch(err => {
res.json({err});
});
});

router.get('/', (req, res) => {
res.json({user: req.user});
});

return router;
};
73 changes: 73 additions & 0 deletions backend/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"use strict";

var Sequelize = require('sequelize');
var sequelize = new Sequelize(process.env.DATABASE_NAME, 'postgres', null, {
dialect: 'postgres'
});

sequelize.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});

const User = sequelize.define('user', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false
},
});

const Post = sequelize.define('post', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: {
type: Sequelize.STRING,
allowNull: false,
},
body: {
type: Sequelize.STRING,
},
parent_id: {
type: Sequelize.INTEGER,
},
});

const Vote = sequelize.define('vote', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
isUpvote: {
type: Sequelize.INTEGER,
allowNull: false,
},
});

Post.belongsTo(User);
Vote.belongsTo(User);
Vote.belongsTo(Post);
Post.hasMany(Vote, { as: 'votes', foreignKey: 'postId' });

module.exports = {
sequelize,
User,
Post,
Vote
};
96 changes: 92 additions & 4 deletions backend/routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
const express = require('express');
const router = express.Router();
const {Post, Vote} = require('./models');
var sequelize = require('sequelize');

// YOUR API ROUTES HERE
router.post('/post/new', (req, res) => {
Post.create({userId: req.user.id, title: req.body.title, body: req.body.body})
.then(() => res.json({msg: 'new post successful'}))
.catch(err => res.json({err}));
});

router.get('/post/all', (req, res) => {
Post.findAll({ where: { parent_id: null }})
.then(resp => res.json({posts: resp}));
});

router.post('/post/:id', (req, res) => {
Post.create({userId: req.user.id, title: req.body.title, body: req.body.body, parent_id: req.params.id})
.then( resp => res.json(resp));
});

router.get('/post/:id', (req, res) => {
Post.findAll({
where: {id: req.params.id},
attributes: ['id', 'title', 'body', 'createdAt', 'userId', 'parent_id',
[sequelize.fn('sum', sequelize.col('votes.isUpvote')), 'votecount']
],
include: [ { model: Vote, as: 'votes' } ],
group: ['post.id', 'votes.id']
})
.then( resp => {// TODO @ROB EXPLAIN TO ME WHAT THE FUCK IS GOING ON HERE
const childrenObj = {};
childrenObj[resp[0].dataValues.id] = resp[0].dataValues;
childrenObj[resp[0].dataValues.id].children = [];
let recurseLeft = 1;
const find = (children) => {
recurseLeft --;
if (!children.length && !recurseLeft) res.json(childrenObj[req.params.id]);
children.forEach(child => {
if (child.dataValues.id !== parseInt(req.params.id, 10)) {
childrenObj[child.dataValues.parent_id].children.push(child.dataValues);
childrenObj[child.dataValues.id] = child.dataValues;
childrenObj[child.dataValues.id].children = [];
}
recurseLeft ++;
(async function x() {
var resp = await Post.findAll({
where: {parent_id: child.id},
attributes: ['id', 'title', 'body', 'createdAt', 'userId', 'parent_id', [sequelize.fn('sum', sequelize.col('votes.isUpvote')), 'votecount']],
include: [
{ model: Vote, as: 'votes' }
],
group: ['post.id', 'votes.id']
});
var resp = await find(resp);
return resp;
})()
});
};
find(resp);
});
});

router.get('/post/:id/:vote', (req, res) => {
Vote.findOne({where: {postId: req.params.id, userId: req.user.id}})
.then( resp => {

if(resp) {
// They upvoted an upvote --> destroy vote
if(resp.dataValues.isUpvote && req.params.vote === 'upvote') {
Vote.destroy({where: {postId: req.params.id, userId: req.user.id}})
.then(() => res.json({success: 'true'}));
}

// They downvoted an upvote --> change isUpvote to false
else if(resp.dataValues.isUpvote && req.params.vote === 'downvote') {
Vote.update({isUpvote: -1}, {where: {postId: req.params.id, userId: req.user.id}})
.then(() => res.json({success: 'true'}));
}

// They upvoted a downvote --> change isUpvote to true
if(!resp.dataValues.isUpvote && req.params.vote === 'upvote') {
Vote.update({isUpvote: 1}, {where: {postId: req.params.id, userId: req.user.id}})
.then(() => res.json({success: 'true'}));
}

// SAMPLE ROUTE
router.use('/users', (req, res) => {
res.json({ success: true });
// They downvoted a downvote --> destroy vote
else if(!resp.dataValues.isUpvote && req.params.vote === 'downvote') {
Vote.destroy({where: {postId: req.params.id, userId: req.user.id}})
.then(() => res.json({success: 'true'}));
}
} else {
Vote.create({postId: req.params.id, userId: req.user.id, isUpvote: req.params.vote === 'upvote' ? 1 : -1})
.then(() => res.json({success: 'true'}));
}
});
});

module.exports = router;
13 changes: 13 additions & 0 deletions backend/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

var models = require('./models');

models.sequelize.sync({ force: true })
.then(function() {
console.log('Successfully updated database tables!');
process.exit(0);
})
.catch(function(error) {
console.log('Error updating database tables', error);
process.exit(1);
});
2 changes: 2 additions & 0 deletions env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export DATABASE_URL=postgresql://postgres@localhost/steemit
export DATABASE_NAME=steemit
33 changes: 33 additions & 0 deletions frontend/assets/stylesheets/appcontainer.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
background-color: #BDBDBD;
}

#root {
width: 100%;
height: 100%;
}

.appcontainer_container {
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
}

.appcontainer_header_container {
display: flex;
width: 100%;
height: 75px;
flex: 1;
}

.appcontainer_body_container {
display: flex;
width: 100%;
height: 100%;
flex: 10;
}
12 changes: 12 additions & 0 deletions frontend/assets/stylesheets/feed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.feed_container {
background-color: #BDBDBD;
width: 100%;
flex: 5;
}

.feed_body {
height: 97%;
width: 96%;
margin: auto;
margin-top: 2%;
}
29 changes: 29 additions & 0 deletions frontend/assets/stylesheets/header.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.header_container {
width: 100%;
overflow: hidden;
flex: 1;
position: fixed;
height: 100px;
}

.header_content {
height: 98px;
width: 100%;
display: flex;
}

.header_container_logo {
max-height: 100%;
max-width: 30%;
width: auto;
height: auto;
margin-left: 5px;
padding: 10px
}

.header_container_title{
color: #06d6a8;
font-family: helvetica, sans-serif;
font-size: 40px;
font-weight: bold;
}
51 changes: 51 additions & 0 deletions frontend/assets/stylesheets/sidebar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.sidebar_container {
width: 100%;
flex: 1;
height: 100%;
margin-right: 10px;
}

.sidebar_content {
width: 100%;
height: 30%;
margin-top: 10%;
}

.submit_post_button {
width: 100%;
}

.logout_button {
width: 100%;
}

.login_register_button_container {
margin-top: 10px;
display: flex;
}

.login_button {
flex: 1;
margin-right: 5px;
}

.register_button {
flex: 1;
}

.description_right_container {
display: flex;
flex-direction: column;
margin-top: 10px;
}

.right_sidebar_description_title {
flex: 1;
margin: 5px;
}

.right_sidebar_description_content {
flex: 1;
margin-top: 0;
margin: 5px;
}
Loading