Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Twoguini committed Nov 15, 2023
1 parent fd82c7b commit f044302
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 149 deletions.
15 changes: 15 additions & 0 deletions src/firstInfoValidation/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function validation(username, password){
const errors = []

if (!username || username === null) { // TODO transform into validation function
errors.push({ text: "Usuário Não Foi Inserido" });
}

if (!password || password === null) {
errors.push({ text: "Senha Não Foi Inserida" });
}

return(errors);
};

export default validation;
61 changes: 5 additions & 56 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,12 @@
import fs from 'fs/promises';
import cors from 'cors';
import { v4 as uuid} from "uuid";
import _ from 'lodash';
import path from 'path';
import handlebars from 'express-handlebars';
import bodyParser from 'body-parser';
import webQuest from './routes/WebQuest.js';
import session from 'express-session';
import flash from 'connect-flash';
import databaseConnetion from './db/db.js';
import userPages from './routes/User.js';
import { fileURLToPath } from 'url';
import webServer from './webServer.js';
import createWebServer from './webServer.js';

// SECTION: ========================= Directories =========================
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const publicPath = path.join(__dirname, 'public'); // NOTE: Public Dir
const createWebServer = webServer();
// !SECTION ===============================================================
const webServer = createWebServer();

// SECTION: =========================== Config ============================
app.use(xpress.json()); // NOTE: To Handle Json
app.use("/style", xpress.static(publicPath + "/style")); // NOTE: Css Styles
app.use("/script", xpress.static(publicPath + "/script")); // NOTE: Js Scripts
app.use("/img", xpress.static(publicPath + "/img")); // NOTE: imgs

app.use(session({ //NOTE: Session configuration
secret: "WebQuest",
resave: true,
saveUninitialized: true
}));
app.use(flash());// NOTE: Messages That Desapear

app.use((req, res, next) => { // NOTE: Flahs Messages
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
next();
});

app.use(bodyParser.urlencoded({ extended: false }));// NOTE: Handle Forms
app.use(bodyParser.json());// NOTE: ...

app.engine("handlebars", handlebars.engine({ defaultLayout: 'main' })); // NOTE: HandleBars as Template engine
app.set('view engine', 'handlebars'); // NOTE: ...
// !SECTION ===============================================================

// SECTION: ============================ Routes ===========================
app.use('/', webQuest);
app.use('/', userPages);
// !SECTION ===============================================================

try{
try {
webServer.start(8080);
webServer.stop();
} catch (err){
console.log('[index] Erro');
console.log(err);
};
console.log(`${0} Erro Encontrado`, err);
}

