Skip to content
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/
1 change: 1 addition & 0 deletions .glitch-assets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"contact-bg.jpg","date":"2019-09-16T13:05:44.909Z","url":"https://cdn.glitch.com/8c4da757-c9c6-4704-99c8-43d77aa7e93e%2Fcontact-bg.jpg","type":"image/jpeg","size":304055,"imageWidth":1400,"imageHeight":788,"thumbnail":"https://cdn.glitch.com/8c4da757-c9c6-4704-99c8-43d77aa7e93e%2Fthumbnails%2Fcontact-bg.jpg","thumbnailWidth":330,"thumbnailHeight":186,"uuid":"zUMJosIHiBF4mTMA"}
36 changes: 2 additions & 34 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@
Assignment 5 - Databases and/or Components
===

Due: October 7th, by 11:59 AM.
**Blogatron Beta (Revamped)**

For this assignment you will complete one of the following tasks, based on your prior experience with the various technologies involved.

1. Rework the server component from Assignment #3 to use MongoDB or some other NoSQL database (like CouchDB). You can remove Passport authentication if you choose, although this might be as much work as simply changing your Passport calls to use MongoDB.
2. Rework the client component from Assignment #3 to use Svelte in some capacity.
3. Rework the client component from Assignmeent #3 to use React in some capacity.

For 2 and 3, make sure to look at [the notes from lecture 10](https://github.com/cs4241-19a/materials/blob/master/lecture10.markdown).

This is really a chance for you to experiment with some additional web technologies that the prior assignments haven't covered yet: non-flatfile databases and web component frameworks.

This project can be implemented on any hosting service (Glitch, DigitalOcean, Heroku etc.), however, you must include all files in your GitHub repo so that the course staff can view them; these files are not available for viewing in many hosting systems.

Deliverables
---

Do the following to complete this assignment:

1. Implement your project with the above requirements.
3. Test your project to make sure that when someone goes to your main page on Glitch/Heroku/etc., it displays correctly.
4. Ensure that your project has the proper naming scheme `a5-yourname` so we can find it.
5. Fork this repository and modify the README to the specifications below. Be sure to add *all* project files.
6. Create and submit a Pull Request to the original repo. Name the pull request using the following template: `a5-gitname-firstname-lastname`.

Sample Readme (delete the above when you're ready to submit, and modify the below so with your links and descriptions)
---

## Your Web Application Title

your hosting link e.g. http://a5-charlieroberts.glitch.me

Include a very brief summary of your project here and what you changed / added to assignment #3. Briefly (3–4 sentences) answer the following question: did the new technology improve or hinder the development experience?

Unlike previous assignments, this assignment will be solely graded on whether or not you successfully complete it. Partial credit will be generously given.
Included a React animated landing page.
76 changes: 76 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
passport = require("passport"),
router = express.Router(),
path = require('path'),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
Item = require("./models/item"),
Comment = require("./models/comment"),
User = require("./models/user"),
seedDB = require("./seeds");

const port = 3000;

// Requiring routes
var commentRoutes = require("./routes/comments"),
itemRoutes = require("./routes/items"),
indexRoutes = require("./routes/index");

app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/views/landing.html'));
//__dirname : It will resolve to your project folder.
});
//var url = process.env.DATABASEURL || "mongodb://localhost/marketplace_beta";

var url = 'mongodb+srv://heartkiller:AsrSNmXn5dMpQgw@operation-mongoose-iyooo.mongodb.net/test?retryWrites=true&w=majority';
mongoose.connect(url,
{
useNewUrlParser: true,
useCreateIndex: true
}).then(() => {
console.log('Connected to DB!');
}).catch(err => {
console.log('ERROR: ', err.message);
});

app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.locals.moment = require('moment');
app.use(flash());

// Add sample data to database
// seedDB();

