Skip to content

Commit

Permalink
Merge pull request #54 from versx/fix-edit-location
Browse files Browse the repository at this point in the history
Add missing edit location endpoint
  • Loading branch information
versx authored May 29, 2021
2 parents 4857b0e + 073b270 commit 6d07b5c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
30 changes: 27 additions & 3 deletions src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1140,10 +1140,10 @@ router.post('/location/new', async (req, res) => {
longitude: split[1],
});
} else {
exists.name = name,
exists.latitude = split[0],
exists.longitude = split[1],
exists.name = name;
exists.distance = distance;
exists.latitude = split[0];
exists.longitude = split[1];
}
const results = await exists.save();
if (results) {
Expand All @@ -1156,6 +1156,30 @@ router.post('/location/new', async (req, res) => {
res.redirect('/locations');
});

router.post('/location/edit/:id', async (req, res) => {
const id = req.params.id;
const { name, location, distance, } = req.body;
const loc = await Location.getById(id);
if (loc) {
const split = location.split(',');
if (split.length !== 2) {
// TODO: Failed to parse selected location
}
loc.name = name;
loc.distance = distance;
loc.latitude = split[0];
loc.longitude = split[1];
const result = loc.save();
if (result) {
// Success
console.log('Location subscription', id, 'updated successfully.');
} else {
showError(res, 'locations/locations', `Failed to update Location subscription ${id}`);
}
}
res.redirect('/locations');
});

router.post('/locations/delete/:id', async (req, res) => {
const id = req.params.id;
const exists = await Location.getById(id);
Expand Down
2 changes: 1 addition & 1 deletion src/views/locations/edit.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h1 align="center" id="header">{{Edit Location}} {{name}}</h1>
<br>
<div class="w-75" style="float: none; margin: 0 auto;">
<form action="/api/locations/edit/{{name}}" method="post">
<form action="/api/location/edit/{{id}}" method="post">
<div class="form-group">
{{Location Name}}
<input list="names" class="form-control" name="name" id="name" value="{{name}}" readonly>
Expand Down

0 comments on commit 6d07b5c

Please sign in to comment.