Skip to content

Commit

Permalink
update workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Oct 12, 2020
1 parent 4995182 commit 3f08eaa
Show file tree
Hide file tree
Showing 7 changed files with 1,594 additions and 35 deletions.
34 changes: 19 additions & 15 deletions ejs/index.ejs
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@

<!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>
<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="/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>

<ul class="list-unstyled"></ul>
<% workshops.forEach(function(workshop) { %>
<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>
<%= workshop.description %>
</div>
</li>
<% }); %>
</ul>
<% }); %>
</ul>

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

</body>
</html>

</html>
29 changes: 29 additions & 0 deletions ejs/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>
15 changes: 15 additions & 0 deletions js/.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: {
}
}
14 changes: 11 additions & 3 deletions js/inMemoryWorkshop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ inMemoryWorkshop = []


function getWorkshopList() {
return new Promise((resolve, ) => {
return new Promise((resolve,) => {
resolve(inMemoryWorkshop)
})
}
Expand Down Expand Up @@ -38,9 +38,17 @@ function removeWorkshopByName(name) {
})
}

function updateWorkshop(name) {
function updateWorkshop(name, oldName) {
return new Promise((resolve, reject) => {
reject(new Error("Not implemented"))
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()

})
}

Expand Down
52 changes: 35 additions & 17 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,72 @@ app.use(bodyParser.urlencoded({ extended: false }))
// set the view engine to ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '..', '/ejs'));
app.use(express.static(path.join(__dirname , '..', 'css')));
app.use(express.static(path.join(__dirname, '..', 'css')));


app.get('/', function (req, res) {
InMemoryWorkshop.getWorkshopList()
.then(workshops => {
res.render("index", {
workshops: workshops
.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) {
console.log("get")
res.render('update-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
.then(workshops => {
res.render("index", {
workshops: workshops
})
})
})
})
.catch(e =>res.send(e.message))
.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))
.then(workshop => {
res.render('ejs/workshop', workshop)
})
.catch(e => ejs.send(e.message))
})

app.post('/remove-workshop', function (req, res) {
res.status(500).send("TODO")
})

app.post('/update-workshop', function(req, res) {
res.status(500).send("TODO")
app.post('/update-workshop', function (req, res) {
// res.status(500).send("TODO")
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!')
console.log('Workshop app listening on port 3000!')
})
Loading

0 comments on commit 3f08eaa

Please sign in to comment.