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

StackOverflow application created by Nistha Gupta #40

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions StackOverFlow_Swiggy i++/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package.json
package-lock.json
57 changes: 57 additions & 0 deletions StackOverFlow_Swiggy i++/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var express = require('express'),
app = express(),
bodyparser = require('body-parser'),
mongoose = require('mongoose'),
flash = require('connect-flash'),
passport = require('passport'),
localStrategy = require('passport-local'),
methodOverride= require('method-override'),
Question = require("./models/question.js"),
Comment = require("./models/comment.js"),
User = require("./models/user.js");


var commentRoutes = require("./routes/comments.js"),
questionRoutes = require("./routes/questions.js"),
authRoutes = require("./routes/index.js");

mongoose.set("useNewUrlParser", true);
mongoose.set("useUnifiedTopology", true);
mongoose.set("useCreateIndex", true);

mongoose.connect(process.env.DATABASEURL || "mongodb://localhost/stack_overflow");

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

app.use(require("express-session")({
secret: "This is the best app",
resave: false,
saveUninitialized: false
}))

//Passport Configration
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
})

app.use("/question/:id/comments",commentRoutes);
app.use("/question",questionRoutes);
app.use(authRoutes);

app.listen(process.env.PORT || 3600,function(req,res){
console.log("StackOverflow server started");
})
58 changes: 58 additions & 0 deletions StackOverFlow_Swiggy i++/middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var Question = require("../models/question");
var Comment = require("../models/comment");

// all the middleare goes here
var middlewareObj = {};

middlewareObj.checkUserOwnership = function(req, res, next) {
if(req.isAuthenticated()){
Question.findById(req.params.id, function(err, foundQuestion){
if(err){
req.flash("error","Question not found");
res.redirect("back");
} else {
// does user own the Question?
if(foundQuestion.author.id.equals(req.user._id) || req.user.isAdmin) {
next();
} else {
req.flash("error","You don't have permission to do that.");
res.redirect("back");
}
}
});
} else {
req.flash("error","You need to be logged in to do that")
res.redirect("back");
}
}

middlewareObj.checkCommentOwnership = function(req, res, next) {
if(req.isAuthenticated()){
Comment.findById(req.params.comment_id, function(err, foundComment){
if(err){
res.redirect("back");
} else {
// does user own the comment?
if(foundComment.author.id.equals(req.user._id) || req.user.isAdmin) {
next();
} else {
req.flash("error","You don't have permission to do that");
res.redirect("back");
}
}
});
} else {
req.flash("error","You need to be logged in to do that");
res.redirect("back");
}
}

middlewareObj.isLoggedIn = function(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;
21 changes: 21 additions & 0 deletions StackOverFlow_Swiggy i++/models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var mongoose = require("mongoose");

var commentSchema = new mongoose.Schema({
text: String,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
username: String,
},
likes: [
{
type: mongoose.Schema.Types.ObjectId, //creating schema in mongo
ref: "User",
},
],
});

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

var questionSchema = new mongoose.Schema({
name: 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",
},
],
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
});
const Comment = require("./comment");
questionSchema.pre("remove", async function () {
await Comment.remove({
_id: {
$in: this.comments,
},
});
});

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

var userSchema = new mongoose.Schema({
username: String,
password: String,
avatar: String,
firstName: String,
lastName: String,
email: { type: String, unique: true, required: true },
points: { type: Number, default: 0 },
resetPasswordToken: String,
resetPasswordExpires: Date,
isAdmin: { type: Boolean, default: false },
});

userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", userSchema);
75 changes: 75 additions & 0 deletions StackOverFlow_Swiggy i++/public/stylesheets/landing.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
body {
background-color: #000;
}

#landing-header {
z-index: 1;
position: relative;
text-align: center;
padding-top: 40vh;
}

#landing-header h1 {
color: #fff;
}

.slideshow {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 0;
list-style: none;
margin: 0;
padding: 0;
}

.slideshow li {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
opacity: 0;
z-index: 0;
animation: imageAnimation 50s linear infinite;
}

.slideshow li:nth-child(1) {
background-image: url(https://miro.medium.com/max/1400/1*dZR-6mVLWHoQr7vBIU2-kw.png)
}
.slideshow li:nth-child(2) {
background-image: url(https://image.freepik.com/free-vector/digital-coding-background-with-numbers-zero-one_1017-30363.jpg);
animation-delay: 10s;
}
.slideshow li:nth-child(3) {
background-image: url(https://image.freepik.com/free-photo/abstract-science-background_1048-5811.jpg);
animation-delay: 20s;
}


@keyframes imageAnimation {
0% {
opacity: 0;
animation-timing-function: ease-in;
}
10% {
opacity: 1;
animation-timing-function: ease-out;
}
20% {
opacity: 1
}
30% {
opacity: 0
}
}

/* Older browser support - .no-cssanimations class added by modernizr */
.no-cssanimations .slideshow li {
opacity: 1;
}
15 changes: 15 additions & 0 deletions StackOverFlow_Swiggy i++/public/stylesheets/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.caption {
padding: 9px;
}

.card{
padding: 0;
}

.navbar{
margin-bottom: 10px;
}

.deleteform{
display: inline;
}
Loading