// Passport Configuration
app.use(require("express-session")({
secret: "This is a sample secret text for encoding",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

// Middleware to pass user and message data to each route
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});

// Use the routes
app.use(indexRoutes);
app.use("/items", itemRoutes);
app.use("/items/:id/comments", commentRoutes);
app.listen(process.env.PORT || 3000, process.env.IP, () => {
console.log("Server is listening on port ", process.env.PORT || 3000, "...");
});
7 changes: 7 additions & 0 deletions bootstrap.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions jquery.min.js

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions jquery.parallax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Plugin: jQuery Parallax
Version 1.1.3
Author: Ian Lunn
Twitter: @IanLunn
Author URL: http://www.ianlunn.co.uk/
Plugin URL: http://www.ianlunn.co.uk/plugins/jquery-parallax/

Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/

(function( $ ){
var $window = $(window);
var windowHeight = $window.height();

$window.resize(function () {
windowHeight = $window.height();
});

$.fn.parallax = function(xpos, speedFactor, outerHeight) {
var $this = $(this);
var getHeight;
var firstTop;
var paddingTop = 0;

//get the starting position of each element to have parallax applied to it
function update (){

$this.each(function(){

firstTop = $this.offset().top;
});

if (outerHeight) {
getHeight = function(jqo) {
return jqo.outerHeight(true);
};
} else {
getHeight = function(jqo) {
return jqo.height();
};
}

// setup defaults if arguments aren't specified
if (arguments.length < 1 || xpos === null) xpos = "50%";
if (arguments.length < 2 || speedFactor === null) speedFactor = 0.5;
if (arguments.length < 3 || outerHeight === null) outerHeight = true;

// function to be called whenever the window is scrolled or resized

var pos = $window.scrollTop();

$this.each(function(){
var $element = $(this);
var top = $element.offset().top;
var height = getHeight($element);

// Check if totally above or totally below viewport
if (top + height < pos || top > pos + windowHeight) {
return;
}

$this.css('backgroundPosition', xpos + " " + Math.round((firstTop - pos) * speedFactor) + "px");

});
}

$window.bind('scroll', update).resize(update);
update();
};
})(jQuery);
9 changes: 9 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*-------------------------------------------------------------------------------
jQuery Parallax
-------------------------------------------------------------------------------*/

function initParallax() {
$('#home').parallax("50%", 0.3);

}
initParallax();
77 changes: 77 additions & 0 deletions middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Item = require("../models/item");
var Comment = require("../models/comment");
var middlewareObj = {};

// Ensure that the user is logged in and owns item
middlewareObj.checkItemOwnership = (req, res, next) => {
// Ensure user is logged in
if(req.isAuthenticated()) {
Item.findById(req.params.id, (err, foundItem) => {
if(err || !foundItem) {
req.flash("error", "Item not found");
res.redirect("/items");
} else {
// Ensure user is the owner of the item
if(foundItem.author.id.equals(req.user._id)){
// User owns the item
next();
} else {
// User does not own the item
req.flash("error", "You don't have permission to do that");
res.redirect("/items/" + req.params.id);
}
}
});
} else {
// User is not logged in
req.flash("error", "You need to be logged in to do that");
res.redirect("/login");
}
}

// Ensure user is logged in and owns comment
middlewareObj.checkCommentOwnership = (req, res, next) => {
// Ensure user is logged in
if(req.isAuthenticated()) {
// Ensure the item corresponding to the comment exists
Item.findById(req.params.id, (err, foundItem) => {
if(err || !foundItem) {
req.flash("error", "Item not found");
return res.redirect("/items");
} else {
// Ensure the comment exists
Comment.findById(req.params.comment_id, (err, foundComment) => {
if(err || !foundComment) {
req.flash("error", "Comment not found");
res.redirect("/items/" + req.params.id);
} else {
// Ensure user is the owner of the comment
if(foundComment.author.id.equals(req.user._id)){
// User owns the comment
next();
} else {
// User does not own the comment
req.flash("error", "You don't have permission to do that")
res.redirect("/items/" + req.params.id);
}
}
});
}
});
} else {
// User is not logged in
req.flash("error", "You need to be logged in to do that");
res.redirect("/login");
}
}

// Check if user is logged in
middlewareObj.isLoggedIn = (req, res, next) => {
if(req.isAuthenticated()) {
return next();
}
req.flash("error", "You need to be logged in to do that");
res.redirect("/login");
}

module.exports = middlewareObj;
15 changes: 15 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var mongoose = require("mongoose");

var commentSchema = mongoose.Schema({
text: String,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});

module.exports = mongoose.model("Comment", commentSchema);
21 changes: 21 additions & 0 deletions models/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var mongoose = require("mongoose");

var itemSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}]
});

module.exports = mongoose.model("Item", itemSchema);
11 changes: 11 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
username: String,
password: String
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);
Loading