Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Complete refactor of app based on OOP principles #102

Open
wants to merge 9 commits 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
3 changes: 1 addition & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"tabWidth": 4,
"useTabs": true,
"printWidth": 150
"useTabs": true
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If you want to contribute to development of Compass, we encourage you to explore

2. Run the `db_setup` script in the `db` folder to build the database.
3. Create `.env` file and insert your secrets into it. Use `.env.example` file as reference for what is required.
4. Run `nodemon` and open the app at `localhost:<port>` specifying the port defined in `index.js`.
4. Run `nodemon` and open the app at `localhost:<port>` specifying the port defined in `app.js`.

$ npm install -g nodemon
$ nodemon
Expand Down
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

require("dotenv").config();

const { Server } = require("./controllers/Server");
new Server();
67 changes: 0 additions & 67 deletions config/auth.js

This file was deleted.

25 changes: 25 additions & 0 deletions controllers/Courses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
const Router = require("./Router");

class Courses extends Router {
constructor(app, passport, db) {
super(app, passport, db);
}

setRoutes(db) {
this.router.get("/", (req, res) => {
db.Course.findAll().then((result) => {
res.render("index.html", {
title: "Compass – Courses",
heading: "Courses",
courseReviewData: result,
user: req.user,
errorMessage: req.flash("error"),
warningMessage: req.flash("warning"),
});
});
});
}
}

module.exports = Courses;
26 changes: 26 additions & 0 deletions controllers/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";

const Router = require("./Router");

class Errors extends Router {
constructor(app, passport, db) {
super(app, passport, db);
}

setRoutes(db) {
// Handle pages not found.
this.app.use((req, res) => {
res.status(404).render("404.html", {
title: "Compass - Page not found",
});
});

// Handle 500 server errors.
this.app.use((err, req, res) => {
console.error(err.stack);
res.status(500).send("<h1>500: Internal server error</h1>");
});
}
}

module.exports = Errors;
21 changes: 21 additions & 0 deletions controllers/HttpServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require("fs");
const http = require("http");

class HttpServer {
constructor(app) {
this.createHttpServer(app);
}

createHttpServer(app) {
const httpServer = http.createServer(app);
const httpPort = process.env.HTTP_PORT || "8087";

httpServer.listen(httpPort, () => {
console.log(`HTTP Server started at port ${httpPort}`);
});

}
}
}

module.exports = HttpServer;
85 changes: 85 additions & 0 deletions controllers/Passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"use strict";

const passport = require("passport");

class Passport {
constructor(app, db) {
this.passport = passport;
this.createPassportSession(app, db);
}

createPassportSession(app, db) {
app.use(passport.initialize(undefined));
app.use(passport.session(undefined));

// use `id` for serializing and deserializing users in database
passport.serializeUser(function (user, done) {
done(null, user.id);
});

passport.deserializeUser(function (id, done) {
let result = db.User.findByPk(id)
.then(done(null, true))
.catch((err) => {
done(err, null, { message: "User does not exist" });
});
});

const SlackStrategy = require("passport-slack").Strategy;

passport.use(
"slack",
new SlackStrategy(
{
clientID: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
scope: [
"identity.basic",
"identity.email",
"identity.avatar",
],
},
(accessToken, refreshToken, profile, done) => {
db.User.findByPk(profile.user.id)
.then((user) => {
if (!user) {
db.User.create({
id: profile.user.id,
name: profile.user.name,
email: profile.user.email,
avatar_url: profile.user.image_512,
})
.then(() => {
return done(null, user);
})
.catch((err) => {
console.log(
"User couldn't be created: " + err
);
});
}

return done(null, user);
})
.catch((err) => {
console.log(err);
});
}
)
);
}

checkAuth(req, res, next) {
if (req.isAuthenticated()) {
next();
} else {
req.flash(
"warning",
"You have to sign in before you can access this page"
);
res.redirect("/courses");
}
}
}

module.exports = Passport;
Loading