Skip to content
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

project-2 #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
65 changes: 65 additions & 0 deletions controllers/imagesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var express = require('express');
var router = express.Router();
var image = require('../models/image');
var auth = require('../middleware/auth');
// router.get('/', image.getAll, renderIndex);
// router.get('/:id', image.find, renderShow);

router.get('/new', renderNew); // render user profile image form

router.get('/:image_id/edit', image.find, renderEdit);
// router.post('/', image.create, redirectShow);
router.put('/:image_id', auth.onlyUsers, image.update, redirectShow);
router.delete('/:image_id', auth.onlyUsers, image.delete, redirectIndex);


// create new Image using user id
router.post('/new', auth.onlyUsers,image.create, redirectShow);



function renderIndex(req, res){
var mustacheVariables = {
image : res.locals.image
}

res.render('./images/index', mustacheVariables);
}

function renderShow(req, res) {
var mustacheVariables = {
image : res.locals.image
}
res.render('./images/show', mustacheVariables);
}

function renderNew(req, res) {

console.log( "test 33 " , req.userId)
var mustacheVariables = {
// image : res.locals.image,
// text : res.locals.text
id: req.userId
}

res.render('./images/new', mustacheVariables);
}

function renderEdit(req, res) {
console.log(res.locals.image);
var mustacheVariables = res.locals.image;
res.render('./images/edit', mustacheVariables);
}

function redirectIndex(req,res){
res.redirect(`/users/${res.locals.id_user}`);
// console.log("res.locals.id")
}


function redirectShow(req, res) {
res.redirect(`/users/${res.locals.id_user}`);
}


module.exports = router;
27 changes: 27 additions & 0 deletions controllers/sessionsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var express = require('express');
var router = express.Router();
var session = require('../models/session');

router.get('/', renderNew);
router.post('/', session.create, redirectShow);
router.delete('/', session.delete, redirectLogin);

function renderNew(req, res){
console.log(req.session.user)
res.render('./login');
}

function redirectShow(req, res){
if(req.session.user){
res.redirect(`/users/${req.session.user.id}`)
}else{
res.redirect('/login');
}
}

function redirectLogin(req, res){
res.redirect('/');
}


module.exports = router;
80 changes: 80 additions & 0 deletions controllers/usersController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var express = require('express');
var router = express.Router();
var user = require('../models/user');
var auth = require('../middleware/auth');
var image = require('../models/image');
var imagesController = require('../controllers/imagesController');
var Gallery = require('express-photo-gallery');



var options = {
title: 'My Awesome Photo Gallery'
};

router.use('/:id/images', Gallery('./public/images', options));

// router.use('/:id/images',imagesController);

// test

router.use('/:id/images', assignUserId, imagesController );


router.get('/',user.getAll,renderIndex);

router.get('/new', renderNew); // create new user

router.get('/:id/edit', user.find , renderEdit); // edit profile

router.get('/:id', user.find, image.findByUser, renderShow);

router.post('/', user.create, redirectShow);
router.delete('/:id', assignUserId, auth.onlyUsers, user.delete, redirectIndex);
router.put('/:id', assignUserId, auth.onlyUsers, user.update,redirectShow);

function assignUserId(req, res, next) {
req.userId = req.params.id;
next()
}

function renderIndex(req,res){
var mustacheVaribles = {
user : res.locals.user,
image : res.locals.image
}
res.render('./users/index',mustacheVaribles);
}

function renderShow(req,res){
var mustacheVaribles = {
user : res.locals.user,
image : res.locals.image
}
res.render('./users/show',mustacheVaribles);
}


function renderEdit(req, res) {
var mustacheVariables = res.locals.user;
console.log('&&&&&&&&');
console.log(mustacheVariables);

res.render('./users/edit', mustacheVariables);
}

function renderNew(req, res){
res.render('./users/new');
}

function redirectShow(req, res) {

res.redirect(`/users/${res.locals.id}`);
}

function redirectIndex(req,res){
res.redirect('/users');
}


module.exports = router;
12 changes: 12 additions & 0 deletions db/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var pgPromise = require('pg-promise');
var pgInstance = pgPromise();

var config = {
host : 'localhost',
port : 5432 ,
database : 'arts_shine_db',
user :'Shahad'
}

var connection = pgInstance(config);
module.exports = connection;
88 changes: 88 additions & 0 deletions db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
DROP DATABASE IF EXISTS arts_shine_db;
CREATE DATABASE arts_shine_db;
\c arts_shine_db

