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

Rendu TD4 #15

Open
wants to merge 7 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
1 change: 0 additions & 1 deletion README.md

This file was deleted.

15 changes: 15 additions & 0 deletions backend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
env: {
browser: true,
es2021: true
},
extends: [
'standard'
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module'
},
rules: {
}
}
68 changes: 68 additions & 0 deletions backend/inMemoryWorkshop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const inMemoryWorkshop = []

function getWorkshopList () {
return new Promise((resolve) => {
resolve(inMemoryWorkshop)
})
}

function getWorkshopByName (name) {
return new Promise((resolve, reject) => {
if (!name) {
reject(new Error('name parameter is required'))
}
resolve(inMemoryWorkshop.find(workshop => workshop.name === workshop))
})
}

function addWorkshop (name, description) {
return new Promise((resolve, reject) => {
if (!name) {
reject(new Error('Workshop name required'))
}
if (!description) {
reject(new Error('Workshop description required'))
}
inMemoryWorkshop.push({
name,
description
})
resolve()
})
}

function removeWorkshopByName (name) {
return new Promise((resolve, reject) => {
var obj = JSON.parse(JSON.stringify(inMemoryWorkshop))
for (var i = 0; i < inMemoryWorkshop.length; i++) {
console.log('obj' + obj[i].name)
console.log(name)
if (obj[i].name === name) {
console.log('here')
inMemoryWorkshop.splice(i, 1)
}
}
resolve()
})
}

function updateWorkshop (name, oldName) {
return new Promise((resolve, reject) => {
var obj = JSON.parse(JSON.stringify(inMemoryWorkshop))
for (var i = 0; i < inMemoryWorkshop.length; i++) {
if (obj[i].name === oldName) {
obj[i].name = name
inMemoryWorkshop[i] = obj[i]
}
}
resolve()
})
}

module.exports = {
getWorkshopList,
getWorkshopByName,
addWorkshop,
removeWorkshopByName,
updateWorkshop
}
90 changes: 90 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

const express = require('express')
const app = express()
const InMemoryWorkshop = require('./inMemoryWorkshop')
const path = require('path')
const ejs = require('ejs')
var bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({ extended: false }))

// set the view engine to ejs
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, '..', 'frontend/'))
app.use(express.static(path.join(__dirname, '..', 'frontend/style')))

app.get('/', function (req, res) {
InMemoryWorkshop.getWorkshopList()
.then(workshops => {
res.render('index', {
workshops: workshops
})
})
})

app.get('/workshop', function (req, res) {
console.log('get')
res.render('workshop')
})

app.get('/update-workshop', function (req, res) {
res.render('update-workshop')
})
app.get('/remove-workshop', function (req, res) {
res.render('remove-workshop')
})
app.post('/workshop', function (req, res) {
console.log('add')
const name = req.body.name
const description = req.body.description
InMemoryWorkshop.addWorkshop(name, description).then(() => {
InMemoryWorkshop.getWorkshopList()
.then(workshops => {
res.render('index', {
workshops: workshops
})
})
})
.catch(e => res.send(e.message))
})

app.get('/workshop/:name', function (req, res) {
const workshopName = req.params.name
InMemoryWorkshop.getWorkshopByName(workshopName)
.then(workshop => {
res.render('ejs/workshop', workshop)
})
.catch(e => ejs.send(e.message))
})

app.post('/remove-workshop', function (req, res) {
const name = req.body.name
InMemoryWorkshop.removeWorkshopByName(name).then(() => {
InMemoryWorkshop.getWorkshopList()
.then(workshops => {
res.render('index', {
workshops: workshops
})
})
})
.catch(e => res.send(e.message))
})

app.post('/update-workshop', function (req, res) {
console.log('update')
const name = req.body.name
const oldName = req.body.oldName
InMemoryWorkshop.updateWorkshop(name, oldName).then(() => {
InMemoryWorkshop.getWorkshopList()
.then(workshops => {
res.render('index', {
workshops: workshops
})
})
})
.catch(e => res.send(e.message))
})

app.listen(3000, function () {
console.log('Workshop app listening on port 3000!')
})
31 changes: 0 additions & 31 deletions ejs/index.ejs

This file was deleted.

File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions frontend/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">

<head>
<% include ./head %>
</head>

<body class="container">

<div class="jumbotron">
<h1>Workshops</h1>
<p>here is the list of workshop</p>
</div>

<a type="button" href="/workshop" class="btn btn-success btn-lg">Create new workshop</a>
<a type="button" href="/update-workshop" class="btn btn-success btn-lg">Update</a>
<a type="button" href="/remove-workshop" class="btn btn-danger btn-lg">Remove</a>

<ul class="list-unstyled"></ul>
<% workshops.forEach(function(workshop) { %>
<li class="media my-4">
<div class="media-body">
<h5 class="mt-0 mb-1"><%= workshop.name %></h5>
<%= workshop.description %>
</div>
</li>
<% }); %>
</ul>

<footer>
<% include ./footer %>
</footer>

</body>

</html>
26 changes: 26 additions & 0 deletions frontend/remove-workshop.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">

<head>
<% include ./head %>
</head>

<body class="container">

<form action="/remove-workshop" method="post">
<div class="form-group">
<label for="name">Workshop name to delete</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Workshop Name" required>
</div>

<button type="submit" class="btn btn-danger">Remove</button>
<a type="button" class="btn btn-secondary" href="/">Cancel</a>
</form>

<footer>
<% include ./footer %>
</footer>

</body>

</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions frontend/update-workshop.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">

<head>
<% include ./head %>
</head>

<body class="container">

<form action="/update-workshop" method="post">
<div class="form-group">
<label for="oldName">Old Name</label>
<input type="text" name="oldName" class="form-control" id="oldName" placeholder="Workshop Name" required>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Workshop Name" required>
</div>
<button type="submit" class="btn btn-primary">Save</button>
<a type="button" class="btn btn-secondary" href="/">Cancel</a>
</form>

<footer>
<% include ./footer %>
</footer>

</body>

</html>
File renamed without changes.
Empty file removed js/README.md
Empty file.
53 changes: 0 additions & 53 deletions js/inMemoryWorkshop.js

This file was deleted.

Loading