Skip to content

Commit 8f4b1d6

Browse files
committed
completed creating a data layer
1 parent dffd001 commit 8f4b1d6

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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;
56
import org.springframework.web.bind.annotation.GetMapping;
@@ -15,12 +16,10 @@
1516
@RequestMapping("events")
1617
public class EventController {
1718

18-
private static List<Event> events = new ArrayList<>();
19-
2019
@GetMapping
2120
public String displayAllEvents(Model model) {
2221
model.addAttribute("title", "All Events");
23-
model.addAttribute("events", events);
22+
model.addAttribute("events", EventData.getAll());
2423
return "events/index";
2524
}
2625

@@ -34,7 +33,7 @@ public String displayCreateEventForm(Model model) {
3433
// lives at /events/create
3534
@PostMapping("create")
3635
public String processCreateEventForm(@RequestParam String eventName, @RequestParam String eventDescription) {
37-
events.add(new Event(eventName, eventDescription));
36+
EventData.add(new Event(eventName, eventDescription));
3837
return "redirect:/events";
3938
}
4039

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.Collections;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class EventData {
11+
12+
private static final Map<Integer, Event> events = new HashMap<>();
13+
14+
public static Collection<Event> getAll() {
15+
return events.values();
16+
}
17+
18+
public static Event getById(int id) {
19+
return events.get(id);
20+
}
21+
22+
public static void add(Event event) {
23+
events.put(event.getId(), event);
24+
}
25+
26+
public static void remove(int id) {
27+
events.remove(id);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)