-- add create tables here

CREATE TABLE users(
id serial primary key,
username varchar(10) UNIQUE NOT NULL,
password varchar NOT NULL,
fname varchar(30),
lname varchar(30),
art_kind text
);

CREATE TABLE images(
id serial primary key,
image varchar,
text text ,
id_user int not null,
foreign key(id_user) references users ON DELETE CASCADE
);

INSERT INTO users
(username,password,fname,lname,art_kind)
VALUES('shuunz','shahad1234','Shahad','Mohammad','Photographer');
-- ('noura1','jkah12','noura','alghamdi','painter');

INSERT INTO images
(image,text,id_user)
VALUES('/static/images/image1.png','Dont believe everything you hear. There are always three sides to a story. Yours , theirs and the truth. ⛅️👌🌟💭',1),
('/static/images/image2.png','If you can take it , you can make it ✨',1),
('/static/images/image3.png','For the highs and lows
And moments in between,
Mountains and valleys
And rivers and streams.
For where you are now
And where you will go,
For “I’ve always known”
And “I told you so,”
For “nothing is happening”
And “All has gone wrong,”
It’s here in this journey
You will learn to be strong
You will get where you’re going,
Landing where you belong
—Morgan Harper Nichols',1),
('/static/images/image4.png','werwr',1),
('/static/images/image5.png','aawe',1),
('/static/images/1.png','aawe',1),
('/static/images/2.png','aawe',1),
('/static/images/3.png','aawe',1),
('/static/images/4.png','aawe',1),
('/static/images/5.png','aawe',1),
('/static/images/6.png','aawe',1),
('/static/images/7.png','aawe',1),
('/static/images/8.png','aawe',1),
('/static/images/9.png','aawe',1),
('/static/images/10.png','aawe',1),
('/static/images/11.png','aawe',1),
('/static/images/12.png','aawe',1),
('/static/images/13.png','aawe',1),
('/static/images/14.png','aawe',1),
('/static/images/15.png','aawe',1),
('/static/images/16.png','aawe',1),
('/static/images/17.png','aawe',1),
('/static/images/18.png','aawe',1),
('/static/images/19.png','aawe',1),
('/static/images/20.png','aawe',1),
('/static/images/21.png','aawe',1),
('/static/images/22.png','aawe',1),
('/static/images/23.png','aawe',1),
('/static/images/24.png','aawe',1),
('/static/images/25.png','aawe',1),
('/static/images/26.png','aawe',1),
('/static/images/27.png','aawe',1),
('/static/images/28.png','aawe',1),
('/static/images/29.png','aawe',1),
('/static/images/30.png','aawe',1),
('/static/images/31.png','aawe',1),
('/static/images/32.png','aawe',1),
('/static/images/33.png','aawe',1),
('/static/images/34.png','aawe',1);

-- INSERT INTO images
-- (image,text,id_user)
-- VALUES('https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940','dsijilsnl',9),
-- ('https://images.pexels.com/photos/459225/pexels-photo-459225.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940','efsf',9);
51 changes: 51 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var express = require('express');
var app = express();
var port = 3000;

// mustache config
var mustache = require('mustache-express');
app.engine('html', mustache());
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

// body parser config
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// logger config
var logger = require('morgan');
app.use(logger('dev'));

// method override config
var methodOverride = require('method-override');
app.use(methodOverride('_method'));

// express session
var session = require('express-session')
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));


app.get('/', (req, res) => {
res.render('./index');

})

var usersController = require('./controllers/usersController');
var sessionsController = require('./controllers/sessionsController');
// var imagesController = require('./controllers/imagesController')
app.use('/users',usersController);

app.use('/login', sessionsController);

// app.use('/images',imagesController);
app.use('/static', express.static(__dirname + '/public'));
app.listen(port, function () {
console.log('---------------------------------------');
console.log('Express listening on localhost:' + port);
console.log('---------------------------------------');
})
25 changes: 25 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var auth = {};

auth.restrict = function(req, res, next){
if(req.session.user){
next();
}else{
res.redirect('/login');
}
}

auth.onlyUsers = function(req, res, next) {
if (req.session.user) {

console.log("update" , req.params.id == req.session.user.id , "id : " , req.session.user.id, req.params.id, req.userId)
if(req.userId == req.session.user.id){
next();
}else{
res.redirect(`/users/${req.session.user.id}`)
}
} else {
res.redirect('/login');
}
}

module.exports = auth;
Loading