Thursday 15 December 2016

Looking for per-review/advice on my express/psql/knex app. I got it to work, but not sure if its done correctly, or that it will cause headaches down the road.

Hello. I have simple commenting system that people can go to others profiles and make comments or replies to others profiles.I just recently switched from mongodb to postgres because I was needing more of a relational database than what mongo was offering.So I choose using express with postgresql database and knex to deal with all the dirty stuff.I am new to knex and I am not sure if I am making my queries correctly. It just seems off with all these if statements. Maybe someone can shed some light on the proper way to do what I am trying to achieve.My knex schemas knex.schema.createTable('users', function(table){ table.increments('uid').primary(); table.string('username'); table.string('password'); table.string('email'); table.timestamps(); }), knex.schema.createTable('profiles', function(table){ table.increments('id').primary(); table.integer('user_id') .references('uid') .inTable('users'); table.string('about'); table.string('flashBanner'); }), knex.schema.createTable('comments', function(table){ table.increments('id').primary(); table.string('body'); table.integer('user_id') .references('uid') .inTable('users') table.integer('profile_id') .references('id') .inTable('profiles'); table.integer('replies_id') .references('id') .inTable('comments'); table.timestamps(); }), ]) My routesThis is the main profile view, you can see all the comments and replies from here. Also make them.app.get('/p/:user', function(req, res, done){ db('users').where('username', req.params.user).then(function(profileUser){ if(profileUser != ''){ db('profiles').where('user_id', profileUser[0].uid) .join('users', 'profiles.user_id', 'users.uid') .then(function(profile){ if(profile != ''){ db('comments').where('comments.profile_id', profile[0].id) .join('users', 'comments.user_id', 'users.uid') .select('comments.id', 'comments.body', 'comments.created_at', 'comments.replies_id', 'users.username', 'users.profileImage') .orderBy('comments.created_at', 'desc').then(function(comments){ if(comments != ''){ res.render('comments.html', { messages: req.flash('alert'), isLogged: req.session.isLogged, user: req.session.user, accountImage: req.session.accountImage, profileImage: profile[0].profileImage, profileUser: req.params.user, comments: comments }); }else{ res.render('comments.html', { messages: req.flash('alert'), isLogged: req.session.isLogged, user: req.session.user, accountImage: req.session.accountImage, profileImage: profile[0].profileImage, profileUser: req.params.user, comments: comments }); } }); } }); }else{ req.flash('alert', 'no profile user: ' + req.params.user); res.redirect('/'); } }); }); This is where a comment is posted and submitted to the databaseapp.post('/p/:user', function(req, res, done){ var loginUser = req.session.user; var profileUser = req.params.user; var comment = req.body.newComment; var userId; var profileId; db('users').where('username', loginUser).then(function(user){ if(user != ''){ userId = user[0].uid; db('users').where('username', profileUser).then(function(profileUser){ if(profileUser != ''){ profileId = profileUser[0].uid; db('profiles').where('user_id', profileId).then(function(profile){ if(profile != ''){ profileId = profile[0].id; db('comments').insert({ body: comment, user_id: userId, profile_id: profileId, created_at: new Date(), }).then(function(comment){ if(comment != ''){ req.flash('alert', 'post successfull'); res.redirect('/p/' + profileUser); } }); } }); } }); } }); }); This is where a reply is POSTED to the databaseapp.post('/p/:user/comments/reply', function(req, res){ var loginUser = req.session.user; var profileUser = req.params.user; var reply = req.body.newReply; var commentId = req.body.commentId; var userId; var profileId; db('users').where('username', loginUser) .select('uid') .then(function(user){ if(user != ''){ userId = user[0].uid; db('users').where('username', profileUser) .select('uid') .then(function(profileUser){ if(profileUser != ''){ profileId = profileUser[0].uid; db('profiles').where('user_id', profileId) .select('id') .then(function(profile){ if(profile != ''){ profileId = profile[0].id; db('comments').insert({ body: reply, user_id: userId, profile_id: profileId, replies_id: commentId, created_at: new Date(), }).then(function(comment){ if(comment != ''){ req.flash('alert', 'post successfull'); res.redirect('/p/' + profileUser); }else{ console.log('error'); } }); }else{ console.log('no profile'); } }); }else{ console.log('no profile user'); } }); }else{ console.log('need to login'); } }); }); and then my view is treated like this So everything I have here is working how I want it to, I just want to make sure its not too messy before I get on to making more spaghetti.Thank you for any replies and insight!

Submitted December 15, 2016 at 05:22PM by clandest

No comments:

Post a Comment