Skip to content

Commit 1256390

Browse files
committed
Added the ability to delete people
1 parent 9c993c6 commit 1256390

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

src/HealthTracker.Mvc/Controllers/PersonController.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ public PersonController(
3030
[HttpGet]
3131
public async Task<IActionResult> Index()
3232
{
33-
// Get the list of current people
34-
var people = await _client.ListAsync(1, _settings.ResultsPageSize);
35-
var personText = people.Count == 1 ? "person" : "people";
36-
_logger.LogDebug($"{people.Count} {personText} loaded via the service");
37-
38-
// Construct the view model and serve the page
39-
var model = new PersonListViewModel();
40-
model.SetEntities(people, 1, _settings.ResultsPageSize);
33+
var model = await CreatePersonListViewModel("");
4134
return View(model);
4235
}
4336

@@ -187,5 +180,45 @@ public async Task<IActionResult> Edit(EditPersonViewModel model)
187180

188181
return result;
189182
}
183+
184+
/// <summary>
185+
/// Handle POST events to delete an existing medication
186+
/// </summary>
187+
/// <param name="id"></param>
188+
/// <returns></returns>
189+
[HttpPost]
190+
[ValidateAntiForgeryToken]
191+
public async Task<IActionResult> Delete(int id)
192+
{
193+
// Delete the item
194+
_logger.LogDebug($"Deleting person: ID = {id}");
195+
await _client.DeleteAsync(id);
196+
197+
// Return the list view with an empty list of items
198+
var message = $"Person with ID {id} successfully deleted";
199+
var model = await CreatePersonListViewModel(message);
200+
return View("Index", model);
201+
}
202+
203+
/// <summary>
204+
/// Build the person list view model
205+
/// </summary>
206+
/// <returns></returns>
207+
private async Task<PersonListViewModel> CreatePersonListViewModel(string message)
208+
{
209+
// Get the list of current people
210+
var people = await _client.ListAsync(1, _settings.ResultsPageSize);
211+
var personText = people.Count == 1 ? "person" : "people";
212+
_logger.LogDebug($"{people.Count} {personText} loaded via the service");
213+
214+
215+
// Construct the view model and serve the page
216+
var model = new PersonListViewModel()
217+
{
218+
Message = message
219+
};
220+
model.SetEntities(people, 1, _settings.ResultsPageSize);
221+
return model;
222+
}
190223
}
191224
}

src/HealthTracker.Mvc/Views/Person/Index.cshtml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
</div>
2929
</div>
3030
}
31+
@using (Html.BeginForm("Delete", "Person", FormMethod.Post, new { name = "delete-form", id = "delete-form" }))
32+
{
33+
@Html.AntiForgeryToken()
34+
@Html.Hidden("id", 0)
35+
}
3136

3237
@using (Html.BeginForm("Index", "Person", FormMethod.Post))
3338
{
@@ -42,6 +47,7 @@
4247
<th>Height</th>
4348
<th>Gender</th>
4449
<th />
50+
<th />
4551
</tr>
4652
@foreach (var person in Model.People)
4753
{
@@ -56,6 +62,11 @@
5662
<i class="fas fa-edit"></i>
5763
</a>
5864
</td>
65+
<td valign="center">
66+
<a class="btn btn-lg" href="javascript:submitDeleteForm(@person.Id)">
67+
<i class="fas fa-trash"></i>
68+
</a>
69+
</td>
5970
</tr>
6071
}
6172
</table>
@@ -67,3 +78,19 @@
6778
</p>
6879
}
6980
</div>
81+
82+
<script type="text/javascript">
83+
function submitDeleteForm(id) {
84+
$.confirm({
85+
text: "Are you sure?",
86+
confirm: function(button) {
87+
$("#delete-form #id").val(id);
88+
$("#delete-form").submit()
89+
},
90+
cancel: function(button) {
91+
},
92+
confirmButton: "Yes",
93+
cancelButton: "No"
94+
});
95+
}
96+
</script>

0 commit comments

Comments
 (0)