Skip to content

Commit 5dc097e

Browse files
Maria UsinaMaria Usina
authored andcommitted
Exercises
1 parent 2f21cae commit 5dc097e

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

src/main/java/org/launchcode/codingevents/controllers/EventController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,21 @@ public String processDeleteEventsForm(@RequestParam(required = false) int[] even
8181
return "redirect:/events";
8282
}
8383

84+
@GetMapping("edit/{eventId}")
85+
public String displayEditForm(Model model, @PathVariable int eventId){
86+
Event eventToEdit = EventData.getById(eventId);
87+
model.addAttribute("event", eventToEdit);
88+
String title = "Edit Event " + eventToEdit.getName() + " (id=" + eventToEdit.getId() + ")";
89+
model.addAttribute("title", title );
90+
return "events/edit";
91+
}
92+
93+
@PostMapping("edit")
94+
public String processEditForm(int eventId, String name, String description) {
95+
Event eventToEdit = EventData.getById(eventId);
96+
eventToEdit.setName(name);
97+
eventToEdit.setDescription(description);
98+
return "redirect:/events";
99+
}
100+
84101
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3+
<head th:replace="fragments :: head"></head>
4+
<body class="container">
5+
6+
<h1>Create Event</h1>
7+
8+
<nav th:replace="fragments :: navigation"></nav>
9+
10+
<form method="post" th:action="@{/events/edit}">
11+
<div class="form-group">
12+
<label>Name
13+
<input type="text" name="name" class="form-control" th:value="${event.name}>
14+
</label>
15+
</div>
16+
17+
<div class="form-group">
18+
<label>Description
19+
<input type="text" name="description" class="form-control" th:value="${event.description}">
20+
</label>
21+
</div>
22+
23+
<div class="form-group">
24+
<input type="hidden" name="eventId" th:value="${event.id}">
25+
</div>
26+
27+
<div class="form-group">
28+
<input type="submit" value="Edit Event" class="btn btn-success">
29+
</div>
30+
</form>
31+
32+
</body>
33+
</html>

src/main/resources/templates/events/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ <h1>All Events</h1>
2828
<th>ID</th>
2929
<th>Name</th>
3030
<th>Description</th>
31+
<th>Edit</th>
3132
</tr>
3233
</thead>
3334
<tr th:each="event : ${events}">
3435
<td th:text="${event.id}"></td>
3536
<td th:text="${event.name}"></td>
3637
<td th:text="${event.description}"></td>
38+
<td><a th:href="@{/events/edit/{id}(id=${event.id})}">Edit</a></td>
3739
</tr>
3840
</table>
3941

0 commit comments

Comments
 (0)