// https://www.youtube.com/watch?v=tgYztVxL41Y
// https://www.youtube.com/watch?v=ldYcgPKEZC8
Expand Down
2 changes: 1 addition & 1 deletion src/queries/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default async function getUsers(req, res, next){
let users = await databaseConnection.any("SELECT * FROM students");
res.status(200).json({
users,
status: "Succes"
status: "Success"
})
}catch(err){
next(err);
Expand Down
23 changes: 5 additions & 18 deletions src/routes/User.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import express from 'express'
import ifNull from '../firstInfoValidation/login.js';
import dbUsers from '../queries/users.js';

const users = express.Router()

function validation(username, password){
const errors = []

if (!username || username === null) { // TODO transform into validation function
errors.push({ text: "Usuário Não Foi Inserido" });
}

if (!password || password === null) {
errors.push({ text: "Senha Não Foi Inserida" });
}

return(errors);
};

users.get('/', (req, res) => {
// NOTE: LogIn Page
res.render('login');
Expand All @@ -26,11 +14,10 @@ users.post("/", (req, res) => {
const username = req.body.User;
const password = req.body.Pass;

const errors = validation(username, password);
const errors = ifNull(username, password);

if (errors.length > 0) {
res.render("signUp", { errors: errors });

return
}

Expand All @@ -48,15 +35,15 @@ users.post('/signup', (req, res) => {
const username = req.body.User;
const password = req.body.Pass;

const errors = validation(username, password);
const errors = ifNull(username, password);

if (errors.length > 0) {
res.render("signUp", { errors: errors });

return
}

req.flash("success_msg", "Login Efetivado com Sucesso");
req.flash("success_msg", "Conta Criada com Sucesso");
res.redirect('/');
});

Expand Down
4 changes: 2 additions & 2 deletions src/views/signUp.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
</section>
<input type="submit" id="sbmt-btn" class="btn" value="SignUp">
{{#each errors}}
<div class="alert alert-danger m-1">{{text}}</div>
<div class="alert alert-danger m-1">{{text}}</div>
{{else}}
{{/each}}
{{/each}}
</form>
</section>
</main>
Expand Down
6 changes: 3 additions & 3 deletions src/views/webQuest/home.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<h1>WEB-QUEST SPINGOLAS</h1>

<ul class="links">
<li class="nav-item"><a class="atv" href="/WebQuest/home">Home</a></li>
<li class="nav-item"><a href="/WebQuest/material">Material de estudo</a></li>
<li class="nav-item"><a href="/WebQuest/questions">Questionarios</a></li>
<li class="nav-item"><a class="atv" href="/home">Home</a></li>
<li class="nav-item"><a href="/material">Material de estudo</a></li>
<li class="nav-item"><a href="/questions">Questionarios</a></li>
</ul>
</nav>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/views/webQuest/material.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<h1>WEB-QUEST SPINGOLAS</h1>

<ul class="links">
<li class="nav-item"><a class="atv" href="/WebQuest/home">Home</a></li>
<li class="nav-item"><a href="/WebQuest/material">Material de estudo</a></li>
<li class="nav-item"><a href="/WebQuest/questions">Questionarios</a></li>
<li class="nav-item"><a class="atv" href="/home">Home</a></li>
<li class="nav-item"><a href="/material">Material de estudo</a></li>
<li class="nav-item"><a href="/questions">Questionarios</a></li>
</ul>
</nav>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/views/webQuest/quests.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<h1>WEB-QUEST SPINGOLAS</h1>

<ul class="links">
<li class="nav-item"><a class="atv" href="/WebQuest/home">Home</a></li>
<li class="nav-item"><a href="/WebQuest/material">Material de estudo</a></li>
<li class="nav-item"><a href="/WebQuest/questions">Questionarios</a></li>
<li class="nav-item"><a class="atv" href="/home">Home</a></li>
<li class="nav-item"><a href="/material">Material de estudo</a></li>
<li class="nav-item"><a href="/questions">Questionarios</a></li>
</ul>
</nav>
</div>
Expand Down
90 changes: 27 additions & 63 deletions src/webServer.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,36 @@
import xpress from 'express';
import _ from 'lodash';
import path from 'path';
import handlebars from 'express-handlebars';
import bodyParser from 'body-parser';
import webQuest from './routes/WebQuest.js';
import session from 'express-session';
import flash from 'connect-flash';
import userPages from './routes/User.js';
import { fileURLToPath } from 'url';
import webServer from './webServer.js';
import webSettings from './webServerSettings.js';

function createWebServer (){

var app = xpress();

// SECTION: ========================= Directories =========================
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const publicPath = path.join(__dirname, 'public'); // NOTE: Public Dir
const createWebServer = webServer();
// !SECTION ===============================================================

// SECTION: =========================== Config ============================
app.use(xpress.json()); // NOTE: To Handle Json
app.use("/style", xpress.static(publicPath + "/style")); // NOTE: Css Styles
app.use("/script", xpress.static(publicPath + "/script")); // NOTE: Js Scripts
app.use("/img", xpress.static(publicPath + "/img")); // NOTE: imgs

app.use(session({ //NOTE: Session configuration
secret: "WebQuest",
resave: true,
saveUninitialized: true
}));
app.use(flash());// NOTE: Messages That Desapear

app.use((req, res, next) => { // NOTE: Flahs Messages
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
next();
});

app.use(bodyParser.urlencoded({ extended: false }));// NOTE: Handle Forms
app.use(bodyParser.json());// NOTE: ...

app.engine("handlebars", handlebars.engine({ defaultLayout: 'main' })); // NOTE: HandleBars as Template engine
app.set('view engine', 'handlebars'); // NOTE: ...
// !SECTION ===============================================================

// SECTION: ============================ Routes ===========================
app.use('/', webQuest);
app.use('/', userPages);
// !SECTION ===============================================================

function start(port){
app.listen(port, () =>{
start.created = '[WebServer] Servidor Criado'
});
};

function stop(){
app.close(()=>{
stop.stopped = '[WebServer] Servidor Fechado'
});
async function start(port){
console.log("[WebServer] Criando Servidor");
try{
await webSettings.serverConfig();
}catch(err){
console.log(`[WebServer] ${0}`, err);
process.exit(1)
};

try{
await webSettings.setRoutes();
}catch(err){
console.log(`[WebServer] ${0}`, err);
process.exit(1)
};

try{
await webSettings.app.listen(port, () =>{
console.log ('[WebServer] Servidor Criado');
});
}catch(err){
console.log(`[WebServer] ${0}`, err);
process.exit(1)
};
return;
};

return{
start,
stop
start
}
};

Expand Down
63 changes: 63 additions & 0 deletions src/webServerSettings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import xpress from 'express';
import path from 'path';
import handlebars from 'express-handlebars';
import bodyParser from 'body-parser';
import webQuest from './routes/WebQuest.js';
import session from 'express-session';
import flash from 'connect-flash';
import userPages from './routes/User.js';
import { fileURLToPath } from 'url';

var app = xpress();

// SECTION: ========================= Directories =========================
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const publicPath = path.join(__dirname, 'public'); // NOTE: Public Dir
// !SECTION ===============================================================

function serverConfig(){
// SECTION: =========================== Config ============================
console.log("[WebServer] Ajustando Configurações do Back End");
app.use(xpress.json()); // NOTE: To Handle Json
app.use("/style", xpress.static(publicPath + "/style")); // NOTE: Css Styles
app.use("/script", xpress.static(publicPath + "/script")); // NOTE: Js Scripts
app.use("/img", xpress.static(publicPath + "/img")); // NOTE: imgs

app.use(session({ //NOTE: Session configuration
secret: "WebQuest",
resave: true,
saveUninitialized: true
}));
app.use(flash());// NOTE: Messages That Desapear

app.use((req, res, next) => { // NOTE: Flahs Messages
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
next();
});

app.use(bodyParser.urlencoded({ extended: false }));// NOTE: Handle Forms
app.use(bodyParser.json());// NOTE: ...

app.engine("handlebars", handlebars.engine({ defaultLayout: 'main' })); // NOTE: HandleBars as Template engine
app.set('view engine', 'handlebars'); // NOTE: ...
console.log("[WebServer] Configurações Prontas");
return;
// !SECTION ===============================================================
};

// SECTION: ============================ Routes ===========================
function setRoutes() {
app.use('/', webQuest);
app.use('/', userPages);
console.log ('[WebServer] Rotas Prontas');
return;
};
// !SECTION ===============================================================

export default{
app,
serverConfig,
setRoutes
};

0 comments on commit f044302

Please sign in to comment.