Skip to content

Commit

Permalink
Merge pull request #2 from kstafa/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kstafa authored Oct 14, 2024
2 parents c0367fc + c29f079 commit be69694
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# M2-Workshops

## Features

- Create new workshops
- View list of workshops
- Edit existing workshops
- Delete existing workshops
8 changes: 6 additions & 2 deletions ejs/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
<%= workshop.name %>
</h5>
<%= workshop.description %>
<a href="/workshop/<%= encodeURIComponent(workshop.name) %>/edit"
class="btn btn-primary btn-sm ml-2">Edit</a>
<a href="/workshop/<%= workshop.name %>/edit" class="btn btn-primary btn-sm ml-2">Modifier</a>
<form action="/remove-workshop" method="POST" class="d-inline">
<input type="hidden" name="name" value="<%= workshop.name %>">
<button type="submit" class="btn btn-danger btn-sm ml-2"
onclick="return confirm('Êtes-vous sûr de vouloir supprimer cet atelier ?');">Supprimer</button>
</form>
</div>
</li>
<% }); %>
Expand Down
11 changes: 8 additions & 3 deletions js/inMemoryWorkshop.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ function addWorkshop(name, description) {

function removeWorkshopByName(name) {
return new Promise((resolve, reject) => {
reject(new Error("Not implemented"))
})
const initialLength = inMemoryWorkshop.length;
inMemoryWorkshop = inMemoryWorkshop.filter(workshop => workshop.name !== name);
if (inMemoryWorkshop.length === initialLength) {
reject(new Error("Atelier non trouvé"));
} else {
resolve();
}
});
}

function updateWorkshop(originalName, name, description) {
return new Promise((resolve, reject) => {
if (!originalName || !name || !description) {
Expand Down
9 changes: 7 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ repository.init().then(() => {


app.post('/remove-workshop', function (req, res) {
res.status(500).send("TODO")
})
const workshopName = req.body.name;
repository.removeWorkshopByName(workshopName)
.then(() => {
res.redirect('/');
})
.catch(e => res.status(500).send(e.message));
});

app.post('/update-workshop', function (req, res) {
const originalName = req.body.originalName;
Expand Down
9 changes: 6 additions & 3 deletions js/mongoWorkshop.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ function addWorkshop(name, description) {

function removeWorkshopByName(name) {
const collection = db.collection(COLLECTION_NAME);
return collection.deleteMany({
name
}).then(() => { return })
return collection.deleteOne({ name: name })
.then(result => {
if (result.deletedCount === 0) {
throw new Error("Atelier non trouvé");
}
});
}

function updateWorkshop(originalName, name, description) {
Expand Down

0 comments on commit be69694

Please sign in to comment.