Skip to content

Commit 3228e03

Browse files
committed
create-data-layer branch complete
1 parent ae43c5d commit 3228e03

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

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;
@@ -18,12 +19,10 @@
1819
@RequestMapping("events")
1920
public class EventController {
2021

21-
private static List<Event> events = new ArrayList<>();
22-
2322
@GetMapping
2423
public String displayAllEvents(Model model) {
2524
model.addAttribute("title", "All Events");
26-
model.addAttribute("events", events);
25+
model.addAttribute("events", EventData.getAll());
2726
return "events/index";
2827
}
2928

@@ -36,7 +35,7 @@ public String displayCreateEventForm(Model model) {
3635
@PostMapping("create")
3736
public String processCreateEventForm(@RequestParam String eventName,
3837
@RequestParam String eventDescription) {
39-
events.add(new Event(eventName, eventDescription));
38+
EventData.add(new Event(eventName, eventDescription));
4039
return "redirect:/events";
4140
}
4241

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/**
10+
* Created by Chris Bay
11+
*/
12+
public class EventData {
13+
14+
private static final Map<Integer, Event> events = new HashMap<>();
15+
16+
public static Collection<Event> getAll() {
17+
return events.values();
18+
}
19+
20+
public static Event getById(int id) {
21+
return events.get(id);
22+
}
23+
24+
public static void add(Event event) {
25+
events.put(event.getId(), event);
26+
}
27+
28+
public static void remove(int id) {
29+
events.remove(id);
30+
}
31+
32+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
<header th:replace="fragments :: header"></header>
77

8-
98
<form method="post">
109
<div class="form-group">
1110
<label>Name

0 commit comments

Comments
 (0)