Skip to content

Commit 24e74a5

Browse files
committed
added delete option and form
1 parent 09ca5b1 commit 24e74a5

6 files changed

Lines changed: 112 additions & 14 deletions

File tree

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.launchcode.codingevents.controllers;
22

3+
import org.launchcode.codingevents.data.EventData;
34
import org.launchcode.codingevents.models.Event;
45
import org.springframework.stereotype.Controller;
5-
import org.springframework.web.bind.annotation.GetMapping;
6-
import org.springframework.web.bind.annotation.PostMapping;
7-
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.*;
87
import org.springframework.ui.Model;
9-
import org.springframework.web.bind.annotation.RequestParam;
108

119
import java.util.ArrayList;
1210
import java.util.List;
@@ -18,12 +16,10 @@
1816
@RequestMapping("events")
1917
public class EventController {
2018

21-
private static List<Event> events = new ArrayList<>();
22-
23-
@GetMapping
19+
@GetMapping
2420
public String displayAllEvents(Model model) {
2521
model.addAttribute("title", "All Events");
26-
model.addAttribute("events", events);
22+
model.addAttribute("events", EventData.getAll());
2723
return "events/index";
2824
}
2925

@@ -34,8 +30,27 @@ public String displayCreateEventForm(Model model) {
3430
}
3531

3632
@PostMapping("create")
37-
public String processCreateEventForm(@RequestParam String eventName, @RequestParam String eventDescription) {
38-
events.add(new Event(eventName, eventDescription));
33+
public String processCreateEventForm(@ModelAttribute Event newEvent) {
34+
EventData.add(newEvent);
35+
return "redirect:";
36+
}
37+
38+
@GetMapping("delete")
39+
public String renderDeleteEventForm(Model model) {
40+
model.addAttribute("title", "Delete Event");
41+
model.addAttribute("events", EventData.getAll());
42+
return "events/delete";
43+
}
44+
45+
@PostMapping("delete")
46+
public String processDeleteEventsForm(@RequestParam(required = false) int[] eventIds) {
47+
48+
if (eventIds != null) {
49+
for (int id : eventIds) {
50+
EventData.remove(id);
51+
}
52+
}
53+
3954
return "redirect:";
4055
}
4156

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.launchcode.codingevents.data;
2+
3+
import org.launchcode.codingevents.models.Event;
4+
5+
import java.util.Collection;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class EventData {
10+
private static final Map<Integer, Event> events = new HashMap<>();
11+
12+
public static void add(Event event){
13+
events.put(event.getId(), event);
14+
}
15+
16+
public static Event getById(Integer id) {
17+
return events.get(id);
18+
}
19+
20+
public static Collection<Event> getAll() {
21+
return events.values();
22+
}
23+
24+
public static void remove(Integer id) {
25+
if (events.containsKey(id)) {
26+
events.remove(id);
27+
}
28+
}
29+
30+
}

src/main/java/org/launchcode/codingevents/models/Event.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package org.launchcode.codingevents.models;
22

3+
import java.util.Objects;
4+
35
public class Event {
46

7+
private int id;
8+
private static int nextId = 1;
9+
510
private String name;
611
private String description;
712

8-
public Event(String name) {
13+
public Event(String name, String description) {
914
this.name = name;
1015
this.description = description;
16+
this.id = nextId;
17+
nextId++;
18+
}
1119

20+
public int getId() {
21+
return id;
1222
}
1323

24+
1425
public String getName() {
1526
return name;
1627
}
@@ -31,4 +42,17 @@ public void setDescription(String description) {
3142
public String toString() {
3243
return name;
3344
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) return true;
49+
if (o == null || getClass() != o.getClass()) return false;
50+
Event event = (Event) o;
51+
return id == event.id;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(id);
57+
}
3458
}

src/main/resources/templates/Events/create.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
<!DOCTYPE html>
22
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3+
<title>
34
<head th:replace="fragments :: head"></head>
5+
</title>
46
<body class="container">
57

68
<header th:replace="fragments :: header"></header>
79

810
<form method="post">
911
<div class="form-group">
1012
<label> Name
11-
<input type="text" name="eventName" class="form-control">
13+
<input type="text" name="name" class="form-control">
1214
</label>
1315
</div>
1416
<br>
1517
<div class="form-group">
1618
<label>
1719
Description
18-
<input type="text" name="eventDescription" class="form-control">
20+
<input type="text" name="description" class="form-control">
1921
</label>
2022
<input type="submit" value="Create" class="btn btn-success">
2123
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<header th:replace="fragments :: header"></header>
7+
8+
<form method="post">
9+
10+
<th:block th:each="event : ${events}">
11+
<div class="form-group">
12+
<label>
13+
<span th:text="${event.name}"></span>
14+
<input type="checkbox" name="eventIds" th:value="${event.id}" class="form-control">
15+
</label>
16+
</div>
17+
</th:block>
18+
19+
<input type="submit" value="Delete Selected Events" class="btn btn-danger">
20+
</form>
21+
22+
</body>
23+
</html>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<!DOCTYPE html>
22
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3-
<head th:replace="fragments :: head"></head>
3+
<title>
4+
<head th:replace="fragments :: head"></head>
5+
</title>
46
<body class="container">
57

68
<header th:replace="fragments :: header"></header>
79

810
<table class="table table-striped">
911
<thead>
1012
<tr>
13+
<th>ID</th>
1114
<th>Name</th>
1215
<th>Description</th>
1316
</tr>
1417
</thead>
1518
<tr th:each="event : ${events}">
19+
<td th:text="${event.id}"></td>
1620
<td th:text="${event.name}"></td>
1721
<td th:text="${event.description}"></td>
1822
</tr>

0 commit comments

Comments
 (0)